From acef4ff244bb3b7af596d657e2f549e33c1daf43 Mon Sep 17 00:00:00 2001 From: seyko Date: Thu, 26 Mar 2015 07:47:45 +0300 Subject: [PATCH] make a bound checking more compatible with Windows 64 On Linux 32: sizeof(long)=32 == sizeof(void *)=32 on Linux 64: sizeof(long)=64 == sizeof(void *)=64 on Windows 64: sizeof(long)=32 != sizeof(void *)=64 --- i386-gen.c | 14 +++++------ lib/bcheck.c | 76 ++++++++++++++++++++++++++++++------------------------------ libtcc.c | 4 ++-- tccelf.c | 2 +- tccgen.c | 8 +++---- tccrun.c | 2 +- 6 files changed, 53 insertions(+), 53 deletions(-) diff --git a/i386-gen.c b/i386-gen.c index c2db3d0a..b9dec83c 100644 --- a/i386-gen.c +++ b/i386-gen.c @@ -99,7 +99,7 @@ ST_DATA const int reg_classes[NB_REGS] = { static unsigned long func_sub_sp_offset; static int func_ret_sub; #ifdef CONFIG_TCC_BCHECK -static unsigned long func_bound_offset; +static addr_t func_bound_offset; #endif /* XXX: make it faster ? */ @@ -587,16 +587,16 @@ ST_FUNC void gfunc_prolog(CType *func_type) /* generate function epilog */ ST_FUNC void gfunc_epilog(void) { - int v, saved_ind; + addr_t v, saved_ind; #ifdef CONFIG_TCC_BCHECK if (tcc_state->do_bounds_check && func_bound_offset != lbounds_section->data_offset) { - int saved_ind; - int *bounds_ptr; + addr_t saved_ind; + addr_t *bounds_ptr; Sym *sym_data; /* add end of table info */ - bounds_ptr = section_ptr_add(lbounds_section, sizeof(int)); + bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t)); *bounds_ptr = 0; /* generate bound local allocation */ saved_ind = ind; @@ -1058,8 +1058,8 @@ ST_FUNC void gen_bounded_ptr_add(void) also tested */ ST_FUNC void gen_bounded_ptr_deref(void) { - int func; - int size, align; + addr_t func; + addr_t size, align; Elf32_Rel *rel; Sym *sym; diff --git a/lib/bcheck.c b/lib/bcheck.c index e5e1042d..a8355ba5 100644 --- a/lib/bcheck.c +++ b/lib/bcheck.c @@ -69,15 +69,15 @@ #define INVALID_SIZE 0 typedef struct BoundEntry { - unsigned long start; - unsigned long size; + size_t start; + size_t size; struct BoundEntry *next; - unsigned long is_invalid; /* true if pointers outside region are invalid */ + size_t is_invalid; /* true if pointers outside region are invalid */ } BoundEntry; /* external interface */ void __bound_init(void); -void __bound_new_region(void *p, unsigned long size); +void __bound_new_region(void *p, size_t size); int __bound_delete_region(void *p); #define FASTCALL __attribute__((regparm(3))) @@ -104,7 +104,7 @@ extern char __bounds_start; /* start of static bounds table */ const char *__bound_error_msg; /* runtime error output */ -extern void rt_error(unsigned long pc, const char *fmt, ...); +extern void rt_error(size_t pc, const char *fmt, ...); #ifdef BOUND_STATIC static BoundEntry *__bound_t1[BOUND_T1_SIZE]; /* page table */ @@ -116,12 +116,12 @@ static BoundEntry *__bound_invalid_t2; /* invalid page, for invalid pointers */ static BoundEntry *__bound_find_region(BoundEntry *e1, void *p) { - unsigned long addr, tmp; + size_t addr, tmp; BoundEntry *e; e = e1; while (e != NULL) { - addr = (unsigned long)p; + addr = (size_t)p; addr -= e->start; if (addr <= e->size) { /* put region at the head */ @@ -156,9 +156,9 @@ 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, int offset) +void * FASTCALL __bound_ptr_add(void *p, size_t offset) { - unsigned long addr = (unsigned long)p; + size_t addr = (size_t)p; BoundEntry *e; #if defined(BOUND_DEBUG) printf("add: 0x%x %d\n", (int)p, offset); @@ -171,7 +171,7 @@ void * FASTCALL __bound_ptr_add(void *p, int offset) addr -= e->start; if (addr > e->size) { e = __bound_find_region(e, p); - addr = (unsigned long)p - e->start; + addr = (size_t)p - e->start; } addr += offset; if (addr > e->size) @@ -184,7 +184,7 @@ void * FASTCALL __bound_ptr_add(void *p, int offset) #define BOUND_PTR_INDIR(dsize) \ void * FASTCALL __bound_ptr_indir ## dsize (void *p, int offset) \ { \ - unsigned long addr = (unsigned long)p; \ + size_t addr = (size_t)p; \ BoundEntry *e; \ \ e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \ @@ -194,7 +194,7 @@ void * FASTCALL __bound_ptr_indir ## dsize (void *p, int offset) \ addr -= e->start; \ if (addr > e->size) { \ e = __bound_find_region(e, p); \ - addr = (unsigned long)p - e->start; \ + addr = (size_t)p - e->start; \ } \ addr += offset + dsize; \ if (addr > e->size) \ @@ -212,13 +212,13 @@ BOUND_PTR_INDIR(16) /* return the frame pointer of the caller */ #define GET_CALLER_FP(fp)\ {\ - fp = (unsigned long)__builtin_frame_address(1);\ + fp = (size_t)__builtin_frame_address(1);\ } /* called when entering a function to add all the local regions */ void FASTCALL __bound_local_new(void *p1) { - unsigned long addr, size, fp, *p = p1; + size_t addr, size, fp, *p = p1; GET_CALLER_FP(fp); for(;;) { addr = p[0]; @@ -234,7 +234,7 @@ void FASTCALL __bound_local_new(void *p1) /* called when leaving a function to delete all the local regions */ void FASTCALL __bound_local_delete(void *p1) { - unsigned long addr, fp, *p = p1; + size_t addr, fp, *p = p1; GET_CALLER_FP(fp); for(;;) { addr = p[0]; @@ -290,11 +290,11 @@ static inline BoundEntry *get_page(int index) } /* mark a region as being invalid (can only be used during init) */ -static void mark_invalid(unsigned long addr, unsigned long size) +static void mark_invalid(size_t addr, size_t size) { - unsigned long start, end; + size_t start, end; BoundEntry *page; - int t1_start, t1_end, i, j, t2_start, t2_end; + size_t t1_start, t1_end, i, j, t2_start, t2_end; start = addr; end = addr + size; @@ -347,7 +347,7 @@ void __bound_init(void) { int i; BoundEntry *page; - unsigned long start, size; + size_t start, size; /* int *p; */ __PTRDIFF_TYPE__ *p; /* 32 or 64 bit integer */ @@ -376,7 +376,7 @@ void __bound_init(void) __bound_invalid_t2 = page; /* invalid pointer zone */ - start = (unsigned long)INVALID_POINTER & ~(BOUND_T23_SIZE - 1); + start = (size_t)INVALID_POINTER & ~(BOUND_T23_SIZE - 1); size = BOUND_T23_SIZE; mark_invalid(start, size); @@ -407,7 +407,7 @@ void __bound_init(void) * (b) on Linux >= v3.3, the alternative is to read * start_brk from /proc/self/stat */ - start = (unsigned long)sbrk(0); + start = (size_t)sbrk(0); size = 128 * 0x100000; mark_invalid(start, size); #endif @@ -433,7 +433,7 @@ void __bound_exit(void) } static inline void add_region(BoundEntry *e, - unsigned long start, unsigned long size) + size_t start, size_t size) { BoundEntry *e1; if (e->start == 0) { @@ -453,13 +453,13 @@ static inline void add_region(BoundEntry *e, } /* create a new region. It should not already exist in the region list */ -void __bound_new_region(void *p, unsigned long size) +void __bound_new_region(void *p, size_t size) { - unsigned long start, end; + size_t start, end; BoundEntry *page, *e, *e2; - int t1_start, t1_end, i, t2_start, t2_end; + size_t t1_start, t1_end, i, t2_start, t2_end; - start = (unsigned long)p; + start = (size_t)p; end = start + size; t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS); t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS); @@ -519,12 +519,12 @@ void __bound_new_region(void *p, unsigned long size) /* delete a region */ static inline void delete_region(BoundEntry *e, - void *p, unsigned long empty_size) + void *p, size_t empty_size) { - unsigned long addr; + size_t addr; BoundEntry *e1; - addr = (unsigned long)p; + addr = (size_t)p; addr -= e->start; if (addr <= e->size) { /* region found is first one */ @@ -548,7 +548,7 @@ static inline void delete_region(BoundEntry *e, /* region not found: do nothing */ if (e == NULL) break; - addr = (unsigned long)p - e->start; + addr = (size_t)p - e->start; if (addr <= e->size) { /* found: remove entry */ e1->next = e->next; @@ -563,11 +563,11 @@ static inline void delete_region(BoundEntry *e, /* return non zero if error */ int __bound_delete_region(void *p) { - unsigned long start, end, addr, size, empty_size; + size_t start, end, addr, size, empty_size; BoundEntry *page, *e, *e2; - int t1_start, t1_end, t2_start, t2_end, i; + size_t t1_start, t1_end, t2_start, t2_end, i; - start = (unsigned long)p; + start = (size_t)p; t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS); t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) & ((BOUND_T2_SIZE - 1) << BOUND_E_BITS); @@ -579,7 +579,7 @@ int __bound_delete_region(void *p) if (addr > e->size) e = __bound_find_region(e, p); /* test if invalid region */ - if (e->size == EMPTY_SIZE || (unsigned long)p != e->start) + if (e->size == EMPTY_SIZE || (size_t)p != e->start) return -1; /* compute the size we put in invalid regions */ if (e->is_invalid) @@ -638,9 +638,9 @@ int __bound_delete_region(void *p) /* return the size of the region starting at p, or EMPTY_SIZE if non existent region. */ -static unsigned long get_region_size(void *p) +static size_t get_region_size(void *p) { - unsigned long addr = (unsigned long)p; + size_t addr = (size_t)p; BoundEntry *e; e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; @@ -650,7 +650,7 @@ static unsigned long get_region_size(void *p) addr -= e->start; if (addr > e->size) e = __bound_find_region(e, p); - if (e->start != (unsigned long)p) + if (e->start != (size_t)p) return EMPTY_SIZE; return e->size; } @@ -764,7 +764,7 @@ void __bound_free(void *ptr, const void *caller) void *__bound_realloc(void *ptr, size_t size, const void *caller) { void *ptr1; - int old_size; + size_t old_size; if (size == 0) { __bound_free(ptr, caller); diff --git a/libtcc.c b/libtcc.c index 5a9681f5..1877bcc1 100644 --- a/libtcc.c +++ b/libtcc.c @@ -388,9 +388,9 @@ ST_FUNC void section_realloc(Section *sec, unsigned long new_size) /* reserve at least 'size' bytes in section 'sec' from sec->data_offset. */ -ST_FUNC void *section_ptr_add(Section *sec, unsigned long size) +ST_FUNC void *section_ptr_add(Section *sec, size_t size) { - unsigned long offset, offset1; + size_t offset, offset1; offset = sec->data_offset; offset1 = offset + size; diff --git a/tccelf.c b/tccelf.c index 502479fd..bd2c147f 100644 --- a/tccelf.c +++ b/tccelf.c @@ -1568,7 +1568,7 @@ static int tcc_add_support(TCCState *s1, const char *filename) ST_FUNC void tcc_add_bcheck(TCCState *s1) { #ifdef CONFIG_TCC_BCHECK - unsigned long *ptr; + addr_t *ptr; Section *init_section; unsigned char *pinit; int sym_index; diff --git a/tccgen.c b/tccgen.c index 2478d4eb..00a80863 100644 --- a/tccgen.c +++ b/tccgen.c @@ -5781,11 +5781,11 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, /* XXX: currently, since we do only one pass, we cannot track '&' operators, so we add only arrays */ if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) { - unsigned long *bounds_ptr; + addr_t *bounds_ptr; /* add padding between regions */ loc--; /* then add local bound info */ - bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long)); + bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(addr_t)); bounds_ptr[0] = addr; bounds_ptr[1] = size; } @@ -5892,11 +5892,11 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, /* handles bounds now because the symbol must be defined before for the relocation */ if (tcc_state->do_bounds_check) { - unsigned long *bounds_ptr; + addr_t *bounds_ptr; greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR); /* then add global bound info */ - bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long)); + bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(addr_t)); bounds_ptr[0] = 0; /* relocated */ bounds_ptr[1] = size; } diff --git a/tccrun.c b/tccrun.c index 8e53a70e..55db3109 100644 --- a/tccrun.c +++ b/tccrun.c @@ -110,7 +110,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) if (s1->do_bounds_check) { void (*bound_init)(void); void (*bound_exit)(void); - void (*bound_new_region)(void *p, unsigned long size); + void (*bound_new_region)(void *p, addr_t size); int (*bound_delete_region)(void *p); int i; -- 2.11.4.GIT