public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
From: "Cheng Jin" <jincheng@ca.ibm.com>
To: Anthony Green <green@moxielogic.com>
Cc: libffi-discuss <libffi-discuss@sourceware.org>
Subject: RE: Incorrect data detected in the nested float struct with x86/libffi on Linux/64bit
Date: Wed, 9 Jun 2021 13:53:32 -0400	[thread overview]
Message-ID: <OFF6D0F33B.B956BA21-ON002586EF.00623CEB-852586EF.006248F5@ibm.com> (raw)
In-Reply-To: <CACxje58wD-98bsScOT4OZ1ZXQxLuyKAwMmO85fDWRyt1Z-7wNw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 5628 bytes --]




Hi Anthony,

Many thanks for the quick response. The PR has been created at
https://github.com/libffi/libffi/pull/640 as requested.

Thanks and Best Regards
Cheng Jin




From:	Anthony Green <green@moxielogic.com>
To:	Cheng Jin <jincheng@ca.ibm.com>
Cc:	libffi-discuss <libffi-discuss@sourceware.org>
Date:	2021-06-09 12:50 PM
Subject:	[EXTERNAL] Re: Incorrect data detected in the nested float
            struct with x86/libffi on Linux/64bit



Thank you, Cheng.  Are you able to submit this test case as a github pull
request?  The resulting CI testing will give us a broader picture of where
we have problems. Thanks, AG On Wed, Jun 9, 2021 at 12:41 PM Cheng Jin via
Libffi-discuss < ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.
ZjQcmQRYFpfptBannerEnd
Thank you, Cheng.  Are you able to submit this test case as a github pull
request?  The resulting CI testing will give us a broader picture of where
we have problems.

Thanks,

AG


