From 43a11a7ed10a36eac6ab45a877fda9770de3ed40 Mon Sep 17 00:00:00 2001 From: Kirill Smelkov Date: Sat, 1 Dec 2012 01:00:23 +0400 Subject: [PATCH] Make tcc work after self-compiling with bounds-check enabled For vstack Fabrice used the trick to initialize vtop to &vstack[-1], so that on first push, vtop becomes &vstack[0] and a value is also stored there - everything works. Except that when tcc is compiled with bounds-checking enabled, vstack - 1 returns INVALID_POINTER and oops... Let's workaround it with artificial 1 vstack slot which will not be used, but only serve as an indicator that pointing to &vstack[-1] is ok. Now, tcc, after being self-compiled with -b works: $ ./tcc -B. -o tccb -DONE_SOURCE -DCONFIG_MULTIARCHDIR=\"i386-linux-gnu\" tcc.c -ldl $ cd tests $ ../tcc -B.. -run tcctest.c >1 $ ../tccb -B.. -run tcctest.c >2 $ diff -u 1 2 and note, tcc's compilation speed is not affected: $ ./tcc -B. -bench -DONE_SOURCE -DCONFIG_MULTIARCHDIR=\"i386-linux-gnu\" -c tcc.c before: 8270 idents, 47221 lines, 1527730 bytes, 0.152 s, 309800 lines/s, 10.0 MB/s after: 8271 idents, 47221 lines, 1527733 bytes, 0.152 s, 310107 lines/s, 10.0 MB/s But note, that `tcc -b -run tcc` is still broken - for example it crashes on $ cat x.c double get100 () { return 100.0; } $ ./tcc -B. -b -DTCC_TARGET_I386 -DCONFIG_MULTIARCHDIR=\"i386-linux-gnu\" -run \ -DONE_SOURCE ./tcc.c -B. -c x.c Runtime error: dereferencing invalid pointer ./tccpp.c:1953: at 0xa7beebdf parse_number() (included from ./libtcc.c, ./tcc.c) ./tccpp.c:3003: by 0xa7bf0708 next() (included from ./libtcc.c, ./tcc.c) ./tccgen.c:4465: by 0xa7bfe348 block() (included from ./libtcc.c, ./tcc.c) ./tccgen.c:4440: by 0xa7bfe212 block() (included from ./libtcc.c, ./tcc.c) ./tccgen.c:5529: by 0xa7c01929 gen_function() (included from ./libtcc.c, ./tcc.c) ./tccgen.c:5767: by 0xa7c02602 decl0() (included from ./libtcc.c, ./tcc.c) that's because lib/bcheck.c runtime needs more fixes -- see next patches. --- tcc.h | 3 ++- tccgen.c | 2 +- tccpp.c | 1 - 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tcc.h b/tcc.h index 9e7c574..859c85f 100644 --- a/tcc.h +++ b/tcc.h @@ -1135,7 +1135,8 @@ ST_DATA Sym *local_label_stack; ST_DATA Sym *global_label_stack; ST_DATA Sym *define_stack; ST_DATA CType char_pointer_type, func_old_type, int_type, size_type; -ST_DATA SValue vstack[VSTACK_SIZE], *vtop; +ST_DATA SValue __vstack[1+/*to make bcheck happy*/ VSTACK_SIZE], *vtop; +#define vstack (__vstack + 1) ST_DATA int rsym, anon_sym, ind, loc; ST_DATA int const_wanted; /* true if constant wanted */ diff --git a/tccgen.c b/tccgen.c index f79da36..b9142d2 100644 --- a/tccgen.c +++ b/tccgen.c @@ -55,7 +55,7 @@ ST_DATA Sym *define_stack; ST_DATA Sym *global_label_stack; ST_DATA Sym *local_label_stack; -ST_DATA SValue vstack[VSTACK_SIZE], *vtop; +ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop; ST_DATA int const_wanted; /* true if constant wanted */ ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */ diff --git a/tccpp.c b/tccpp.c index aff5a53..b80c986 100644 --- a/tccpp.c +++ b/tccpp.c @@ -3041,7 +3041,6 @@ ST_FUNC void preprocess_init(TCCState *s1) s1->ifdef_stack_ptr = s1->ifdef_stack; file->ifdef_stack_ptr = s1->ifdef_stack_ptr; - /* XXX: not ANSI compliant: bound checking says error */ vtop = vstack - 1; s1->pack_stack[0] = 0; s1->pack_stack_ptr = s1->pack_stack;