source: EcnlProtoTool/trunk/tcc-0.9.27/tests/vla_test.c

Last change on this file was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 1.6 KB
Line 
1/*
2 * Test that allocating a variable length array in a loop
3 * does not use up a linear amount of memory
4 */
5
6#include <stdlib.h>
7#include <stdio.h>
8#include <string.h>
9
10#define LOOP_COUNT 1000
11#define ARRAY_SIZE 100
12
13/* Overwrite a VLA. This will overwrite the return address if SP is incorrect */
14void smash(char *p, int n) {
15 memset(p, 0, n);
16}
17
18int test1(int n) {
19 int i;
20 char *array_ptrs[LOOP_COUNT];
21
22 for (i = 0; i < LOOP_COUNT; ++i) {
23 char test[n];
24 smash(test, n);
25 array_ptrs[i] = test;
26 }
27
28 return (array_ptrs[0]-array_ptrs[LOOP_COUNT-1] < n) ? 0 : 1;
29}
30
31/* ensure goto does not circumvent array free */
32int test2(int n) {
33 char *array_ptrs[LOOP_COUNT];
34
35 int i = 0;
36loop:;
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;
44
45end:
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;
50}
51
52int test3(int n) {
53 char test[n];
54 smash(test, n);
55 goto label;
56label:
57 smash(test, n);
58 char test2[n];
59 smash(test2, n);
60 return (test-test2 >= n) ? 0 : 1;
61}
62
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 } \
73 }
74
75int 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;
84}
Note: See TracBrowser for help on using the repository browser.