Add vla bound support for arm,arm64 and riscv64

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
This commit is contained in:
herman ten brugge 2020-06-17 11:24:17 +02:00
parent 44019e874f
commit 8fb8d88ea6
6 changed files with 68 additions and 5 deletions

View File

@ -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 */

View File

@ -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 */

View File

@ -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

View File

@ -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")

View File

@ -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 ------------ $@ ------------

View File

@ -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<TAB_SIZE+1;i++) {
sum += tab[i];
}
return sum;
}
int (*table_test[])(void) = {
test1,
test2,
@ -248,7 +258,8 @@ int (*table_test[])(void) = {
test14,
test15,
test16,
test17
test17,
test18
};
int main(int argc, char **argv)