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

Showing the changes to lib (1 of 6 files). Show all

Unified · Split

lib/bcheck.c+38−38
@@ -69,15 +69,15 @@ · 68 unmodified lines
6969 #define INVALID_SIZE 0
7070
7171 typedef struct BoundEntry {
72− unsigned long start;
73− unsigned long size;
72+ size_t start;
73+ size_t size;
7474 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 */
7676 } BoundEntry;
7777
7878 /* external interface */
7979 void __bound_init(void);
80−void __bound_new_region(void *p, unsigned long size);
80+void __bound_new_region(void *p, size_t size);
8181 int __bound_delete_region(void *p);
8282
8383 #define FASTCALL __attribute__((regparm(3)))
@@ -104,7 +104,7 @@ extern char __bounds_start; /* start of static bounds table */ · 20 unmodified lines
104104 const char *__bound_error_msg;
105105
106106 /* runtime error output */
107−extern void rt_error(unsigned long pc, const char *fmt, ...);
107+extern void rt_error(size_t pc, const char *fmt, ...);
108108
109109 #ifdef BOUND_STATIC
110110 static 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
116116
117117 static BoundEntry *__bound_find_region(BoundEntry *e1, void *p)
118118 {
119− unsigned long addr, tmp;
119+ size_t addr, tmp;
120120 BoundEntry *e;
121121
122122 e = e1;
123123 while (e != NULL) {
124− addr = (unsigned long)p;
124+ addr = (size_t)p;
125125 addr -= e->start;
126126 if (addr <= e->size) {
127127 /* put region at the head */
@@ -156,9 +156,9 @@ static void bound_alloc_error(void) · 28 unmodified lines
156156
157157 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
158158 the end of a region in this case */
159−void * FASTCALL __bound_ptr_add(void *p, int offset)
159+void * FASTCALL __bound_ptr_add(void *p, size_t offset)
160160 {
161− unsigned long addr = (unsigned long)p;
161+ size_t addr = (size_t)p;
162162 BoundEntry *e;
163163 #if defined(BOUND_DEBUG)
164164 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
171171 addr -= e->start;
172172 if (addr > e->size) {
173173 e = __bound_find_region(e, p);
174− addr = (unsigned long)p - e->start;
174+ addr = (size_t)p - e->start;
175175 }
176176 addr += offset;
177177 if (addr > e->size)
@@ -184,7 +184,7 @@ void * FASTCALL __bound_ptr_add(void *p, int offset) · 6 unmodified lines
184184 #define BOUND_PTR_INDIR(dsize) \
185185 void * FASTCALL __bound_ptr_indir ## dsize (void *p, int offset) \
186186 { \
187− unsigned long addr = (unsigned long)p; \
187+ size_t addr = (size_t)p; \
188188 BoundEntry *e; \
189189 \
190190 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
194194 addr -= e->start; \
195195 if (addr > e->size) { \
196196 e = __bound_find_region(e, p); \
197− addr = (unsigned long)p - e->start; \
197+ addr = (size_t)p - e->start; \
198198 } \
199199 addr += offset + dsize; \
200200 if (addr > e->size) \
@@ -212,13 +212,13 @@ BOUND_PTR_INDIR(16) · 11 unmodified lines
212212 /* return the frame pointer of the caller */
213213 #define GET_CALLER_FP(fp)\
214214 {\
215− fp = (unsigned long)__builtin_frame_address(1);\
215+ fp = (size_t)__builtin_frame_address(1);\
216216 }
217217
218218 /* called when entering a function to add all the local regions */
219219 void FASTCALL __bound_local_new(void *p1)
220220 {
221− unsigned long addr, size, fp, *p = p1;
221+ size_t addr, size, fp, *p = p1;
222222 GET_CALLER_FP(fp);
223223 for(;;) {
224224 addr = p[0];
@@ -234,7 +234,7 @@ void FASTCALL __bound_local_new(void *p1) · 9 unmodified lines
234234 /* called when leaving a function to delete all the local regions */
235235 void FASTCALL __bound_local_delete(void *p1)
236236 {
237− unsigned long addr, fp, *p = p1;
237+ size_t addr, fp, *p = p1;
238238 GET_CALLER_FP(fp);
239239 for(;;) {
240240 addr = p[0];
@@ -290,11 +290,11 @@ static inline BoundEntry *get_page(int index) · 49 unmodified lines
290290 }
291291
292292 /* mark a region as being invalid (can only be used during init) */
293−static void mark_invalid(unsigned long addr, unsigned long size)
293+static void mark_invalid(size_t addr, size_t size)
294294 {
295− unsigned long start, end;
295+ size_t start, end;
296296 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;
298298
299299 start = addr;
300300 end = addr + size;
@@ -347,7 +347,7 @@ void __bound_init(void) · 46 unmodified lines
347347 {
348348 int i;
349349 BoundEntry *page;
350− unsigned long start, size;
350+ size_t start, size;
351351
352352 /* int *p; */
353353 __PTRDIFF_TYPE__ *p; /* 32 or 64 bit integer */
@@ -376,7 +376,7 @@ void __bound_init(void) · 22 unmodified lines
376376 __bound_invalid_t2 = page;
377377
378378 /* invalid pointer zone */
379− start = (unsigned long)INVALID_POINTER & ~(BOUND_T23_SIZE - 1);
379+ start = (size_t)INVALID_POINTER & ~(BOUND_T23_SIZE - 1);
380380 size = BOUND_T23_SIZE;
381381 mark_invalid(start, size);
382382
@@ -407,7 +407,7 @@ void __bound_init(void) · 24 unmodified lines
407407 * (b) on Linux >= v3.3, the alternative is to read
408408 * start_brk from /proc/self/stat
409409 */
410− start = (unsigned long)sbrk(0);
410+ start = (size_t)sbrk(0);
411411 size = 128 * 0x100000;
412412 mark_invalid(start, size);
413413 #endif
@@ -433,7 +433,7 @@ void __bound_exit(void) · 19 unmodified lines
433433 }
434434
435435 static inline void add_region(BoundEntry *e,
436− unsigned long start, unsigned long size)
436+ size_t start, size_t size)
437437 {
438438 BoundEntry *e1;
439439 if (e->start == 0) {
@@ -453,13 +453,13 @@ static inline void add_region(BoundEntry *e, · 13 unmodified lines
453453 }
454454
455455 /* create a new region. It should not already exist in the region list */
456−void __bound_new_region(void *p, unsigned long size)
456+void __bound_new_region(void *p, size_t size)
457457 {
458− unsigned long start, end;
458+ size_t start, end;
459459 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;
461461
462− start = (unsigned long)p;
462+ start = (size_t)p;
463463 end = start + size;
464464 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
465465 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
519519
520520 /* delete a region */
521521 static inline void delete_region(BoundEntry *e,
522− void *p, unsigned long empty_size)
522+ void *p, size_t empty_size)
523523 {
524− unsigned long addr;
524+ size_t addr;
525525 BoundEntry *e1;
526526
527− addr = (unsigned long)p;
527+ addr = (size_t)p;
528528 addr -= e->start;
529529 if (addr <= e->size) {
530530 /* region found is first one */
@@ -548,7 +548,7 @@ static inline void delete_region(BoundEntry *e, · 17 unmodified lines
548548 /* region not found: do nothing */
549549 if (e == NULL)
550550 break;
551− addr = (unsigned long)p - e->start;
551+ addr = (size_t)p - e->start;
552552 if (addr <= e->size) {
553553 /* found: remove entry */
554554 e1->next = e->next;
@@ -563,11 +563,11 @@ static inline void delete_region(BoundEntry *e, · 8 unmodified lines
563563 /* return non zero if error */
564564 int __bound_delete_region(void *p)
565565 {
566− unsigned long start, end, addr, size, empty_size;
566+ size_t start, end, addr, size, empty_size;
567567 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;
569569
570− start = (unsigned long)p;
570+ start = (size_t)p;
571571 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
572572 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
573573 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
@@ -579,7 +579,7 @@ int __bound_delete_region(void *p) · 5 unmodified lines
579579 if (addr > e->size)
580580 e = __bound_find_region(e, p);
581581 /* 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)
583583 return -1;
584584 /* compute the size we put in invalid regions */
585585 if (e->is_invalid)
@@ -638,9 +638,9 @@ int __bound_delete_region(void *p) · 52 unmodified lines
638638
639639 /* return the size of the region starting at p, or EMPTY_SIZE if non
640640 existent region. */
641−static unsigned long get_region_size(void *p)
641+static size_t get_region_size(void *p)
642642 {
643− unsigned long addr = (unsigned long)p;
643+ size_t addr = (size_t)p;
644644 BoundEntry *e;
645645
646646 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
650650 addr -= e->start;
651651 if (addr > e->size)
652652 e = __bound_find_region(e, p);
653− if (e->start != (unsigned long)p)
653+ if (e->start != (size_t)p)
654654 return EMPTY_SIZE;
655655 return e->size;
656656 }
@@ -764,7 +764,7 @@ void __bound_free(void *ptr, const void *caller) · 107 unmodified lines
764764 void *__bound_realloc(void *ptr, size_t size, const void *caller)
765765 {
766766 void *ptr1;
767− int old_size;
767+ size_t old_size;
768768
769769 if (size == 0) {
770770 __bound_free(ptr, caller);

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