public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
* Help: correct way to handle struct return values.
@ 2017-05-28  3:14 Kaz Kylheku
  2017-05-28  4:13 ` Kaz Kylheku
  0 siblings, 1 reply; 2+ messages in thread
From: Kaz Kylheku @ 2017-05-28  3:14 UTC (permalink / raw)
  To: libffi-discuss

Hi all,

In the current texinfo doc, the callback stub does this to
return a value of type int:

   /* Acts like puts with the file given at time of enclosure. */
   void puts_binding(ffi_cif *cif, void *ret, void* args[],
                   void *stream)
   {
     *(ffi_arg *)ret = fputs(*(char **)args[0], (FILE *)stream);
   }

Can someone show what the code would look like for a function
that returns the following type, and work everywhere: all
supported platforms, big or little endian:

   struct little { char a, b };

Obvously, this can't be used:

    *(ffi_arg *)ret = fun_returning_struct_little(...);

After everything, I'm not confident that this is correct, either:

    *(struct little *)ret = fun_returning_struct_little(...);

Also, can someone show how to extract "struct little" in after
a ffi down call? If the type is int, we can do this:

    ffi_arg rc_buf;
    int rc;
    ffi_call(&cif, puts, &rc_buf, values);
    rc = rc_buf;

But what if rc is of type "little struct"? Again, we can't just use a 
cast.

Lastly, how does any of this change for  struct bigger_struct { long x, 
y, z; }?

Thanks.

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

* Re: Help: correct way to handle struct return values.
  2017-05-28  3:14 Help: correct way to handle struct return values Kaz Kylheku
@ 2017-05-28  4:13 ` Kaz Kylheku
  0 siblings, 0 replies; 2+ messages in thread
From: Kaz Kylheku @ 2017-05-28  4:13 UTC (permalink / raw)
  To: libffi-discuss

On 27.05.2017 20:14, Kaz Kylheku wrote:
> Hi all,
> 
> In the current texinfo doc, the callback stub does this to
> return a value of type int:
> 
>   /* Acts like puts with the file given at time of enclosure. */
>   void puts_binding(ffi_cif *cif, void *ret, void* args[],
>                   void *stream)
>   {
>     *(ffi_arg *)ret = fputs(*(char **)args[0], (FILE *)stream);
>   }
> 
> Can someone show what the code would look like for a function
> that returns the following type, and work everywhere: all
> supported platforms, big or little endian:
> 
>   struct little { char a, b };

This corresponds to libffi.call/struct5.c

Everything I'm seeing in the test suite seems like structs are handled
in the straighforward way in both ffi_call and closures. But
in this small struct test case there is this comment:

   /* This is a hack to get a properly aligned result buffer */
   test_structure_5 *ts5_result =
     (test_structure_5 *) malloc (sizeof(test_structure_5));


What are the alignment requirements here; must the small
structure have the same alignment as ffi_arg?

And why so, if its size is smaller? If this has to be,
say, 8 bytes aligned, doesn't it mean something will be writing
an 8 byte data unit into it? In that case, shouldn't
the malloc request be padded up to 8 so there is no overrun?

I am not concerned about the alignment because I'm using alloca
for the return value buffer given to ffi_call.

I am worried though about the size. alloca(2) could give me an
8 byte aligned pointer which is only two byte away from the next
object on the stack; if something writes 8 bytes there, that is
very bad.


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

end of thread, other threads:[~2017-05-28  4:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-28  3:14 Help: correct way to handle struct return values Kaz Kylheku
2017-05-28  4:13 ` Kaz Kylheku

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).