public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
* Incorrect data detected in the nested float struct with x86/libffi on Linux/64bit
@ 2021-06-09 16:41 Cheng Jin
  2021-06-09 16:50 ` Anthony Green
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Cheng Jin @ 2021-06-09 16:41 UTC (permalink / raw)
  To: libffi-discuss



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


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

end of thread, other threads:[~2021-06-15 19:29 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-09 16:41 Incorrect data detected in the nested float struct with x86/libffi on Linux/64bit Cheng Jin
2021-06-09 16:50 ` Anthony Green
2021-06-09 17:53   ` Cheng Jin
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

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