On Wed, Jun 9, 2021 at 12:41 PM Cheng Jin via Libffi-discuss <
libffi-discuss@sourceware.org> wrote:


  Hi,

  We found that the nested struct [[float, float], float] works good with
  x86/libffi (at https://github.com/libffi/libffi/tree/master/src/x86)
  on Linux/64bit (verified on Ubuntu 19) while a variant of it like
  [float,[float,float]] was messed up at the 3rd element when invoking
  ffi_call() in the following test:


  typedef struct stru_Nested_F stru_Nested_F;

  struct stru_FF {
          float elem1;
          float elem2;
  };

  struct stru_Nested_F {
          float elem1;
          stru_FF elem2;
  };

  static float testNestedFloatStruct(float arg1, stru_Nested_F arg2)
  {
          float floatSum = arg1 + arg2.elem1 + arg2.elem2.elem1 +
  arg2.elem2.elem2;
          return floatSum;
  }

  int main() {
            float returnValue = 0;
            ffi_cif cif_temp;
            UDATA structElemNum = 2;
            UDATA nestedStructElemNum = 2;
            UDATA argNum = 2;
            ffi_type **cls_struct_fields1 = (ffi_type
  **)malloc(sizeof(ffi_type
  *) * (structElemNum + 1));
            ffi_type **cls_struct_fields2 = (ffi_type
  **)malloc(sizeof(ffi_type
  *) * (nestedStructElemNum + 1));
            ffi_type **dbl_arg_types = (ffi_type **)malloc(sizeof(ffi_type
  *) *
  (argNum + 1));
            void **args_db = (void **)malloc(sizeof(void *) * (argNum +
  1));
            ffi_type cls_struct_type1, cls_struct_type2;
            ffi_type *retType = &ffi_type_float;
            float arg1;
            float *arg2 = (float *)malloc(sizeof(stru_Nested_F));

            cls_struct_fields2[0] = &ffi_type_float;
            cls_struct_fields2[1] = &ffi_type_float;
            cls_struct_fields2[2] = NULL;

            cls_struct_type2.size = 0;
            cls_struct_type2.alignment = 0;
            cls_struct_type2.type = FFI_TYPE_STRUCT;
            cls_struct_type2.elements = cls_struct_fields2;

            cls_struct_fields1[0] = &ffi_type_float;
            cls_struct_fields1[1] = &cls_struct_type2;
            cls_struct_fields1[2] = NULL;

            cls_struct_type1.size = 0;
            cls_struct_type1.alignment = 0;
            cls_struct_type1.type = FFI_TYPE_STRUCT;
            cls_struct_type1.elements = cls_struct_fields1;

            dbl_arg_types[0] = &ffi_type_float;
            dbl_arg_types[1] = &cls_struct_type1;
            dbl_arg_types[2] = NULL;

           ffi_prep_cif(&cif_temp, FFI_DEFAULT_ABI, 2, retType,
  dbl_arg_types);
           arg1 = 37.88;
           arg2[0] = 31.22;
           arg2[1] = 33.44;
           arg2[2] = 35.66;
           args_db[0] = &arg1;
           args_db[1] = arg2;
           args_db[2] = NULL;

          ffi_call(&cif_temp, FFI_FN(testNestedFloatStruct), &returnValue,
  args_db);

          free(cls_struct_fields1);
          free(cls_struct_fields2);
          free(dbl_arg_types);
          free(args_db);

       return 0;
  }

  with the debugging output as follows:

  (gdb) p arg2[0]
  $1 = 31.2199993
  (gdb) p arg2[1]
  $2 = 33.4399986
  (gdb) p arg2[2]
  $3 = 35.6599998
  (gdb) x/10x  arg2
  0x7ffff025b540: 0x41f9c28f      0x4205c28f      0x420ea3d7
  0xdddddddd
  0x7ffff025b550: 0xb7654321      0x475d4352      0x0000000c
  0x00000000
  0x7ffff025b560: 0xf749c970      0x00007fff


  (gdb) bt
  #0  testNestedFloatStruct(arg1=37.8800011, arg2=...) at main.c:xxx
  #1  0x00007ffff746e4e0 in ffi_call_unix64 () at x86/unix64.S:xxx
  #2  0x00007ffff746dac8 in ffi_call (cif=0x7ffff7d44e90,
  fn=0x7ffff7f9e060,
  rvalue=0x16818, avalue=0x7ffff7d44f30) at x86/ffi64.c:xxx

  Thread 2 "main" hit Breakpoint 2, testNestedFloatStruct(arg1=37.8800011,
  arg2=...) at main.c
                  float floatSum = arg1 + arg2.elem1 + arg2.elem2.elem1 +
  arg2.elem2.elem2;
  (gdb) p arg2
  $4 = {
    elem1 = 31.2199993,
    elem2 = {
      elem1 = 33.4399986,
      elem2 = 4.20389539e-45  <------------------ messed up
    }
  }

  (gdb) x/10x  &arg2
  0x7ffff7d44458: 0x41f9c28f      0x4205c28f      0x00000003
  0x00007fff
  0x7ffff7d44468: 0xf70cf1e6      0x4217851f      0xf7d44510
  0x00007fff
  0x7ffff7d44478: 0xf7d44660      0x00007fff


  -------------------------------------------

  It looks like there is something wrong in dealing with the nested stuct
  on
  Linux/64bit in x86/libffi as the test above works good on Windows/64bit.


  Thanks and Best Regards
  Cheng Jin



[-- Attachment #2: graycol.gif --]
[-- Type: image/gif, Size: 105 bytes --]

  reply	other threads:[~2021-06-09 17:53 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-09 16:41 Cheng Jin
2021-06-09 16:50 ` Anthony Green
2021-06-09 17:53   ` Cheng Jin [this message]
2021-06-15 18:02   ` Jakub Jelinek
2021-06-15 19:02     ` Jakub Jelinek
2021-06-15 19:29       ` Anthony Green
2021-06-09 23:48 ` Kaz Kylheku (libffi)
2021-06-10  0:21   ` Cheng Jin
2021-06-10  2:05 ` Kaz Kylheku (libffi)
2021-06-10  2:39   ` Cheng Jin

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=OFF6D0F33B.B956BA21-ON002586EF.00623CEB-852586EF.006248F5@ibm.com \
    --to=jincheng@ca.ibm.com \
    --cc=green@moxielogic.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).