History

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.

herman ten brugge committed 2 years ago

Commit 8d8d75c1 parent f2e8b2a3 files · +25 −9Browse files

Unified · Split

3 files
  1. lib/bt-exe.c +5 −5
  2. tccrun.c +9 −3
  3. tests/Makefile +11 −1
lib/bt-exe.c+5−5
@@ -18,7 +18,6 @@ void __bt_init(rt_context *p, int is_exe) · 17 unmodified lines
1818 __attribute__((weak)) int main();
1919 __attribute__((weak)) void __bound_init(void*, int);
2020
21− WAIT_SEM(&rt_sem);
2221 //fprintf(stderr, "__bt_init %d %p %p %p\n", is_exe, p, p->stab_sym, p->bounds_start), fflush(stderr);
2322
2423 /* call __bound_init here due to redirection of sigaction */
@@ -27,13 +26,14 @@ void __bt_init(rt_context *p, int is_exe) · 2 unmodified lines
2726 __bound_init(p->bounds_start, -1);
2827
2928 /* add to chain */
29+ WAIT_SEM(&rt_sem);
3030 p->next = g_rc, g_rc = p;
31− if (is_exe) {
31+ if (is_exe)
3232 /* we are the executable (not a dll) */
3333 p->top_func = main;
34− set_exception_handler();
35− }
3634 POST_SEM(&rt_sem);
35+ if (is_exe)
36+ set_exception_handler();
3737 }
3838
3939 __declspec(dllexport)
@@ -42,13 +42,13 @@ void __bt_exit(rt_context *p) · 2 unmodified lines
4242 struct rt_context *rc, **pp;
4343 __attribute__((weak)) void __bound_exit_dll(void*);
4444
45− WAIT_SEM(&rt_sem);
4645 //fprintf(stderr, "__bt_exit %d %p\n", !!p->top_func, p);
4746
4847 if (p->bounds_start)
4948 __bound_exit_dll(p->bounds_start);
5049
5150 /* remove from chain */
51+ WAIT_SEM(&rt_sem);
5252 for (pp = &g_rc; rc = *pp, rc; pp = &rc->next)
5353 if (rc == p) {
5454 *pp = rc->next;
tccrun.c+9−3
@@ -66,9 +66,15 @@ TCC_SEM(static rt_sem); · 65 unmodified lines
6666 static int signal_set;
6767 static void set_exception_handler(void);
6868
69−int _rt_error(rt_frame *f, const char *msg, const char *fmt, va_list ap);
70−void rt_wait_sem(void) { WAIT_SEM(&rt_sem); }
71−void rt_post_sem(void) { POST_SEM(&rt_sem); }
69+#ifdef _WIN32
70+#define ATTR_WEAK
71+#else
72+#define ATTR_WEAK __attribute__((weak))
73+#endif
74+int ATTR_WEAK _rt_error(rt_frame *f, const char *msg, const char *fmt, va_list ap);
75+void ATTR_WEAK rt_wait_sem(void) { WAIT_SEM(&rt_sem); }
76+void ATTR_WEAK rt_post_sem(void) { POST_SEM(&rt_sem); }
77+#undef ATTR_WEAK
7278 #endif /* def CONFIG_TCC_BACKTRACE */
7379
7480 /* handle exit/atexit for tcc_run() -- thread-unsafe */
tests/Makefile+11−1
@@ -28,6 +28,9 @@ TESTS = \ · 27 unmodified lines
2828
2929 ifneq ($(CONFIG_bcheck),no)
3030 TESTS += btest test1b
31+ ifndef CONFIG_WIN32
32+ TESTS += tccb
33+ endif
3134 endif
3235 ifeq ($(CONFIG_dll),no)
3336 TESTS := $(filter-out dlltest, $(TESTS))
@@ -198,6 +201,13 @@ btest: boundtest.c · 164 unmodified lines
198201 done ;\
199202 echo Bound test OK
200203
204+tccb:
205+ @echo ------------ $@ ------------
206+ $(TCC) -b $(TOPSRC)/tcc.c $(TCCFLAGS) $(NATIVE_DEFINES) -o v1-tcc$(EXESUF)
207+ mv v1-tcc$(EXESUF) v2-tcc$(EXESUF)
208+ ./v2-tcc$(EXESUF) -b $(TOPSRC)/tcc.c $(TCCFLAGS) $(NATIVE_DEFINES) -o v1-tcc$(EXESUF)
209+ cmp -s v1-tcc$(EXESUF) v2-tcc$(EXESUF) && echo "Bound-Test tcc OK"
210+
201211 # speed test
202212 speedtest: ex2 ex3
203213 @echo ------------ $@ ------------
@@ -260,7 +270,7 @@ vla_test-run: vla_test$(EXESUF) · 56 unmodified lines
260270 @echo ------------ $@ ------------
261271 ./vla_test$(EXESUF)
262272
263−.PHONY: abitest vla_test
273+.PHONY: abitest vla_test tccb
264274
265275 asm-c-connect$(EXESUF): asm-c-connect-1.c asm-c-connect-2.c
266276 $(TCC) -o $@ $^

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t