From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd35.google.com (mail-io1-xd35.google.com [IPv6:2607:f8b0:4864:20::d35]) by sourceware.org (Postfix) with ESMTPS id C43CE384841E for ; Wed, 9 Jun 2021 16:50:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C43CE384841E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=moxielogic.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=moxielogic.com Received: by mail-io1-xd35.google.com with SMTP id l64so1806148ioa.7 for ; Wed, 09 Jun 2021 09:50:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=moxielogic-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=fT7O2dePPEpEdmwf483sFzxsJK1AUDLPaYrVBmUnMo8=; b=YrdAdIm4PrAPX0INMFyYBukwlVIgGIt4mw9/3rCCH+aI2x/H2WAg7vuCuMCqOUxC2p gi5THznbhlqJiq786Rb3+PZhgcRZiBl3+vrKVWcTtnhP37Xf0CJ7Awfuu0cBzAL/5rKO Fgq6l0rFLJjqzXyRGof9yzBz0ptU99oeGxBDi13xqduCtXVMWP56oqTu/X0M6f+tBqwJ AUmcx/mZ5810+SmYl0MnkqCBe/DoEGrh1xBHjQ27go15ucKOCpCg8TquBHLDTPE/uVpv 47Tr1bkGmOjlRR+iYhFJALm20KWSrtrAYIn5y5KpNGiIRHgqpORKyO7/FmKqoh4F+/fM mzGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=fT7O2dePPEpEdmwf483sFzxsJK1AUDLPaYrVBmUnMo8=; b=m0vAuDFNhuG7SHyQewWWn79ERZHRRV5HVupdsrVPa6QOp1WsAcIha08/rPcdmNdKtY mhNQDMUe9RK8HloTFhHCFkkUq93ym8eYLa3Kf3Ne/o3FMyUnHQQ+38OJ56TXmLVzCI4M o1/tE6JeOLbTK5SKkI042nVYyrL/sX+RgQI/4InpyBp9+6CtWzn6SIN9zr3V/Y3krUXj nhay9nYTL3s634w+MiPjZBOS07Ag2jZtPU67gv5C4caRreGhxMEn6V5PWD4Hy0GQeRk0 ZfeWINOB37MYbgzbieskt3CULCyppIVuFPAB/zO6aO7ol63GOBjxXB45oPxfC+/y7Xa0 Hmsg== X-Gm-Message-State: AOAM530jjbIowWEbI7GZSzGmFtAmGx9OW6xq1b0kHqjXS/ZiqaHSQN6o 53i2sHi1tU7xgM53qT4Q4K/aIyv1k4EZg2QX8Ip3ew== X-Google-Smtp-Source: ABdhPJy5i0Zc5ruOjeLc5g/JfHSlqgP+AWMMWZtxoOtasa2Sc/OhRngF8G0hwN4cHA+LboqPK9VleytEG9qpcFKiBB8= X-Received: by 2002:a02:a815:: with SMTP id f21mr526910jaj.118.1623257419214; Wed, 09 Jun 2021 09:50:19 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Anthony Green Date: Wed, 9 Jun 2021 12:50:08 -0400 Message-ID: Subject: Re: Incorrect data detected in the nested float struct with x86/libffi on Linux/64bit To: Cheng Jin Cc: libffi-discuss X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: libffi-discuss@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libffi-discuss mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Jun 2021 16:50:21 -0000 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 > >