History

Repair bounds-checking runtime

On this weekend a thought came to me again that tinycc could be used for scripting. Only its bounds-checking mode turned out to be broken, which is a pity because bounds-checking is sometimes handy especially for quickly written scripts. Since tinycc is one of those small beautiful things, seldomly happening in our times, I couldn't resist trying to fix it. Thanks, Kirill * bcheck: Now btest pass! lib/bcheck: Prevent __bound_local_new / __bound_local_delete from being miscompiled lib/bcheck: Prevent libc_malloc/libc_free etc from being miscompiled

Kirill Smelkov committed 14 years ago

Commit 40a54c42 parents 1af3bca 5d648482 files · +8 −16Browse files

Unified · Split

2 files
  1. lib/bcheck.c +8 −15
  2. tests/Makefile +0 −1
lib/bcheck.c+8−15
@@ -208,25 +208,11 @@ BOUND_PTR_INDIR(8) · 207 unmodified lines
208208 BOUND_PTR_INDIR(12)
209209 BOUND_PTR_INDIR(16)
210210
211−#ifdef __i386__
212211 /* return the frame pointer of the caller */
213212 #define GET_CALLER_FP(fp)\
214213 {\
215− unsigned long *fp1;\
216− __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp1));\
217− fp = fp1[0];\
214+ fp = (unsigned long)__builtin_frame_address(1);\
218215 }
219−#elif defined(__x86_64__)
220−/* TCC always creates %rbp frames also on x86_64, so use them. */
221−#define GET_CALLER_FP(fp)\
222−{\
223− unsigned long *fp1;\
224− __asm__ __volatile__ ("movq %%rbp,%0" :"=g" (fp1));\
225− fp = fp1[0];\
226−}
227−#else
228−#error put code to extract the calling frame pointer
229−#endif
230216
231217 /* called when entering a function to add all the local regions */
232218 void FASTCALL __bound_local_new(void *p1)
@@ -638,6 +624,9 @@ static unsigned long get_region_size(void *p) · 405 unmodified lines
638624
639625 /* patched memory functions */
640626
627+/* force compiler to perform stores coded up to this point */
628+#define barrier() __asm__ __volatile__ ("": : : "memory")
629+
641630 static void install_malloc_hooks(void)
642631 {
643632 #ifdef CONFIG_TCC_MALLOC_HOOKS
@@ -649,6 +638,8 @@ static void install_malloc_hooks(void) · 5 unmodified lines
649638 __free_hook = __bound_free;
650639 __realloc_hook = __bound_realloc;
651640 __memalign_hook = __bound_memalign;
641+
642+ barrier();
652643 #endif
653644 }
654645
@@ -659,6 +650,8 @@ static void restore_malloc_hooks(void) · 4 unmodified lines
659650 __free_hook = saved_free_hook;
660651 __realloc_hook = saved_realloc_hook;
661652 __memalign_hook = saved_memalign_hook;
653+
654+ barrier();
662655 #endif
663656 }
664657
tests/Makefile+0−1
@@ -22,7 +22,6 @@ TESTS = libtest \ · 21 unmodified lines
2222
2323 # some tests do not pass on all platforms, remove them for now
2424 ifeq ($(TARGETOS),Linux)
25− TESTS := $(filter-out btest,$(TESTS))
2625 TESTS := $(filter-out weaktest,$(TESTS))
2726 endif
2827 ifeq ($(TARGETOS),Darwin)