public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
* Also: problem with return value in ffi_call on PPC64.
@ 2017-05-28  1:36 Kaz Kylheku (libffi)
  2017-05-30  8:27 ` Andrew Haley
  0 siblings, 1 reply; 5+ messages in thread
From: Kaz Kylheku (libffi) @ 2017-05-28  1:36 UTC (permalink / raw)
  To: libffi-discuss

Hi all,

It turns out that return values from foreign calls are also not working 
in the way I expect.

For instance, the int return value of dup comes out as zero if a file 
descriptor is returned.
The -1 value emerges properly due to sign extension:

1> (with-dyn-lib nil (deffi dup-fd "dup" int (int)))
#:lib-0175
2> (dup-fd 0)
0
3> (dup-fd 4)
-1
4> (dup-fd 3)
0
5> (dup-fd 4)
0
6> (dup-fd 5)
0
7> (dup-fd 7)
-1
8> (dup-fd 7)
-1

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?

Why doesn't that convention apply to the arguments, then? When dup is 
being called above, the int value is being written at the bottom of the 
argument buffer, not displaced by four bytes.

^ permalink raw reply	[flat|nested] 5+ messages in thread
* Re: Also: problem with return value in ffi_call on PPC64.
@ 2017-05-28  2:16 Kaz Kylheku (libffi)
  2017-05-28 18:23 ` Sergei Trofimovich via libffi-discuss
  0 siblings, 1 reply; 5+ messages in thread
From: Kaz Kylheku (libffi) @ 2017-05-28  2:16 UTC (permalink / raw)
  To: libffi-discuss

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!");

:)

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-05-30  8:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-28  1:36 Also: problem with return value in ffi_call on PPC64 Kaz Kylheku (libffi)
2017-05-30  8:27 ` Andrew Haley
2017-05-28  2:16 Kaz Kylheku (libffi)
2017-05-28 18:23 ` Sergei Trofimovich via libffi-discuss
2017-05-30  1:24   ` Kaz Kylheku (libffi)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).