Debian Codegraph @

testsuite/libffi.closures/cls_pointer_stack.c

raw
1 /* Area: ffi_call, closure_call
2 Purpose: Check pointer arguments across multiple hideous stack frames.
3 Limitations: none.
4 PR: none.
5 Originator: Blake Chaffin 6/7/2007 */
6
7 /* { dg-do run { xfail strongarm*-*-* xscale*-*-* } } */
8 #include "ffitest.h"
9
10 static long dummyVar;
11
12 long dummy_func(
13 long double a1, char b1,
14 long double a2, char b2,
15 long double a3, char b3,
16 long double a4, char b4)
17 {
18 return a1 + b1 + a2 + b2 + a3 + b3 + a4 + b4;
19 }
20
21 void* cls_pointer_fn2(void* a1, void* a2)
22 {
23 long double trample1 = (intptr_t)a1 + (intptr_t)a2;
24 char trample2 = ((char*)&a1)[0] + ((char*)&a2)[0];
25 long double trample3 = (intptr_t)trample1 + (intptr_t)a1;
26 char trample4 = trample2 + ((char*)&a1)[1];
27 long double trample5 = (intptr_t)trample3 + (intptr_t)a2;
28 char trample6 = trample4 + ((char*)&a2)[1];
29 long double trample7 = (intptr_t)trample5 + (intptr_t)trample1;
30 char trample8 = trample6 + trample2;
31 void* result;
32
33 dummyVar = dummy_func(trample1, trample2, trample3, trample4,
34 trample5, trample6, trample7, trample8);
35
36 result = (void*)((intptr_t)a1 + (intptr_t)a2);
37
38 printf("0x%08x 0x%08x: 0x%08x\n",
39 (unsigned int)(uintptr_t) a1,
40 (unsigned int)(uintptr_t) a2,
41 (unsigned int)(uintptr_t) result);
42
43 CHECK((unsigned int)(uintptr_t) a1 == 0x8acf1356);
44 CHECK((unsigned int)(uintptr_t) a2 == 0x01234567);
45 CHECK((unsigned int)(uintptr_t) result == 0x8bf258bd);
46
47 return result;
48 }
49
50 void* cls_pointer_fn1(void* a1, void* a2)
51 {
52 long double trample1 = (intptr_t)a1 + (intptr_t)a2;
53 char trample2 = ((char*)&a1)[0] + ((char*)&a2)[0];
54 long double trample3 = (intptr_t)trample1 + (intptr_t)a1;
55 char trample4 = trample2 + ((char*)&a1)[1];
56 long double trample5 = (intptr_t)trample3 + (intptr_t)a2;
57 char trample6 = trample4 + ((char*)&a2)[1];
58 long double trample7 = (intptr_t)trample5 + (intptr_t)trample1;
59 char trample8 = trample6 + trample2;
60 void* result;
61
62 dummyVar = dummy_func(trample1, trample2, trample3, trample4,
63 trample5, trample6, trample7, trample8);
64
65 result = (void*)((intptr_t)a1 + (intptr_t)a2);
66
67 printf("0x%08x 0x%08x: 0x%08x\n",
68 (unsigned int)(intptr_t) a1,
69 (unsigned int)(intptr_t) a2,
70 (unsigned int)(intptr_t) result);
71
72 CHECK((unsigned int)(uintptr_t) a1 == 0x01234567);
73 CHECK((unsigned int)(uintptr_t) a2 == 0x89abcdef);
74 CHECK((unsigned int)(uintptr_t) result == 0x8acf1356);
75
76 result = cls_pointer_fn2(result, a1);
77
78 return result;
79 }
80
81 static void
82 cls_pointer_gn(ffi_cif* cif __UNUSED__, void* resp,
83 void** args, void* userdata __UNUSED__)
84 {
85 void* a1 = *(void**)(args[0]);
86 void* a2 = *(void**)(args[1]);
87
88 long double trample1 = (intptr_t)a1 + (intptr_t)a2;
89 char trample2 = ((char*)&a1)[0] + ((char*)&a2)[0];
90 long double trample3 = (intptr_t)trample1 + (intptr_t)a1;
91 char trample4 = trample2 + ((char*)&a1)[1];
92 long double trample5 = (intptr_t)trample3 + (intptr_t)a2;
93 char trample6 = trample4 + ((char*)&a2)[1];
94 long double trample7 = (intptr_t)trample5 + (intptr_t)trample1;
95 char trample8 = trample6 + trample2;
96
97 dummyVar = dummy_func(trample1, trample2, trample3, trample4,
98 trample5, trample6, trample7, trample8);
99
100 *(void**)resp = cls_pointer_fn1(a1, a2);
101 }
102
103 int main (void)
104 {
105 ffi_cif cif;
106 void *code;
107 ffi_closure* pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
108 void* args[3];
109 /* ffi_type cls_pointer_type; */
110 ffi_type* arg_types[3];
111
112 /* cls_pointer_type.size = sizeof(void*);
113 cls_pointer_type.alignment = 0;
114 cls_pointer_type.type = FFI_TYPE_POINTER;
115 cls_pointer_type.elements = NULL;*/
116
117 void* arg1 = (void*)0x01234567;
118 void* arg2 = (void*)0x89abcdef;
119 ffi_arg res = 0;
120
121 arg_types[0] = &ffi_type_pointer;
122 arg_types[1] = &ffi_type_pointer;
123 arg_types[2] = NULL;
124
125 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_pointer,
126 arg_types) == FFI_OK);
127
128 args[0] = &arg1;
129 args[1] = &arg2;
130 args[2] = NULL;
131
132 printf("\n");
133 ffi_call(&cif, FFI_FN(cls_pointer_fn1), &res, args);
134
135 printf("res: 0x%08x\n", (unsigned int) res);
136 /* { dg-output "\n0x01234567 0x89abcdef: 0x8acf1356" } */
137 /* { dg-output "\n0x8acf1356 0x01234567: 0x8bf258bd" } */
138 /* { dg-output "\nres: 0x8bf258bd" } */
139 CHECK(res == 0x8bf258bd);
140
141 CHECK(ffi_prep_closure_loc(pcl, &cif, cls_pointer_gn, NULL, code) == FFI_OK);
142
143 res = (ffi_arg)(uintptr_t)((void*(*)(void*, void*))(code))(arg1, arg2);
144
145 printf("res: 0x%08x\n", (unsigned int) res);
146 /* { dg-output "\n0x01234567 0x89abcdef: 0x8acf1356" } */
147 /* { dg-output "\n0x8acf1356 0x01234567: 0x8bf258bd" } */
148 /* { dg-output "\nres: 0x8bf258bd" } */
149 CHECK(res == 0x8bf258bd);
150
151 exit(0);
152 }