lib/bcheck: Prevent libc_malloc/libc_free etc from being miscompiled
[tinycc.git] / lib / bcheck.c
blob700ceda6c6bea1c95274a2975a89dd2a6588ce5e
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 /* force compiler to perform stores coded up to this point */
642 #define barrier() __asm__ __volatile__ ("": : : "memory")
644 static void install_malloc_hooks(void)
646 #ifdef CONFIG_TCC_MALLOC_HOOKS
647 saved_malloc_hook = __malloc_hook;
648 saved_free_hook = __free_hook;
649 saved_realloc_hook = __realloc_hook;
650 saved_memalign_hook = __memalign_hook;
651 __malloc_hook = __bound_malloc;
652 __free_hook = __bound_free;
653 __realloc_hook = __bound_realloc;
654 __memalign_hook = __bound_memalign;
656 barrier();
657 #endif
660 static void restore_malloc_hooks(void)
662 #ifdef CONFIG_TCC_MALLOC_HOOKS
663 __malloc_hook = saved_malloc_hook;
664 __free_hook = saved_free_hook;
665 __realloc_hook = saved_realloc_hook;
666 __memalign_hook = saved_memalign_hook;
668 barrier();
669 #endif
672 static void *libc_malloc(size_t size)
674 void *ptr;
675 restore_malloc_hooks();
676 ptr = malloc(size);
677 install_malloc_hooks();
678 return ptr;
681 static void libc_free(void *ptr)
683 restore_malloc_hooks();
684 free(ptr);
685 install_malloc_hooks();
688 /* XXX: we should use a malloc which ensure that it is unlikely that
689 two malloc'ed data have the same address if 'free' are made in
690 between. */
691 void *__bound_malloc(size_t size, const void *caller)
693 void *ptr;
695 /* we allocate one more byte to ensure the regions will be
696 separated by at least one byte. With the glibc malloc, it may
697 be in fact not necessary */
698 ptr = libc_malloc(size + 1);
700 if (!ptr)
701 return NULL;
702 __bound_new_region(ptr, size);
703 return ptr;
706 void *__bound_memalign(size_t size, size_t align, const void *caller)
708 void *ptr;
710 restore_malloc_hooks();
712 #ifndef HAVE_MEMALIGN
713 if (align > 4) {
714 /* XXX: handle it ? */
715 ptr = NULL;
716 } else {
717 /* we suppose that malloc aligns to at least four bytes */
718 ptr = malloc(size + 1);
720 #else
721 /* we allocate one more byte to ensure the regions will be
722 separated by at least one byte. With the glibc malloc, it may
723 be in fact not necessary */
724 ptr = memalign(size + 1, align);
725 #endif
727 install_malloc_hooks();
729 if (!ptr)
730 return NULL;
731 __bound_new_region(ptr, size);
732 return ptr;
735 void __bound_free(void *ptr, const void *caller)
737 if (ptr == NULL)
738 return;
739 if (__bound_delete_region(ptr) != 0)
740 bound_error("freeing invalid region");
742 libc_free(ptr);
745 void *__bound_realloc(void *ptr, size_t size, const void *caller)
747 void *ptr1;
748 int old_size;
750 if (size == 0) {
751 __bound_free(ptr, caller);
752 return NULL;
753 } else {
754 ptr1 = __bound_malloc(size, caller);
755 if (ptr == NULL || ptr1 == NULL)
756 return ptr1;
757 old_size = get_region_size(ptr);
758 if (old_size == EMPTY_SIZE)
759 bound_error("realloc'ing invalid pointer");
760 memcpy(ptr1, ptr, old_size);
761 __bound_free(ptr, caller);
762 return ptr1;
766 #ifndef CONFIG_TCC_MALLOC_HOOKS
767 void *__bound_calloc(size_t nmemb, size_t size)
769 void *ptr;
770 size = size * nmemb;
771 ptr = __bound_malloc(size, NULL);
772 if (!ptr)
773 return NULL;
774 memset(ptr, 0, size);
775 return ptr;
777 #endif
779 #if 0
780 static void bound_dump(void)
782 BoundEntry *page, *e;
783 int i, j;
785 printf("region dump:\n");
786 for(i=0;i<BOUND_T1_SIZE;i++) {
787 page = __bound_t1[i];
788 for(j=0;j<BOUND_T2_SIZE;j++) {
789 e = page + j;
790 /* do not print invalid or empty entries */
791 if (e->size != EMPTY_SIZE && e->start != 0) {
792 printf("%08x:",
793 (i << (BOUND_T2_BITS + BOUND_T3_BITS)) +
794 (j << BOUND_T3_BITS));
795 do {
796 printf(" %08lx:%08lx", e->start, e->start + e->size);
797 e = e->next;
798 } while (e != NULL);
799 printf("\n");
804 #endif
806 /* some useful checked functions */
808 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
809 static void __bound_check(const void *p, size_t size)
811 if (size == 0)
812 return;
813 p = __bound_ptr_add((void *)p, size);
814 if (p == INVALID_POINTER)
815 bound_error("invalid pointer");
818 void *__bound_memcpy(void *dst, const void *src, size_t size)
820 __bound_check(dst, size);
821 __bound_check(src, size);
822 /* check also region overlap */
823 if (src >= dst && src < dst + size)
824 bound_error("overlapping regions in memcpy()");
825 return memcpy(dst, src, size);
828 void *__bound_memmove(void *dst, const void *src, size_t size)
830 __bound_check(dst, size);
831 __bound_check(src, size);
832 return memmove(dst, src, size);
835 void *__bound_memset(void *dst, int c, size_t size)
837 __bound_check(dst, size);
838 return memset(dst, c, size);
841 /* XXX: could be optimized */
842 int __bound_strlen(const char *s)
844 const char *p;
845 int len;
847 len = 0;
848 for(;;) {
849 p = __bound_ptr_indir1((char *)s, len);
850 if (p == INVALID_POINTER)
851 bound_error("bad pointer in strlen()");
852 if (*p == '\0')
853 break;
854 len++;
856 return len;
859 char *__bound_strcpy(char *dst, const char *src)
861 int len;
862 len = __bound_strlen(src);
863 return __bound_memcpy(dst, src, len + 1);