aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKirill Smelkov <kirr@navytux.spb.ru>2012-12-09 19:51:20 +0400
committerKirill Smelkov <kirr@navytux.spb.ru>2012-12-09 19:51:20 +0400
commita55ecf6d2cecae75dd456ec288d36a31344db1cf (patch)
tree59214457d2e592b9b1a995fcb54fd91e577cdc07 /lib
parentc4a18f47a24ccc1345944120faf52a983be8fa33 (diff)
parent031ff872be7be76c2c088981a827cbb2a29dfc13 (diff)
downloadtinycc-a55ecf6d2cecae75dd456ec288d36a31344db1cf.tar.gz
tinycc-a55ecf6d2cecae75dd456ec288d36a31344db1cf.tar.bz2
Repair bounds-checking more, this time `tcc -b -run tcc.c -run tcc.c -run tcctest.c` works
Hello up there. On the list Grischka made a point that we can't recommend using -b as long as tcc -b tcc.c doesn't produce anything useful. Now it does, so please don't treat -b mode as second class citizen anymore. Thanks, Kirill * bcheck2: tests: Add tests for compile/run tcc.c with `tcc -b` then compile tcc.c again, then run tcctest.c lib/bcheck: Fix code typo in __bound_delete_region() lib/bcheck: Don't assume heap goes right after bss Make tcc work after self-compiling with bounds-check enabled
Diffstat (limited to 'lib')
-rw-r--r--lib/bcheck.c35
1 files changed, 28 insertions, 7 deletions
diff --git a/lib/bcheck.c b/lib/bcheck.c
index 5ea2d0b..00b90fe 100644
--- a/lib/bcheck.c
+++ b/lib/bcheck.c
@@ -25,6 +25,7 @@
&& !defined(__DragonFly__) && !defined(__OpenBSD__)
#include <malloc.h>
#endif
+#include <unistd.h>
//#define BOUND_DEBUG
@@ -94,9 +95,6 @@ static void *saved_realloc_hook;
static void *saved_memalign_hook;
#endif
-/* linker definitions */
-extern char _end;
-
/* TCC definitions */
extern char __bounds_start; /* start of static bounds table */
/* error message, just for TCC */
@@ -379,9 +377,32 @@ void __bound_init(void)
#if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
/* malloc zone is also marked invalid. can only use that with
- hooks because all libs should use the same malloc. The solution
- would be to build a new malloc for tcc. */
- start = (unsigned long)&_end;
+ * hooks because all libs should use the same malloc. The solution
+ * would be to build a new malloc for tcc.
+ *
+ * usually heap (= malloc zone) comes right after bss, i.e. after _end, but
+ * not always - either if we are running from under `tcc -b -run`, or if
+ * address space randomization is turned on(a), heap start will be separated
+ * from bss end.
+ *
+ * So sbrk(0) will be a good approximation for start_brk:
+ *
+ * - if we are a separately compiled program, __bound_init() runs early,
+ * and sbrk(0) should be equal or very near to start_brk(b) (in case other
+ * constructors malloc something), or
+ *
+ * - if we are running from under `tcc -b -run`, sbrk(0) will return
+ * start of heap portion which is under this program control, and not
+ * mark as invalid earlier allocated memory.
+ *
+ *
+ * (a) /proc/sys/kernel/randomize_va_space = 2, on Linux;
+ * usually turned on by default.
+ *
+ * (b) on Linux >= v3.3, the alternative is to read
+ * start_brk from /proc/self/stat
+ */
+ start = (unsigned long)sbrk(0);
size = 128 * 0x100000;
mark_invalid(start, size);
#endif
@@ -592,7 +613,7 @@ int __bound_delete_region(void *p)
}
}
/* last page */
- page = get_page(t2_end);
+ page = get_page(t1_end);
e2 = (BoundEntry *)((char *)page + t2_end);
for(e=page;e<e2;e++) {
e->start = 0;