How to call 'printf' using libffi?

Anthony Green green@moxielogic.com
Sat Mar 20 23:56:57 GMT 2021


It's worth noting that JNA, one of libffi's big users, does the
float-to-double promotion at their layer.
https://github.com/java-native-access/jna/issues/463

Ruby's FFI does the same thing:
https://github.com/ffi/ffi/blob/6d14c0a9107c0d5febb3bf92a60d2581e768fa2f/ext/ffi_c/Variadic.c

This is making me feel more comfortable about returning an error on float
varargs.  (I've change my mind about aborting)

AG


On Sat, Mar 20, 2021 at 6:40 PM Anthony Green <green@moxielogic.com> wrote:

> It turns out there are problems with ffi_prep_cif_var().  One issue is
> that libffi doesn't promote floats to doubles for varargs, like most C
> hackers expect.  There was a bug filed against that recently:
> https://github.com/libffi/libffi/issues/608
> Should libffi do the promotion, or leave it as an exercise to the
> developer?  Right now I'm thinking that it should simply abort in
> ffi_prep_cif_var() if passed any of ffi_type_float, ffi_type_*short, or
> ffi_type_*char and require that the caller handle promotions and only pass
> pointers, structs, doubles, ints or larger, as it seems that we're not
> promoting short integer values either.  If I pass a very long list of
> varargs that end in a sequence of ffi_type_schar, for instance, the callee
> doesn't find the right values in the right stack locations.  So maybe we
> shouldn't allow that in the first place.
>
> I'm interested in opinions.
>
> Thanks,
>
> AG
>
>
> On Fri, Mar 19, 2021 at 7:43 AM Andrew Haley <aph@redhat.com> wrote:
>
>> On 3/19/21 11:01 AM, Anthony Green wrote:
>> >>>   ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 3, &ffi_type_sint,
>> >> arg_types);
>> >>
>> >> Surely that only works if a varargs call uses the same ABI as a normal
>> >> call. They don't always,
>> >>
>> > I'm using the '_var' variant of ffi_prep_cif that takes this into
>> account.
>>
>> Aha! Missed it.
>>
>> --
>> Andrew Haley  (he/him)
>> Java Platform Lead Engineer
>> Red Hat UK Ltd. <https://www.redhat.com>
>> https://keybase.io/andrewhaley
>> EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671
>>
>>


More information about the Libffi-discuss mailing list