48d76061cd28f40543a0178fd6fc780dfcc26a9f
[tinycc.git] / lib / bcheck.c
blob48d76061cd28f40543a0178fd6fc780dfcc26a9f
1 /*
2 * Tiny C Memory and bounds checker
3 *
4 * Copyright (c) 2002 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) \
25 && !defined(__DragonFly__) && !defined(__OpenBSD__)
26 #include <malloc.h>
27 #endif
29 //#define BOUND_DEBUG
31 /* define so that bound array is static (faster, but use memory if
32 bound checking not used) */
33 //#define BOUND_STATIC
35 /* use malloc hooks. Currently the code cannot be reliable if no hooks */
36 #define CONFIG_TCC_MALLOC_HOOKS
37 #define HAVE_MEMALIGN
39 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
40 || defined(__DragonFly__) || defined(__dietlibc__) \
41 || defined(__UCLIBC__) || defined(__OpenBSD__) || defined(_WIN32)
42 #warning Bound checking does not support malloc (etc.) in this environment.
43 #undef CONFIG_TCC_MALLOC_HOOKS
44 #undef HAVE_MEMALIGN
45 #endif
47 #define BOUND_T1_BITS 13
48 #define BOUND_T2_BITS 11
49 #define BOUND_T3_BITS (32 - BOUND_T1_BITS - BOUND_T2_BITS)
51 #define BOUND_T1_SIZE (1 << BOUND_T1_BITS)
52 #define BOUND_T2_SIZE (1 << BOUND_T2_BITS)
53 #define BOUND_T3_SIZE (1 << BOUND_T3_BITS)
54 #define BOUND_E_BITS 4
56 #define BOUND_T23_BITS (BOUND_T2_BITS + BOUND_T3_BITS)
57 #define BOUND_T23_SIZE (1 << BOUND_T23_BITS)
60 /* this pointer is generated when bound check is incorrect */
61 #define INVALID_POINTER ((void *)(-2))
62 /* size of an empty region */
63 #define EMPTY_SIZE 0xffffffff
64 /* size of an invalid region */
65 #define INVALID_SIZE 0
67 typedef struct BoundEntry {
68 unsigned long start;
69 unsigned long size;
70 struct BoundEntry *next;
71 unsigned long is_invalid; /* true if pointers outside region are invalid */
72 } BoundEntry;
74 /* external interface */
75 void __bound_init(void);
76 void __bound_new_region(void *p, unsigned long size);
77 int __bound_delete_region(void *p);
79 #define FASTCALL __attribute__((regparm(3)))
81 void *__bound_malloc(size_t size, const void *caller);
82 void *__bound_memalign(size_t size, size_t align, const void *caller);
83 void __bound_free(void *ptr, const void *caller);
84 void *__bound_realloc(void *ptr, size_t size, const void *caller);
85 static void *libc_malloc(size_t size);
86 static void libc_free(void *ptr);
87 static void install_malloc_hooks(void);
88 static void restore_malloc_hooks(void);
90 #ifdef CONFIG_TCC_MALLOC_HOOKS
91 static void *saved_malloc_hook;
92 static void *saved_free_hook;
93 static void *saved_realloc_hook;
94 static void *saved_memalign_hook;
95 #endif
97 /* linker definitions */
98 extern char _end;
100 /* TCC definitions */
101 extern char __bounds_start; /* start of static bounds table */
102 /* error message, just for TCC */
103 const char *__bound_error_msg;
105 /* runtime error output */
106 extern void rt_error(unsigned long pc, const char *fmt, ...);
108 #ifdef BOUND_STATIC
109 static BoundEntry *__bound_t1[BOUND_T1_SIZE]; /* page table */
110 #else
111 static BoundEntry **__bound_t1; /* page table */
112 #endif
113 static BoundEntry *__bound_empty_t2; /* empty page, for unused pages */
114 static BoundEntry *__bound_invalid_t2; /* invalid page, for invalid pointers */
116 static BoundEntry *__bound_find_region(BoundEntry *e1, void *p)
118 unsigned long addr, tmp;
119 BoundEntry *e;
121 e = e1;
122 while (e != NULL) {
123 addr = (unsigned long)p;
124 addr -= e->start;
125 if (addr <= e->size) {
126 /* put region at the head */
127 tmp = e1->start;
128 e1->start = e->start;
129 e->start = tmp;
130 tmp = e1->size;
131 e1->size = e->size;
132 e->size = tmp;
133 return e1;
135 e = e->next;
137 /* no entry found: return empty entry or invalid entry */
138 if (e1->is_invalid)
139 return __bound_invalid_t2;
140 else
141 return __bound_empty_t2;
144 /* print a bound error message */
145 static void bound_error(const char *fmt, ...)
147 __bound_error_msg = fmt;
148 *(int *)0 = 0; /* force a runtime error */
151 static void bound_alloc_error(void)
153 bound_error("not enough memory for bound checking code");
156 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
157 the end of a region in this case */
158 void * FASTCALL __bound_ptr_add(void *p, int offset)
160 unsigned long addr = (unsigned long)p;
161 BoundEntry *e;
162 #if defined(BOUND_DEBUG)
163 printf("add: 0x%x %d\n", (int)p, offset);
164 #endif
166 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
167 e = (BoundEntry *)((char *)e +
168 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
169 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
170 addr -= e->start;
171 if (addr > e->size) {
172 e = __bound_find_region(e, p);
173 addr = (unsigned long)p - e->start;
175 addr += offset;
176 if (addr > e->size)
177 return INVALID_POINTER; /* return an invalid pointer */
178 return p + offset;
181 /* return '(p + offset)' for pointer indirection (the resulting must
182 be strictly inside the region */
183 #define BOUND_PTR_INDIR(dsize) \
184 void * FASTCALL __bound_ptr_indir ## dsize (void *p, int offset) \
186 unsigned long addr = (unsigned long)p; \
187 BoundEntry *e; \
189 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
190 e = (BoundEntry *)((char *)e + \
191 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
192 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
193 addr -= e->start; \
194 if (addr > e->size) { \
195 e = __bound_find_region(e, p); \
196 addr = (unsigned long)p - e->start; \
198 addr += offset + dsize; \
199 if (addr > e->size) \
200 return INVALID_POINTER; /* return an invalid pointer */ \
201 return p + offset; \
204 BOUND_PTR_INDIR(1)
205 BOUND_PTR_INDIR(2)
206 BOUND_PTR_INDIR(4)
207 BOUND_PTR_INDIR(8)
208 BOUND_PTR_INDIR(12)
209 BOUND_PTR_INDIR(16)
211 #ifdef __i386__
212 /* return the frame pointer of the caller */
213 #define GET_CALLER_FP(fp)\
215 unsigned long *fp1;\
216 __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp1));\
217 fp = fp1[0];\
219 #elif defined(__x86_64__)
220 /* TCC always creates %rbp frames also on x86_64, so use them. */
221 #define GET_CALLER_FP(fp)\
223 unsigned long *fp1;\
224 __asm__ __volatile__ ("movq %%rbp,%0" :"=g" (fp1));\
225 fp = fp1[0];\
227 #else
228 #error put code to extract the calling frame pointer
229 #endif
231 /* called when entering a function to add all the local regions */
232 void FASTCALL __bound_local_new(void *p1)
234 unsigned long addr, size, fp, *p = p1;
235 GET_CALLER_FP(fp);
236 for(;;) {
237 addr = p[0];
238 if (addr == 0)
239 break;
240 addr += fp;
241 size = p[1];
242 p += 2;
243 __bound_new_region((void *)addr, size);
247 /* called when leaving a function to delete all the local regions */
248 void FASTCALL __bound_local_delete(void *p1)
250 unsigned long addr, fp, *p = p1;
251 GET_CALLER_FP(fp);
252 for(;;) {
253 addr = p[0];
254 if (addr == 0)
255 break;
256 addr += fp;
257 p += 2;
258 __bound_delete_region((void *)addr);
262 static BoundEntry *__bound_new_page(void)
264 BoundEntry *page;
265 int i;
267 page = libc_malloc(sizeof(BoundEntry) * BOUND_T2_SIZE);
268 if (!page)
269 bound_alloc_error();
270 for(i=0;i<BOUND_T2_SIZE;i++) {
271 /* put empty entries */
272 page[i].start = 0;
273 page[i].size = EMPTY_SIZE;
274 page[i].next = NULL;
275 page[i].is_invalid = 0;
277 return page;
280 /* currently we use malloc(). Should use bound_new_page() */
281 static BoundEntry *bound_new_entry(void)
283 BoundEntry *e;
284 e = libc_malloc(sizeof(BoundEntry));
285 return e;
288 static void bound_free_entry(BoundEntry *e)
290 libc_free(e);
293 static inline BoundEntry *get_page(int index)
295 BoundEntry *page;
296 page = __bound_t1[index];
297 if (page == __bound_empty_t2 || page == __bound_invalid_t2) {
298 /* create a new page if necessary */
299 page = __bound_new_page();
300 __bound_t1[index] = page;
302 return page;
305 /* mark a region as being invalid (can only be used during init) */
306 static void mark_invalid(unsigned long addr, unsigned long size)
308 unsigned long start, end;
309 BoundEntry *page;
310 int t1_start, t1_end, i, j, t2_start, t2_end;
312 start = addr;
313 end = addr + size;
315 t2_start = (start + BOUND_T3_SIZE - 1) >> BOUND_T3_BITS;
316 if (end != 0)
317 t2_end = end >> BOUND_T3_BITS;
318 else
319 t2_end = 1 << (BOUND_T1_BITS + BOUND_T2_BITS);
321 #if 0
322 printf("mark_invalid: start = %x %x\n", t2_start, t2_end);
323 #endif
325 /* first we handle full pages */
326 t1_start = (t2_start + BOUND_T2_SIZE - 1) >> BOUND_T2_BITS;
327 t1_end = t2_end >> BOUND_T2_BITS;
329 i = t2_start & (BOUND_T2_SIZE - 1);
330 j = t2_end & (BOUND_T2_SIZE - 1);
332 if (t1_start == t1_end) {
333 page = get_page(t2_start >> BOUND_T2_BITS);
334 for(; i < j; i++) {
335 page[i].size = INVALID_SIZE;
336 page[i].is_invalid = 1;
338 } else {
339 if (i > 0) {
340 page = get_page(t2_start >> BOUND_T2_BITS);
341 for(; i < BOUND_T2_SIZE; i++) {
342 page[i].size = INVALID_SIZE;
343 page[i].is_invalid = 1;
346 for(i = t1_start; i < t1_end; i++) {
347 __bound_t1[i] = __bound_invalid_t2;
349 if (j != 0) {
350 page = get_page(t1_end);
351 for(i = 0; i < j; i++) {
352 page[i].size = INVALID_SIZE;
353 page[i].is_invalid = 1;
359 void __bound_init(void)
361 int i;
362 BoundEntry *page;
363 unsigned long start, size;
364 int *p;
366 /* save malloc hooks and install bound check hooks */
367 install_malloc_hooks();
369 #ifndef BOUND_STATIC
370 __bound_t1 = libc_malloc(BOUND_T1_SIZE * sizeof(BoundEntry *));
371 if (!__bound_t1)
372 bound_alloc_error();
373 #endif
374 __bound_empty_t2 = __bound_new_page();
375 for(i=0;i<BOUND_T1_SIZE;i++) {
376 __bound_t1[i] = __bound_empty_t2;
379 page = __bound_new_page();
380 for(i=0;i<BOUND_T2_SIZE;i++) {
381 /* put invalid entries */
382 page[i].start = 0;
383 page[i].size = INVALID_SIZE;
384 page[i].next = NULL;
385 page[i].is_invalid = 1;
387 __bound_invalid_t2 = page;
389 /* invalid pointer zone */
390 start = (unsigned long)INVALID_POINTER & ~(BOUND_T23_SIZE - 1);
391 size = BOUND_T23_SIZE;
392 mark_invalid(start, size);
394 #if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
395 /* malloc zone is also marked invalid. can only use that with
396 hooks because all libs should use the same malloc. The solution
397 would be to build a new malloc for tcc. */
398 start = (unsigned long)&_end;
399 size = 128 * 0x100000;
400 mark_invalid(start, size);
401 #endif
403 /* add all static bound check values */
404 p = (int *)&__bounds_start;
405 while (p[0] != 0) {
406 __bound_new_region((void *)p[0], p[1]);
407 p += 2;
411 void __bound_exit(void)
413 restore_malloc_hooks();
416 static inline void add_region(BoundEntry *e,
417 unsigned long start, unsigned long size)
419 BoundEntry *e1;
420 if (e->start == 0) {
421 /* no region : add it */
422 e->start = start;
423 e->size = size;
424 } else {
425 /* already regions in the list: add it at the head */
426 e1 = bound_new_entry();
427 e1->start = e->start;
428 e1->size = e->size;
429 e1->next = e->next;
430 e->start = start;
431 e->size = size;
432 e->next = e1;
436 /* create a new region. It should not already exist in the region list */
437 void __bound_new_region(void *p, unsigned long size)
439 unsigned long start, end;
440 BoundEntry *page, *e, *e2;
441 int t1_start, t1_end, i, t2_start, t2_end;
443 start = (unsigned long)p;
444 end = start + size;
445 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
446 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
448 /* start */
449 page = get_page(t1_start);
450 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
451 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
452 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
453 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
454 #ifdef BOUND_DEBUG
455 printf("new %lx %lx %x %x %x %x\n",
456 start, end, t1_start, t1_end, t2_start, t2_end);
457 #endif
459 e = (BoundEntry *)((char *)page + t2_start);
460 add_region(e, start, size);
462 if (t1_end == t1_start) {
463 /* same ending page */
464 e2 = (BoundEntry *)((char *)page + t2_end);
465 if (e2 > e) {
466 e++;
467 for(;e<e2;e++) {
468 e->start = start;
469 e->size = size;
471 add_region(e, start, size);
473 } else {
474 /* mark until end of page */
475 e2 = page + BOUND_T2_SIZE;
476 e++;
477 for(;e<e2;e++) {
478 e->start = start;
479 e->size = size;
481 /* mark intermediate pages, if any */
482 for(i=t1_start+1;i<t1_end;i++) {
483 page = get_page(i);
484 e2 = page + BOUND_T2_SIZE;
485 for(e=page;e<e2;e++) {
486 e->start = start;
487 e->size = size;
490 /* last page */
491 page = get_page(t1_end);
492 e2 = (BoundEntry *)((char *)page + t2_end);
493 for(e=page;e<e2;e++) {
494 e->start = start;
495 e->size = size;
497 add_region(e, start, size);
501 /* delete a region */
502 static inline void delete_region(BoundEntry *e,
503 void *p, unsigned long empty_size)
505 unsigned long addr;
506 BoundEntry *e1;
508 addr = (unsigned long)p;
509 addr -= e->start;
510 if (addr <= e->size) {
511 /* region found is first one */
512 e1 = e->next;
513 if (e1 == NULL) {
514 /* no more region: mark it empty */
515 e->start = 0;
516 e->size = empty_size;
517 } else {
518 /* copy next region in head */
519 e->start = e1->start;
520 e->size = e1->size;
521 e->next = e1->next;
522 bound_free_entry(e1);
524 } else {
525 /* find the matching region */
526 for(;;) {
527 e1 = e;
528 e = e->next;
529 /* region not found: do nothing */
530 if (e == NULL)
531 break;
532 addr = (unsigned long)p - e->start;
533 if (addr <= e->size) {
534 /* found: remove entry */
535 e1->next = e->next;
536 bound_free_entry(e);
537 break;
543 /* WARNING: 'p' must be the starting point of the region. */
544 /* return non zero if error */
545 int __bound_delete_region(void *p)
547 unsigned long start, end, addr, size, empty_size;
548 BoundEntry *page, *e, *e2;
549 int t1_start, t1_end, t2_start, t2_end, i;
551 start = (unsigned long)p;
552 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
553 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
554 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
556 /* find region size */
557 page = __bound_t1[t1_start];
558 e = (BoundEntry *)((char *)page + t2_start);
559 addr = start - e->start;
560 if (addr > e->size)
561 e = __bound_find_region(e, p);
562 /* test if invalid region */
563 if (e->size == EMPTY_SIZE || (unsigned long)p != e->start)
564 return -1;
565 /* compute the size we put in invalid regions */
566 if (e->is_invalid)
567 empty_size = INVALID_SIZE;
568 else
569 empty_size = EMPTY_SIZE;
570 size = e->size;
571 end = start + size;
573 /* now we can free each entry */
574 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
575 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
576 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
578 delete_region(e, p, empty_size);
579 if (t1_end == t1_start) {
580 /* same ending page */
581 e2 = (BoundEntry *)((char *)page + t2_end);
582 if (e2 > e) {
583 e++;
584 for(;e<e2;e++) {
585 e->start = 0;
586 e->size = empty_size;
588 delete_region(e, p, empty_size);
590 } else {
591 /* mark until end of page */
592 e2 = page + BOUND_T2_SIZE;
593 e++;
594 for(;e<e2;e++) {
595 e->start = 0;
596 e->size = empty_size;
598 /* mark intermediate pages, if any */
599 /* XXX: should free them */
600 for(i=t1_start+1;i<t1_end;i++) {
601 page = get_page(i);
602 e2 = page + BOUND_T2_SIZE;
603 for(e=page;e<e2;e++) {
604 e->start = 0;
605 e->size = empty_size;
608 /* last page */
609 page = get_page(t2_end);
610 e2 = (BoundEntry *)((char *)page + t2_end);
611 for(e=page;e<e2;e++) {
612 e->start = 0;
613 e->size = empty_size;
615 delete_region(e, p, empty_size);
617 return 0;
620 /* return the size of the region starting at p, or EMPTY_SIZE if non
621 existant region. */
622 static unsigned long get_region_size(void *p)
624 unsigned long addr = (unsigned long)p;
625 BoundEntry *e;
627 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
628 e = (BoundEntry *)((char *)e +
629 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
630 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
631 addr -= e->start;
632 if (addr > e->size)
633 e = __bound_find_region(e, p);
634 if (e->start != (unsigned long)p)
635 return EMPTY_SIZE;
636 return e->size;
639 /* patched memory functions */
641 static void install_malloc_hooks(void)
643 #ifdef CONFIG_TCC_MALLOC_HOOKS
644 saved_malloc_hook = __malloc_hook;
645 saved_free_hook = __free_hook;
646 saved_realloc_hook = __realloc_hook;
647 saved_memalign_hook = __memalign_hook;
648 __malloc_hook = __bound_malloc;
649 __free_hook = __bound_free;
650 __realloc_hook = __bound_realloc;
651 __memalign_hook = __bound_memalign;
652 #endif
655 static void restore_malloc_hooks(void)
657 #ifdef CONFIG_TCC_MALLOC_HOOKS
658 __malloc_hook = saved_malloc_hook;
659 __free_hook = saved_free_hook;
660 __realloc_hook = saved_realloc_hook;
661 __memalign_hook = saved_memalign_hook;
662 #endif
665 static void *libc_malloc(size_t size)
667 void *ptr;
668 restore_malloc_hooks();
669 ptr = malloc(size);
670 install_malloc_hooks();
671 return ptr;
674 static void libc_free(void *ptr)
676 restore_malloc_hooks();
677 free(ptr);
678 install_malloc_hooks();
681 /* XXX: we should use a malloc which ensure that it is unlikely that
682 two malloc'ed data have the same address if 'free' are made in
683 between. */
684 void *__bound_malloc(size_t size, const void *caller)
686 void *ptr;
688 /* we allocate one more byte to ensure the regions will be
689 separated by at least one byte. With the glibc malloc, it may
690 be in fact not necessary */
691 ptr = libc_malloc(size + 1);
693 if (!ptr)
694 return NULL;
695 __bound_new_region(ptr, size);
696 return ptr;
699 void *__bound_memalign(size_t size, size_t align, const void *caller)
701 void *ptr;
703 restore_malloc_hooks();
705 #ifndef HAVE_MEMALIGN
706 if (align > 4) {
707 /* XXX: handle it ? */
708 ptr = NULL;
709 } else {
710 /* we suppose that malloc aligns to at least four bytes */
711 ptr = malloc(size + 1);
713 #else
714 /* we allocate one more byte to ensure the regions will be
715 separated by at least one byte. With the glibc malloc, it may
716 be in fact not necessary */
717 ptr = memalign(size + 1, align);
718 #endif
720 install_malloc_hooks();
722 if (!ptr)
723 return NULL;
724 __bound_new_region(ptr, size);
725 return ptr;
728 void __bound_free(void *ptr, const void *caller)
730 if (ptr == NULL)
731 return;
732 if (__bound_delete_region(ptr) != 0)
733 bound_error("freeing invalid region");
735 libc_free(ptr);
738 void *__bound_realloc(void *ptr, size_t size, const void *caller)
740 void *ptr1;
741 int old_size;
743 if (size == 0) {
744 __bound_free(ptr, caller);
745 return NULL;
746 } else {
747 ptr1 = __bound_malloc(size, caller);
748 if (ptr == NULL || ptr1 == NULL)
749 return ptr1;
750 old_size = get_region_size(ptr);
751 if (old_size == EMPTY_SIZE)
752 bound_error("realloc'ing invalid pointer");
753 memcpy(ptr1, ptr, old_size);
754 __bound_free(ptr, caller);
755 return ptr1;
759 #ifndef CONFIG_TCC_MALLOC_HOOKS
760 void *__bound_calloc(size_t nmemb, size_t size)
762 void *ptr;
763 size = size * nmemb;
764 ptr = __bound_malloc(size, NULL);
765 if (!ptr)
766 return NULL;
767 memset(ptr, 0, size);
768 return ptr;
770 #endif
772 #if 0
773 static void bound_dump(void)
775 BoundEntry *page, *e;
776 int i, j;
778 printf("region dump:\n");
779 for(i=0;i<BOUND_T1_SIZE;i++) {
780 page = __bound_t1[i];
781 for(j=0;j<BOUND_T2_SIZE;j++) {
782 e = page + j;
783 /* do not print invalid or empty entries */
784 if (e->size != EMPTY_SIZE && e->start != 0) {
785 printf("%08x:",
786 (i << (BOUND_T2_BITS + BOUND_T3_BITS)) +
787 (j << BOUND_T3_BITS));
788 do {
789 printf(" %08lx:%08lx", e->start, e->start + e->size);
790 e = e->next;
791 } while (e != NULL);
792 printf("\n");
797 #endif
799 /* some useful checked functions */
801 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
802 static void __bound_check(const void *p, size_t size)
804 if (size == 0)
805 return;
806 p = __bound_ptr_add((void *)p, size);
807 if (p == INVALID_POINTER)
808 bound_error("invalid pointer");
811 void *__bound_memcpy(void *dst, const void *src, size_t size)
813 __bound_check(dst, size);
814 __bound_check(src, size);
815 /* check also region overlap */
816 if (src >= dst && src < dst + size)
817 bound_error("overlapping regions in memcpy()");
818 return memcpy(dst, src, size);
821 void *__bound_memmove(void *dst, const void *src, size_t size)
823 __bound_check(dst, size);
824 __bound_check(src, size);
825 return memmove(dst, src, size);
828 void *__bound_memset(void *dst, int c, size_t size)
830 __bound_check(dst, size);
831 return memset(dst, c, size);
834 /* XXX: could be optimized */
835 int __bound_strlen(const char *s)
837 const char *p;
838 int len;
840 len = 0;
841 for(;;) {
842 p = __bound_ptr_indir1((char *)s, len);
843 if (p == INVALID_POINTER)
844 bound_error("bad pointer in strlen()");
845 if (*p == '\0')
846 break;
847 len++;
849 return len;
852 char *__bound_strcpy(char *dst, const char *src)
854 int len;
855 len = __bound_strlen(src);
856 return __bound_memcpy(dst, src, len + 1);