public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
From: "Kaz Kylheku (libffi)" <382-725-6798@kylheku.com>
To: libffi-discuss@sourceware.org
Subject: Re: Also: problem with return value in ffi_call on PPC64.
Date: Sun, 28 May 2017 02:16:00 -0000	[thread overview]
Message-ID: <3b9c322ddca2e20fa30c7eba0e5ebda2@mail.kylheku.com> (raw)

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

:)

             reply	other threads:[~2017-05-28  2:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-28  2:16 Kaz Kylheku (libffi) [this message]
2017-05-28 18:23 ` Sergei Trofimovich via libffi-discuss
2017-05-30  1:24   ` Kaz Kylheku (libffi)
  -- strict thread matches above, loose matches on Subject: below --
2017-05-28  1:36 Kaz Kylheku (libffi)
2017-05-30  8:27 ` Andrew Haley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3b9c322ddca2e20fa30c7eba0e5ebda2@mail.kylheku.com \
    --to=382-725-6798@kylheku.com \
    --cc=libffi-discuss@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).