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.
This commit is contained in:
grischka
2019-12-12 15:45:45 +01:00
parent a86f47889c
commit 56db092ab7
19 changed files with 322 additions and 504 deletions

View File

@@ -7,8 +7,6 @@ include $(TOP)/Makefile
VPATH = $(TOPSRC)/lib $(TOPSRC)/win32/lib
T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown)
X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-)
BIN = $(TOP)/$(X)libtcc1.a
BINB = $(TOP)/$(X)libtccb1.a
XTCC ?= $(TOP)/$(X)tcc$(EXESUF)
XCC = $(XTCC)
@@ -20,8 +18,6 @@ XCFG = $(or $(findstring -win,$T),-unx)
# in order to use gcc, tyoe: make <target>-libtcc1-usegcc=yes
arm-libtcc1-usegcc ?= no
x86_64-libtcc1-usegcc ?= no
i386-libtcc1-usegcc ?= no
ifeq "$($(T)-libtcc1-usegcc)" "yes"
XCC = $(CC)
@@ -42,8 +38,6 @@ ifdef CONFIG_OSX
XFLAGS += -D_ANSI_SOURCE
endif
XFLAGS += -g
I386_O = libtcc1.o alloca86.o alloca86-bt.o
X86_64_O = libtcc1.o alloca86_64.o alloca86_64-bt.o
ARM_O = libtcc1.o armeabi.o alloca-arm.o armflush.o
@@ -51,11 +45,11 @@ ARM64_O = lib-arm64.o
RISCV64_O = lib-arm64.o
WIN_O = crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o
OBJ-i386 = $(I386_O) $(DSO_O)
OBJ-x86_64 = $(X86_64_O) va_list.o $(DSO_O)
OBJ-i386 = $(I386_O) $(BCHECK_O) $(DSO_O)
OBJ-x86_64 = $(X86_64_O) va_list.o $(BCHECK_O) $(DSO_O)
OBJ-x86_64-osx = $(X86_64_O) va_list.o
OBJ-i386-win32 = $(I386_O) chkstk.o $(WIN_O)
OBJ-x86_64-win32 = $(X86_64_O) chkstk.o (WIN_O)
OBJ-i386-win32 = $(I386_O) chkstk.o bcheck.o $(WIN_O)
OBJ-x86_64-win32 = $(X86_64_O) chkstk.o bcheck.o $(WIN_O)
OBJ-arm64 = $(ARM64_O) $(DSO_O)
OBJ-arm = $(ARM_O) $(DSO_O)
OBJ-arm-fpa = $(ARM_O) $(DSO_O)
@@ -66,27 +60,14 @@ OBJ-arm-eabihf = $(ARM_O) $(DSO_O)
OBJ-arm-wince = $(ARM_O) $(WIN_O)
OBJ-riscv64 = $(RISCV64_O) $(DSO_O)
OBJB-i386 = $(BCHECK_O)
OBJB-x86_64 = $(BCHECK_O)
OBJB-x86_64-osx = dummy.o
OBJB-i386-win32 = bcheck.o
OBJB-x86_64-win32 = bcheck.o
OBJB-arm64 = dummy.o
OBJB-arm = dummy.o
OBJB-arm-fpa = dummy.o
OBJB-arm-fpa-ld = dummy.o
OBJB-arm-vfp = dummy.o
OBJB-arm-eabi = dummy.o
OBJB-arm-eabihf = dummy.o
OBJB-arm-wince = dummy.o
OBJB-riscv64 = dummy.o
OBJ-extra = $(filter bcheck.o,$(OBJ-$T))
OBJ-libtcc1 = $(addprefix $(X),$(filter-out $(OBJ-extra),$(OBJ-$T)))
all: $(BIN) $(BINB)
ALL = $(addprefix $(TOP)/,$(X)libtcc1.a $(OBJ-extra))
$(BIN) : $(patsubst %.o,$(X)%.o,$(OBJ-$T))
$(XAR) rcs $@ $^
all: $(ALL)
$(BINB) : $(patsubst %.o,$(X)%.o,$(OBJB-$T))
$(TOP)/$(X)libtcc1.a : $(OBJ-libtcc1)
$(XAR) rcs $@ $^
$(X)%.o : %.c
@@ -95,8 +76,12 @@ $(X)%.o : %.c
$(X)%.o : %.S
$(XCC) -c $< -o $@ $(XFLAGS)
$(TOP)/%.o : %.c
$(XCC) -c $< -o $@ $(XFLAGS)
$(TOP)/bcheck.o : XFLAGS += -g
$(X)crt1w.o : crt1.c
$(X)wincrt1w.o : wincrt1.c
clean :
rm -f *.a *.o $(BIN) $(BINB)
rm -f *.a *.o $(ALL)

View File

