Incorrect data detected in the nested float struct with x86/libffi on Linux/64bit

Anthony Green green@moxielogic.com
Wed Jun 9 16:50:08 GMT 2021


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


More information about the Libffi-discuss mailing list