tccgen.c: move 'alloca_used' complication to *-gen files

related to commit 8370bc03a1
mob
grischka 2020-05-05 14:36:35 +02:00
parent 8de7c092f0
commit 5bc1720776
4 changed files with 30 additions and 26 deletions

View File

@ -97,6 +97,7 @@ static int func_ret_sub;
#ifdef CONFIG_TCC_BCHECK
static addr_t func_bound_offset;
static unsigned long func_bound_ind;
static int func_bound_alloca_used;
static void gen_bounds_prolog(void);
static void gen_bounds_epilog(void);
#endif
@ -357,6 +358,10 @@ static void gcall_or_jmp(int is_jmp)
/* constant and relocation case */
greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32);
oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
#ifdef CONFIG_TCC_BCHECK
if (tcc_state->do_bounds_check && vtop->sym->v == TOK_alloca)
func_bound_alloca_used = 1;
#endif
} else {
/* otherwise, indirect call */
r = gv(RC_INT);
@ -588,9 +593,7 @@ ST_FUNC void gfunc_epilog(void)
addr_t v, saved_ind;
#ifdef CONFIG_TCC_BCHECK
if (tcc_state->do_bounds_check &&
(func_bound_offset != lbounds_section->data_offset ||
tcc_state->alloca_vla_used))
if (tcc_state->do_bounds_check)
gen_bounds_epilog();
#endif
@ -1068,6 +1071,7 @@ static void gen_bounds_prolog(void)
/* leave some room for bound checking code */
func_bound_offset = lbounds_section->data_offset;
func_bound_ind = ind;
func_bound_alloca_used = 0;
oad(0xb8, 0); /* lbound section pointer */
oad(0xb8, 0); /* call to function */
}
@ -1078,6 +1082,9 @@ static void gen_bounds_epilog(void)
addr_t *bounds_ptr;
Sym *sym_data;
if (func_bound_offset == lbounds_section->data_offset && !func_bound_alloca_used)
return;
/* add end of table info */
bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
*bounds_ptr = 0;

1
tcc.h
View File

@ -838,7 +838,6 @@ struct TCCState {
/* bound check related sections */
Section *bounds_section; /* contains global data bound description */
Section *lbounds_section; /* contains local data bound description */
int alloca_vla_used;
#endif
/* symbol sections */
Section *symtab_section;

View File

@ -5914,11 +5914,6 @@ special_math_val:
Sym *sa;
int nb_args, ret_nregs, ret_align, regsize, variadic;
#ifdef CONFIG_TCC_BCHECK
tcc_state->alloca_vla_used |= tcc_state->do_bounds_check &&
(vtop->r & VT_SYM) &&
vtop->sym->v == TOK_alloca;
#endif
/* function call */
if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
/* pointer test (no array accepted) */
@ -7913,9 +7908,6 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
gen_vla_sp_save(addr);
cur_scope->vla.loc = addr;
cur_scope->vla.num++;
#ifdef CONFIG_TCC_BCHECK
tcc_state->alloca_vla_used |= bcheck;
#endif
} else if (has_init) {
size_t oldreloc_offset = 0;
if (sec && sec->reloc)
@ -7948,9 +7940,6 @@ static void gen_function(Sym *sym)
cur_scope = root_scope = &f;
nocode_wanted = 0;
#ifdef CONFIG_TCC_BCHECK
tcc_state->alloca_vla_used = 0;
#endif
ind = cur_text_section->data_offset;
if (sym->a.aligned) {
size_t newoff = section_add(cur_text_section, 0,

View File

@ -146,6 +146,16 @@ ST_DATA const int reg_classes[NB_REGS] = {
static unsigned long func_sub_sp_offset;
static int func_ret_sub;
#if defined(CONFIG_TCC_BCHECK)
static addr_t func_bound_offset;
static unsigned long func_bound_ind;
static int func_bound_alloca_used;
#endif
#ifdef TCC_TARGET_PE
static int func_scratch, func_alloca;
#endif
/* XXX: make it faster ? */
ST_FUNC void g(int c)
{
@ -626,6 +636,10 @@ static void gcall_or_jmp(int is_jmp)
greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PLT32, (int)(vtop->c.i-4));
#endif
oad(0xe8 + is_jmp, 0); /* call/jmp im */
#ifdef CONFIG_TCC_BCHECK
if (tcc_state->do_bounds_check && vtop->sym->v == TOK_alloca)
func_bound_alloca_used = 1;
#endif
} else {
/* otherwise, indirect call */
r = TREG_R11;
@ -637,8 +651,6 @@ static void gcall_or_jmp(int is_jmp)
}
#if defined(CONFIG_TCC_BCHECK)
static addr_t func_bound_offset;
static unsigned long func_bound_ind;
static void gen_bounds_call(int v)
{
@ -713,6 +725,7 @@ static void gen_bounds_prolog(void)
/* leave some room for bound checking code */
func_bound_offset = lbounds_section->data_offset;
func_bound_ind = ind;
func_bound_alloca_used = 0;
o(0xb848 + TREG_FASTCALL_1 * 0x100); /*lbound section pointer */
gen_le64 (0);
oad(0xb8, 0); /* call to function */
@ -724,6 +737,9 @@ static void gen_bounds_epilog(void)
addr_t *bounds_ptr;
Sym *sym_data;
if (func_bound_offset == lbounds_section->data_offset && !func_bound_alloca_used)
return;
/* add end of table info */
bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
*bounds_ptr = 0;
@ -750,8 +766,6 @@ static void gen_bounds_epilog(void)
#ifdef TCC_TARGET_PE
static int func_scratch, func_alloca;
#define REGN 4
static const uint8_t arg_regs[REGN] = {
TREG_RCX, TREG_RDX, TREG_R8, TREG_R9
@ -948,7 +962,6 @@ void gfunc_call(int nb_args)
if (tcc_state->do_bounds_check)
gen_bounds_call(TOK___bound_alloca_nr); /* new region */
#endif
}
vtop--;
}
@ -1041,9 +1054,7 @@ void gfunc_epilog(void)
loc = (loc & -16) - func_scratch;
#ifdef CONFIG_TCC_BCHECK
if (tcc_state->do_bounds_check &&
(func_bound_offset != lbounds_section->data_offset ||
tcc_state->alloca_vla_used))
if (tcc_state->do_bounds_check)
gen_bounds_epilog();
#endif
@ -1623,9 +1634,7 @@ void gfunc_epilog(void)
int v, saved_ind;
#ifdef CONFIG_TCC_BCHECK
if (tcc_state->do_bounds_check &&
(func_bound_offset != lbounds_section->data_offset ||
tcc_state->alloca_vla_used))
if (tcc_state->do_bounds_check)
gen_bounds_epilog();
#endif
o(0xc9); /* leave */