Skip to content

Commit 43e4ad4

Browse files
committed
Test passing structs by value
1 parent 68485e3 commit 43e4ad4

2 files changed

Lines changed: 154 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* Area: ffi_call
2+
Purpose: Check structures.
3+
Limitations: none.
4+
PR: none.
5+
Originator: From the original ffitest.c */
6+
7+
/* { dg-do run } */
8+
#include "ffitest.h"
9+
10+
typedef struct
11+
{
12+
unsigned int ui01;
13+
unsigned int ui02;
14+
unsigned int ui03;
15+
unsigned int ui04;
16+
unsigned int ui05;
17+
unsigned int ui06;
18+
unsigned int ui07;
19+
unsigned int ui08;
20+
unsigned int ui09;
21+
unsigned int ui10;
22+
unsigned int ui11;
23+
unsigned int ui12;
24+
unsigned int ui13;
25+
unsigned int ui14;
26+
unsigned int ui15;
27+
unsigned int ui16;
28+
unsigned int ui17;
29+
} test_structure_1;
30+
31+
static test_structure_1 ABI_ATTR struct1(test_structure_1 ts)
32+
{
33+
ts.ui17++;
34+
35+
return ts;
36+
}
37+
38+
int main (void)
39+
{
40+
ffi_cif cif;
41+
ffi_type *args[MAX_ARGS];
42+
void *values[MAX_ARGS];
43+
ffi_type ts1_type;
44+
ffi_type *ts1_type_elements[18];
45+
46+
test_structure_1 ts1_arg;
47+
48+
/* This is a hack to get a properly aligned result buffer */
49+
test_structure_1 *ts1_result =
50+
(test_structure_1 *) malloc (sizeof(test_structure_1));
51+
52+
ts1_type.size = 0;
53+
ts1_type.alignment = 0;
54+
ts1_type.type = FFI_TYPE_STRUCT;
55+
ts1_type.elements = ts1_type_elements;
56+
ts1_type_elements[0] = &ffi_type_uint;
57+
ts1_type_elements[1] = &ffi_type_uint;
58+
ts1_type_elements[2] = &ffi_type_uint;
59+
ts1_type_elements[3] = &ffi_type_uint;
60+
ts1_type_elements[4] = &ffi_type_uint;
61+
ts1_type_elements[5] = &ffi_type_uint;
62+
ts1_type_elements[6] = &ffi_type_uint;
63+
ts1_type_elements[7] = &ffi_type_uint;
64+
ts1_type_elements[8] = &ffi_type_uint;
65+
ts1_type_elements[9] = &ffi_type_uint;
66+
ts1_type_elements[10] = &ffi_type_uint;
67+
ts1_type_elements[11] = &ffi_type_uint;
68+
ts1_type_elements[12] = &ffi_type_uint;
69+
ts1_type_elements[13] = &ffi_type_uint;
70+
ts1_type_elements[14] = &ffi_type_uint;
71+
ts1_type_elements[15] = &ffi_type_uint;
72+
ts1_type_elements[16] = &ffi_type_uint;
73+
ts1_type_elements[17] = NULL;
74+
75+
args[0] = &ts1_type;
76+
values[0] = &ts1_arg;
77+
78+
/* Initialize the cif */
79+
CHECK(ffi_prep_cif(&cif, ABI_NUM, 1,
80+
&ts1_type, args) == FFI_OK);
81+
82+
ts1_arg.ui17 = 555;
83+
84+
ffi_call(&cif, FFI_FN(struct1), ts1_result, values);
85+
86+
CHECK(ts1_result->ui17 == 556);
87+
88+
/* This will fail if ffi_call isn't passing the struct by value. */
89+
CHECK(ts1_arg.ui17 == 555);
90+
91+
free (ts1_result);
92+
exit(0);
93+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* Area: ffi_call
2+
Purpose: Check structures.
3+
Limitations: none.
4+
PR: none.
5+
Originator: From the original ffitest.c */
6+
7+
/* { dg-do run } */
8+
#include "ffitest.h"
9+
10+
typedef struct
11+
{
12+
unsigned int ui17;
13+
} test_structure_1;
14+
15+
static test_structure_1 ABI_ATTR struct1(test_structure_1 ts)
16+
{
17+
ts.ui17++;
18+
19+
return ts;
20+
}
21+
22+
int main (void)
23+
{
24+
ffi_cif cif;
25+
ffi_type *args[MAX_ARGS];
26+
void *values[MAX_ARGS];
27+
ffi_type ts1_type;
28+
ffi_type *ts1_type_elements[2];
29+
30+
test_structure_1 ts1_arg;
31+
32+
/* This is a hack to get a properly aligned result buffer */
33+
test_structure_1 *ts1_result =
34+
(test_structure_1 *) malloc (sizeof(test_structure_1));
35+
36+
ts1_type.size = 0;
37+
ts1_type.alignment = 0;
38+
ts1_type.type = FFI_TYPE_STRUCT;
39+
ts1_type.elements = ts1_type_elements;
40+
ts1_type_elements[0] = &ffi_type_uint;
41+
ts1_type_elements[1] = NULL;
42+
43+
args[0] = &ts1_type;
44+
values[0] = &ts1_arg;
45+
46+
/* Initialize the cif */
47+
CHECK(ffi_prep_cif(&cif, ABI_NUM, 1,
48+
&ts1_type, args) == FFI_OK);
49+
50+
ts1_arg.ui17 = 555;
51+
52+
ffi_call(&cif, FFI_FN(struct1), ts1_result, values);
53+
54+
CHECK(ts1_result->ui17 == 556);
55+
56+
/* This will fail if ffi_call isn't passing the struct by value. */
57+
CHECK(ts1_arg.ui17 == 555);
58+
59+
free (ts1_result);
60+
exit(0);
61+
}

0 commit comments

Comments
 (0)