fixed VT_LLOCAL dereferencing
[tinycc.git] / bcheck.c
blob6f018c681502907096a99f91a2191cc9e2927090
1 /*
2 * Tiny C Memory and bounds checker
3 *
4 * Copyright (c) 2002 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //#define BOUND_DEBUG
23 /* define so that bound array is static (faster, but use memory if
24 bound checking not used) */
25 //#define BOUND_STATIC
27 #define BOUND_T1_BITS 13
28 #define BOUND_T2_BITS 11
29 #define BOUND_T3_BITS (32 - BOUND_T1_BITS - BOUND_T2_BITS)
31 #define BOUND_T1_SIZE (1 << BOUND_T1_BITS)
32 #define BOUND_T2_SIZE (1 << BOUND_T2_BITS)
33 #define BOUND_T3_SIZE (1 << BOUND_T3_BITS)
34 #define BOUND_E_BITS 4
36 #define BOUND_T23_BITS (BOUND_T2_BITS + BOUND_T3_BITS)
37 #define BOUND_T23_SIZE (1 << BOUND_T23_BITS)
40 /* this pointer is generated when bound check is incorrect */
41 #define INVALID_POINTER ((void *)(-2))
42 /* size of an empty region */
43 #define EMPTY_SIZE 0xffffffff
44 /* size of an invalid region */
45 #define INVALID_SIZE 0
47 typedef struct BoundEntry {
48 unsigned long start;
49 unsigned long size;
50 struct BoundEntry *next;
51 unsigned long is_invalid; /* true if pointers outside region are invalid */
52 } BoundEntry;
54 /* external interface */
55 void __bound_init(void);
56 void __bound_new_region(void *p, unsigned long size);
57 void __bound_delete_region(void *p);
59 void *__bound_ptr_add(void *p, int offset) __attribute__((regparm(2)));
60 char __bound_ptr_indir_b(void *p, int offset) __attribute__((regparm(2)));
61 unsigned char __bound_ptr_indir_ub(void *p, int offset) __attribute__((regparm(2)));
62 short __bound_ptr_indir_w(void *p, int offset) __attribute__((regparm(2)));
63 unsigned short __bound_ptr_indir_uw(void *p, int offset) __attribute__((regparm(2)));
64 int __bound_ptr_indir_i(void *p, int offset) __attribute((regparm(2)));
66 void *__bound_calloc(size_t nmemb, size_t size);
67 void *__bound_malloc(size_t size);
68 void __bound_free(void *ptr);
69 void *__bound_realloc(void *ptr, size_t size);
71 extern char _end;
73 #ifdef BOUND_STATIC
74 static BoundEntry *__bound_t1[BOUND_T1_SIZE]; /* page table */
75 #else
76 static BoundEntry **__bound_t1; /* page table */
77 #endif
78 static BoundEntry *__bound_empty_t2; /* empty page, for unused pages */
79 static BoundEntry *__bound_invalid_t2; /* invalid page, for invalid pointers */
81 static BoundEntry *__bound_find_region(BoundEntry *e1, void *p)
83 unsigned long addr, tmp;
84 BoundEntry *e;
86 e = e1;
87 while (e != NULL) {
88 addr = (unsigned long)p;
89 addr -= e->start;
90 if (addr <= e->size) {
91 /* put region at the head */
92 tmp = e1->start;
93 e1->start = e->start;
94 e->start = tmp;
95 tmp = e1->size;
96 e1->size = e->size;
97 e->size = tmp;
98 return e1;
100 e = e->next;
102 /* no entry found: return empty entry or invalid entry */
103 if (e1->is_invalid)
104 return __bound_invalid_t2;
105 else
106 return __bound_empty_t2;
109 static void bound_error(const char *fmt, ...)
111 va_list ap;
112 va_start(ap, fmt);
113 fprintf(stderr, "bounds check: ");
114 vfprintf(stderr, fmt, ap);
115 fprintf(stderr, "\n");
116 exit(1);
117 va_end(ap);
120 static void bound_alloc_error(void)
122 bound_error("not enough memory for bound checking code\n");
125 /* do: return (p + offset); */
126 void *__bound_ptr_add(void *p, int offset)
128 unsigned long addr = (unsigned long)p;
129 BoundEntry *e;
130 #ifdef BOUND_DEBUG
131 printf("add: 0x%x %d\n", (int)p, offset);
132 #endif
134 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
135 e = (BoundEntry *)((char *)e +
136 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
137 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
138 addr -= e->start;
139 if (addr > e->size) {
140 e = __bound_find_region(e, p);
141 addr = (unsigned long)p - e->start;
143 addr += offset;
144 if (addr > e->size)
145 return INVALID_POINTER; /* return an invalid pointer */
146 return p + offset;
149 /* do: return *(type *)(p + offset); */
150 #define BOUND_PTR_INDIR(suffix, type) \
151 type __bound_ptr_indir_ ## suffix (void *p, int offset) \
153 unsigned long addr = (unsigned long)p; \
154 BoundEntry *e; \
156 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
157 e = (BoundEntry *)((char *)e + \
158 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
159 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
160 addr -= e->start; \
161 if (addr > e->size) { \
162 e = __bound_find_region(e, p); \
163 addr = (unsigned long)p - e->start; \
165 addr += offset + sizeof(type); \
166 if (addr > e->size) \
167 bound_error("dereferencing invalid pointer"); \
168 return *(type *)(p + offset); \
171 BOUND_PTR_INDIR(b, char)
172 BOUND_PTR_INDIR(ub, unsigned char)
173 BOUND_PTR_INDIR(w, short)
174 BOUND_PTR_INDIR(uw, unsigned short)
175 BOUND_PTR_INDIR(i, int)
177 static BoundEntry *__bound_new_page(void)
179 BoundEntry *page;
180 int i;
182 page = malloc(sizeof(BoundEntry) * BOUND_T2_SIZE);
183 if (!page)
184 bound_alloc_error();
185 for(i=0;i<BOUND_T2_SIZE;i++) {
186 /* put empty entries */
187 page[i].start = 0;
188 page[i].size = EMPTY_SIZE;
189 page[i].next = NULL;
190 page[i].is_invalid = 0;
192 return page;
195 /* currently we use malloc(). Should use bound_new_page() */
196 static BoundEntry *bound_new_entry(void)
198 BoundEntry *e;
199 e = malloc(sizeof(BoundEntry));
200 return e;
203 static void bound_free_entry(BoundEntry *e)
205 free(e);
208 static inline BoundEntry *get_page(int index)
210 BoundEntry *page;
211 page = __bound_t1[index];
212 if (page == __bound_empty_t2 || page == __bound_invalid_t2) {
213 /* create a new page if necessary */
214 page = __bound_new_page();
215 __bound_t1[index] = page;
217 return page;
220 /* mark a region as being invalid (can only be used during init) */
221 static void mark_invalid(unsigned long addr, unsigned long size)
223 unsigned long start, end;
224 BoundEntry *page;
225 int t1_start, t1_end, i, j, t2_start, t2_end;
227 start = addr;
228 end = addr + size;
230 t2_start = (start + BOUND_T3_SIZE - 1) >> BOUND_T3_BITS;
231 if (end != 0)
232 t2_end = end >> BOUND_T3_BITS;
233 else
234 t2_end = 1 << (BOUND_T1_BITS + BOUND_T2_BITS);
236 printf("mark_invalid: start = %x %x\n", t2_start, t2_end);
238 /* first we handle full pages */
239 t1_start = (t2_start + BOUND_T2_SIZE - 1) >> BOUND_T2_BITS;
240 t1_end = t2_end >> BOUND_T2_BITS;
242 i = t2_start & (BOUND_T2_SIZE - 1);
243 j = t2_end & (BOUND_T2_SIZE - 1);
245 if (t1_start == t1_end) {
246 page = get_page(t2_start >> BOUND_T2_BITS);
247 for(; i < j; i++) {
248 page[i].size = INVALID_SIZE;
249 page[i].is_invalid = 1;
251 } else {
252 if (i > 0) {
253 page = get_page(t2_start >> BOUND_T2_BITS);
254 for(; i < BOUND_T2_SIZE; i++) {
255 page[i].size = INVALID_SIZE;
256 page[i].is_invalid = 1;
259 for(i = t1_start; i < t1_end; i++) {
260 __bound_t1[i] = __bound_invalid_t2;
262 if (j != 0) {
263 page = get_page(t1_end);
264 for(i = 0; i < j; i++) {
265 page[i].size = INVALID_SIZE;
266 page[i].is_invalid = 1;
272 void __bound_init(void)
274 int i;
275 BoundEntry *page;
276 unsigned long start, size;
278 #ifndef BOUND_STATIC
279 __bound_t1 = malloc(BOUND_T1_SIZE * sizeof(BoundEntry *));
280 if (!__bound_t1)
281 bound_alloc_error();
282 #endif
283 __bound_empty_t2 = __bound_new_page();
284 for(i=0;i<BOUND_T1_SIZE;i++) {
285 __bound_t1[i] = __bound_empty_t2;
288 page = __bound_new_page();
289 for(i=0;i<BOUND_T2_SIZE;i++) {
290 /* put invalid entries */
291 page[i].start = 0;
292 page[i].size = INVALID_SIZE;
293 page[i].next = NULL;
294 page[i].is_invalid = 1;
296 __bound_invalid_t2 = page;
298 /* invalid pointer zone */
299 start = (unsigned long)INVALID_POINTER & ~(BOUND_T23_SIZE - 1);
300 size = BOUND_T23_SIZE;
301 mark_invalid(start, size);
303 /* malloc zone is also marked invalid */
304 start = (unsigned long)&_end;
305 size = 128 * 0x100000;
306 mark_invalid(start, size);
309 static inline void add_region(BoundEntry *e,
310 unsigned long start, unsigned long size)
312 BoundEntry *e1;
313 if (e->start == 0) {
314 /* no region : add it */
315 e->start = start;
316 e->size = size;
317 } else {
318 /* already regions in the list: add it at the head */
319 e1 = bound_new_entry();
320 e1->start = e->start;
321 e1->size = e->size;
322 e1->next = e->next;
323 e->start = start;
324 e->size = size;
325 e->next = e1;
329 /* create a new region. It should not already exist in the region list */
330 void __bound_new_region(void *p, unsigned long size)
332 unsigned long start, end;
333 BoundEntry *page, *e, *e2;
334 int t1_start, t1_end, i, t2_start, t2_end;
336 start = (unsigned long)p;
337 end = start + size;
338 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
339 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
341 /* start */
342 page = get_page(t1_start);
343 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
344 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
345 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
346 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
347 #ifdef BOUND_DEBUG
348 printf("new %lx %lx %x %x %x %x\n",
349 start, end, t1_start, t1_end, t2_start, t2_end);
350 #endif
352 e = (BoundEntry *)((char *)page + t2_start);
353 add_region(e, start, size);
355 if (t1_end == t1_start) {
356 /* same ending page */
357 e2 = (BoundEntry *)((char *)page + t2_end);
358 if (e2 > e) {
359 e++;
360 for(;e<e2;e++) {
361 e->start = start;
362 e->size = size;
364 add_region(e, start, size);
366 } else {
367 /* mark until end of page */
368 e2 = page + BOUND_T2_SIZE;
369 e++;
370 for(;e<e2;e++) {
371 e->start = start;
372 e->size = size;
374 /* mark intermediate pages, if any */
375 for(i=t1_start+1;i<t1_end;i++) {
376 page = get_page(i);
377 e2 = page + BOUND_T2_SIZE;
378 for(e=page;e<e2;e++) {
379 e->start = start;
380 e->size = size;
383 /* last page */
384 page = get_page(t2_end);
385 e2 = (BoundEntry *)((char *)page + t2_end);
386 for(e=page;e<e2;e++) {
387 e->start = start;
388 e->size = size;
390 add_region(e, start, size);
394 /* delete a region */
395 static inline void delete_region(BoundEntry *e,
396 void *p, unsigned long empty_size)
398 unsigned long addr;
399 BoundEntry *e1;
401 addr = (unsigned long)p;
402 addr -= e->start;
403 if (addr <= e->size) {
404 /* region found is first one */
405 e1 = e->next;
406 if (e1 == NULL) {
407 /* no more region: mark it empty */
408 e->start = 0;
409 e->size = empty_size;
410 } else {
411 /* copy next region in head */
412 e->start = e1->start;
413 e->size = e1->size;
414 e->next = e1->next;
415 bound_free_entry(e1);
417 } else {
418 /* find the matching region */
419 for(;;) {
420 e1 = e;
421 e = e->next;
422 /* region not found: do nothing */
423 if (e == NULL)
424 break;
425 addr = (unsigned long)p - e->start;
426 if (addr <= e->size) {
427 /* found: remove entry */
428 e1->next = e->next;
429 bound_free_entry(e);
430 break;
436 /* WARNING: 'p' must be the starting point of the region. */
437 void __bound_delete_region(void *p)
439 unsigned long start, end, addr, size, empty_size;
440 BoundEntry *page, *e, *e2;
441 int t1_start, t1_end, t2_start, t2_end, i;
443 start = (unsigned long)p;
444 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
445 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
446 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
448 /* find region size */
449 page = __bound_t1[t1_start];
450 e = (BoundEntry *)((char *)page + t2_start);
451 addr = start - e->start;
452 if (addr > e->size)
453 e = __bound_find_region(e, p);
454 /* test if invalid region */
455 if (e->size == EMPTY_SIZE || (unsigned long)p != e->start)
456 bound_error("deleting invalid region");
457 /* compute the size we put in invalid regions */
458 if (e->is_invalid)
459 empty_size = INVALID_SIZE;
460 else
461 empty_size = EMPTY_SIZE;
462 size = e->size;
463 end = start + size;
465 /* now we can free each entry */
466 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
467 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
468 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
470 delete_region(e, p, empty_size);
471 if (t1_end == t1_start) {
472 /* same ending page */
473 e2 = (BoundEntry *)((char *)page + t2_end);
474 if (e2 > e) {
475 e++;
476 for(;e<e2;e++) {
477 e->start = 0;
478 e->size = empty_size;
480 delete_region(e, p, empty_size);
482 } else {
483 /* mark until end of page */
484 e2 = page + BOUND_T2_SIZE;
485 e++;
486 for(;e<e2;e++) {
487 e->start = 0;
488 e->size = empty_size;
490 /* mark intermediate pages, if any */
491 /* XXX: should free them */
492 for(i=t1_start+1;i<t1_end;i++) {
493 page = get_page(i);
494 e2 = page + BOUND_T2_SIZE;
495 for(e=page;e<e2;e++) {
496 e->start = 0;
497 e->size = empty_size;
500 /* last page */
501 page = get_page(t2_end);
502 e2 = (BoundEntry *)((char *)page + t2_end);
503 for(e=page;e<e2;e++) {
504 e->start = 0;
505 e->size = empty_size;
507 delete_region(e, p, empty_size);
511 /* return the size of the region starting at p, or EMPTY_SIZE if non
512 existant region. */
513 static unsigned long get_region_size(void *p)
515 unsigned long addr = (unsigned long)p;
516 BoundEntry *e;
518 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
519 e = (BoundEntry *)((char *)e +
520 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
521 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
522 addr -= e->start;
523 if (addr > e->size)
524 e = __bound_find_region(e, p);
525 if (e->start != (unsigned long)p)
526 return EMPTY_SIZE;
527 return e->size;
530 /* patched memory functions */
532 /* XXX: we should use a malloc which ensure that it is unlikely that
533 two malloc'ed data have the same address if 'free' are made in
534 between. */
535 void *__bound_malloc(size_t size)
537 void *ptr;
538 /* we allocate one more byte to ensure the regions will be
539 separated by at least one byte. With the glibc malloc, it may
540 be in fact not necessary */
541 ptr = malloc(size + 1);
542 if (!ptr)
543 return NULL;
544 __bound_new_region(ptr, size);
545 return ptr;
548 void __bound_free(void *ptr)
550 if (ptr == NULL)
551 return;
552 __bound_delete_region(ptr);
553 free(ptr);
556 void *__bound_realloc(void *ptr, size_t size)
558 void *ptr1;
559 int old_size;
561 if (size == 0) {
562 __bound_free(ptr);
563 return NULL;
564 } else {
565 ptr1 = __bound_malloc(size);
566 if (ptr == NULL || ptr1 == NULL)
567 return ptr1;
568 old_size = get_region_size(ptr);
569 if (old_size == EMPTY_SIZE)
570 bound_error("realloc'ing invalid pointer");
571 memcpy(ptr1, ptr, old_size);
572 __bound_free(ptr);
573 return ptr1;
577 void *__bound_calloc(size_t nmemb, size_t size)
579 void *ptr;
580 size = size * nmemb;
581 ptr = __bound_malloc(size);
582 if (!ptr)
583 return NULL;
584 memset(ptr, 0, size);
585 return ptr;
588 #if 0
589 static void bound_dump(void)
591 BoundEntry *page, *e;
592 int i, j;
594 printf("region dump:\n");
595 for(i=0;i<BOUND_T1_SIZE;i++) {
596 page = __bound_t1[i];
597 for(j=0;j<BOUND_T2_SIZE;j++) {
598 e = page + j;
599 /* do not print invalid or empty entries */
600 if (e->size != EMPTY_SIZE && e->start != 0) {
601 printf("%08x:",
602 (i << (BOUND_T2_BITS + BOUND_T3_BITS)) +
603 (j << BOUND_T3_BITS));
604 do {
605 printf(" %08lx:%08lx", e->start, e->start + e->size);
606 e = e->next;
607 } while (e != NULL);
608 printf("\n");
613 #endif