tcctok.h: - Add __bound_new_region arm-gen.c arm64-gen.c riscv64-gen.c: - Add bound checking support to gen_vla_alloc tests/Makefile boundtest.c - Add test18 vla bound checking test
Unified · Split
| @@ -2315,7 +2315,12 @@ ST_FUNC void gen_vla_sp_restore(int addr) { · 2314 unmodified lines | ||
| 2315 | 2315 | |
| 2316 | 2316 | /* Subtract from the stack pointer, and push the resulting value onto the stack */ |
| 2317 | 2317 | ST_FUNC void gen_vla_alloc(CType *type, int align) { |
| 2318 | − int r = intr(gv(RC_INT)); | |
| 2318 | + int r; | |
| 2319 | +#if defined(CONFIG_TCC_BCHECK) | |
| 2320 | + if (tcc_state->do_bounds_check) | |
| 2321 | + vpushv(vtop); | |
| 2322 | +#endif | |
| 2323 | + r = intr(gv(RC_INT)); | |
| 2319 | 2324 | o(0xE04D0000|(r<<12)|r); /* sub r, sp, r */ |
| 2320 | 2325 | #ifdef TCC_ARM_EABI |
| 2321 | 2326 | if (align < 8) |
| @@ -2328,6 +2333,18 @@ ST_FUNC void gen_vla_alloc(CType *type, int align) { · 6 unmodified lines | ||
| 2328 | 2333 | tcc_error("alignment is not a power of 2: %i", align); |
| 2329 | 2334 | o(stuff_const(0xE3C0D000|(r<<16), align - 1)); /* bic sp, r, #align-1 */ |
| 2330 | 2335 | vpop(); |
| 2336 | +#if defined(CONFIG_TCC_BCHECK) | |
| 2337 | + if (tcc_state->do_bounds_check) { | |
| 2338 | + vpushi(0); | |
| 2339 | + vtop->r = TREG_R0; | |
| 2340 | + o(0xe1a0000d | (vtop->r << 12)); // mov r0,sp | |
| 2341 | + vswap(); | |
| 2342 | + vpush_global_sym(&func_old_type, TOK___bound_new_region); | |
| 2343 | + vrott(3); | |
| 2344 | + gfunc_call(2); | |
| 2345 | + func_bound_add_epilog = 1; | |
| 2346 | + } | |
| 2347 | +#endif | |
| 2331 | 2348 | } |
| 2332 | 2349 | |
| 2333 | 2350 | /* end of ARM code generator */ |
| @@ -2049,11 +2049,28 @@ ST_FUNC void gen_vla_sp_restore(int addr) { · 2048 unmodified lines | ||
| 2049 | 2049 | } |
| 2050 | 2050 | |
| 2051 | 2051 | ST_FUNC void gen_vla_alloc(CType *type, int align) { |
| 2052 | − uint32_t r = intr(gv(RC_INT)); | |
| 2052 | + uint32_t r; | |
| 2053 | +#if defined(CONFIG_TCC_BCHECK) | |
| 2054 | + if (tcc_state->do_bounds_check) | |
| 2055 | + vpushv(vtop); | |
| 2056 | +#endif | |
| 2057 | + r = intr(gv(RC_INT)); | |
| 2053 | 2058 | o(0x91003c00 | r | r << 5); // add x(r),x(r),#15 |
| 2054 | 2059 | o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15 |
| 2055 | 2060 | o(0xcb2063ff | r << 16); // sub sp,sp,x(r) |
| 2056 | 2061 | vpop(); |
| 2062 | +#if defined(CONFIG_TCC_BCHECK) | |
| 2063 | + if (tcc_state->do_bounds_check) { | |
| 2064 | + vpushi(0); | |
| 2065 | + vtop->r = TREG_R(0); | |
| 2066 | + o(0x910003e0 | vtop->r); // mov r0,sp | |
| 2067 | + vswap(); | |
| 2068 | + vpush_global_sym(&func_old_type, TOK___bound_new_region); | |
| 2069 | + vrott(3); | |
| 2070 | + gfunc_call(2); | |
| 2071 | + func_bound_add_epilog = 1; | |
| 2072 | + } | |
| 2073 | +#endif | |
| 2057 | 2074 | } |
| 2058 | 2075 | |
| 2059 | 2076 | /* end of A64 code generator */ |
| @@ -1355,10 +1355,27 @@ ST_FUNC void gen_vla_sp_restore(int addr) · 1354 unmodified lines | ||
| 1355 | 1355 | |
| 1356 | 1356 | ST_FUNC void gen_vla_alloc(CType *type, int align) |
| 1357 | 1357 | { |
| 1358 | − int rr = ireg(gv(RC_INT)); | |
| 1358 | + int rr; | |
| 1359 | +#if defined(CONFIG_TCC_BCHECK) | |
| 1360 | + if (tcc_state->do_bounds_check) | |
| 1361 | + vpushv(vtop); | |
| 1362 | +#endif | |
| 1363 | + rr = ireg(gv(RC_INT)); | |
| 1359 | 1364 | EI(0x13, 0, rr, rr, 15); // addi RR, RR, 15 |
| 1360 | 1365 | EI(0x13, 7, rr, rr, -16); // andi, RR, RR, -16 |
| 1361 | 1366 | ER(0x33, 0, 2, 2, rr, 0x20); // sub sp, sp, rr |
| 1362 | 1367 | vpop(); |
| 1368 | +#if defined(CONFIG_TCC_BCHECK) | |
| 1369 | + if (tcc_state->do_bounds_check) { | |
| 1370 | + vpushi(0); | |
| 1371 | + vtop->r = TREG_R(0); | |
| 1372 | + o(0x00010513); /* mv a0,sp */ | |
| 1373 | + vswap(); | |
| 1374 | + vpush_global_sym(&func_old_type, TOK___bound_new_region); | |
| 1375 | + vrott(3); | |
| 1376 | + gfunc_call(2); | |
| 1377 | + func_bound_add_epilog = 1; | |
| 1378 | + } | |
| 1379 | +#endif | |
| 1363 | 1380 | } |
| 1364 | 1381 | #endif |
| @@ -306,6 +306,7 @@ · 305 unmodified lines | ||
| 306 | 306 | DEF(TOK___bound_local_new, "__bound_local_new") |
| 307 | 307 | DEF(TOK___bound_local_delete, "__bound_local_delete") |
| 308 | 308 | DEF(TOK___bound_setjmp, "__bound_setjmp") |
| 309 | + DEF(TOK___bound_new_region, "__bound_new_region") | |
| 309 | 310 | # ifdef TCC_TARGET_PE |
| 310 | 311 | # ifdef TCC_TARGET_X86_64 |
| 311 | 312 | DEF(TOK___bound_alloca_nr, "__bound_alloca_nr") |
| @@ -179,7 +179,7 @@ memtest: · 178 unmodified lines | ||
| 179 | 179 | |
| 180 | 180 | # memory and bound check auto test |
| 181 | 181 | BOUNDS_OK = 1 4 8 10 14 16 |
| 182 | −BOUNDS_FAIL= 2 5 6 7 9 11 12 13 15 17 | |
| 182 | +BOUNDS_FAIL= 2 5 6 7 9 11 12 13 15 17 18 | |
| 183 | 183 | |
| 184 | 184 | btest: boundtest.c |
| 185 | 185 | @echo ------------ $@ ------------ |
| @@ -231,6 +231,16 @@ int test17() · 230 unmodified lines | ||
| 231 | 231 | return 0; |
| 232 | 232 | } |
| 233 | 233 | |
| 234 | +int test18(void) | |
| 235 | +{ | |
| 236 | + int i, sum = 0, n = TAB_SIZE; | |
| 237 | + int tab[n]; | |
| 238 | + for(i=0;i<TAB_SIZE+1;i++) { | |
| 239 | + sum += tab[i]; | |
| 240 | + } | |
| 241 | + return sum; | |
| 242 | +} | |
| 243 | + | |
| 234 | 244 | int (*table_test[])(void) = { |
| 235 | 245 | test1, |
| 236 | 246 | test2, |
| @@ -248,7 +258,8 @@ int (*table_test[])(void) = { · 11 unmodified lines | ||
| 248 | 258 | test14, |
| 249 | 259 | test15, |
| 250 | 260 | test16, |
| 251 | − test17 | |
| 261 | + test17, | |
| 262 | + test18 | |
| 252 | 263 | }; |
| 253 | 264 | |
| 254 | 265 | int main(int argc, char **argv) |