1
0
Fork 0

Allow tcc to run with bounds checking enabled

tcc failed to run with bounds checking enabled because the functions
rt_wait_sem, rt_post_sem and _rt_error where defined twice.
This is solved by making them weak in tccrun.c

Also a nested lock was present when setting TCC_BOUNDS_PRINT_CALLS=1
This is solved in lib/bt-exe.c by moving lock/unlock code.

Also added a testcase in tests/Makefile to test tcc with bounds
checking enabled.
This commit is contained in:
herman ten brugge 2024-02-15 07:17:15 +01:00
parent f2e8b2ad79
commit 8d8d75ca75
3 changed files with 25 additions and 9 deletions

View File

@ -18,7 +18,6 @@ void __bt_init(rt_context *p, int is_exe)
__attribute__((weak)) int main(); __attribute__((weak)) int main();
__attribute__((weak)) void __bound_init(void*, int); __attribute__((weak)) void __bound_init(void*, int);
WAIT_SEM(&rt_sem);
//fprintf(stderr, "__bt_init %d %p %p %p\n", is_exe, p, p->stab_sym, p->bounds_start), fflush(stderr); //fprintf(stderr, "__bt_init %d %p %p %p\n", is_exe, p, p->stab_sym, p->bounds_start), fflush(stderr);
/* call __bound_init here due to redirection of sigaction */ /* call __bound_init here due to redirection of sigaction */
@ -27,13 +26,14 @@ void __bt_init(rt_context *p, int is_exe)
__bound_init(p->bounds_start, -1); __bound_init(p->bounds_start, -1);
/* add to chain */ /* add to chain */
WAIT_SEM(&rt_sem);
p->next = g_rc, g_rc = p; p->next = g_rc, g_rc = p;
if (is_exe) { if (is_exe)
/* we are the executable (not a dll) */ /* we are the executable (not a dll) */
p->top_func = main; p->top_func = main;
set_exception_handler();
}
POST_SEM(&rt_sem); POST_SEM(&rt_sem);
if (is_exe)
set_exception_handler();
} }
__declspec(dllexport) __declspec(dllexport)
@ -42,13 +42,13 @@ void __bt_exit(rt_context *p)
struct rt_context *rc, **pp; struct rt_context *rc, **pp;
__attribute__((weak)) void __bound_exit_dll(void*); __attribute__((weak)) void __bound_exit_dll(void*);
WAIT_SEM(&rt_sem);
//fprintf(stderr, "__bt_exit %d %p\n", !!p->top_func, p); //fprintf(stderr, "__bt_exit %d %p\n", !!p->top_func, p);
if (p->bounds_start) if (p->bounds_start)
__bound_exit_dll(p->bounds_start); __bound_exit_dll(p->bounds_start);
/* remove from chain */ /* remove from chain */
WAIT_SEM(&rt_sem);
for (pp = &g_rc; rc = *pp, rc; pp = &rc->next) for (pp = &g_rc; rc = *pp, rc; pp = &rc->next)
if (rc == p) { if (rc == p) {
*pp = rc->next; *pp = rc->next;

View File

@ -66,9 +66,15 @@ TCC_SEM(static rt_sem);
static int signal_set; static int signal_set;
static void set_exception_handler(void); static void set_exception_handler(void);
int _rt_error(rt_frame *f, const char *msg, const char *fmt, va_list ap); #ifdef _WIN32
void rt_wait_sem(void) { WAIT_SEM(&rt_sem); } #define ATTR_WEAK
void rt_post_sem(void) { POST_SEM(&rt_sem); } #else
#define ATTR_WEAK __attribute__((weak))
#endif
int ATTR_WEAK _rt_error(rt_frame *f, const char *msg, const char *fmt, va_list ap);
void ATTR_WEAK rt_wait_sem(void) { WAIT_SEM(&rt_sem); }
void ATTR_WEAK rt_post_sem(void) { POST_SEM(&rt_sem); }
#undef ATTR_WEAK
#endif /* def CONFIG_TCC_BACKTRACE */ #endif /* def CONFIG_TCC_BACKTRACE */
/* handle exit/atexit for tcc_run() -- thread-unsafe */ /* handle exit/atexit for tcc_run() -- thread-unsafe */

View File

@ -28,6 +28,9 @@ TESTS = \
ifneq ($(CONFIG_bcheck),no) ifneq ($(CONFIG_bcheck),no)
TESTS += btest test1b TESTS += btest test1b
ifndef CONFIG_WIN32
TESTS += tccb
endif
endif endif
ifeq ($(CONFIG_dll),no) ifeq ($(CONFIG_dll),no)
TESTS := $(filter-out dlltest, $(TESTS)) TESTS := $(filter-out dlltest, $(TESTS))
@ -198,6 +201,13 @@ btest: boundtest.c
done ;\ done ;\
echo Bound test OK echo Bound test OK
tccb:
@echo ------------ $@ ------------
$(TCC) -b $(TOPSRC)/tcc.c $(TCCFLAGS) $(NATIVE_DEFINES) -o v1-tcc$(EXESUF)
mv v1-tcc$(EXESUF) v2-tcc$(EXESUF)
./v2-tcc$(EXESUF) -b $(TOPSRC)/tcc.c $(TCCFLAGS) $(NATIVE_DEFINES) -o v1-tcc$(EXESUF)
cmp -s v1-tcc$(EXESUF) v2-tcc$(EXESUF) && echo "Bound-Test tcc OK"
# speed test # speed test
speedtest: ex2 ex3 speedtest: ex2 ex3
@echo ------------ $@ ------------ @echo ------------ $@ ------------
@ -260,7 +270,7 @@ vla_test-run: vla_test$(EXESUF)
@echo ------------ $@ ------------ @echo ------------ $@ ------------
./vla_test$(EXESUF) ./vla_test$(EXESUF)
.PHONY: abitest vla_test .PHONY: abitest vla_test tccb
asm-c-connect$(EXESUF): asm-c-connect-1.c asm-c-connect-2.c asm-c-connect$(EXESUF): asm-c-connect-1.c asm-c-connect-2.c
$(TCC) -o $@ $^ $(TCC) -o $@ $^