bcheck: restore malloc hooks when done

This commit is contained in:
grischka 2009-07-06 21:10:14 +02:00 committed by unknown
parent c93ddac9aa
commit 0085c648f6
4 changed files with 39 additions and 29 deletions

View File

@ -423,6 +423,11 @@ void __bound_init(void)
} }
} }
void __bound_exit(void)
{
restore_malloc_hooks();
}
static inline void add_region(BoundEntry *e, static inline void add_region(BoundEntry *e,
unsigned long start, unsigned long size) unsigned long start, unsigned long size)
{ {

View File

@ -110,6 +110,7 @@ static int tcc_ext = 1;
#ifdef CONFIG_TCC_BACKTRACE #ifdef CONFIG_TCC_BACKTRACE
int num_callers = 6; int num_callers = 6;
const char **rt_bound_error_msg; const char **rt_bound_error_msg;
unsigned long rt_prog_main;
#endif #endif
/* XXX: get rid of this ASAP */ /* XXX: get rid of this ASAP */
@ -1340,7 +1341,7 @@ static void asm_global_instr(void)
#ifdef CONFIG_TCC_BACKTRACE #ifdef CONFIG_TCC_BACKTRACE
/* print the position in the source file of PC value 'pc' by reading /* print the position in the source file of PC value 'pc' by reading
the stabs debug information */ the stabs debug information */
static void rt_printline(unsigned long wanted_pc) static unsigned long rt_printline(unsigned long wanted_pc)
{ {
Stab_Sym *sym, *sym_end; Stab_Sym *sym, *sym_end;
char func_name[128], last_func_name[128]; char func_name[128], last_func_name[128];
@ -1438,6 +1439,7 @@ static void rt_printline(unsigned long wanted_pc)
wanted_pc < sym->st_value + sym->st_size) { wanted_pc < sym->st_value + sym->st_size) {
pstrcpy(last_func_name, sizeof(last_func_name), pstrcpy(last_func_name, sizeof(last_func_name),
strtab_section->data + sym->st_name); strtab_section->data + sym->st_name);
func_addr = sym->st_value;
goto found; goto found;
} }
} }
@ -1445,7 +1447,7 @@ static void rt_printline(unsigned long wanted_pc)
} }
/* did not find any info: */ /* did not find any info: */
fprintf(stderr, " ???\n"); fprintf(stderr, " ???\n");
return; return 0;
found: found:
if (last_func_name[0] != '\0') { if (last_func_name[0] != '\0') {
fprintf(stderr, " %s()", last_func_name); fprintf(stderr, " %s()", last_func_name);
@ -1458,6 +1460,7 @@ static void rt_printline(unsigned long wanted_pc)
fprintf(stderr, ")"); fprintf(stderr, ")");
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
return func_addr;
} }
#ifdef __i386__ #ifdef __i386__
@ -1552,7 +1555,9 @@ void rt_error(ucontext_t *uc, const char *fmt, ...)
fprintf(stderr, "at "); fprintf(stderr, "at ");
else else
fprintf(stderr, "by "); fprintf(stderr, "by ");
rt_printline(pc); pc = rt_printline(pc);
if (pc == rt_prog_main && pc)
break;
} }
exit(255); exit(255);
va_end(ap); va_end(ap);
@ -1712,14 +1717,17 @@ int tcc_run(TCCState *s1, int argc, char **argv)
#ifdef CONFIG_TCC_BCHECK #ifdef CONFIG_TCC_BCHECK
if (s1->do_bounds_check) { if (s1->do_bounds_check) {
void (*bound_init)(void); void (*bound_init)(void);
void (*bound_exit)(void);
/* set error function */ /* set error function */
rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg"); rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg");
rt_prog_main = (unsigned long)prog_main;
/* XXX: use .init section so that it also work in binary ? */ /* XXX: use .init section so that it also work in binary ? */
bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init"); bound_init = tcc_get_symbol_err(s1, "__bound_init");
bound_exit = tcc_get_symbol_err(s1, "__bound_exit");
bound_init(); bound_init();
} ret = (*prog_main)(argc, argv);
bound_exit();
} else
#endif #endif
ret = (*prog_main)(argc, argv); ret = (*prog_main)(argc, argv);
tcc_free(ptr); tcc_free(ptr);

14
tcc.c
View File

@ -529,22 +529,19 @@ int main(int argc, char **argv)
/* free all files */ /* free all files */
tcc_free(files); tcc_free(files);
if (ret) if (0 == ret) {
goto the_end;
if (do_bench) if (do_bench)
tcc_print_stats(s, getclock_us() - start_time); tcc_print_stats(s, getclock_us() - start_time);
if (s->output_type == TCC_OUTPUT_PREPROCESS) { if (s->output_type == TCC_OUTPUT_PREPROCESS) {
if (outfile) if (outfile)
fclose(s->outfile); fclose(s->outfile);
} else if (s->output_type == TCC_OUTPUT_MEMORY) { } else if (s->output_type == TCC_OUTPUT_MEMORY)
ret = tcc_run(s, argc - optind, argv + optind); ret = tcc_run(s, argc - optind, argv + optind);
} else else
ret = tcc_output_file(s, outfile) ? 1 : 0; ret = tcc_output_file(s, outfile) ? 1 : 0;
the_end: }
/* XXX: cannot do it with bound checking because of the malloc hooks */
if (!s->do_bounds_check)
tcc_delete(s); tcc_delete(s);
#ifdef MEM_DEBUG #ifdef MEM_DEBUG
@ -554,4 +551,3 @@ int main(int argc, char **argv)
#endif #endif
return ret; return ret;
} }

View File

@ -6,11 +6,10 @@
TESTS = libtest test3 TESTS = libtest test3
# these should work too # these should work too
# TESTS += test1 test2 speed # TESTS += test1 test2 speed btest
# these don't work as they should # these don't work as they should
# TESTS += test4 btest asmtest # TESTS += test4 asmtest
TOP = .. TOP = ..
include $(TOP)/Makefile include $(TOP)/Makefile
@ -87,20 +86,22 @@ BOUNDS_FAIL= 2 5 7 9 11 12 13
btest: boundtest.c btest: boundtest.c
@echo ------------ $@ ------------ @echo ------------ $@ ------------
@for i in $(BOUNDS_OK); do \ @for i in $(BOUNDS_OK); do \
echo ; echo --- boundtest $$i ---; \
if $(TCC) -b -run boundtest.c $$i ; then \ if $(TCC) -b -run boundtest.c $$i ; then \
/bin/true ; \ echo succeded as expected; \
else\ else\
echo Failed positive test $$i ; exit 1 ; \ echo Failed positive test $$i ; exit 1 ; \
fi ;\ fi ;\
done ;\ done ;\
for i in $(BOUNDS_FAIL); do \ for i in $(BOUNDS_FAIL); do \
echo ; echo --- boundtest $$i ---; \
if $(TCC) -b -run boundtest.c $$i ; then \ if $(TCC) -b -run boundtest.c $$i ; then \
echo Failed negative test $$i ; exit 1 ;\ echo Failed negative test $$i ; exit 1 ;\
else\ else\
/bin/true ; \ echo failed as expected; \
fi ;\ fi ;\
done ;\ done ;\
echo Bound test OK echo; echo Bound test OK
# speed test # speed test
speed: ex2 ex3 speed: ex2 ex3