This is the mail archive of the libffi-discuss@sourceware.org mailing list for the libffi project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Also: problem with return value in ffi_call on PPC64.


On 27.05.2017 18:36, Kaz Kylheku (libffi) wrote:
Are users supposed to assume that the return value has been widened to
a register-wide (8 byte) value regardless of its declared FFI type?

Indeed, it seems yes.

I now see in some documentation that "ffi_arg" C type must be used for capturing return values.

I'm not a complete idiot; I was taken for a ride by the simple example from some (perhaps outdated?) libffi texinfo documentation. This one:

     #include <stdio.h>
     #include <ffi.h>

     int main()
     {
       ffi_cif cif;
       ffi_type *args[1];
       void *values[1];
       char *s;
       int rc;

       /* Initialize the argument info vectors */
       args[0] = &ffi_type_pointer;
       values[0] = &s;

       /* Initialize the cif */
       if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
                       &ffi_type_uint, args) == FFI_OK)
         {
           s = "Hello World!";
           ffi_call(&cif, puts, &rc, values);
           /* rc now holds the result of the call to puts */

           /* values holds a pointer to the function's arg, so to
              call puts() again all we need to do is change the
              value of s */
           s = "This is cool!";
           ffi_call(&cif, puts, &rc, values);
         }
       return 0;
     }

Here, the return buffer rc is just "int" and not "ffi_arg". So, this isn't correct for PPC64. The rc variable isn't large enough to buffer the return value, and will alias the wrong end of it.

Oops!

puts("This is .. not so cool!");

:)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]