[279] | 1 | #include <stdlib.h>
|
---|
| 2 | #include <stdio.h>
|
---|
[331] | 3 | #include <string.h>
|
---|
[279] | 4 |
|
---|
| 5 | #define NB_ITS 1000000
|
---|
| 6 | //#define NB_ITS 1
|
---|
| 7 | #define TAB_SIZE 100
|
---|
| 8 |
|
---|
| 9 | int tab[TAB_SIZE];
|
---|
| 10 | int ret_sum;
|
---|
| 11 | char tab3[256];
|
---|
| 12 |
|
---|
| 13 | int test1(void)
|
---|
| 14 | {
|
---|
| 15 | int i, sum = 0;
|
---|
| 16 | for(i=0;i<TAB_SIZE;i++) {
|
---|
| 17 | sum += tab[i];
|
---|
| 18 | }
|
---|
| 19 | return sum;
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | /* error */
|
---|
| 23 | int test2(void)
|
---|
| 24 | {
|
---|
| 25 | int i, sum = 0;
|
---|
| 26 | for(i=0;i<TAB_SIZE + 1;i++) {
|
---|
| 27 | sum += tab[i];
|
---|
| 28 | }
|
---|
| 29 | return sum;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /* actually, profiling test */
|
---|
| 33 | int test3(void)
|
---|
| 34 | {
|
---|
| 35 | int sum;
|
---|
| 36 | int i, it;
|
---|
| 37 |
|
---|
| 38 | sum = 0;
|
---|
| 39 | for(it=0;it<NB_ITS;it++) {
|
---|
| 40 | for(i=0;i<TAB_SIZE;i++) {
|
---|
| 41 | sum += tab[i];
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 | return sum;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /* ok */
|
---|
| 48 | int test4(void)
|
---|
| 49 | {
|
---|
| 50 | int i, sum = 0;
|
---|
| 51 | int *tab4;
|
---|
| 52 |
|
---|
[331] | 53 | fprintf(stderr, "%s start\n", __FUNCTION__);
|
---|
| 54 |
|
---|
[279] | 55 | tab4 = malloc(20 * sizeof(int));
|
---|
| 56 | for(i=0;i<20;i++) {
|
---|
| 57 | sum += tab4[i];
|
---|
| 58 | }
|
---|
| 59 | free(tab4);
|
---|
| 60 |
|
---|
[331] | 61 | fprintf(stderr, "%s end\n", __FUNCTION__);
|
---|
[279] | 62 | return sum;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /* error */
|
---|
| 66 | int test5(void)
|
---|
| 67 | {
|
---|
| 68 | int i, sum = 0;
|
---|
| 69 | int *tab4;
|
---|
| 70 |
|
---|
[331] | 71 | fprintf(stderr, "%s start\n", __FUNCTION__);
|
---|
| 72 |
|
---|
[279] | 73 | tab4 = malloc(20 * sizeof(int));
|
---|
| 74 | for(i=0;i<21;i++) {
|
---|
| 75 | sum += tab4[i];
|
---|
| 76 | }
|
---|
| 77 | free(tab4);
|
---|
| 78 |
|
---|
[331] | 79 | fprintf(stderr, "%s end\n", __FUNCTION__);
|
---|
[279] | 80 | return sum;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /* error */
|
---|
| 84 | /* XXX: currently: bug */
|
---|
| 85 | int test6(void)
|
---|
| 86 | {
|
---|
| 87 | int i, sum = 0;
|
---|
| 88 | int *tab4;
|
---|
| 89 |
|
---|
| 90 | tab4 = malloc(20 * sizeof(int));
|
---|
| 91 | free(tab4);
|
---|
| 92 | for(i=0;i<21;i++) {
|
---|
| 93 | sum += tab4[i];
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | return sum;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /* error */
|
---|
| 100 | int test7(void)
|
---|
| 101 | {
|
---|
| 102 | int i, sum = 0;
|
---|
| 103 | int *p;
|
---|
| 104 |
|
---|
| 105 | for(i=0;i<TAB_SIZE + 1;i++) {
|
---|
| 106 | p = &tab[i];
|
---|
| 107 | if (i == TAB_SIZE)
|
---|
| 108 | printf("i=%d %x\n", i, p);
|
---|
| 109 | sum += *p;
|
---|
| 110 | }
|
---|
| 111 | return sum;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /* ok */
|
---|
| 115 | int test8(void)
|
---|
| 116 | {
|
---|
| 117 | int i, sum = 0;
|
---|
| 118 | int tab[10];
|
---|
| 119 |
|
---|
| 120 | for(i=0;i<10;i++) {
|
---|
| 121 | sum += tab[i];
|
---|
| 122 | }
|
---|
| 123 | return sum;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /* error */
|
---|
| 127 | int test9(void)
|
---|
| 128 | {
|
---|
| 129 | int i, sum = 0;
|
---|
| 130 | char tab[10];
|
---|
| 131 |
|
---|
| 132 | for(i=0;i<11;i++) {
|
---|
| 133 | sum += tab[i];
|
---|
| 134 | }
|
---|
| 135 | return sum;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /* ok */
|
---|
| 139 | int test10(void)
|
---|
| 140 | {
|
---|
| 141 | char tab[10];
|
---|
| 142 | char tab1[10];
|
---|
| 143 |
|
---|
| 144 | memset(tab, 0, 10);
|
---|
| 145 | memcpy(tab, tab1, 10);
|
---|
| 146 | memmove(tab, tab1, 10);
|
---|
| 147 | return 0;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /* error */
|
---|
| 151 | int test11(void)
|
---|
| 152 | {
|
---|
| 153 | char tab[10];
|
---|
| 154 |
|
---|
| 155 | memset(tab, 0, 11);
|
---|
| 156 | return 0;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /* error */
|
---|
| 160 | int test12(void)
|
---|
| 161 | {
|
---|
| 162 | void *ptr;
|
---|
| 163 | ptr = malloc(10);
|
---|
| 164 | free(ptr);
|
---|
| 165 | free(ptr);
|
---|
| 166 | return 0;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /* error */
|
---|
| 170 | int test13(void)
|
---|
| 171 | {
|
---|
| 172 | char pad1 = 0;
|
---|
| 173 | char tab[10];
|
---|
| 174 | char pad2 = 0;
|
---|
| 175 | memset(tab, 'a', sizeof(tab));
|
---|
| 176 | return strlen(tab);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | int test14(void)
|
---|
| 180 | {
|
---|
| 181 | char *p = alloca(TAB_SIZE);
|
---|
| 182 | memset(p, 'a', TAB_SIZE);
|
---|
| 183 | p[TAB_SIZE-1] = 0;
|
---|
| 184 | return strlen(p);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /* error */
|
---|
| 188 | int test15(void)
|
---|
| 189 | {
|
---|
| 190 | char *p = alloca(TAB_SIZE-1);
|
---|
| 191 | memset(p, 'a', TAB_SIZE);
|
---|
| 192 | p[TAB_SIZE-1] = 0;
|
---|
| 193 | return strlen(p);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[331] | 196 | /* ok */
|
---|
| 197 | int test16()
|
---|
| 198 | {
|
---|
| 199 | char *demo = "This is only a test.";
|
---|
| 200 | char *p;
|
---|
| 201 |
|
---|
| 202 | fprintf(stderr, "%s start\n", __FUNCTION__);
|
---|
| 203 |
|
---|
| 204 | p = alloca(16);
|
---|
| 205 | strcpy(p,"12345678901234");
|
---|
| 206 | printf("alloca: p is %s\n", p);
|
---|
| 207 |
|
---|
| 208 | /* Test alloca embedded in a larger expression */
|
---|
| 209 | printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
|
---|
| 210 |
|
---|
| 211 | fprintf(stderr, "%s end\n", __FUNCTION__);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /* error */
|
---|
| 215 | int test17()
|
---|
| 216 | {
|
---|
| 217 | char *demo = "This is only a test.";
|
---|
| 218 | char *p;
|
---|
| 219 |
|
---|
| 220 | fprintf(stderr, "%s start\n", __FUNCTION__);
|
---|
| 221 |
|
---|
| 222 | p = alloca(16);
|
---|
| 223 | strcpy(p,"12345678901234");
|
---|
| 224 | printf("alloca: p is %s\n", p);
|
---|
| 225 |
|
---|
| 226 | /* Test alloca embedded in a larger expression */
|
---|
| 227 | printf("alloca: %s\n", strcpy(alloca(strlen(demo)),demo) );
|
---|
| 228 |
|
---|
| 229 | fprintf(stderr, "%s end\n", __FUNCTION__);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[279] | 232 | int (*table_test[])(void) = {
|
---|
| 233 | test1,
|
---|
| 234 | test2,
|
---|
| 235 | test3,
|
---|
| 236 | test4,
|
---|
| 237 | test5,
|
---|
| 238 | test6,
|
---|
| 239 | test7,
|
---|
| 240 | test8,
|
---|
| 241 | test9,
|
---|
| 242 | test10,
|
---|
| 243 | test11,
|
---|
| 244 | test12,
|
---|
| 245 | test13,
|
---|
| 246 | test14,
|
---|
| 247 | test15,
|
---|
[331] | 248 | test16,
|
---|
| 249 | test17,
|
---|
[279] | 250 | };
|
---|
| 251 |
|
---|
| 252 | int main(int argc, char **argv)
|
---|
| 253 | {
|
---|
| 254 | int index;
|
---|
| 255 | int (*ftest)(void);
|
---|
[331] | 256 | int index_max = sizeof(table_test)/sizeof(table_test[0]);
|
---|
[279] | 257 |
|
---|
| 258 | if (argc < 2) {
|
---|
[331] | 259 | printf(
|
---|
| 260 | "test TCC bound checking system\n"
|
---|
| 261 | "usage: boundtest N\n"
|
---|
| 262 | " 1 <= N <= %d\n", index_max);
|
---|
[279] | 263 | exit(1);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | index = 0;
|
---|
| 267 | if (argc >= 2)
|
---|
[331] | 268 | index = atoi(argv[1]) - 1;
|
---|
| 269 |
|
---|
| 270 | if ((index < 0) || (index >= index_max)) {
|
---|
| 271 | printf("N is outside of the valid range (%d)\n", index);
|
---|
| 272 | exit(2);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[279] | 275 | /* well, we also use bounds on this ! */
|
---|
| 276 | ftest = table_test[index];
|
---|
| 277 | ftest();
|
---|
| 278 |
|
---|
| 279 | return 0;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | /*
|
---|
| 283 | * without bound 0.77 s
|
---|
| 284 | * with bounds 4.73
|
---|
| 285 | */
|
---|