History

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

seyko committed 11 years ago

Commit acef4ff1 parent 548a55e6 files · +53 −53Browse files

Unified · Split

6 files
  1. i386-gen.c +7 −7
  2. lib/bcheck.c +38 −38
  3. libtcc.c +2 −2
  4. tccelf.c +1 −1
  5. tccgen.c +4 −4
  6. tccrun.c +1 −1
i386-gen.c+7−7
@@ -99,7 +99,7 @@ ST_DATA const int reg_classes[NB_REGS] = { · 98 unmodified lines
99static unsigned long func_sub_sp_offset;99static unsigned long func_sub_sp_offset;
100static int func_ret_sub;100static int func_ret_sub;
101#ifdef CONFIG_TCC_BCHECK101#ifdef CONFIG_TCC_BCHECK
102static unsigned long func_bound_offset;102static addr_t func_bound_offset;
103#endif103#endif
104 104
105/* XXX: make it faster ? */105/* XXX: make it faster ? */
@@ -587,16 +587,16 @@ ST_FUNC void gfunc_prolog(CType *func_type) · 481 unmodified lines
587/* generate function epilog */587/* generate function epilog */
588ST_FUNC void gfunc_epilog(void)588ST_FUNC void gfunc_epilog(void)
589{589{
590 int v, saved_ind;590 addr_t v, saved_ind;
591 591
592#ifdef CONFIG_TCC_BCHECK592#ifdef CONFIG_TCC_BCHECK
593 if (tcc_state->do_bounds_check593 if (tcc_state->do_bounds_check
594 && func_bound_offset != lbounds_section->data_offset) {594 && func_bound_offset != lbounds_section->data_offset) {
595 int saved_ind;595 addr_t saved_ind;
596 int *bounds_ptr;596 addr_t *bounds_ptr;
597 Sym *sym_data;597 Sym *sym_data;
598 /* add end of table info */598 /* add end of table info */
599 bounds_ptr = section_ptr_add(lbounds_section, sizeof(int));599 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
600 *bounds_ptr = 0;600 *bounds_ptr = 0;
601 /* generate bound local allocation */601 /* generate bound local allocation */
602 saved_ind = ind;602 saved_ind = ind;
@@ -1058,8 +1058,8 @@ ST_FUNC void gen_bounded_ptr_add(void) · 455 unmodified lines
1058 also tested */1058 also tested */
1059ST_FUNC void gen_bounded_ptr_deref(void)1059ST_FUNC void gen_bounded_ptr_deref(void)
1060{1060{
1061 int func;1061 addr_t func;
1062 int size, align;1062 addr_t size, align;
1063 Elf32_Rel *rel;1063 Elf32_Rel *rel;
1064 Sym *sym;1064 Sym *sym;
1065 1065
lib/bcheck.c+38−38
@@ -69,15 +69,15 @@ · 68 unmodified lines
69#define INVALID_SIZE 069#define INVALID_SIZE 0
70 70
71typedef struct BoundEntry {71typedef struct BoundEntry {
72 unsigned long start;72 size_t start;
73 unsigned long size;73 size_t size;
74 struct BoundEntry *next;74 struct BoundEntry *next;
75 unsigned long is_invalid; /* true if pointers outside region are invalid */75 size_t is_invalid; /* true if pointers outside region are invalid */
76} BoundEntry;76} BoundEntry;
77 77
78/* external interface */78/* external interface */
79void __bound_init(void);79void __bound_init(void);
80void __bound_new_region(void *p, unsigned long size);80void __bound_new_region(void *p, size_t size);
81int __bound_delete_region(void *p);81int __bound_delete_region(void *p);
82 82
83#define FASTCALL __attribute__((regparm(3)))83#define FASTCALL __attribute__((regparm(3)))
@@ -104,7 +104,7 @@ extern char __bounds_start; /* start of static bounds table */ · 20 unmodified lines
104const char *__bound_error_msg;104const char *__bound_error_msg;
105 105
106/* runtime error output */106/* runtime error output */
107extern void rt_error(unsigned long pc, const char *fmt, ...);107extern void rt_error(size_t pc, const char *fmt, ...);
108 108
109#ifdef BOUND_STATIC109#ifdef BOUND_STATIC
110static BoundEntry *__bound_t1[BOUND_T1_SIZE]; /* page table */110static BoundEntry *__bound_t1[BOUND_T1_SIZE]; /* page table */
@@ -116,12 +116,12 @@ static BoundEntry *__bound_invalid_t2; /* invalid page, for invalid pointers */ · 5 unmodified lines
116 116
117static BoundEntry *__bound_find_region(BoundEntry *e1, void *p)117static BoundEntry *__bound_find_region(BoundEntry *e1, void *p)
118{118{
119 unsigned long addr, tmp;119 size_t addr, tmp;
120 BoundEntry *e;120 BoundEntry *e;
121 121
122 e = e1;122 e = e1;
123 while (e != NULL) {123 while (e != NULL) {
124 addr = (unsigned long)p;124 addr = (size_t)p;
125 addr -= e->start;125 addr -= e->start;
126 if (addr <= e->size) {126 if (addr <= e->size) {
127 /* put region at the head */127 /* put region at the head */
@@ -156,9 +156,9 @@ static void bound_alloc_error(void) · 28 unmodified lines
156 156
157/* return '(p + offset)' for pointer arithmetic (a pointer can reach157/* return '(p + offset)' for pointer arithmetic (a pointer can reach
158 the end of a region in this case */158 the end of a region in this case */
159void * FASTCALL __bound_ptr_add(void *p, int offset)159void * FASTCALL __bound_ptr_add(void *p, size_t offset)
160{160{
161 unsigned long addr = (unsigned long)p;161 size_t addr = (size_t)p;
162 BoundEntry *e;162 BoundEntry *e;
163#if defined(BOUND_DEBUG)163#if defined(BOUND_DEBUG)
164 printf("add: 0x%x %d\n", (int)p, offset);164 printf("add: 0x%x %d\n", (int)p, offset);
@@ -171,7 +171,7 @@ void * FASTCALL __bound_ptr_add(void *p, int offset) · 6 unmodified lines
171 addr -= e->start;171 addr -= e->start;
172 if (addr > e->size) {172 if (addr > e->size) {
173 e = __bound_find_region(e, p);173 e = __bound_find_region(e, p);
174 addr = (unsigned long)p - e->start;174 addr = (size_t)p - e->start;
175 }175 }
176 addr += offset;176 addr += offset;
177 if (addr > e->size)177 if (addr > e->size)
@@ -184,7 +184,7 @@ void * FASTCALL __bound_ptr_add(void *p, int offset) · 6 unmodified lines
184#define BOUND_PTR_INDIR(dsize) \184#define BOUND_PTR_INDIR(dsize) \
185void * FASTCALL __bound_ptr_indir ## dsize (void *p, int offset) \185void * FASTCALL __bound_ptr_indir ## dsize (void *p, int offset) \
186{ \186{ \
187 unsigned long addr = (unsigned long)p; \187 size_t addr = (size_t)p; \
188 BoundEntry *e; \188 BoundEntry *e; \
189 \189 \
190 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \190 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
@@ -194,7 +194,7 @@ void * FASTCALL __bound_ptr_indir ## dsize (void *p, int offset) \ · 3 unmodified lines
194 addr -= e->start; \194 addr -= e->start; \
195 if (addr > e->size) { \195 if (addr > e->size) { \
196 e = __bound_find_region(e, p); \196 e = __bound_find_region(e, p); \
197 addr = (unsigned long)p - e->start; \197 addr = (size_t)p - e->start; \
198 } \198 } \
199 addr += offset + dsize; \199 addr += offset + dsize; \
200 if (addr > e->size) \200 if (addr > e->size) \
@@ -212,13 +212,13 @@ BOUND_PTR_INDIR(16) · 11 unmodified lines
212/* return the frame pointer of the caller */212/* return the frame pointer of the caller */
213#define GET_CALLER_FP(fp)\213#define GET_CALLER_FP(fp)\
214{\214{\
215 fp = (unsigned long)__builtin_frame_address(1);\215 fp = (size_t)__builtin_frame_address(1);\
216}216}
217 217
218/* called when entering a function to add all the local regions */218/* called when entering a function to add all the local regions */
219void FASTCALL __bound_local_new(void *p1) 219void FASTCALL __bound_local_new(void *p1)
220{220{
221 unsigned long addr, size, fp, *p = p1;221 size_t addr, size, fp, *p = p1;
222 GET_CALLER_FP(fp);222 GET_CALLER_FP(fp);
223 for(;;) {223 for(;;) {
224 addr = p[0];224 addr = p[0];
@@ -234,7 +234,7 @@ void FASTCALL __bound_local_new(void *p1) · 9 unmodified lines
234/* called when leaving a function to delete all the local regions */234/* called when leaving a function to delete all the local regions */
235void FASTCALL __bound_local_delete(void *p1) 235void FASTCALL __bound_local_delete(void *p1)
236{236{
237 unsigned long addr, fp, *p = p1;237 size_t addr, fp, *p = p1;
238 GET_CALLER_FP(fp);238 GET_CALLER_FP(fp);
239 for(;;) {239 for(;;) {
240 addr = p[0];240 addr = p[0];
@@ -290,11 +290,11 @@ static inline BoundEntry *get_page(int index) · 49 unmodified lines
290}290}
291 291
292/* mark a region as being invalid (can only be used during init) */292/* mark a region as being invalid (can only be used during init) */
293static void mark_invalid(unsigned long addr, unsigned long size)293static void mark_invalid(size_t addr, size_t size)
294{294{
295 unsigned long start, end;295 size_t start, end;
296 BoundEntry *page;296 BoundEntry *page;
297 int t1_start, t1_end, i, j, t2_start, t2_end;297 size_t t1_start, t1_end, i, j, t2_start, t2_end;
298 298
299 start = addr;299 start = addr;
300 end = addr + size;300 end = addr + size;
@@ -347,7 +347,7 @@ void __bound_init(void) · 46 unmodified lines
347{347{
348 int i;348 int i;
349 BoundEntry *page;349 BoundEntry *page;
350 unsigned long start, size;350 size_t start, size;
351 351
352 /* int *p; */352 /* int *p; */
353 __PTRDIFF_TYPE__ *p; /* 32 or 64 bit integer */353 __PTRDIFF_TYPE__ *p; /* 32 or 64 bit integer */
@@ -376,7 +376,7 @@ void __bound_init(void) · 22 unmodified lines
376 __bound_invalid_t2 = page;376 __bound_invalid_t2 = page;
377 377
378 /* invalid pointer zone */378 /* invalid pointer zone */
379 start = (unsigned long)INVALID_POINTER & ~(BOUND_T23_SIZE - 1);379 start = (size_t)INVALID_POINTER & ~(BOUND_T23_SIZE - 1);
380 size = BOUND_T23_SIZE;380 size = BOUND_T23_SIZE;
381 mark_invalid(start, size);381 mark_invalid(start, size);
382 382
@@ -407,7 +407,7 @@ void __bound_init(void) · 24 unmodified lines
407 * (b) on Linux >= v3.3, the alternative is to read407 * (b) on Linux >= v3.3, the alternative is to read
408 * start_brk from /proc/self/stat408 * start_brk from /proc/self/stat
409 */409 */
410 start = (unsigned long)sbrk(0);410 start = (size_t)sbrk(0);
411 size = 128 * 0x100000;411 size = 128 * 0x100000;
412 mark_invalid(start, size);412 mark_invalid(start, size);
413#endif413#endif
@@ -433,7 +433,7 @@ void __bound_exit(void) · 19 unmodified lines
433}433}
434 434
435static inline void add_region(BoundEntry *e, 435static inline void add_region(BoundEntry *e,
436 unsigned long start, unsigned long size)436 size_t start, size_t size)
437{437{
438 BoundEntry *e1;438 BoundEntry *e1;
439 if (e->start == 0) {439 if (e->start == 0) {
@@ -453,13 +453,13 @@ static inline void add_region(BoundEntry *e, · 13 unmodified lines
453}453}
454 454
455/* create a new region. It should not already exist in the region list */455/* create a new region. It should not already exist in the region list */
456void __bound_new_region(void *p, unsigned long size)456void __bound_new_region(void *p, size_t size)
457{457{
458 unsigned long start, end;458 size_t start, end;
459 BoundEntry *page, *e, *e2;459 BoundEntry *page, *e, *e2;
460 int t1_start, t1_end, i, t2_start, t2_end;460 size_t t1_start, t1_end, i, t2_start, t2_end;
461 461
462 start = (unsigned long)p;462 start = (size_t)p;
463 end = start + size;463 end = start + size;
464 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);464 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
465 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);465 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
@@ -519,12 +519,12 @@ void __bound_new_region(void *p, unsigned long size) · 53 unmodified lines
519 519
520/* delete a region */520/* delete a region */
521static inline void delete_region(BoundEntry *e, 521static inline void delete_region(BoundEntry *e,
522 void *p, unsigned long empty_size)522 void *p, size_t empty_size)
523{523{
524 unsigned long addr;524 size_t addr;
525 BoundEntry *e1;525 BoundEntry *e1;
526 526
527 addr = (unsigned long)p;527 addr = (size_t)p;
528 addr -= e->start;528 addr -= e->start;
529 if (addr <= e->size) {529 if (addr <= e->size) {
530 /* region found is first one */530 /* region found is first one */
@@ -548,7 +548,7 @@ static inline void delete_region(BoundEntry *e, · 17 unmodified lines
548 /* region not found: do nothing */548 /* region not found: do nothing */
549 if (e == NULL)549 if (e == NULL)
550 break;550 break;
551 addr = (unsigned long)p - e->start;551 addr = (size_t)p - e->start;
552 if (addr <= e->size) {552 if (addr <= e->size) {
553 /* found: remove entry */553 /* found: remove entry */
554 e1->next = e->next;554 e1->next = e->next;
@@ -563,11 +563,11 @@ static inline void delete_region(BoundEntry *e, · 8 unmodified lines
563/* return non zero if error */563/* return non zero if error */
564int __bound_delete_region(void *p)564int __bound_delete_region(void *p)
565{565{
566 unsigned long start, end, addr, size, empty_size;566 size_t start, end, addr, size, empty_size;
567 BoundEntry *page, *e, *e2;567 BoundEntry *page, *e, *e2;
568 int t1_start, t1_end, t2_start, t2_end, i;568 size_t t1_start, t1_end, t2_start, t2_end, i;
569 569
570 start = (unsigned long)p;570 start = (size_t)p;
571 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);571 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
572 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) & 572 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
573 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);573 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
@@ -579,7 +579,7 @@ int __bound_delete_region(void *p) · 5 unmodified lines
579 if (addr > e->size)579 if (addr > e->size)
580 e = __bound_find_region(e, p);580 e = __bound_find_region(e, p);
581 /* test if invalid region */581 /* test if invalid region */
582 if (e->size == EMPTY_SIZE || (unsigned long)p != e->start) 582 if (e->size == EMPTY_SIZE || (size_t)p != e->start)
583 return -1;583 return -1;
584 /* compute the size we put in invalid regions */584 /* compute the size we put in invalid regions */
585 if (e->is_invalid)585 if (e->is_invalid)
@@ -638,9 +638,9 @@ int __bound_delete_region(void *p) · 52 unmodified lines
638 638
639/* return the size of the region starting at p, or EMPTY_SIZE if non639/* return the size of the region starting at p, or EMPTY_SIZE if non
640 existent region. */640 existent region. */
641static unsigned long get_region_size(void *p)641static size_t get_region_size(void *p)
642{642{
643 unsigned long addr = (unsigned long)p;643 size_t addr = (size_t)p;
644 BoundEntry *e;644 BoundEntry *e;
645 645
646 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];646 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
@@ -650,7 +650,7 @@ static unsigned long get_region_size(void *p) · 3 unmodified lines
650 addr -= e->start;650 addr -= e->start;
651 if (addr > e->size)651 if (addr > e->size)
652 e = __bound_find_region(e, p);652 e = __bound_find_region(e, p);
653 if (e->start != (unsigned long)p)653 if (e->start != (size_t)p)
654 return EMPTY_SIZE;654 return EMPTY_SIZE;
655 return e->size;655 return e->size;
656}656}
@@ -764,7 +764,7 @@ void __bound_free(void *ptr, const void *caller) · 107 unmodified lines
764void *__bound_realloc(void *ptr, size_t size, const void *caller)764void *__bound_realloc(void *ptr, size_t size, const void *caller)
765{765{
766 void *ptr1;766 void *ptr1;
767 int old_size;767 size_t old_size;
768 768
769 if (size == 0) {769 if (size == 0) {
770 __bound_free(ptr, caller);770 __bound_free(ptr, caller);
libtcc.c+2−2
@@ -388,9 +388,9 @@ ST_FUNC void section_realloc(Section *sec, unsigned long new_size) · 387 unmodified lines
388 388
389/* reserve at least 'size' bytes in section 'sec' from389/* reserve at least 'size' bytes in section 'sec' from
390 sec->data_offset. */390 sec->data_offset. */
391ST_FUNC void *section_ptr_add(Section *sec, unsigned long size)391ST_FUNC void *section_ptr_add(Section *sec, size_t size)
392{392{
393 unsigned long offset, offset1;393 size_t offset, offset1;
394 394
395 offset = sec->data_offset;395 offset = sec->data_offset;
396 offset1 = offset + size;396 offset1 = offset + size;
tccelf.c+1−1
@@ -1568,7 +1568,7 @@ static int tcc_add_support(TCCState *s1, const char *filename) · 1567 unmodified lines
1568ST_FUNC void tcc_add_bcheck(TCCState *s1)1568ST_FUNC void tcc_add_bcheck(TCCState *s1)
1569{1569{
1570#ifdef CONFIG_TCC_BCHECK1570#ifdef CONFIG_TCC_BCHECK
1571 unsigned long *ptr;1571 addr_t *ptr;
1572 Section *init_section;1572 Section *init_section;
1573 unsigned char *pinit;1573 unsigned char *pinit;
1574 int sym_index;1574 int sym_index;
tccgen.c+4−4
@@ -5781,11 +5781,11 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 5780 unmodified lines
5781 /* XXX: currently, since we do only one pass, we cannot track5781 /* XXX: currently, since we do only one pass, we cannot track
5782 '&' operators, so we add only arrays */5782 '&' operators, so we add only arrays */
5783 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {5783 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5784 unsigned long *bounds_ptr;5784 addr_t *bounds_ptr;
5785 /* add padding between regions */5785 /* add padding between regions */
5786 loc--;5786 loc--;
5787 /* then add local bound info */5787 /* then add local bound info */
5788 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));5788 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(addr_t));
5789 bounds_ptr[0] = addr;5789 bounds_ptr[0] = addr;
5790 bounds_ptr[1] = size;5790 bounds_ptr[1] = size;
5791 }5791 }
@@ -5892,11 +5892,11 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 100 unmodified lines
5892 /* handles bounds now because the symbol must be defined5892 /* handles bounds now because the symbol must be defined
5893 before for the relocation */5893 before for the relocation */
5894 if (tcc_state->do_bounds_check) {5894 if (tcc_state->do_bounds_check) {
5895 unsigned long *bounds_ptr;5895 addr_t *bounds_ptr;
5896 5896
5897 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);5897 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5898 /* then add global bound info */5898 /* then add global bound info */
5899 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));5899 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(addr_t));
5900 bounds_ptr[0] = 0; /* relocated */5900 bounds_ptr[0] = 0; /* relocated */
5901 bounds_ptr[1] = size;5901 bounds_ptr[1] = size;
5902 }5902 }
tccrun.c+1−1
@@ -110,7 +110,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) · 109 unmodified lines
110 if (s1->do_bounds_check) {110 if (s1->do_bounds_check) {
111 void (*bound_init)(void);111 void (*bound_init)(void);
112 void (*bound_exit)(void);112 void (*bound_exit)(void);
113 void (*bound_new_region)(void *p, unsigned long size);113 void (*bound_new_region)(void *p, addr_t size);
114 int (*bound_delete_region)(void *p);114 int (*bound_delete_region)(void *p);
115 int i;115 int i;
116 116

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