removed many memory leaks - suppressed many global variables
[tinycc.git] / bcheck.c
blob1f08f20aca0a2f07d62a01a11f0b594b358dbcc8
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.
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include <malloc.h>
26 //#define BOUND_DEBUG
28 /* define so that bound array is static (faster, but use memory if
29 bound checking not used) */
30 //#define BOUND_STATIC
32 /* use malloc hooks. Currently the code cannot be reliable if no hooks */
33 #define CONFIG_TCC_MALLOC_HOOKS
35 #define BOUND_T1_BITS 13
36 #define BOUND_T2_BITS 11
37 #define BOUND_T3_BITS (32 - BOUND_T1_BITS - BOUND_T2_BITS)
39 #define BOUND_T1_SIZE (1 << BOUND_T1_BITS)
40 #define BOUND_T2_SIZE (1 << BOUND_T2_BITS)
41 #define BOUND_T3_SIZE (1 << BOUND_T3_BITS)
42 #define BOUND_E_BITS 4
44 #define BOUND_T23_BITS (BOUND_T2_BITS + BOUND_T3_BITS)
45 #define BOUND_T23_SIZE (1 << BOUND_T23_BITS)
48 /* this pointer is generated when bound check is incorrect */
49 #define INVALID_POINTER ((void *)(-2))
50 /* size of an empty region */
51 #define EMPTY_SIZE 0xffffffff
52 /* size of an invalid region */
53 #define INVALID_SIZE 0
55 typedef struct BoundEntry {
56 unsigned long start;
57 unsigned long size;
58 struct BoundEntry *next;
59 unsigned long is_invalid; /* true if pointers outside region are invalid */
60 } BoundEntry;
62 /* external interface */
63 void __bound_init(void);
64 void __bound_new_region(void *p, unsigned long size);
65 int __bound_delete_region(void *p);
67 /* currently, tcc cannot compile that because we use unsupported GNU C
68 extensions */
69 #if !defined(__TINYC__)
70 void *__bound_ptr_add(void *p, int offset) __attribute__((regparm(2)));
71 void *__bound_ptr_indir1(void *p, int offset) __attribute__((regparm(2)));
72 void *__bound_ptr_indir2(void *p, int offset) __attribute__((regparm(2)));
73 void *__bound_ptr_indir4(void *p, int offset) __attribute__((regparm(2)));
74 void *__bound_ptr_indir8(void *p, int offset) __attribute__((regparm(2)));
75 void *__bound_ptr_indir12(void *p, int offset) __attribute__((regparm(2)));
76 void *__bound_ptr_indir16(void *p, int offset) __attribute__((regparm(2)));
77 void __bound_local_new(void *p) __attribute__((regparm(1)));
78 void __bound_local_delete(void *p) __attribute__((regparm(1)));
79 #endif
80 static void *get_caller_pc(int n);
82 void *__bound_malloc(size_t size, const void *caller);
83 void *__bound_memalign(size_t size, size_t align, const void *caller);
84 void __bound_free(void *ptr, const void *caller);
85 void *__bound_realloc(void *ptr, size_t size, const void *caller);
86 static void *libc_malloc(size_t size);
87 static void libc_free(void *ptr);
88 static void install_malloc_hooks(void);
89 static void restore_malloc_hooks(void);
91 #ifdef CONFIG_TCC_MALLOC_HOOKS
92 static void *saved_malloc_hook;
93 static void *saved_free_hook;
94 static void *saved_realloc_hook;
95 static void *saved_memalign_hook;
96 #endif
98 /* linker definitions */
99 extern char _end;
101 /* TCC definitions */
102 extern char __bounds_start; /* start of static bounds table */
103 /* error function. if NULL, simply do abort() */
104 void (*__bound_error_func)(unsigned long caller, const char *msg);
106 /* runtime error output */
107 extern void rt_error(unsigned long pc, const char *fmt, ...);
109 #ifdef BOUND_STATIC
110 static BoundEntry *__bound_t1[BOUND_T1_SIZE]; /* page table */
111 #else
112 static BoundEntry **__bound_t1; /* page table */
113 #endif
114 static BoundEntry *__bound_empty_t2; /* empty page, for unused pages */
115 static BoundEntry *__bound_invalid_t2; /* invalid page, for invalid pointers */
117 static BoundEntry *__bound_find_region(BoundEntry *e1, void *p)
119 unsigned long addr, tmp;
120 BoundEntry *e;
122 e = e1;
123 while (e != NULL) {
124 addr = (unsigned long)p;
125 addr -= e->start;
126 if (addr <= e->size) {
127 /* put region at the head */
128 tmp = e1->start;
129 e1->start = e->start;
130 e->start = tmp;
131 tmp = e1->size;
132 e1->size = e->size;
133 e->size = tmp;
134 return e1;
136 e = e->next;
138 /* no entry found: return empty entry or invalid entry */
139 if (e1->is_invalid)
140 return __bound_invalid_t2;
141 else
142 return __bound_empty_t2;
145 /* print a bound error message */
146 static void bound_error(const void *caller, const char *fmt, ...)
148 if (!__bound_error_func)
149 abort();
150 __bound_error_func((unsigned long)caller, fmt);
153 static void bound_alloc_error(void)
155 bound_error(NULL, "not enough memory for bound checking code");
158 /* currently, tcc cannot compile that because we use GNUC extensions */
159 #if !defined(__TINYC__)
161 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
162 the end of a region in this case */
163 void *__bound_ptr_add(void *p, int offset)
165 unsigned long addr = (unsigned long)p;
166 BoundEntry *e;
167 #if defined(BOUND_DEBUG)
168 printf("add: 0x%x %d\n", (int)p, offset);
169 #endif
171 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
172 e = (BoundEntry *)((char *)e +
173 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
174 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
175 addr -= e->start;
176 if (addr > e->size) {
177 e = __bound_find_region(e, p);
178 addr = (unsigned long)p - e->start;
180 addr += offset;
181 if (addr > e->size)
182 return INVALID_POINTER; /* return an invalid pointer */
183 return p + offset;
186 /* return '(p + offset)' for pointer indirection (the resulting must
187 be strictly inside the region */
188 #define BOUND_PTR_INDIR(dsize) \
189 void *__bound_ptr_indir ## dsize (void *p, int offset) \
191 unsigned long addr = (unsigned long)p; \
192 BoundEntry *e; \
194 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
195 e = (BoundEntry *)((char *)e + \
196 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
197 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
198 addr -= e->start; \
199 if (addr > e->size) { \
200 e = __bound_find_region(e, p); \
201 addr = (unsigned long)p - e->start; \
203 addr += offset + dsize; \
204 if (addr > e->size) \
205 return INVALID_POINTER; /* return an invalid pointer */ \
206 return p + offset; \
209 #ifdef __i386__
211 /* return the PC of the N'th caller (N=1: first caller) */
212 static void *get_caller_pc(int n)
214 unsigned long fp;
215 int i;
217 __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp));
218 for(i=0;i<n;i++)
219 fp = ((unsigned long *)fp)[0];
220 return ((void **)fp)[1];
223 /* return the frame pointer of the caller */
224 #define GET_CALLER_FP(fp)\
226 unsigned long *fp1;\
227 __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp1));\
228 fp = fp1[0];\
230 #else
231 #error put code to extract the calling frame pointer
232 #endif
234 /* called when entering a function to add all the local regions */
235 void __bound_local_new(void *p1)
237 unsigned long addr, size, fp, *p = p1;
238 GET_CALLER_FP(fp);
239 for(;;) {
240 addr = p[0];
241 if (addr == 0)
242 break;
243 addr += fp;
244 size = p[1];
245 p += 2;
246 __bound_new_region((void *)addr, size);
250 /* called when leaving a function to delete all the local regions */
251 void __bound_local_delete(void *p1)
253 unsigned long addr, fp, *p = p1;
254 GET_CALLER_FP(fp);
255 for(;;) {
256 addr = p[0];
257 if (addr == 0)
258 break;
259 addr += fp;
260 p += 2;
261 __bound_delete_region((void *)addr);
265 #else
267 void __bound_local_new(void *p)
270 void __bound_local_delete(void *p)
274 void *__bound_ptr_add(void *p, int offset)
276 return p + offset;
279 #define BOUND_PTR_INDIR(dsize) \
280 void *__bound_ptr_indir ## dsize (void *p, int offset) \
282 return p + offset; \
285 static void *get_caller_pc(int n)
287 return 0;
289 #endif
291 BOUND_PTR_INDIR(1)
292 BOUND_PTR_INDIR(2)
293 BOUND_PTR_INDIR(4)
294 BOUND_PTR_INDIR(8)
295 BOUND_PTR_INDIR(12)
296 BOUND_PTR_INDIR(16)
298 static BoundEntry *__bound_new_page(void)
300 BoundEntry *page;
301 int i;
303 page = libc_malloc(sizeof(BoundEntry) * BOUND_T2_SIZE);
304 if (!page)
305 bound_alloc_error();
306 for(i=0;i<BOUND_T2_SIZE;i++) {
307 /* put empty entries */
308 page[i].start = 0;
309 page[i].size = EMPTY_SIZE;
310 page[i].next = NULL;
311 page[i].is_invalid = 0;
313 return page;
316 /* currently we use malloc(). Should use bound_new_page() */
317 static BoundEntry *bound_new_entry(void)
319 BoundEntry *e;
320 e = libc_malloc(sizeof(BoundEntry));
321 return e;
324 static void bound_free_entry(BoundEntry *e)
326 libc_free(e);
329 static inline BoundEntry *get_page(int index)
331 BoundEntry *page;
332 page = __bound_t1[index];
333 if (page == __bound_empty_t2 || page == __bound_invalid_t2) {
334 /* create a new page if necessary */
335 page = __bound_new_page();
336 __bound_t1[index] = page;
338 return page;
341 /* mark a region as being invalid (can only be used during init) */
342 static void mark_invalid(unsigned long addr, unsigned long size)
344 unsigned long start, end;
345 BoundEntry *page;
346 int t1_start, t1_end, i, j, t2_start, t2_end;
348 start = addr;
349 end = addr + size;
351 t2_start = (start + BOUND_T3_SIZE - 1) >> BOUND_T3_BITS;
352 if (end != 0)
353 t2_end = end >> BOUND_T3_BITS;
354 else
355 t2_end = 1 << (BOUND_T1_BITS + BOUND_T2_BITS);
357 #if 0
358 printf("mark_invalid: start = %x %x\n", t2_start, t2_end);
359 #endif
361 /* first we handle full pages */
362 t1_start = (t2_start + BOUND_T2_SIZE - 1) >> BOUND_T2_BITS;
363 t1_end = t2_end >> BOUND_T2_BITS;
365 i = t2_start & (BOUND_T2_SIZE - 1);
366 j = t2_end & (BOUND_T2_SIZE - 1);
368 if (t1_start == t1_end) {
369 page = get_page(t2_start >> BOUND_T2_BITS);
370 for(; i < j; i++) {
371 page[i].size = INVALID_SIZE;
372 page[i].is_invalid = 1;
374 } else {
375 if (i > 0) {
376 page = get_page(t2_start >> BOUND_T2_BITS);
377 for(; i < BOUND_T2_SIZE; i++) {
378 page[i].size = INVALID_SIZE;
379 page[i].is_invalid = 1;
382 for(i = t1_start; i < t1_end; i++) {
383 __bound_t1[i] = __bound_invalid_t2;
385 if (j != 0) {
386 page = get_page(t1_end);
387 for(i = 0; i < j; i++) {
388 page[i].size = INVALID_SIZE;
389 page[i].is_invalid = 1;
395 void __bound_init(void)
397 int i;
398 BoundEntry *page;
399 unsigned long start, size;
400 int *p;
402 /* save malloc hooks and install bound check hooks */
403 install_malloc_hooks();
405 #ifndef BOUND_STATIC
406 __bound_t1 = libc_malloc(BOUND_T1_SIZE * sizeof(BoundEntry *));
407 if (!__bound_t1)
408 bound_alloc_error();
409 #endif
410 __bound_empty_t2 = __bound_new_page();
411 for(i=0;i<BOUND_T1_SIZE;i++) {
412 __bound_t1[i] = __bound_empty_t2;
415 page = __bound_new_page();
416 for(i=0;i<BOUND_T2_SIZE;i++) {
417 /* put invalid entries */
418 page[i].start = 0;
419 page[i].size = INVALID_SIZE;
420 page[i].next = NULL;
421 page[i].is_invalid = 1;
423 __bound_invalid_t2 = page;
425 /* invalid pointer zone */
426 start = (unsigned long)INVALID_POINTER & ~(BOUND_T23_SIZE - 1);
427 size = BOUND_T23_SIZE;
428 mark_invalid(start, size);
430 #if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
431 /* malloc zone is also marked invalid. can only use that with
432 hooks because all libs should use the same malloc. The solution
433 would be to build a new malloc for tcc. */
434 start = (unsigned long)&_end;
435 size = 128 * 0x100000;
436 mark_invalid(start, size);
437 #endif
439 /* add all static bound check values */
440 p = (int *)&__bounds_start;
441 while (p[0] != 0) {
442 __bound_new_region((void *)p[0], p[1]);
443 p += 2;
447 static inline void add_region(BoundEntry *e,
448 unsigned long start, unsigned long size)
450 BoundEntry *e1;
451 if (e->start == 0) {
452 /* no region : add it */
453 e->start = start;
454 e->size = size;
455 } else {
456 /* already regions in the list: add it at the head */
457 e1 = bound_new_entry();
458 e1->start = e->start;
459 e1->size = e->size;
460 e1->next = e->next;
461 e->start = start;
462 e->size = size;
463 e->next = e1;
467 /* create a new region. It should not already exist in the region list */
468 void __bound_new_region(void *p, unsigned long size)
470 unsigned long start, end;
471 BoundEntry *page, *e, *e2;
472 int t1_start, t1_end, i, t2_start, t2_end;
474 start = (unsigned long)p;
475 end = start + size;
476 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
477 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
479 /* start */
480 page = get_page(t1_start);
481 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
482 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
483 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
484 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
485 #ifdef BOUND_DEBUG
486 printf("new %lx %lx %x %x %x %x\n",
487 start, end, t1_start, t1_end, t2_start, t2_end);
488 #endif
490 e = (BoundEntry *)((char *)page + t2_start);
491 add_region(e, start, size);
493 if (t1_end == t1_start) {
494 /* same ending page */
495 e2 = (BoundEntry *)((char *)page + t2_end);
496 if (e2 > e) {
497 e++;
498 for(;e<e2;e++) {
499 e->start = start;
500 e->size = size;
502 add_region(e, start, size);
504 } else {
505 /* mark until end of page */
506 e2 = page + BOUND_T2_SIZE;
507 e++;
508 for(;e<e2;e++) {
509 e->start = start;
510 e->size = size;
512 /* mark intermediate pages, if any */
513 for(i=t1_start+1;i<t1_end;i++) {
514 page = get_page(i);
515 e2 = page + BOUND_T2_SIZE;
516 for(e=page;e<e2;e++) {
517 e->start = start;
518 e->size = size;
521 /* last page */
522 page = get_page(t1_end);
523 e2 = (BoundEntry *)((char *)page + t2_end);
524 for(e=page;e<e2;e++) {
525 e->start = start;
526 e->size = size;
528 add_region(e, start, size);
532 /* delete a region */
533 static inline void delete_region(BoundEntry *e,
534 void *p, unsigned long empty_size)
536 unsigned long addr;
537 BoundEntry *e1;
539 addr = (unsigned long)p;
540 addr -= e->start;
541 if (addr <= e->size) {
542 /* region found is first one */
543 e1 = e->next;
544 if (e1 == NULL) {
545 /* no more region: mark it empty */
546 e->start = 0;
547 e->size = empty_size;
548 } else {
549 /* copy next region in head */
550 e->start = e1->start;
551 e->size = e1->size;
552 e->next = e1->next;
553 bound_free_entry(e1);
555 } else {
556 /* find the matching region */
557 for(;;) {
558 e1 = e;
559 e = e->next;
560 /* region not found: do nothing */
561 if (e == NULL)
562 break;
563 addr = (unsigned long)p - e->start;
564 if (addr <= e->size) {
565 /* found: remove entry */
566 e1->next = e->next;
567 bound_free_entry(e);
568 break;
574 /* WARNING: 'p' must be the starting point of the region. */
575 /* return non zero if error */
576 int __bound_delete_region(void *p)
578 unsigned long start, end, addr, size, empty_size;
579 BoundEntry *page, *e, *e2;
580 int t1_start, t1_end, t2_start, t2_end, i;
582 start = (unsigned long)p;
583 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
584 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
585 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
587 /* find region size */
588 page = __bound_t1[t1_start];
589 e = (BoundEntry *)((char *)page + t2_start);
590 addr = start - e->start;
591 if (addr > e->size)
592 e = __bound_find_region(e, p);
593 /* test if invalid region */
594 if (e->size == EMPTY_SIZE || (unsigned long)p != e->start)
595 return -1;
596 /* compute the size we put in invalid regions */
597 if (e->is_invalid)
598 empty_size = INVALID_SIZE;
599 else
600 empty_size = EMPTY_SIZE;
601 size = e->size;
602 end = start + size;
604 /* now we can free each entry */
605 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
606 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
607 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
609 delete_region(e, p, empty_size);
610 if (t1_end == t1_start) {
611 /* same ending page */
612 e2 = (BoundEntry *)((char *)page + t2_end);
613 if (e2 > e) {
614 e++;
615 for(;e<e2;e++) {
616 e->start = 0;
617 e->size = empty_size;
619 delete_region(e, p, empty_size);
621 } else {
622 /* mark until end of page */
623 e2 = page + BOUND_T2_SIZE;
624 e++;
625 for(;e<e2;e++) {
626 e->start = 0;
627 e->size = empty_size;
629 /* mark intermediate pages, if any */
630 /* XXX: should free them */
631 for(i=t1_start+1;i<t1_end;i++) {
632 page = get_page(i);
633 e2 = page + BOUND_T2_SIZE;
634 for(e=page;e<e2;e++) {
635 e->start = 0;
636 e->size = empty_size;
639 /* last page */
640 page = get_page(t2_end);
641 e2 = (BoundEntry *)((char *)page + t2_end);
642 for(e=page;e<e2;e++) {
643 e->start = 0;
644 e->size = empty_size;
646 delete_region(e, p, empty_size);
648 return 0;
651 /* return the size of the region starting at p, or EMPTY_SIZE if non
652 existant region. */
653 static unsigned long get_region_size(void *p)
655 unsigned long addr = (unsigned long)p;
656 BoundEntry *e;
658 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
659 e = (BoundEntry *)((char *)e +
660 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
661 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
662 addr -= e->start;
663 if (addr > e->size)
664 e = __bound_find_region(e, p);
665 if (e->start != (unsigned long)p)
666 return EMPTY_SIZE;
667 return e->size;
670 /* patched memory functions */
672 static void install_malloc_hooks(void)
674 #ifdef CONFIG_TCC_MALLOC_HOOKS
675 saved_malloc_hook = __malloc_hook;
676 saved_free_hook = __free_hook;
677 saved_realloc_hook = __realloc_hook;
678 saved_memalign_hook = __memalign_hook;
679 __malloc_hook = __bound_malloc;
680 __free_hook = __bound_free;
681 __realloc_hook = __bound_realloc;
682 __memalign_hook = __bound_memalign;
683 #endif
686 static void restore_malloc_hooks(void)
688 #ifdef CONFIG_TCC_MALLOC_HOOKS
689 __malloc_hook = saved_malloc_hook;
690 __free_hook = saved_free_hook;
691 __realloc_hook = saved_realloc_hook;
692 __memalign_hook = saved_memalign_hook;
693 #endif
696 static void *libc_malloc(size_t size)
698 void *ptr;
699 restore_malloc_hooks();
700 ptr = malloc(size);
701 install_malloc_hooks();
702 return ptr;
705 static void libc_free(void *ptr)
707 restore_malloc_hooks();
708 free(ptr);
709 install_malloc_hooks();
712 /* XXX: we should use a malloc which ensure that it is unlikely that
713 two malloc'ed data have the same address if 'free' are made in
714 between. */
715 void *__bound_malloc(size_t size, const void *caller)
717 void *ptr;
719 /* we allocate one more byte to ensure the regions will be
720 separated by at least one byte. With the glibc malloc, it may
721 be in fact not necessary */
722 ptr = libc_malloc(size + 1);
724 if (!ptr)
725 return NULL;
726 __bound_new_region(ptr, size);
727 return ptr;
730 void *__bound_memalign(size_t size, size_t align, const void *caller)
732 void *ptr;
734 restore_malloc_hooks();
736 /* we allocate one more byte to ensure the regions will be
737 separated by at least one byte. With the glibc malloc, it may
738 be in fact not necessary */
739 ptr = memalign(size + 1, align);
741 install_malloc_hooks();
743 if (!ptr)
744 return NULL;
745 __bound_new_region(ptr, size);
746 return ptr;
749 void __bound_free(void *ptr, const void *caller)
751 if (ptr == NULL)
752 return;
753 if (__bound_delete_region(ptr) != 0)
754 bound_error(caller, "freeing invalid region");
756 libc_free(ptr);
759 void *__bound_realloc(void *ptr, size_t size, const void *caller)
761 void *ptr1;
762 int old_size;
764 if (size == 0) {
765 __bound_free(ptr, caller);
766 return NULL;
767 } else {
768 ptr1 = __bound_malloc(size, caller);
769 if (ptr == NULL || ptr1 == NULL)
770 return ptr1;
771 old_size = get_region_size(ptr);
772 if (old_size == EMPTY_SIZE)
773 bound_error(caller, "realloc'ing invalid pointer");
774 memcpy(ptr1, ptr, old_size);
775 __bound_free(ptr, caller);
776 return ptr1;
780 #ifndef CONFIG_TCC_MALLOC_HOOKS
781 void *__bound_calloc(size_t nmemb, size_t size)
783 void *ptr;
784 size = size * nmemb;
785 ptr = __bound_malloc(size);
786 if (!ptr)
787 return NULL;
788 memset(ptr, 0, size);
789 return ptr;
791 #endif
793 #if 0
794 static void bound_dump(void)
796 BoundEntry *page, *e;
797 int i, j;
799 printf("region dump:\n");
800 for(i=0;i<BOUND_T1_SIZE;i++) {
801 page = __bound_t1[i];
802 for(j=0;j<BOUND_T2_SIZE;j++) {
803 e = page + j;
804 /* do not print invalid or empty entries */
805 if (e->size != EMPTY_SIZE && e->start != 0) {
806 printf("%08x:",
807 (i << (BOUND_T2_BITS + BOUND_T3_BITS)) +
808 (j << BOUND_T3_BITS));
809 do {
810 printf(" %08lx:%08lx", e->start, e->start + e->size);
811 e = e->next;
812 } while (e != NULL);
813 printf("\n");
818 #endif
820 /* some useful checked functions */
822 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
823 static void __bound_check(const void *p, size_t size)
825 if (size == 0)
826 return;
827 p = __bound_ptr_add((void *)p, size);
828 if (p == INVALID_POINTER)
829 bound_error(get_caller_pc(2), "invalid pointer");
832 void *__bound_memcpy(void *dst, const void *src, size_t size)
834 __bound_check(dst, size);
835 __bound_check(src, size);
836 /* check also region overlap */
837 if (src >= dst && src < dst + size)
838 bound_error(get_caller_pc(1), "overlapping regions in memcpy()");
839 return memcpy(dst, src, size);
842 void *__bound_memmove(void *dst, const void *src, size_t size)
844 __bound_check(dst, size);
845 __bound_check(src, size);
846 return memmove(dst, src, size);
849 void *__bound_memset(void *dst, int c, size_t size)
851 __bound_check(dst, size);
852 return memset(dst, c, size);
855 /* XXX: could be optimized */
856 int __bound_strlen(const char *s)
858 const char *p;
859 int len;
861 len = 0;
862 for(;;) {
863 p = __bound_ptr_indir1((char *)s, len);
864 if (p == INVALID_POINTER)
865 bound_error(get_caller_pc(1), "bad pointer in strlen()");
866 if (*p == '\0')
867 break;
868 len++;
870 return len;
873 char *__bound_strcpy(char *dst, const char *src)
875 int len;
876 len = __bound_strlen(src);
877 return __bound_memcpy(dst, src, len + 1);