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
This commit is contained in:
seyko
2015-03-26 07:47:45 +03:00
parent 548a55eda5
commit acef4ff244
6 changed files with 53 additions and 53 deletions

View File

@ -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);