Fix Extended Asm ignored constraints
[tinycc.git] / tests / vla_test.c
blob3616c46d4423e7b19f1f00c5378dde14cc990e0c
1 /*
2 * Test that allocating a variable length array in a loop
3 * does not use up a linear amount of memory
4 */
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
10 #define LOOP_COUNT 1000
11 #define ARRAY_SIZE 100
13 /* Overwrite a VLA. This will overwrite the return address if SP is incorrect */
14 void smash(char *p, int n) {
15 memset(p, 0, n);
18 int test1(int n) {
19 int i;
20 char *array_ptrs[LOOP_COUNT];
22 for (i = 0; i < LOOP_COUNT; ++i) {
23 char test[n];
24 smash(test, n);
25 array_ptrs[i] = test;
28 return (array_ptrs[0]-array_ptrs[LOOP_COUNT-1] < n) ? 0 : 1;
31 /* ensure goto does not circumvent array free */
32 int test2(int n) {
33 char *array_ptrs[LOOP_COUNT];
35 int i = 0;
36 loop:;
37 char test[n];
38 smash(test, n);
39 if (i >= LOOP_COUNT)
40 goto end;
41 array_ptrs[i] = test;
42 ++i;
43 goto loop;
45 end:
46 smash(test, n);
47 char test2[n];
48 smash(test2, n);
49 return (array_ptrs[0] - array_ptrs[LOOP_COUNT-1] < n) ? 0 : 1;
52 int test3(int n) {
53 char test[n];
54 smash(test, n);
55 goto label;
56 label:
57 smash(test, n);
58 char test2[n];
59 smash(test2, n);
60 return (test-test2 >= n) ? 0 : 1;
63 #define RUN_TEST(t) \
64 if (!testname || (strcmp(#t, testname) == 0)) { \
65 fputs(#t "... ", stdout); \
66 fflush(stdout); \
67 if (t(ARRAY_SIZE) == 0) { \
68 fputs("success\n", stdout); \
69 } else { \
70 fputs("failure\n", stdout); \
71 retval = EXIT_FAILURE; \
72 } \
75 int main(int argc, char **argv) {
76 const char *testname = NULL;
77 int retval = EXIT_SUCCESS;
78 if (argc > 1)
79 testname = argv[1];
80 RUN_TEST(test1)
81 RUN_TEST(test2)
82 RUN_TEST(test3)
83 return retval;