aboutsummaryrefslogtreecommitdiff
path: root/tests/vla_test.c
diff options
context:
space:
mode:
authorJames Lyon <jamesly0n@hotmail.com>2013-04-27 20:39:34 +0100
committerJames Lyon <jamesly0n@hotmail.com>2013-04-27 22:58:52 +0100
commit41b3c7a50727f715111d09af982e2e9a0deeb688 (patch)
tree0192d5295fd3cfca625ba769be2188ef72b15d64 /tests/vla_test.c
parent6ee366e765483f6b48537d97fc81ae56f13d1b7f (diff)
downloadtinycc-41b3c7a50727f715111d09af982e2e9a0deeb688.tar.gz
tinycc-41b3c7a50727f715111d09af982e2e9a0deeb688.tar.bz2
Improved variable length array support.
VLA storage is now freed when it goes out of scope. This makes it possible to use a VLA inside a loop without consuming an unlimited amount of memory. Combining VLAs with alloca() should work as in GCC - when a VLA is freed, memory allocated by alloca() after the VLA was created is also freed. There are some exceptions to this rule when using goto: if a VLA is in scope at the goto, jumping to a label will reset the stack pointer to where it was immediately after the last VLA was created prior to the label, or to what it was before the first VLA was created if the label is outside the scope of any VLA. This means that in some cases combining alloca() and VLAs will free alloca() memory where GCC would not.
Diffstat (limited to 'tests/vla_test.c')
-rw-r--r--tests/vla_test.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/tests/vla_test.c b/tests/vla_test.c
new file mode 100644
index 0000000..3616c46
--- /dev/null
+++ b/tests/vla_test.c
@@ -0,0 +1,84 @@
+/*
+ * Test that allocating a variable length array in a loop
+ * does not use up a linear amount of memory
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#define LOOP_COUNT 1000
+#define ARRAY_SIZE 100
+
+/* Overwrite a VLA. This will overwrite the return address if SP is incorrect */
+void smash(char *p, int n) {
+ memset(p, 0, n);
+}
+
+int test1(int n) {
+ int i;
+ char *array_ptrs[LOOP_COUNT];
+
+ for (i = 0; i < LOOP_COUNT; ++i) {
+ char test[n];
+ smash(test, n);
+ array_ptrs[i] = test;
+ }
+
+ return (array_ptrs[0]-array_ptrs[LOOP_COUNT-1] < n) ? 0 : 1;
+}
+
+/* ensure goto does not circumvent array free */
+int test2(int n) {
+ char *array_ptrs[LOOP_COUNT];
+
+ int i = 0;
+loop:;
+ char test[n];
+ smash(test, n);
+ if (i >= LOOP_COUNT)
+ goto end;
+ array_ptrs[i] = test;
+ ++i;
+ goto loop;
+
+end:
+ smash(test, n);
+ char test2[n];
+ smash(test2, n);
+ return (array_ptrs[0] - array_ptrs[LOOP_COUNT-1] < n) ? 0 : 1;
+}
+
+int test3(int n) {
+ char test[n];
+ smash(test, n);
+ goto label;
+label:
+ smash(test, n);
+ char test2[n];
+ smash(test2, n);
+ return (test-test2 >= n) ? 0 : 1;
+}
+
+#define RUN_TEST(t) \
+ if (!testname || (strcmp(#t, testname) == 0)) { \
+ fputs(#t "... ", stdout); \
+ fflush(stdout); \
+ if (t(ARRAY_SIZE) == 0) { \
+ fputs("success\n", stdout); \
+ } else { \
+ fputs("failure\n", stdout); \
+ retval = EXIT_FAILURE; \
+ } \
+ }
+
+int main(int argc, char **argv) {
+ const char *testname = NULL;
+ int retval = EXIT_SUCCESS;
+ if (argc > 1)
+ testname = argv[1];
+ RUN_TEST(test1)
+ RUN_TEST(test2)
+ RUN_TEST(test3)
+ return retval;
+}