From 5bc1720776f9f018225dd1c12bd23405a54bd73f Mon Sep 17 00:00:00 2001 From: grischka Date: Tue, 5 May 2020 14:36:35 +0200 Subject: [PATCH] tccgen.c: move 'alloca_used' complication to *-gen files related to commit 8370bc03a1c9d8b93e476420039ff8200229e5b3 --- i386-gen.c | 13 ++++++++++--- tcc.h | 1 - tccgen.c | 11 ----------- x86_64-gen.c | 31 ++++++++++++++++++++----------- 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/i386-gen.c b/i386-gen.c index 2e29a6c2..a44521de 100644 --- a/i386-gen.c +++ b/i386-gen.c @@ -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; diff --git a/tcc.h b/tcc.h index 2b2e577f..55422ae4 100644 --- a/tcc.h +++ b/tcc.h @@ -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; diff --git a/tccgen.c b/tccgen.c index 511cd8b5..f3015b27 100644 --- a/tccgen.c +++ b/tccgen.c @@ -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, diff --git a/x86_64-gen.c b/x86_64-gen.c index 1337ddd6..7a192a38 100644 --- a/x86_64-gen.c +++ b/x86_64-gen.c @@ -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 */