Added missing file.
[tinycc/k1w1.git] / lib / bcheck.c
blob3b37e85ab2de40878fe0e68f3d9680c5375ffd1d
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(__DragonFly__) && !defined(__OpenBSD__)
25 #include <malloc.h>
26 #endif
28 //#define BOUND_DEBUG
30 /* define so that bound array is static (faster, but use memory if
31 bound checking not used) */
32 //#define BOUND_STATIC
34 /* use malloc hooks. Currently the code cannot be reliable if no hooks */
35 #define CONFIG_TCC_MALLOC_HOOKS
37 #define HAVE_MEMALIGN
39 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__dietlibc__) \
40 || defined(__UCLIBC__) || defined(__OpenBSD__) || defined(__CYGWIN__) \
41 || defined(_WIN32)
42 #warning Bound checking not fully supported 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 /* currently, tcc cannot compile that because we use GNUC extensions */
157 #if !defined(__TINYC__)
159 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
160 the end of a region in this case */
161 void * FASTCALL __bound_ptr_add(void *p, int offset)
163 unsigned long addr = (unsigned long)p;
164 BoundEntry *e;
165 #if defined(BOUND_DEBUG)
166 printf("add: 0x%x %d\n", (int)p, offset);
167 #endif
169 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
170 e = (BoundEntry *)((char *)e +
171 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
172 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
173 addr -= e->start;
174 if (addr > e->size) {
175 e = __bound_find_region(e, p);
176 addr = (unsigned long)p - e->start;
178 addr += offset;
179 if (addr > e->size)
180 return INVALID_POINTER; /* return an invalid pointer */
181 return p + offset;
184 /* return '(p + offset)' for pointer indirection (the resulting must
185 be strictly inside the region */
186 #define BOUND_PTR_INDIR(dsize) \
187 void * FASTCALL __bound_ptr_indir ## dsize (void *p, int offset) \
189 unsigned long addr = (unsigned long)p; \
190 BoundEntry *e; \
192 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
193 e = (BoundEntry *)((char *)e + \
194 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
195 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
196 addr -= e->start; \
197 if (addr > e->size) { \
198 e = __bound_find_region(e, p); \
199 addr = (unsigned long)p - e->start; \
201 addr += offset + dsize; \
202 if (addr > e->size) \
203 return INVALID_POINTER; /* return an invalid pointer */ \
204 return p + offset; \
207 #ifdef __i386__
208 /* return the frame pointer of the caller */
209 #define GET_CALLER_FP(fp)\
211 unsigned long *fp1;\
212 __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp1));\
213 fp = fp1[0];\
215 #else
216 #error put code to extract the calling frame pointer
217 #endif
219 /* called when entering a function to add all the local regions */
220 void FASTCALL __bound_local_new(void *p1)
222 unsigned long addr, size, fp, *p = p1;
223 GET_CALLER_FP(fp);
224 for(;;) {
225 addr = p[0];
226 if (addr == 0)
227 break;
228 addr += fp;
229 size = p[1];
230 p += 2;
231 __bound_new_region((void *)addr, size);
235 /* called when leaving a function to delete all the local regions */
236 void FASTCALL __bound_local_delete(void *p1)
238 unsigned long addr, fp, *p = p1;
239 GET_CALLER_FP(fp);
240 for(;;) {
241 addr = p[0];
242 if (addr == 0)
243 break;
244 addr += fp;
245 p += 2;
246 __bound_delete_region((void *)addr);
250 #else
252 void __bound_local_new(void *p)
255 void __bound_local_delete(void *p)
259 void *__bound_ptr_add(void *p, int offset)
261 return p + offset;
264 #define BOUND_PTR_INDIR(dsize) \
265 void *__bound_ptr_indir ## dsize (void *p, int offset) \
267 return p + offset; \
269 #endif
271 BOUND_PTR_INDIR(1)
272 BOUND_PTR_INDIR(2)
273 BOUND_PTR_INDIR(4)
274 BOUND_PTR_INDIR(8)
275 BOUND_PTR_INDIR(12)
276 BOUND_PTR_INDIR(16)
278 static BoundEntry *__bound_new_page(void)
280 BoundEntry *page;
281 int i;
283 page = libc_malloc(sizeof(BoundEntry) * BOUND_T2_SIZE);
284 if (!page)
285 bound_alloc_error();
286 for(i=0;i<BOUND_T2_SIZE;i++) {
287 /* put empty entries */
288 page[i].start = 0;
289 page[i].size = EMPTY_SIZE;
290 page[i].next = NULL;
291 page[i].is_invalid = 0;
293 return page;
296 /* currently we use malloc(). Should use bound_new_page() */
297 static BoundEntry *bound_new_entry(void)
299 BoundEntry *e;
300 e = libc_malloc(sizeof(BoundEntry));
301 return e;
304 static void bound_free_entry(BoundEntry *e)
306 libc_free(e);
309 static inline BoundEntry *get_page(int index)
311 BoundEntry *page;
312 page = __bound_t1[index];
313 if (page == __bound_empty_t2 || page == __bound_invalid_t2) {
314 /* create a new page if necessary */
315 page = __bound_new_page();
316 __bound_t1[index] = page;
318 return page;
321 /* mark a region as being invalid (can only be used during init) */
322 static void mark_invalid(unsigned long addr, unsigned long size)
324 unsigned long start, end;
325 BoundEntry *page;
326 int t1_start, t1_end, i, j, t2_start, t2_end;
328 start = addr;
329 end = addr + size;
331 t2_start = (start + BOUND_T3_SIZE - 1) >> BOUND_T3_BITS;
332 if (end != 0)
333 t2_end = end >> BOUND_T3_BITS;
334 else
335 t2_end = 1 << (BOUND_T1_BITS + BOUND_T2_BITS);
337 #if 0
338 printf("mark_invalid: start = %x %x\n", t2_start, t2_end);
339 #endif
341 /* first we handle full pages */
342 t1_start = (t2_start + BOUND_T2_SIZE - 1) >> BOUND_T2_BITS;
343 t1_end = t2_end >> BOUND_T2_BITS;
345 i = t2_start & (BOUND_T2_SIZE - 1);
346 j = t2_end & (BOUND_T2_SIZE - 1);
348 if (t1_start == t1_end) {
349 page = get_page(t2_start >> BOUND_T2_BITS);
350 for(; i < j; i++) {
351 page[i].size = INVALID_SIZE;
352 page[i].is_invalid = 1;
354 } else {
355 if (i > 0) {
356 page = get_page(t2_start >> BOUND_T2_BITS);
357 for(; i < BOUND_T2_SIZE; i++) {
358 page[i].size = INVALID_SIZE;
359 page[i].is_invalid = 1;
362 for(i = t1_start; i < t1_end; i++) {
363 __bound_t1[i] = __bound_invalid_t2;
365 if (j != 0) {
366 page = get_page(t1_end);
367 for(i = 0; i < j; i++) {
368 page[i].size = INVALID_SIZE;
369 page[i].is_invalid = 1;
375 void __bound_init(void)
377 int i;
378 BoundEntry *page;
379 unsigned long start, size;
380 int *p;
382 /* save malloc hooks and install bound check hooks */
383 install_malloc_hooks();
385 #ifndef BOUND_STATIC
386 __bound_t1 = libc_malloc(BOUND_T1_SIZE * sizeof(BoundEntry *));
387 if (!__bound_t1)
388 bound_alloc_error();
389 #endif
390 __bound_empty_t2 = __bound_new_page();
391 for(i=0;i<BOUND_T1_SIZE;i++) {
392 __bound_t1[i] = __bound_empty_t2;
395 page = __bound_new_page();
396 for(i=0;i<BOUND_T2_SIZE;i++) {
397 /* put invalid entries */
398 page[i].start = 0;
399 page[i].size = INVALID_SIZE;
400 page[i].next = NULL;
401 page[i].is_invalid = 1;
403 __bound_invalid_t2 = page;
405 /* invalid pointer zone */
406 start = (unsigned long)INVALID_POINTER & ~(BOUND_T23_SIZE - 1);
407 size = BOUND_T23_SIZE;
408 mark_invalid(start, size);
410 #if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
411 /* malloc zone is also marked invalid. can only use that with
412 hooks because all libs should use the same malloc. The solution
413 would be to build a new malloc for tcc. */
414 start = (unsigned long)&_end;
415 size = 128 * 0x100000;
416 mark_invalid(start, size);
417 #endif
419 /* add all static bound check values */
420 p = (int *)&__bounds_start;
421 while (p[0] != 0) {
422 __bound_new_region((void *)p[0], p[1]);
423 p += 2;
427 void __bound_exit(void)
429 restore_malloc_hooks();
432 static inline void add_region(BoundEntry *e,
433 unsigned long start, unsigned long size)
435 BoundEntry *e1;
436 if (e->start == 0) {
437 /* no region : add it */
438 e->start = start;
439 e->size = size;
440 } else {
441 /* already regions in the list: add it at the head */
442 e1 = bound_new_entry();
443 e1->start = e->start;
444 e1->size = e->size;
445 e1->next = e->next;
446 e->start = start;
447 e->size = size;
448 e->next = e1;
452 /* create a new region. It should not already exist in the region list */
453 void __bound_new_region(void *p, unsigned long size)
455 unsigned long start, end;
456 BoundEntry *page, *e, *e2;
457 int t1_start, t1_end, i, t2_start, t2_end;
459 start = (unsigned long)p;
460 end = start + size;
461 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
462 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
464 /* start */
465 page = get_page(t1_start);
466 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
467 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
468 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
469 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
470 #ifdef BOUND_DEBUG
471 printf("new %lx %lx %x %x %x %x\n",
472 start, end, t1_start, t1_end, t2_start, t2_end);
473 #endif
475 e = (BoundEntry *)((char *)page + t2_start);
476 add_region(e, start, size);
478 if (t1_end == t1_start) {
479 /* same ending page */
480 e2 = (BoundEntry *)((char *)page + t2_end);
481 if (e2 > e) {
482 e++;
483 for(;e<e2;e++) {
484 e->start = start;
485 e->size = size;
487 add_region(e, start, size);
489 } else {
490 /* mark until end of page */
491 e2 = page + BOUND_T2_SIZE;
492 e++;
493 for(;e<e2;e++) {
494 e->start = start;
495 e->size = size;
497 /* mark intermediate pages, if any */
498 for(i=t1_start+1;i<t1_end;i++) {
499 page = get_page(i);
500 e2 = page + BOUND_T2_SIZE;
501 for(e=page;e<e2;e++) {
502 e->start = start;
503 e->size = size;
506 /* last page */
507 page = get_page(t1_end);
508 e2 = (BoundEntry *)((char *)page + t2_end);
509 for(e=page;e<e2;e++) {
510 e->start = start;
511 e->size = size;
513 add_region(e, start, size);
517 /* delete a region */
518 static inline void delete_region(BoundEntry *e,
519 void *p, unsigned long empty_size)
521 unsigned long addr;
522 BoundEntry *e1;
524 addr = (unsigned long)p;
525 addr -= e->start;
526 if (addr <= e->size) {
527 /* region found is first one */
528 e1 = e->next;
529 if (e1 == NULL) {
530 /* no more region: mark it empty */
531 e->start = 0;
532 e->size = empty_size;
533 } else {
534 /* copy next region in head */
535 e->start = e1->start;
536 e->size = e1->size;
537 e->next = e1->next;
538 bound_free_entry(e1);
540 } else {
541 /* find the matching region */
542 for(;;) {
543 e1 = e;
544 e = e->next;
545 /* region not found: do nothing */
546 if (e == NULL)
547 break;
548 addr = (unsigned long)p - e->start;
549 if (addr <= e->size) {
550 /* found: remove entry */
551 e1->next = e->next;
552 bound_free_entry(e);
553 break;
559 /* WARNING: 'p' must be the starting point of the region. */
560 /* return non zero if error */
561 int __bound_delete_region(void *p)
563 unsigned long start, end, addr, size, empty_size;
564 BoundEntry *page, *e, *e2;
565 int t1_start, t1_end, t2_start, t2_end, i;
567 start = (unsigned long)p;
568 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
569 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
570 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
572 /* find region size */
573 page = __bound_t1[t1_start];
574 e = (BoundEntry *)((char *)page + t2_start);
575 addr = start - e->start;
576 if (addr > e->size)
577 e = __bound_find_region(e, p);
578 /* test if invalid region */
579 if (e->size == EMPTY_SIZE || (unsigned long)p != e->start)
580 return -1;
581 /* compute the size we put in invalid regions */
582 if (e->is_invalid)
583 empty_size = INVALID_SIZE;
584 else
585 empty_size = EMPTY_SIZE;
586 size = e->size;
587 end = start + size;
589 /* now we can free each entry */
590 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
591 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
592 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
594 delete_region(e, p, empty_size);
595 if (t1_end == t1_start) {
596 /* same ending page */
597 e2 = (BoundEntry *)((char *)page + t2_end);
598 if (e2 > e) {
599 e++;
600 for(;e<e2;e++) {
601 e->start = 0;
602 e->size = empty_size;
604 delete_region(e, p, empty_size);
606 } else {
607 /* mark until end of page */
608 e2 = page + BOUND_T2_SIZE;
609 e++;
610 for(;e<e2;e++) {
611 e->start = 0;
612 e->size = empty_size;
614 /* mark intermediate pages, if any */
615 /* XXX: should free them */
616 for(i=t1_start+1;i<t1_end;i++) {
617 page = get_page(i);
618 e2 = page + BOUND_T2_SIZE;
619 for(e=page;e<e2;e++) {
620 e->start = 0;
621 e->size = empty_size;
624 /* last page */
625 page = get_page(t2_end);
626 e2 = (BoundEntry *)((char *)page + t2_end);
627 for(e=page;e<e2;e++) {
628 e->start = 0;
629 e->size = empty_size;
631 delete_region(e, p, empty_size);
633 return 0;
636 /* return the size of the region starting at p, or EMPTY_SIZE if non
637 existant region. */
638 static unsigned long get_region_size(void *p)
640 unsigned long addr = (unsigned long)p;
641 BoundEntry *e;
643 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
644 e = (BoundEntry *)((char *)e +
645 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
646 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
647 addr -= e->start;
648 if (addr > e->size)
649 e = __bound_find_region(e, p);
650 if (e->start != (unsigned long)p)
651 return EMPTY_SIZE;
652 return e->size;
655 /* patched memory functions */
657 static void install_malloc_hooks(void)
659 #ifdef CONFIG_TCC_MALLOC_HOOKS
660 saved_malloc_hook = __malloc_hook;
661 saved_free_hook = __free_hook;
662 saved_realloc_hook = __realloc_hook;
663 saved_memalign_hook = __memalign_hook;
664 __malloc_hook = __bound_malloc;
665 __free_hook = __bound_free;
666 __realloc_hook = __bound_realloc;
667 __memalign_hook = __bound_memalign;
668 #endif
671 static void restore_malloc_hooks(void)
673 #ifdef CONFIG_TCC_MALLOC_HOOKS
674 __malloc_hook = saved_malloc_hook;
675 __free_hook = saved_free_hook;
676 __realloc_hook = saved_realloc_hook;
677 __memalign_hook = saved_memalign_hook;
678 #endif
681 static void *libc_malloc(size_t size)
683 void *ptr;
684 restore_malloc_hooks();
685 ptr = malloc(size);
686 install_malloc_hooks();
687 return ptr;
690 static void libc_free(void *ptr)
692 restore_malloc_hooks();
693 free(ptr);
694 install_malloc_hooks();
697 /* XXX: we should use a malloc which ensure that it is unlikely that
698 two malloc'ed data have the same address if 'free' are made in
699 between. */
700 void *__bound_malloc(size_t size, const void *caller)
702 void *ptr;
704 /* we allocate one more byte to ensure the regions will be
705 separated by at least one byte. With the glibc malloc, it may
706 be in fact not necessary */
707 ptr = libc_malloc(size + 1);
709 if (!ptr)
710 return NULL;
711 __bound_new_region(ptr, size);
712 return ptr;
715 void *__bound_memalign(size_t size, size_t align, const void *caller)
717 void *ptr;
719 restore_malloc_hooks();
721 #ifndef HAVE_MEMALIGN
722 if (align > 4) {
723 /* XXX: handle it ? */
724 ptr = NULL;
725 } else {
726 /* we suppose that malloc aligns to at least four bytes */
727 ptr = malloc(size + 1);
729 #else
730 /* we allocate one more byte to ensure the regions will be
731 separated by at least one byte. With the glibc malloc, it may
732 be in fact not necessary */
733 ptr = memalign(size + 1, align);
734 #endif
736 install_malloc_hooks();
738 if (!ptr)
739 return NULL;
740 __bound_new_region(ptr, size);
741 return ptr;
744 void __bound_free(void *ptr, const void *caller)
746 if (ptr == NULL)
747 return;
748 if (__bound_delete_region(ptr) != 0)
749 bound_error("freeing invalid region");
751 libc_free(ptr);
754 void *__bound_realloc(void *ptr, size_t size, const void *caller)
756 void *ptr1;
757 int old_size;
759 if (size == 0) {
760 __bound_free(ptr, caller);
761 return NULL;
762 } else {
763 ptr1 = __bound_malloc(size, caller);
764 if (ptr == NULL || ptr1 == NULL)
765 return ptr1;
766 old_size = get_region_size(ptr);
767 if (old_size == EMPTY_SIZE)
768 bound_error("realloc'ing invalid pointer");
769 memcpy(ptr1, ptr, old_size);
770 __bound_free(ptr, caller);
771 return ptr1;
775 #ifndef CONFIG_TCC_MALLOC_HOOKS
776 void *__bound_calloc(size_t nmemb, size_t size)
778 void *ptr;
779 size = size * nmemb;
780 ptr = __bound_malloc(size, NULL);
781 if (!ptr)
782 return NULL;
783 memset(ptr, 0, size);
784 return ptr;
786 #endif
788 #if 0
789 static void bound_dump(void)
791 BoundEntry *page, *e;
792 int i, j;
794 printf("region dump:\n");
795 for(i=0;i<BOUND_T1_SIZE;i++) {
796 page = __bound_t1[i];
797 for(j=0;j<BOUND_T2_SIZE;j++) {
798 e = page + j;
799 /* do not print invalid or empty entries */
800 if (e->size != EMPTY_SIZE && e->start != 0) {
801 printf("%08x:",
802 (i << (BOUND_T2_BITS + BOUND_T3_BITS)) +
803 (j << BOUND_T3_BITS));
804 do {
805 printf(" %08lx:%08lx", e->start, e->start + e->size);
806 e = e->next;
807 } while (e != NULL);
808 printf("\n");
813 #endif
815 /* some useful checked functions */
817 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
818 static void __bound_check(const void *p, size_t size)
820 if (size == 0)
821 return;
822 p = __bound_ptr_add((void *)p, size);
823 if (p == INVALID_POINTER)
824 bound_error("invalid pointer");
827 void *__bound_memcpy(void *dst, const void *src, size_t size)
829 __bound_check(dst, size);
830 __bound_check(src, size);
831 /* check also region overlap */
832 if (src >= dst && src < dst + size)
833 bound_error("overlapping regions in memcpy()");
834 return memcpy(dst, src, size);
837 void *__bound_memmove(void *dst, const void *src, size_t size)
839 __bound_check(dst, size);
840 __bound_check(src, size);
841 return memmove(dst, src, size);
844 void *__bound_memset(void *dst, int c, size_t size)
846 __bound_check(dst, size);
847 return memset(dst, c, size);
850 /* XXX: could be optimized */
851 int __bound_strlen(const char *s)
853 const char *p;
854 int len;
856 len = 0;
857 for(;;) {
858 p = __bound_ptr_indir1((char *)s, len);
859 if (p == INVALID_POINTER)
860 bound_error("bad pointer in strlen()");
861 if (*p == '\0')
862 break;
863 len++;
865 return len;
868 char *__bound_strcpy(char *dst, const char *src)
870 int len;
871 len = __bound_strlen(src);
872 return __bound_memcpy(dst, src, len + 1);