On 2020-01-19 02:01, grischka wrote:
Herman ten Brugge via Tinycc-devel wrote:
Thanks for doing this. This was indeed needed.
There are 2 problems I see.
The first one is that you removed the alloca/vla code for bounds
checking.
The following program:
extern void *alloca(int size);
void tst1(void) { void *a = alloca(16); }
void tst2(void) { int i; int a[10]; for (i=0;i<10;i++)a[i]=0; }
int main(void) { tst1(); tst2(); }
When compiled with bounds checking it fails. The alloca
is not deleted because no __bound_local_delete is generated.
The original code in tccgen.c created a dummy lbounds section
so that __bound_local_new(not needed) and __bound_local_delete
were generated.
How many dummy entries do you need to make sure __bound_local_delete
is called, and do you need any at all, really?
I need only 1. I modified the code. See attached patch.
This solves both problems.
There is no alloca_free() so I need a trigger to remove the alloca/vla data.
The other problem is that testcase 112 produces on linux:
# ./tcc -b -dt 112_backtrace.c -o 112_backtrace -Dtest_bcheck_100
# ./112_backtrace
112_backtrace.c:107: at main: BCHECK: 0x7ffed562f6e4 is outside of the
region
112_backtrace.c:107: at main: BCHECK: invalid pointer 0x7ffed562f6da,
size 0xa in memcpy dest
This looks correct.
But on windows (and wine) I get:
# ./tcc.exe -b -dt 112_backtrace.c -o 112_backtrace.exe
-Dtest_bcheck_100
# ./112_backtrace.exe
00401535 : at ???: BCHECK: 000000000022FA84 is outside of the region
00404447 : by ???
00404666 : by ???
112_backtrace.c:107: by main
0040447a : at ???: BCHECK: invalid pointer 000000000022FA7A, size 0xa in
memcpy dest
00404666 : by ???
112_backtrace.c:107: by main
That looks like missing -g when bcheck.c was compiled.
Oops. Need more coffee.
Fixed the Makefile.
Btw, there seems to be a bug in the sym-version code that crashes
tcc when it tries to link with a .so, for example:
echo "void f() {}" | tcc - -shared -o a.so
echo "main() {}" | tcc - a.so
<segmentation fault>
I cannot reproduce this. I checkout a fresh copy and see no problem.
I use Fedora 31 with all updates.
Regards,
Herman
diff --git a/lib/bcheck.c b/lib/bcheck.c
index 1e90aa9..a338caa 100644
--- a/lib/bcheck.c
+++ b/lib/bcheck.c
@@ -449,7 +449,7 @@ void FASTCALL __bound_local_new(void *p1)
dprintf(stderr, "%s, %s(): p1=%p fp=%p\n",
__FILE__, __FUNCTION__, p, (void *)fp);
WAIT_SEM ();
- while ((addr = p[0])) {
+ while ((addr = p[0]) > 1) {
INCR_COUNT(bound_local_new_count);
tree = splay_insert(addr + fp, p[1], tree);
p += 2;
@@ -486,33 +486,32 @@ void FASTCALL __bound_local_delete(void *p1)
dprintf(stderr, "%s, %s(): p1=%p fp=%p\n",
__FILE__, __FUNCTION__, p, (void *)fp);
WAIT_SEM ();
- while ((addr = p[0])) {
+ while ((addr = p[0]) > 1) {
INCR_COUNT(bound_local_delete_count);
tree = splay_delete(addr + fp, tree);
p += 2;
}
- {
- alloca_list_type *last = NULL;
- alloca_list_type *cur = alloca_list;
-
- while (cur) {
- if (cur->fp == fp) {
- if (last)
- last->next = cur->next;
- else
- alloca_list = cur->next;
- tree = splay_delete ((size_t) cur->p, tree);
- cur->next = free_list;
- free_list = cur;
- cur = last ? last->next : alloca_list;
- }
- else {
- last = cur;
- cur = cur->next;
- }
+ if (addr == 1) {
+ alloca_list_type *last = NULL;
+ alloca_list_type *cur = alloca_list;
+
+ while (cur) {
+ if (cur->fp == fp) {
+ if (last)
+ last->next = cur->next;
+ else
+ alloca_list = cur->next;
+ tree = splay_delete ((size_t) cur->p, tree);
+ cur->next = free_list;
+ free_list = cur;
+ cur = last ? last->next : alloca_list;
+ }
+ else {
+ last = cur;
+ cur = cur->next;
}
+ }
}
-
POST_SEM ();
while (free_list) {
alloca_list_type *next = free_list->next;
diff --git a/tccgen.c b/tccgen.c
index 71d3d04..8a5cc6c 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -46,6 +46,7 @@ static int local_scope;
static int in_sizeof;
static int in_generic;
static int section_sym;
+static int alloc_vla_used;
ST_DATA SValue *vtop;
static SValue _vstack[1 + VSTACK_SIZE];
@@ -1621,6 +1622,20 @@ static void pop_local_syms(Sym **ptop, Sym *b, int keep,
int ellipsis)
sym_pop(ptop, b, keep);
}
+static void bound_add_alloc_vla(void)
+{
+#ifdef CONFIG_TCC_BCHECK
+ if (tcc_state->do_bounds_check && alloc_vla_used) {
+ addr_t *bounds_ptr;
+
+ /* Make sure __bound_local_delete is called */
+ bounds_ptr = section_ptr_add(lbounds_section, 4 * sizeof(addr_t));
+ bounds_ptr[0] = 1; /* marks alloca/vla used */
+ bounds_ptr[1] = 0;
+ }
+#endif
+}
+
static void incr_bf_adr(int o)
{
vtop->type = char_pointer_type;
@@ -5545,6 +5560,10 @@ special_math_val:
Sym *sa;
int nb_args, ret_nregs, ret_align, regsize, variadic;
+#ifdef CONFIG_TCC_BCHECK
+ alloc_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) */
@@ -7580,6 +7599,9 @@ 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
+ alloc_vla_used |= bcheck;
+#endif
} else if (has_init) {
size_t oldreloc_offset = 0;
if (sec && sec->reloc)
@@ -7612,6 +7634,7 @@ static void gen_function(Sym *sym)
cur_scope = root_scope = &f;
nocode_wanted = 0;
+ alloc_vla_used = 0;
ind = cur_text_section->data_offset;
if (sym->a.aligned) {
size_t newoff = section_add(cur_text_section, 0,
@@ -7641,6 +7664,7 @@ static void gen_function(Sym *sym)
/* reset local stack */
pop_local_syms(&local_stack, NULL, 0,
sym->type.ref->f.func_type == FUNC_ELLIPSIS);
+ bound_add_alloc_vla();
gfunc_epilog();
cur_text_section->data_offset = ind;
local_scope = 0;
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel