diff --git a/arm-gen.c b/arm-gen.c index a2974d5a..b925fe33 100644 --- a/arm-gen.c +++ b/arm-gen.c @@ -2315,7 +2315,12 @@ ST_FUNC void gen_vla_sp_restore(int addr) { /* Subtract from the stack pointer, and push the resulting value onto the stack */ ST_FUNC void gen_vla_alloc(CType *type, int align) { - int r = intr(gv(RC_INT)); + int r; +#if defined(CONFIG_TCC_BCHECK) + if (tcc_state->do_bounds_check) + vpushv(vtop); +#endif + r = intr(gv(RC_INT)); o(0xE04D0000|(r<<12)|r); /* sub r, sp, r */ #ifdef TCC_ARM_EABI if (align < 8) @@ -2328,6 +2333,18 @@ ST_FUNC void gen_vla_alloc(CType *type, int align) { tcc_error("alignment is not a power of 2: %i", align); o(stuff_const(0xE3C0D000|(r<<16), align - 1)); /* bic sp, r, #align-1 */ vpop(); +#if defined(CONFIG_TCC_BCHECK) + if (tcc_state->do_bounds_check) { + vpushi(0); + vtop->r = TREG_R0; + o(0xe1a0000d | (vtop->r << 12)); // mov r0,sp + vswap(); + vpush_global_sym(&func_old_type, TOK___bound_new_region); + vrott(3); + gfunc_call(2); + func_bound_add_epilog = 1; + } +#endif } /* end of ARM code generator */ diff --git a/arm64-gen.c b/arm64-gen.c index 8edb9e80..34659c5b 100644 --- a/arm64-gen.c +++ b/arm64-gen.c @@ -2049,11 +2049,28 @@ ST_FUNC void gen_vla_sp_restore(int addr) { } ST_FUNC void gen_vla_alloc(CType *type, int align) { - uint32_t r = intr(gv(RC_INT)); + uint32_t r; +#if defined(CONFIG_TCC_BCHECK) + if (tcc_state->do_bounds_check) + vpushv(vtop); +#endif + r = intr(gv(RC_INT)); o(0x91003c00 | r | r << 5); // add x(r),x(r),#15 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15 o(0xcb2063ff | r << 16); // sub sp,sp,x(r) vpop(); +#if defined(CONFIG_TCC_BCHECK) + if (tcc_state->do_bounds_check) { + vpushi(0); + vtop->r = TREG_R(0); + o(0x910003e0 | vtop->r); // mov r0,sp + vswap(); + vpush_global_sym(&func_old_type, TOK___bound_new_region); + vrott(3); + gfunc_call(2); + func_bound_add_epilog = 1; + } +#endif } /* end of A64 code generator */ diff --git a/riscv64-gen.c b/riscv64-gen.c index 4d7f366b..37677027 100644 --- a/riscv64-gen.c +++ b/riscv64-gen.c @@ -1355,10 +1355,27 @@ ST_FUNC void gen_vla_sp_restore(int addr) ST_FUNC void gen_vla_alloc(CType *type, int align) { - int rr = ireg(gv(RC_INT)); + int rr; +#if defined(CONFIG_TCC_BCHECK) + if (tcc_state->do_bounds_check) + vpushv(vtop); +#endif + rr = ireg(gv(RC_INT)); EI(0x13, 0, rr, rr, 15); // addi RR, RR, 15 EI(0x13, 7, rr, rr, -16); // andi, RR, RR, -16 ER(0x33, 0, 2, 2, rr, 0x20); // sub sp, sp, rr vpop(); +#if defined(CONFIG_TCC_BCHECK) + if (tcc_state->do_bounds_check) { + vpushi(0); + vtop->r = TREG_R(0); + o(0x00010513); /* mv a0,sp */ + vswap(); + vpush_global_sym(&func_old_type, TOK___bound_new_region); + vrott(3); + gfunc_call(2); + func_bound_add_epilog = 1; + } +#endif } #endif diff --git a/tcctok.h b/tcctok.h index 61e17485..7c2c2c14 100644 --- a/tcctok.h +++ b/tcctok.h @@ -306,6 +306,7 @@ DEF(TOK___bound_local_new, "__bound_local_new") DEF(TOK___bound_local_delete, "__bound_local_delete") DEF(TOK___bound_setjmp, "__bound_setjmp") + DEF(TOK___bound_new_region, "__bound_new_region") # ifdef TCC_TARGET_PE # ifdef TCC_TARGET_X86_64 DEF(TOK___bound_alloca_nr, "__bound_alloca_nr") diff --git a/tests/Makefile b/tests/Makefile index afdcdcfb..dfda50c1 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -179,7 +179,7 @@ memtest: # memory and bound check auto test BOUNDS_OK = 1 4 8 10 14 16 -BOUNDS_FAIL= 2 5 6 7 9 11 12 13 15 17 +BOUNDS_FAIL= 2 5 6 7 9 11 12 13 15 17 18 btest: boundtest.c @echo ------------ $@ ------------ diff --git a/tests/boundtest.c b/tests/boundtest.c index 50f3d30e..0d833950 100644 --- a/tests/boundtest.c +++ b/tests/boundtest.c @@ -231,6 +231,16 @@ int test17() return 0; } +int test18(void) +{ + int i, sum = 0, n = TAB_SIZE; + int tab[n]; + for(i=0;i