History

bcheck cleanup

- revert Makefiles to state before last bcheck additions Instead, just load bcheck.o explicitly if that is what is wanted. - move tcc_add_bcheck() to the <target>-link.c files and remove revently added arguments. This function is to support tccelf.c with linking, not for tccgen.c to support compilation. - remove -ba option: It said: "-ba Enable better address checking with bounds checker" Okay, if it is better then to have it is not an option. - remove va_copy. It is C99 and we try to stay C89 in tinycc when possible. For example, MS compilers do not have va_copy. - win64: revert any 'fixes' to alloca It was correct as it was before, except for bound_checking where it was not implemented. This should now work too. - remove parasitic filename:linenum features Such feature is already present with rt_printline in tccrun.c. If it doesn't work it can be fixed. - revert changes to gen_bounded_ptr_add() gen_bounded_ptr_add() was working as it should before (mostly). For the sake of simplicity I switched it to CDECL. Anyway, FASTCALL means SLOWCALL with tinycc. In exchange you get one addition which is required for bounds_cnecking function arguments. The important thing is to check them *BEFORE* they are loaded into registers. New function gbound_args() does that. In any case, code instrumentation with the bounds-check functions as such now seems to work flawlessly again, which means when they are inserted as NOPs, any code that tcc can compile, seems to behave just the same as without them. What these functions then do when fully enabled, is a differnt story. I did not touch this.

grischka committed 6 years ago

Commit 56db0921 parent a86f47819 files · +324 −506Browse files

Showing the changes to lib (5 of 19 files). Show all

Unified · Split

5 files
  1. lib/Makefile +17 −32
  2. lib/alloca86_64-bt.S +9 −28
  3. lib/alloca86_64.S +0 −4
  4. lib/bcheck.c +17 −14
  5. lib/dummy.c +0 −1