@@ -5,44 +5,25 @@
__bound_alloca:
#ifdef _WIN32
pop %rdx
mov %rcx,%rax
add $15,%rax
and $-16,%rax
jz p3
p1:
cmp $4096,%rax
jbe p2
test %rax,-4096(%rsp)
sub $4096,%rsp
sub $4096,%rax
jmp p1
p2:
sub %rax,%rsp
mov %rsp,%rax
push %rdx
inc %rcx # add one extra to separate regions
jmp alloca
.globl __bound_alloca_nr
__bound_alloca_nr:
dec %rcx
push %rax
mov %rcx,%rdx
mov %rax,%rcx
sub $20,%rsp
call __bound_new_region
add $20,%rsp
sub $32,%rsp
call __bound_new_region
add $32,%rsp
pop %rax
pop %rdx
add $32,%rax
p3:
push %rdx
ret
#else
pop %rdx
mov %rdi,%rax
mov %rax,%rsi # size, a second parm to the __bound_new_region
add $15,%rax
add $15 + 1,%rax # add one extra to separate regions
and $-16,%rax
jz p3

View File

@@ -24,12 +24,8 @@ p1:
jmp p1
p2:
#endif
sub %rax,%rsp
mov %rsp,%rax
#ifdef _WIN32
add $32,%rax
#endif
p3:
push %rdx
ret

View File

@@ -205,6 +205,9 @@ void __bound_checking (int no_check)
no_checking = no_check;
}
#define no_FASTCALL
//#define no_checking 1
/* print a bound error message */
static void bound_error(const char *fmt, ...)
{
@@ -221,8 +224,7 @@ static void bound_alloc_error(void)
/* return '(p + offset)' for pointer arithmetic (a pointer can reach
the end of a region in this case */
void * FASTCALL __bound_ptr_add(void *p, size_t offset,
size_t line, const char *filename)
void * no_FASTCALL __bound_ptr_add(void *p, size_t offset)
{
size_t addr = (size_t)p;
@@ -230,8 +232,8 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset,
return p + offset;
}
dprintf(stderr, "%s %s (%s:%u): %p 0x%x\n",
__FILE__, __FUNCTION__, filename, line, p, (unsigned)offset);
dprintf(stderr, "%s %s : %p 0x%x\n",
__FILE__, __FUNCTION__, p, (unsigned)offset);
WAIT_SEM ();
INCR_COUNT(bound_ptr_add_count);
@@ -250,8 +252,10 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset,
if (addr <= tree->size) {
addr += offset;
if (tree->is_invalid || addr > tree->size) {
fprintf(stderr,"%s %s (%s:%u): %p is outside of the region\n",
__FILE__, __FUNCTION__, filename, line, p + offset);
#if 0
fprintf(stderr,"%s %s : %p is outside of the region\n",
__FILE__, __FUNCTION__, p + offset);
#endif
if (never_fatal == 0) {
POST_SEM ();
return INVALID_POINTER; /* return an invalid pointer */
@@ -266,16 +270,15 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset,
/* return '(p + offset)' for pointer indirection (the resulting must
be strictly inside the region */
#define BOUND_PTR_INDIR(dsize) \
void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset, \
size_t line, const char *filename) \
void * no_FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \
{ \
size_t addr = (size_t)p; \
\
if (no_checking) { \
return p + offset; \
} \
dprintf(stderr, "%s %s (%s:%u): %p 0x%x start\n", \
__FILE__, __FUNCTION__, filename, line, p, (unsigned)offset); \
dprintf(stderr, "%s %s : %p 0x%x start\n", \
__FILE__, __FUNCTION__, p, (unsigned)offset); \
WAIT_SEM (); \
INCR_COUNT(bound_ptr_indir ## dsize ## _count); \
if (tree) { \
@@ -293,8 +296,8 @@ void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset, \
if (addr <= tree->size) { \
addr += offset + dsize; \
if (tree->is_invalid || addr > tree->size) { \
fprintf(stderr,"%s %s (%s:%u): %p is outside of the region\n", \
__FILE__, __FUNCTION__, filename, line, p + offset); \
fprintf(stderr,"%s %s : %p is outside of the region\n", \
__FILE__, __FUNCTION__, p + offset); \
if (never_fatal == 0) { \
POST_SEM (); \
return INVALID_POINTER; /* return an invalid pointer */ \
@@ -907,7 +910,7 @@ static void __bound_check(const void *p, size_t size, const char *function)
return;
if (size == 0)
return;
p = __bound_ptr_add((void *)p, size, 0, function);
p = __bound_ptr_add((void *)p, size);
if (p == INVALID_POINTER)
bound_error("invalid pointer");
}
@@ -959,7 +962,7 @@ int __bound_strlen(const char *s)
INCR_COUNT(bound_strlen_count);
while (*p++);
len = (p - s) - 1;
p = __bound_ptr_indir1((char *)s, len, 0, "strlen");
p = __bound_ptr_indir1((char *)s, len);
if (p == INVALID_POINTER)
bound_error("bad pointer in strlen()");
return len;

View File

@@ -1 +0,0 @@
static char dummy;