-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest.c
More file actions
496 lines (421 loc) · 11.3 KB
/
Copy pathtest.c
File metadata and controls
496 lines (421 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include "stackman.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define assert_align(p) assert((intptr_t)(p) == STACKMAN_SP_ALIGN(p))
/* simple test without modifying stack pointers.
* test that the callback is called with valid
* stack pointers and the stage member in the
* correct order.
*/
typedef struct ctxt01
{
void *sp[2];
} ctxt01;
/* test retrieval of stack pointer */
void *test_01_cb(void* context, int _opcode, void *sp)
{
ctxt01 *c = (ctxt01*)context;
stackman_op_t opcode = (stackman_op_t)_opcode;
assert(opcode == STACKMAN_OP_SAVE || opcode == STACKMAN_OP_RESTORE);
/* check that stages are init in order */
assert(!c->sp[1]);
if (opcode == STACKMAN_OP_RESTORE)
assert(c->sp[0]);
else
assert(!c->sp[1]);
c->sp[opcode] = sp;
if (opcode == (int)STACKMAN_OP_SAVE)
return sp ;/* no stack switching */
return (void*)1; /* test return argument */
}
void test_01(void)
{
/* since we are not switching stacks, it is ok to keep context on stack */
ctxt01 c;
void *sp;
memset(&c, 0, sizeof(c));
sp = stackman_switch(&test_01_cb, &c);
assert(sp == (void*)1);
assert(c.sp[0] == c.sp[1]);
assert(STACKMAN_SP_LE(c.sp[0], (void*)&c));
}
/* a more complex test, performing a long jump out of a subroutine
* the context now stores a stub that can be jumped to
*/
typedef struct jmp
{
void *stack_far, *stack_near;
void *buf;
size_t size;
/* every call to cb logs val in log and increments */
int val;
int log[10];
int counter;
} jmp;
/* helpers to save and restore stack contents to a buffer */
void save_stack(void *sp, void *buf, size_t size)
{
if (STACKMAN_STACK_DIR == 0)
memcpy(buf, sp, size);
else
memcpy(buf, (void*)((char*)sp-size), size);
}
void restore_stack(void *sp, void *buf, size_t size)
{
if (STACKMAN_STACK_DIR == 0)
memcpy(sp, buf, size);
else
memcpy((void*)((char*)sp-size), buf, size);
}
void *jmp_cb(void* context, int opcode, void *sp)
{
assert_align(sp);
jmp *c = (jmp*)context;
c->log[c->counter++] = c->val;
if (c->val == 0) {
/* storing stub */
if (opcode == (int)STACKMAN_OP_SAVE) {
c->stack_near = sp;
c->size = STACKMAN_SP_DIFF((char*)c->stack_far, (char*)c->stack_near);
c->buf = malloc(c->size);
save_stack(sp, c->buf, c->size);
}
return sp;
}
/* now we are jumping. no need to save old stack */
if (opcode == (int)STACKMAN_OP_SAVE) {
return c->stack_near;
} else {
/* RESTORE must receive the active switched stack pointer. */
assert(sp == c->stack_near);
restore_stack(c->stack_near, c->buf, c->size);
return sp;
}
}
/* Create a far jump destination. */
jmp *jmp_save(void *farptr)
{
jmp *res = (jmp*)malloc(sizeof(jmp));
assert(res);
memset(res, 0, sizeof(jmp));
/* add safety margin */
/* far end of stack, add buffer to catch memory backed registers, etc. */
res->stack_far = STACKMAN_SP_ADD((char*)farptr, 32);
/* first time around, get the stack pointer */
stackman_switch(&jmp_cb, (void*) res);
if (res->val == 0)
return res;
return 0;
}
/* execute a far jump */
void jmp_jmp(jmp *c)
{
/* right, perform the jump to the original stack pointer */
c->val = 1;
stackman_switch(&jmp_cb, c);
}
void test_02_called(jmp *c)
{
/* right, perform the jump to the original stack pointer */
jmp_jmp(c);
assert(0);
}
void test_02(void *stack_marker)
{
static jmp *c;
jmp * tmp;
tmp = jmp_save(stack_marker);
if (tmp) {
c = tmp;
assert(c->counter == 2);
/* first time. We now have the sp to return to the original one
* lets long jump out of a recursive function here
*/
assert(c->buf);
assert(c->size);
test_02_called(c);
assert(0); /* never reach this */
}
assert (tmp == 0);
assert(c->val == 1);
assert(c->counter == 4);
}
/* test stack stuff not changing */
void test_03(void *stack_marker)
{
static jmp *c;
jmp * tmp;
int foo[2];
foo[0] = 7;
tmp = jmp_save(&stack_marker);
if (tmp) {
/* saved stack */
c = tmp;
foo[0] = 11;
test_02_called(c);
assert(0); /* never reach this */
}
assert (foo[0] == 7);
}
#if defined(STACKMAN_HAVE_CALL)
#define TEST_04
#endif
#ifdef TEST_04
/* test retrieval of stack pointer */
void *test_04_cb(void* context, int _opcode, void *old_sp)
{
assert_align(old_sp);
ctxt01 *c = (ctxt01*)context;
stackman_op_t opcode = (stackman_op_t)_opcode;
assert(opcode == STACKMAN_OP_CALL || opcode == 100);
/* one level of recursion here so that we actually use some of the new stack */
if (opcode == STACKMAN_OP_CALL)
test_04_cb(context, 100, old_sp);
c->sp[0] = &c;
c->sp[1] = old_sp;
return 0;
}
/* test stackman_call() with a non-null stack pointer */
void test_04(void)
{
char *block, *stack, *stack2;
intptr_t stacksize=1024;
int i, cnt;
ctxt01 ctxt;
assert(STACKMAN_STACK_FULL_DESCENDING);
block = (char*)malloc(stacksize);
assert(block);
stack = block + stacksize;
/* clear it all to a certain value */
memset(block, '\x7f', stacksize);
/* align stack properly */
stack = (void*)STACKMAN_SP_ALIGN(stack);
stacksize = stack-block;
/* allocate some guard memory at the stack top */
stack2 = stack-64;
/* perform the call */
stackman_call(test_04_cb, &ctxt, stack2);
/* verify that the called function saw stack in the right range */
assert(STACKMAN_SP_LE(block, ctxt.sp[0]));
assert(STACKMAN_SP_LS(ctxt.sp[0], stack2));
/* verify that old stack was outside the stack we allocated */
assert(STACKMAN_SP_LE(stack2, ctxt.sp[1]) || STACKMAN_SP_LS(ctxt.sp[1], block));
/* verify that the guard memory is correct */
cnt = 0;
for(i=0; i<64; i++)
cnt += stack2[i] == '\x7f';
assert(cnt == 64);
/* verify that the memory below the stack is something else */
cnt = 0;
for(i=0; i<64; i++)
cnt += stack2[-i] == '\x7f';
assert(cnt != 64);
}
/* test stackman_call() with a null stack pointer */
void test_05(void)
{
ctxt01 ctxt;
assert(STACKMAN_STACK_FULL_DESCENDING);
/* perform the call */
stackman_call(test_04_cb, &ctxt, 0);
/* verify that it was passed a stack */
assert(ctxt.sp[1]);
assert(STACKMAN_SP_LE(ctxt.sp[0], ctxt.sp[1]));
/* and that it was passed valid lower stack pointer */
assert(STACKMAN_SP_LE(ctxt.sp[1], &ctxt));
}
#endif
/* Test our various macros */
void test_06()
{
int local=0;
void *away = STACKMAN_SP_FURTHEST;
void *close = STACKMAN_SP_NEAREST;
assert(STACKMAN_SP_LE(&local, away));
assert(STACKMAN_SP_LS(&local, away));
assert(STACKMAN_SP_LE(close, &local));
assert(STACKMAN_SP_LS(close, &local));
assert(STACKMAN_SP_LE(&local, &local));
assert(!STACKMAN_SP_LS(&local, &local));
assert((void*)STACKMAN_SP_ALIGN(away) == away);
}
/* Test floating point register preservation across stack switches.
* This test verifies that callee-saved FP registers are properly
* preserved across stack switches on all platforms.
*
* Uses arrays to encourage compiler to use multiple FP registers
* and potentially SIMD instructions where available.
*/
typedef struct fp_test_context {
void *saved_sp;
int switch_done;
} fp_test_context;
void *test_07_cb(void* context, int opcode, void *sp)
{
fp_test_context *c = (fp_test_context*)context;
if (opcode == STACKMAN_OP_SAVE) {
c->saved_sp = sp;
return sp; /* No stack switch */
} else {
c->switch_done = 1;
return sp;
}
}
/* Aggressive FP computation using arrays to encourage vectorization
* and use of multiple FP registers including SIMD if available
*/
void test_07_compute(double *input, double *output, int count)
{
int i;
/* Multiple operations to stress FP register usage */
for (i = 0; i < count; i++) {
double a = input[i];
double b = a * 1.234567;
double c = b + 0.987654;
double d = c * c;
double e = d - a;
output[i] = e / (a + 1.0);
}
}
void test_07(void)
{
fp_test_context ctx;
double input[16], output1[16], output2[16];
int i;
/* Initialize input with varying values */
for (i = 0; i < 16; i++) {
input[i] = (i + 1) * 3.14159 + 0.123456 * i;
}
/* First computation - fills FP registers */
test_07_compute(input, output1, 16);
/* Additional FP work to maximize register pressure */
double sum1 = 0.0, sum2 = 0.0;
for (i = 0; i < 16; i++) {
sum1 += output1[i] * (i + 1);
sum2 += input[i] / (i + 2);
}
/* Stack switch - all FP registers must be preserved */
memset(&ctx, 0, sizeof(ctx));
stackman_switch(&test_07_cb, &ctx);
assert(ctx.switch_done);
/* Second computation - should produce identical results */
test_07_compute(input, output2, 16);
/* Verify outputs match exactly */
for (i = 0; i < 16; i++) {
assert(output1[i] == output2[i]);
}
/* Verify accumulated sums still work */
double sum3 = 0.0, sum4 = 0.0;
for (i = 0; i < 16; i++) {
sum3 += output2[i] * (i + 1);
sum4 += input[i] / (i + 2);
}
assert(sum1 == sum3);
assert(sum2 == sum4);
}
/* Test integer register preservation across stack switches.
* This test verifies that callee-saved integer registers are properly
* preserved across stack switches on all platforms.
*
* Uses complex integer arithmetic with many intermediate values to
* pressure the compiler to use multiple registers.
*/
typedef struct int_test_context {
void *saved_sp;
int switch_done;
} int_test_context;
void *test_08_cb(void* context, int opcode, void *sp)
{
int_test_context *c = (int_test_context*)context;
if (opcode == STACKMAN_OP_SAVE) {
c->saved_sp = sp;
return sp; /* No stack switch */
} else {
c->switch_done = 1;
return sp;
}
}
/* Complex integer computation with many intermediate values
* to encourage use of multiple callee-saved registers
*/
void test_08_compute(long *input, long *output, int count)
{
int i;
/* Multiple operations creating intermediate values to stress register usage */
for (i = 0; i < count; i++) {
long a = input[i];
long b = a * 123456789L;
long c = b + 987654321L;
long d = c ^ (a << 3);
long e = d - (a * 7);
long f = e | (b & 0xFF00FF00);
long g = f + (c >> 2);
long h = g ^ (d * 13);
output[i] = h - (a + b + c);
}
}
void test_08(void)
{
int_test_context ctx;
long input[16], output1[16], output2[16];
int i;
/* Initialize input with varying values */
for (i = 0; i < 16; i++) {
input[i] = (long)(i + 1) * 314159L + i * 271828L;
}
/* First computation - fills integer registers */
test_08_compute(input, output1, 16);
/* Additional integer work to maximize register pressure */
long sum1 = 0, sum2 = 0, xor1 = 0;
for (i = 0; i < 16; i++) {
sum1 += output1[i] * (i + 1);
sum2 += input[i] << (i & 3);
xor1 ^= (output1[i] + input[i]);
}
/* Stack switch - all callee-saved integer registers must be preserved */
memset(&ctx, 0, sizeof(ctx));
stackman_switch(&test_08_cb, &ctx);
assert(ctx.switch_done);
/* Second computation - should produce identical results */
test_08_compute(input, output2, 16);
/* Verify outputs match exactly */
for (i = 0; i < 16; i++) {
assert(output1[i] == output2[i]);
}
/* Verify accumulated values still work */
long sum3 = 0, sum4 = 0, xor2 = 0;
for (i = 0; i < 16; i++) {
sum3 += output2[i] * (i + 1);
sum4 += input[i] << (i & 3);
xor2 ^= (output2[i] + input[i]);
}
assert(sum1 == sum3);
assert(sum2 == sum4);
assert(xor1 == xor2);
}
int main(int argc, char*argv[])
{
int stack_marker = 0;
test_01();
printf("test_01 ok\n");
test_02((void*)&stack_marker);
printf("test_02 ok\n");
test_03((void*)&stack_marker);
printf("test_03 ok\n");
#ifdef TEST_04
test_04();
printf("test_04 ok\n");
test_05();
printf("test_05 ok\n");
#endif
test_06();
printf("test_06 ok\n");
test_07();
printf("test_07 ok\n");
test_08();
printf("test_08 ok\n");
return 0;
}