summaryrefslogtreecommitdiffstats
path: root/libffi
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2021-06-16 10:45:27 +0200
committerJakub Jelinek <jakub@redhat.com>2021-06-16 10:45:27 +0200
commit041f74177072df1d66502319205990a4d970c92a (patch)
tree60e47c68aa73269439f7c1cbd7f873f6121266e8 /libffi
parent[Ada] Fix Is_Volatile_Function for functions declared in protected bodies (diff)
downloadgcc-041f74177072df1d66502319205990a4d970c92a.tar.gz
gcc-041f74177072df1d66502319205990a4d970c92a.tar.bz2
gcc-041f74177072df1d66502319205990a4d970c92a.tar.xz
libffi: Fix up x86_64 classify_argument
As the following testcase shows, libffi didn't handle properly classify_arguments of structures at byte offsets not divisible by UNITS_PER_WORD. The following patch adjusts it to match what config/i386/ classify_argument does for that and also ports the PR38781 fix there (the second chunk). This has been committed to upstream libffi already: https://github.com/libffi/libffi/commit/5651bea284ad0822eafe768e3443c2f4d7da2c8f 2021-06-16 Jakub Jelinek <jakub@redhat.com> * src/x86/ffi64.c (classify_argument): For FFI_TYPE_STRUCT set words to number of words needed for type->size + byte_offset bytes rather than just type->size bytes. Compute pos before the loop and check total size of the structure. * testsuite/libffi.call/nested_struct12.c: New test.
Diffstat (limited to 'libffi')
-rw-r--r--libffi/src/x86/ffi64.c9
-rw-r--r--libffi/testsuite/libffi.call/nested_struct12.c107
2 files changed, 112 insertions, 4 deletions
diff --git a/libffi/src/x86/ffi64.c b/libffi/src/x86/ffi64.c
index 131b5e3d148..243cbc7689a 100644
--- a/libffi/src/x86/ffi64.c
+++ b/libffi/src/x86/ffi64.c
@@ -217,7 +217,8 @@ classify_argument (ffi_type *type, enum x86_64_reg_class classes[],
217 case FFI_TYPE_STRUCT: 217 case FFI_TYPE_STRUCT:
218 { 218 {
219 const size_t UNITS_PER_WORD = 8; 219 const size_t UNITS_PER_WORD = 8;
220 size_t words = (type->size + UNITS_PER_WORD - 1) / UNITS_PER_WORD; 220 size_t words = (type->size + byte_offset + UNITS_PER_WORD - 1)
221 / UNITS_PER_WORD;
221 ffi_type **ptr; 222 ffi_type **ptr;
222 int i; 223 int i;
223 enum x86_64_reg_class subclasses[MAX_CLASSES]; 224 enum x86_64_reg_class subclasses[MAX_CLASSES];
@@ -241,16 +242,16 @@ classify_argument (ffi_type *type, enum x86_64_reg_class classes[],
241 /* Merge the fields of structure. */ 242 /* Merge the fields of structure. */
242 for (ptr = type->elements; *ptr != NULL; ptr++) 243 for (ptr = type->elements; *ptr != NULL; ptr++)
243 { 244 {
244 size_t num; 245 size_t num, pos;
245 246
246 byte_offset = ALIGN (byte_offset, (*ptr)->alignment); 247 byte_offset = ALIGN (byte_offset, (*ptr)->alignment);
247 248
248 num = classify_argument (*ptr, subclasses, byte_offset % 8); 249 num = classify_argument (*ptr, subclasses, byte_offset % 8);
249 if (num == 0) 250 if (num == 0)
250 return 0; 251 return 0;
251 for (i = 0; i < num; i++) 252 pos = byte_offset / 8;
253 for (i = 0; i < num && (i + pos) < words; i++)
252 { 254 {
253 size_t pos = byte_offset / 8;
254 classes[i + pos] = 255 classes[i + pos] =
255 merge_classes (subclasses[i], classes[i + pos]); 256 merge_classes (subclasses[i], classes[i + pos]);
256 } 257 }
diff --git a/libffi/testsuite/libffi.call/nested_struct12.c b/libffi/testsuite/libffi.call/nested_struct12.c
new file mode 100644
index 00000000000..6f3d7369f27
--- /dev/null
+++ b/libffi/testsuite/libffi.call/nested_struct12.c
@@ -0,0 +1,107 @@
1/* Area: ffi_call, closure_call
2 Purpose: Check structure passing.
3 Limitations: none.
4 PR: none.
5 Originator: <jincheng@ca.ibm.com> and <jakub@redhat.com> 20210609 */
6
7/* { dg-do run } */
8#include "ffitest.h"
9
10typedef struct A {
11 float a, b;
12} A;
13
14typedef struct B {
15 float x;
16 struct A y;
17} B;
18
19B B_fn(float b0, struct B b1)
20{
21 struct B result;
22
23 result.x = b0 + b1.x;
24 result.y.a = b0 + b1.y.a;
25 result.y.b = b0 + b1.y.b;
26
27 printf("%g %g %g %g: %g %g %g\n", b0, b1.x, b1.y.a, b1.y.b,
28 result.x, result.y.a, result.y.b);
29
30 return result;
31}
32
33static void
34B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
35 void* userdata __UNUSED__)
36{
37 float b0;
38 struct B b1;
39
40 b0 = *(float*)(args[0]);
41 b1 = *(struct B*)(args[1]);
42
43 *(B*)resp = B_fn(b0, b1);
44}
45
46int main (void)
47{
48 ffi_cif cif;
49 void *code;
50 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
51 void* args_dbl[3];
52 ffi_type* cls_struct_fields[3];
53 ffi_type* cls_struct_fields1[3];
54 ffi_type cls_struct_type, cls_struct_type1;
55 ffi_type* dbl_arg_types[3];
56
57 float e_dbl = 12.125f;
58 struct B f_dbl = { 24.75f, { 31.625f, 32.25f } };
59
60 struct B res_dbl;
61
62 cls_struct_type.size = 0;
63 cls_struct_type.alignment = 0;
64 cls_struct_type.type = FFI_TYPE_STRUCT;
65 cls_struct_type.elements = cls_struct_fields;
66
67 cls_struct_type1.size = 0;
68 cls_struct_type1.alignment = 0;
69 cls_struct_type1.type = FFI_TYPE_STRUCT;
70 cls_struct_type1.elements = cls_struct_fields1;
71
72 cls_struct_fields[0] = &ffi_type_float;
73 cls_struct_fields[1] = &ffi_type_float;
74 cls_struct_fields[2] = NULL;
75
76 cls_struct_fields1[0] = &ffi_type_float;
77 cls_struct_fields1[1] = &cls_struct_type;
78 cls_struct_fields1[2] = NULL;
79
80
81 dbl_arg_types[0] = &ffi_type_float;
82 dbl_arg_types[1] = &cls_struct_type1;
83 dbl_arg_types[2] = NULL;
84
85 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type1,
86 dbl_arg_types) == FFI_OK);
87
88 args_dbl[0] = &e_dbl;
89 args_dbl[1] = &f_dbl;
90 args_dbl[2] = NULL;
91
92 ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl);
93 /* { dg-output "12.125 24.75 31.625 32.25: 36.875 43.75 44.375" } */
94 CHECK( res_dbl.x == (e_dbl + f_dbl.x));
95 CHECK( res_dbl.y.a == (e_dbl + f_dbl.y.a));
96 CHECK( res_dbl.y.b == (e_dbl + f_dbl.y.b));
97
98 CHECK(ffi_prep_closure_loc(pcl, &cif, B_gn, NULL, code) == FFI_OK);
99
100 res_dbl = ((B(*)(float, B))(code))(e_dbl, f_dbl);
101 /* { dg-output "\n12.125 24.75 31.625 32.25: 36.875 43.75 44.375" } */
102 CHECK( res_dbl.x == (e_dbl + f_dbl.x));
103 CHECK( res_dbl.y.a == (e_dbl + f_dbl.y.a));
104 CHECK( res_dbl.y.b == (e_dbl + f_dbl.y.b));
105
106 exit(0);
107}