lib/Makefile+17−32
@@ -7,8 +7,6 @@ include $(TOP)/Makefile · 6 unmodified lines
77 VPATH = $(TOPSRC)/lib $(TOPSRC)/win32/lib
88 T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown)
99 X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-)
10−BIN = $(TOP)/$(X)libtcc1.a
11−BINB = $(TOP)/$(X)libtccb1.a
1210
1311 XTCC ?= $(TOP)/$(X)tcc$(EXESUF)
1412 XCC = $(XTCC)
@@ -20,8 +18,6 @@ XCFG = $(or $(findstring -win,$T),-unx) · 5 unmodified lines
2018
2119 # in order to use gcc, tyoe: make <target>-libtcc1-usegcc=yes
2220 arm-libtcc1-usegcc ?= no
23−x86_64-libtcc1-usegcc ?= no
24−i386-libtcc1-usegcc ?= no
2521
2622 ifeq "$($(T)-libtcc1-usegcc)" "yes"
2723 XCC = $(CC)
@@ -42,8 +38,6 @@ ifdef CONFIG_OSX · 14 unmodified lines
4238 XFLAGS += -D_ANSI_SOURCE
4339 endif
4440
45−XFLAGS += -g
46−
4741 I386_O = libtcc1.o alloca86.o alloca86-bt.o
4842 X86_64_O = libtcc1.o alloca86_64.o alloca86_64-bt.o
4943 ARM_O = libtcc1.o armeabi.o alloca-arm.o armflush.o
@@ -51,11 +45,11 @@ ARM64_O = lib-arm64.o · 1 unmodified line
5145 RISCV64_O = lib-arm64.o
5246 WIN_O = crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o
5347
54−OBJ-i386 = $(I386_O) $(DSO_O)
55−OBJ-x86_64 = $(X86_64_O) va_list.o $(DSO_O)
48+OBJ-i386 = $(I386_O) $(BCHECK_O) $(DSO_O)
49+OBJ-x86_64 = $(X86_64_O) va_list.o $(BCHECK_O) $(DSO_O)
5650 OBJ-x86_64-osx = $(X86_64_O) va_list.o
57−OBJ-i386-win32 = $(I386_O) chkstk.o $(WIN_O)
58−OBJ-x86_64-win32 = $(X86_64_O) chkstk.o (WIN_O)
51+OBJ-i386-win32 = $(I386_O) chkstk.o bcheck.o $(WIN_O)
52+OBJ-x86_64-win32 = $(X86_64_O) chkstk.o bcheck.o $(WIN_O)
5953 OBJ-arm64 = $(ARM64_O) $(DSO_O)
6054 OBJ-arm = $(ARM_O) $(DSO_O)
6155 OBJ-arm-fpa = $(ARM_O) $(DSO_O)
@@ -66,27 +60,14 @@ OBJ-arm-eabihf = $(ARM_O) $(DSO_O) · 4 unmodified lines
6660 OBJ-arm-wince = $(ARM_O) $(WIN_O)
6761 OBJ-riscv64 = $(RISCV64_O) $(DSO_O)
6862
69−OBJB-i386 = $(BCHECK_O)
70−OBJB-x86_64 = $(BCHECK_O)
71−OBJB-x86_64-osx = dummy.o
72−OBJB-i386-win32 = bcheck.o
73−OBJB-x86_64-win32 = bcheck.o
74−OBJB-arm64 = dummy.o
75−OBJB-arm = dummy.o
76−OBJB-arm-fpa = dummy.o
77−OBJB-arm-fpa-ld = dummy.o
78−OBJB-arm-vfp = dummy.o
79−OBJB-arm-eabi = dummy.o
80−OBJB-arm-eabihf = dummy.o
81−OBJB-arm-wince = dummy.o
82−OBJB-riscv64 = dummy.o
83−
84−all: $(BIN) $(BINB)
85−
86−$(BIN) : $(patsubst %.o,$(X)%.o,$(OBJ-$T))
87− $(XAR) rcs $@ $^
63+OBJ-extra = $(filter bcheck.o,$(OBJ-$T))
64+OBJ-libtcc1 = $(addprefix $(X),$(filter-out $(OBJ-extra),$(OBJ-$T)))
65+
66+ALL = $(addprefix $(TOP)/,$(X)libtcc1.a $(OBJ-extra))
67+
68+all: $(ALL)
8869
89−$(BINB) : $(patsubst %.o,$(X)%.o,$(OBJB-$T))
70+$(TOP)/$(X)libtcc1.a : $(OBJ-libtcc1)
9071 $(XAR) rcs $@ $^
9172
9273 $(X)%.o : %.c
@@ -95,8 +76,12 @@ $(X)%.o : %.c · 2 unmodified lines
9576 $(X)%.o : %.S
9677 $(XCC) -c $< -o $@ $(XFLAGS)
9778
79+$(TOP)/%.o : %.c
80+ $(XCC) -c $< -o $@ $(XFLAGS)
81+
82+$(TOP)/bcheck.o : XFLAGS += -g
83+
9884 $(X)crt1w.o : crt1.c
9985 $(X)wincrt1w.o : wincrt1.c
100−
10186 clean :
102− rm -f *.a *.o $(BIN) $(BINB)
87+ rm -f *.a *.o $(ALL)
lib/alloca86_64-bt.S+9−28
@@ -5,44 +5,25 @@ · 4 unmodified lines
55 __bound_alloca:
66
77 #ifdef _WIN32
8− pop %rdx
9− mov %rcx,%rax
10− add $15,%rax
11− and $-16,%rax
12− jz p3
13−
14−p1:
15− cmp $4096,%rax
16− jbe p2
17− test %rax,-4096(%rsp)
18− sub $4096,%rsp
19− sub $4096,%rax
20− jmp p1
21−p2:
22−
23− sub %rax,%rsp
24− mov %rsp,%rax
25−
26− push %rdx
8+ inc %rcx # add one extra to separate regions
9+ jmp alloca
10+.globl __bound_alloca_nr
11+__bound_alloca_nr:
12+ dec %rcx
2713 push %rax
2814 mov %rcx,%rdx
2915 mov %rax,%rcx
30− sub $20,%rsp
31− call __bound_new_region
32− add $20,%rsp
16+ sub $32,%rsp
17+ call __bound_new_region
18+ add $32,%rsp
3319 pop %rax
34− pop %rdx
35−
36− add $32,%rax
37−p3:
38− push %rdx
3920 ret
4021 #else
4122 pop %rdx
4223 mov %rdi,%rax
4324 mov %rax,%rsi # size, a second parm to the __bound_new_region
4425
45− add $15,%rax
26+ add $15 + 1,%rax # add one extra to separate regions
4627 and $-16,%rax
4728 jz p3
4829
lib/alloca86_64.S+0−4
@@ -24,12 +24,8 @@ p1: · 23 unmodified lines
2424 jmp p1
2525 p2:
2626 #endif
27−
2827 sub %rax,%rsp
2928 mov %rsp,%rax
30−#ifdef _WIN32
31− add $32,%rax
32−#endif
3329 p3:
3430 push %rdx
3531 ret
lib/bcheck.c+17−14
@@ -205,6 +205,9 @@ void __bound_checking (int no_check) · 204 unmodified lines
205205 no_checking = no_check;
206206 }
207207
208+#define no_FASTCALL
209+//#define no_checking 1
210+
208211 /* print a bound error message */
209212 static void bound_error(const char *fmt, ...)
210213 {
@@ -221,8 +224,7 @@ static void bound_alloc_error(void) · 10 unmodified lines
221224
222225 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
223226 the end of a region in this case */
224−void * FASTCALL __bound_ptr_add(void *p, size_t offset,
225− size_t line, const char *filename)
227+void * no_FASTCALL __bound_ptr_add(void *p, size_t offset)
226228 {
227229 size_t addr = (size_t)p;
228230
@@ -230,8 +232,8 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset, · 1 unmodified line
230232 return p + offset;
231233 }
232234
233− dprintf(stderr, "%s %s (%s:%u): %p 0x%x\n",
234− __FILE__, __FUNCTION__, filename, line, p, (unsigned)offset);
235+ dprintf(stderr, "%s %s : %p 0x%x\n",
236+ __FILE__, __FUNCTION__, p, (unsigned)offset);
235237
236238 WAIT_SEM ();
237239 INCR_COUNT(bound_ptr_add_count);
@@ -250,8 +252,10 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset, · 12 unmodified lines
250252 if (addr <= tree->size) {
251253 addr += offset;
252254 if (tree->is_invalid || addr > tree->size) {
253− fprintf(stderr,"%s %s (%s:%u): %p is outside of the region\n",
254− __FILE__, __FUNCTION__, filename, line, p + offset);
255+ #if 0
256+ fprintf(stderr,"%s %s : %p is outside of the region\n",
257+ __FILE__, __FUNCTION__, p + offset);
258+ #endif
255259 if (never_fatal == 0) {
256260 POST_SEM ();
257261 return INVALID_POINTER; /* return an invalid pointer */
@@ -266,16 +270,15 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset, · 8 unmodified lines
266270 /* return '(p + offset)' for pointer indirection (the resulting must
267271 be strictly inside the region */
268272 #define BOUND_PTR_INDIR(dsize) \
269−void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset, \
270− size_t line, const char *filename) \
273+void * no_FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \
271274 { \
272275 size_t addr = (size_t)p; \
273276 \
274277 if (no_checking) { \
275278 return p + offset; \
276279 } \
277− dprintf(stderr, "%s %s (%s:%u): %p 0x%x start\n", \
278− __FILE__, __FUNCTION__, filename, line, p, (unsigned)offset); \
280+ dprintf(stderr, "%s %s : %p 0x%x start\n", \
281+ __FILE__, __FUNCTION__, p, (unsigned)offset); \
279282 WAIT_SEM (); \
280283 INCR_COUNT(bound_ptr_indir ## dsize ## _count); \
281284 if (tree) { \
@@ -293,8 +296,8 @@ void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset, \ · 11 unmodified lines
293296 if (addr <= tree->size) { \
294297 addr += offset + dsize; \
295298 if (tree->is_invalid || addr > tree->size) { \
296− fprintf(stderr,"%s %s (%s:%u): %p is outside of the region\n", \
297− __FILE__, __FUNCTION__, filename, line, p + offset); \
299+ fprintf(stderr,"%s %s : %p is outside of the region\n", \
300+ __FILE__, __FUNCTION__, p + offset); \
298301 if (never_fatal == 0) { \
299302 POST_SEM (); \
300303 return INVALID_POINTER; /* return an invalid pointer */ \
@@ -907,7 +910,7 @@ static void __bound_check(const void *p, size_t size, const char *function) · 606 unmodified lines
907910 return;
908911 if (size == 0)
909912 return;
910− p = __bound_ptr_add((void *)p, size, 0, function);
913+ p = __bound_ptr_add((void *)p, size);
911914 if (p == INVALID_POINTER)
912915 bound_error("invalid pointer");
913916 }
@@ -959,7 +962,7 @@ int __bound_strlen(const char *s) · 45 unmodified lines
959962 INCR_COUNT(bound_strlen_count);
960963 while (*p++);
961964 len = (p - s) - 1;
962− p = __bound_ptr_indir1((char *)s, len, 0, "strlen");
965+ p = __bound_ptr_indir1((char *)s, len);
963966 if (p == INVALID_POINTER)
964967 bound_error("bad pointer in strlen()");
965968 return len;
lib/dummy.c+0−1
@@ -1 +0,0 @@
1−static char dummy;

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