How to call 'printf' using libffi?
Anthony Green
green@moxielogic.com
Sat Mar 20 22:40:06 GMT 2021
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