make a bound checking more compatible with Windows 64
[tinycc.git] / lib / bcheck.c
bloba8355ba5a7a26b99c4a1ca03331e9c986429ef49
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__) && !defined(__NetBSD__)
26 #include <malloc.h>
27 #endif
28 #if !defined(_WIN32)
29 #include <unistd.h>
30 #endif
32 /* #define BOUND_DEBUG */
34 /* define so that bound array is static (faster, but use memory if
35 bound checking not used) */
36 /* #define BOUND_STATIC */
38 /* use malloc hooks. Currently the code cannot be reliable if no hooks */
39 #define CONFIG_TCC_MALLOC_HOOKS
40 #define HAVE_MEMALIGN
42 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
43 || defined(__DragonFly__) || defined(__dietlibc__) \
44 || defined(__UCLIBC__) || defined(__OpenBSD__) || defined(__NetBSD__) \
45 || defined(_WIN32) || defined(TCC_UCLIBC)
46 #warning Bound checking does not support malloc (etc.) in this environment.
47 #undef CONFIG_TCC_MALLOC_HOOKS
48 #undef HAVE_MEMALIGN
49 #endif
51 #define BOUND_T1_BITS 13
52 #define BOUND_T2_BITS 11
53 #define BOUND_T3_BITS (32 - BOUND_T1_BITS - BOUND_T2_BITS)
55 #define BOUND_T1_SIZE (1 << BOUND_T1_BITS)
56 #define BOUND_T2_SIZE (1 << BOUND_T2_BITS)
57 #define BOUND_T3_SIZE (1 << BOUND_T3_BITS)
58 #define BOUND_E_BITS 4
60 #define BOUND_T23_BITS (BOUND_T2_BITS + BOUND_T3_BITS)
61 #define BOUND_T23_SIZE (1 << BOUND_T23_BITS)
64 /* this pointer is generated when bound check is incorrect */
65 #define INVALID_POINTER ((void *)(-2))
66 /* size of an empty region */
67 #define EMPTY_SIZE 0xffffffff
68 /* size of an invalid region */
69 #define INVALID_SIZE 0
71 typedef struct BoundEntry {
72 size_t start;
73 size_t size;
74 struct BoundEntry *next;
75 size_t is_invalid; /* true if pointers outside region are invalid */
76 } BoundEntry;
78 /* external interface */
79 void __bound_init(void);
80 void __bound_new_region(void *p, size_t size);
81 int __bound_delete_region(void *p);
83 #define FASTCALL __attribute__((regparm(3)))
85 void *__bound_malloc(size_t size, const void *caller);
86 void *__bound_memalign(size_t size, size_t align, const void *caller);
87 void __bound_free(void *ptr, const void *caller);
88 void *__bound_realloc(void *ptr, size_t size, const void *caller);
89 static void *libc_malloc(size_t size);
90 static void libc_free(void *ptr);
91 static void install_malloc_hooks(void);
92 static void restore_malloc_hooks(void);
94 #ifdef CONFIG_TCC_MALLOC_HOOKS
95 static void *saved_malloc_hook;
96 static void *saved_free_hook;
97 static void *saved_realloc_hook;
98 static void *saved_memalign_hook;
99 #endif
101 /* TCC definitions */
102 extern char __bounds_start; /* start of static bounds table */
103 /* error message, just for TCC */
104 const char *__bound_error_msg;
106 /* runtime error output */
107 extern void rt_error(size_t 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 size_t addr, tmp;
120 BoundEntry *e;
122 e = e1;
123 while (e != NULL) {
124 addr = (size_t)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 char *fmt, ...)
148 __bound_error_msg = fmt;
149 *(int *)0 = 0; /* force a runtime error */
152 static void bound_alloc_error(void)
154 bound_error("not enough memory for bound checking code");
157 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
158 the end of a region in this case */
159 void * FASTCALL __bound_ptr_add(void *p, size_t offset)
161 size_t addr = (size_t)p;
162 BoundEntry *e;
163 #if defined(BOUND_DEBUG)
164 printf("add: 0x%x %d\n", (int)p, offset);
165 #endif
167 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
168 e = (BoundEntry *)((char *)e +
169 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
170 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
171 addr -= e->start;
172 if (addr > e->size) {
173 e = __bound_find_region(e, p);
174 addr = (size_t)p - e->start;
176 addr += offset;
177 if (addr > e->size)
178 return INVALID_POINTER; /* return an invalid pointer */
179 return p + offset;
182 /* return '(p + offset)' for pointer indirection (the resulting must
183 be strictly inside the region */
184 #define BOUND_PTR_INDIR(dsize) \
185 void * FASTCALL __bound_ptr_indir ## dsize (void *p, int offset) \
187 size_t addr = (size_t)p; \
188 BoundEntry *e; \
190 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
191 e = (BoundEntry *)((char *)e + \
192 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
193 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
194 addr -= e->start; \
195 if (addr > e->size) { \
196 e = __bound_find_region(e, p); \
197 addr = (size_t)p - e->start; \
199 addr += offset + dsize; \
200 if (addr > e->size) \
201 return INVALID_POINTER; /* return an invalid pointer */ \
202 return p + offset; \
205 BOUND_PTR_INDIR(1)
206 BOUND_PTR_INDIR(2)
207 BOUND_PTR_INDIR(4)
208 BOUND_PTR_INDIR(8)
209 BOUND_PTR_INDIR(12)
210 BOUND_PTR_INDIR(16)
212 /* return the frame pointer of the caller */
213 #define GET_CALLER_FP(fp)\
215 fp = (size_t)__builtin_frame_address(1);\
218 /* called when entering a function to add all the local regions */
219 void FASTCALL __bound_local_new(void *p1)
221 size_t addr, size, fp, *p = p1;
222 GET_CALLER_FP(fp);
223 for(;;) {
224 addr = p[0];
225 if (addr == 0)
226 break;
227 addr += fp;
228 size = p[1];
229 p += 2;
230 __bound_new_region((void *)addr, size);
234 /* called when leaving a function to delete all the local regions */
235 void FASTCALL __bound_local_delete(void *p1)
237 size_t addr, fp, *p = p1;
238 GET_CALLER_FP(fp);
239 for(;;) {
240 addr = p[0];
241 if (addr == 0)
242 break;
243 addr += fp;
244 p += 2;
245 __bound_delete_region((void *)addr);
249 static BoundEntry *__bound_new_page(void)
251 BoundEntry *page;
252 int i;
254 page = libc_malloc(sizeof(BoundEntry) * BOUND_T2_SIZE);
255 if (!page)
256 bound_alloc_error();
257 for(i=0;i<BOUND_T2_SIZE;i++) {
258 /* put empty entries */
259 page[i].start = 0;
260 page[i].size = EMPTY_SIZE;
261 page[i].next = NULL;
262 page[i].is_invalid = 0;
264 return page;
267 /* currently we use malloc(). Should use bound_new_page() */
268 static BoundEntry *bound_new_entry(void)
270 BoundEntry *e;
271 e = libc_malloc(sizeof(BoundEntry));
272 return e;
275 static void bound_free_entry(BoundEntry *e)
277 libc_free(e);
280 static inline BoundEntry *get_page(int index)
282 BoundEntry *page;
283 page = __bound_t1[index];
284 if (page == __bound_empty_t2 || page == __bound_invalid_t2) {
285 /* create a new page if necessary */
286 page = __bound_new_page();
287 __bound_t1[index] = page;
289 return page;
292 /* mark a region as being invalid (can only be used during init) */
293 static void mark_invalid(size_t addr, size_t size)
295 size_t start, end;
296 BoundEntry *page;
297 size_t t1_start, t1_end, i, j, t2_start, t2_end;
299 start = addr;
300 end = addr + size;
302 t2_start = (start + BOUND_T3_SIZE - 1) >> BOUND_T3_BITS;
303 if (end != 0)
304 t2_end = end >> BOUND_T3_BITS;
305 else
306 t2_end = 1 << (BOUND_T1_BITS + BOUND_T2_BITS);
308 #if 0
309 printf("mark_invalid: start = %x %x\n", t2_start, t2_end);
310 #endif
312 /* first we handle full pages */
313 t1_start = (t2_start + BOUND_T2_SIZE - 1) >> BOUND_T2_BITS;
314 t1_end = t2_end >> BOUND_T2_BITS;
316 i = t2_start & (BOUND_T2_SIZE - 1);
317 j = t2_end & (BOUND_T2_SIZE - 1);
319 if (t1_start == t1_end) {
320 page = get_page(t2_start >> BOUND_T2_BITS);
321 for(; i < j; i++) {
322 page[i].size = INVALID_SIZE;
323 page[i].is_invalid = 1;
325 } else {
326 if (i > 0) {
327 page = get_page(t2_start >> BOUND_T2_BITS);
328 for(; i < BOUND_T2_SIZE; i++) {
329 page[i].size = INVALID_SIZE;
330 page[i].is_invalid = 1;
333 for(i = t1_start; i < t1_end; i++) {
334 __bound_t1[i] = __bound_invalid_t2;
336 if (j != 0) {
337 page = get_page(t1_end);
338 for(i = 0; i < j; i++) {
339 page[i].size = INVALID_SIZE;
340 page[i].is_invalid = 1;
346 void __bound_init(void)
348 int i;
349 BoundEntry *page;
350 size_t start, size;
352 /* int *p; */
353 __PTRDIFF_TYPE__ *p; /* 32 or 64 bit integer */
355 /* save malloc hooks and install bound check hooks */
356 install_malloc_hooks();
358 #ifndef BOUND_STATIC
359 __bound_t1 = libc_malloc(BOUND_T1_SIZE * sizeof(BoundEntry *));
360 if (!__bound_t1)
361 bound_alloc_error();
362 #endif
363 __bound_empty_t2 = __bound_new_page();
364 for(i=0;i<BOUND_T1_SIZE;i++) {
365 __bound_t1[i] = __bound_empty_t2;
368 page = __bound_new_page();
369 for(i=0;i<BOUND_T2_SIZE;i++) {
370 /* put invalid entries */
371 page[i].start = 0;
372 page[i].size = INVALID_SIZE;
373 page[i].next = NULL;
374 page[i].is_invalid = 1;
376 __bound_invalid_t2 = page;
378 /* invalid pointer zone */
379 start = (size_t)INVALID_POINTER & ~(BOUND_T23_SIZE - 1);
380 size = BOUND_T23_SIZE;
381 mark_invalid(start, size);
383 #if defined(CONFIG_TCC_MALLOC_HOOKS)
384 /* malloc zone is also marked invalid. can only use that with
385 * hooks because all libs should use the same malloc. The solution
386 * would be to build a new malloc for tcc.
388 * usually heap (= malloc zone) comes right after bss, i.e. after _end, but
389 * not always - either if we are running from under `tcc -b -run`, or if
390 * address space randomization is turned on(a), heap start will be separated
391 * from bss end.
393 * So sbrk(0) will be a good approximation for start_brk:
395 * - if we are a separately compiled program, __bound_init() runs early,
396 * and sbrk(0) should be equal or very near to start_brk(b) (in case other
397 * constructors malloc something), or
399 * - if we are running from under `tcc -b -run`, sbrk(0) will return
400 * start of heap portion which is under this program control, and not
401 * mark as invalid earlier allocated memory.
404 * (a) /proc/sys/kernel/randomize_va_space = 2, on Linux;
405 * usually turned on by default.
407 * (b) on Linux >= v3.3, the alternative is to read
408 * start_brk from /proc/self/stat
410 start = (size_t)sbrk(0);
411 size = 128 * 0x100000;
412 mark_invalid(start, size);
413 #endif
415 /* add all static bound check values */
416 p = (__PTRDIFF_TYPE__ *)&__bounds_start;
417 while (p[0] != 0) {
418 __bound_new_region((void *)p[0], p[1]);
419 p += 2;
423 void __bound_main_arg(void **p)
425 void *start = p;
426 while (*p++);
427 __bound_new_region(start, (void *) p - start);
430 void __bound_exit(void)
432 restore_malloc_hooks();
435 static inline void add_region(BoundEntry *e,
436 size_t start, size_t size)
438 BoundEntry *e1;
439 if (e->start == 0) {
440 /* no region : add it */
441 e->start = start;
442 e->size = size;
443 } else {
444 /* already regions in the list: add it at the head */
445 e1 = bound_new_entry();
446 e1->start = e->start;
447 e1->size = e->size;
448 e1->next = e->next;
449 e->start = start;
450 e->size = size;
451 e->next = e1;
455 /* create a new region. It should not already exist in the region list */
456 void __bound_new_region(void *p, size_t size)
458 size_t start, end;
459 BoundEntry *page, *e, *e2;
460 size_t t1_start, t1_end, i, t2_start, t2_end;
462 start = (size_t)p;
463 end = start + size;
464 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
465 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
467 /* start */
468 page = get_page(t1_start);
469 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
470 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
471 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
472 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
473 #ifdef BOUND_DEBUG
474 printf("new %lx %lx %x %x %x %x\n",
475 start, end, t1_start, t1_end, t2_start, t2_end);
476 #endif
478 e = (BoundEntry *)((char *)page + t2_start);
479 add_region(e, start, size);
481 if (t1_end == t1_start) {
482 /* same ending page */
483 e2 = (BoundEntry *)((char *)page + t2_end);
484 if (e2 > e) {
485 e++;
486 for(;e<e2;e++) {
487 e->start = start;
488 e->size = size;
490 add_region(e, start, size);
492 } else {
493 /* mark until end of page */
494 e2 = page + BOUND_T2_SIZE;
495 e++;
496 for(;e<e2;e++) {
497 e->start = start;
498 e->size = size;
500 /* mark intermediate pages, if any */
501 for(i=t1_start+1;i<t1_end;i++) {
502 page = get_page(i);
503 e2 = page + BOUND_T2_SIZE;
504 for(e=page;e<e2;e++) {
505 e->start = start;
506 e->size = size;
509 /* last page */
510 page = get_page(t1_end);
511 e2 = (BoundEntry *)((char *)page + t2_end);
512 for(e=page;e<e2;e++) {
513 e->start = start;
514 e->size = size;
516 add_region(e, start, size);
520 /* delete a region */
521 static inline void delete_region(BoundEntry *e,
522 void *p, size_t empty_size)
524 size_t addr;
525 BoundEntry *e1;
527 addr = (size_t)p;
528 addr -= e->start;
529 if (addr <= e->size) {
530 /* region found is first one */
531 e1 = e->next;
532 if (e1 == NULL) {
533 /* no more region: mark it empty */
534 e->start = 0;
535 e->size = empty_size;
536 } else {
537 /* copy next region in head */
538 e->start = e1->start;
539 e->size = e1->size;
540 e->next = e1->next;
541 bound_free_entry(e1);
543 } else {
544 /* find the matching region */
545 for(;;) {
546 e1 = e;
547 e = e->next;
548 /* region not found: do nothing */
549 if (e == NULL)
550 break;
551 addr = (size_t)p - e->start;
552 if (addr <= e->size) {
553 /* found: remove entry */
554 e1->next = e->next;
555 bound_free_entry(e);
556 break;
562 /* WARNING: 'p' must be the starting point of the region. */
563 /* return non zero if error */
564 int __bound_delete_region(void *p)
566 size_t start, end, addr, size, empty_size;
567 BoundEntry *page, *e, *e2;
568 size_t t1_start, t1_end, t2_start, t2_end, i;
570 start = (size_t)p;
571 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
572 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
573 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
575 /* find region size */
576 page = __bound_t1[t1_start];
577 e = (BoundEntry *)((char *)page + t2_start);
578 addr = start - e->start;
579 if (addr > e->size)
580 e = __bound_find_region(e, p);
581 /* test if invalid region */
582 if (e->size == EMPTY_SIZE || (size_t)p != e->start)
583 return -1;
584 /* compute the size we put in invalid regions */
585 if (e->is_invalid)
586 empty_size = INVALID_SIZE;
587 else
588 empty_size = EMPTY_SIZE;
589 size = e->size;
590 end = start + size;
592 /* now we can free each entry */
593 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
594 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
595 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
597 delete_region(e, p, empty_size);
598 if (t1_end == t1_start) {
599 /* same ending page */
600 e2 = (BoundEntry *)((char *)page + t2_end);
601 if (e2 > e) {
602 e++;
603 for(;e<e2;e++) {
604 e->start = 0;
605 e->size = empty_size;
607 delete_region(e, p, empty_size);
609 } else {
610 /* mark until end of page */
611 e2 = page + BOUND_T2_SIZE;
612 e++;
613 for(;e<e2;e++) {
614 e->start = 0;
615 e->size = empty_size;
617 /* mark intermediate pages, if any */
618 /* XXX: should free them */
619 for(i=t1_start+1;i<t1_end;i++) {
620 page = get_page(i);
621 e2 = page + BOUND_T2_SIZE;
622 for(e=page;e<e2;e++) {
623 e->start = 0;
624 e->size = empty_size;
627 /* last page */
628 page = get_page(t1_end);
629 e2 = (BoundEntry *)((char *)page + t2_end);
630 for(e=page;e<e2;e++) {
631 e->start = 0;
632 e->size = empty_size;
634 delete_region(e, p, empty_size);
636 return 0;
639 /* return the size of the region starting at p, or EMPTY_SIZE if non
640 existent region. */
641 static size_t get_region_size(void *p)
643 size_t addr = (size_t)p;
644 BoundEntry *e;
646 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
647 e = (BoundEntry *)((char *)e +
648 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
649 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
650 addr -= e->start;
651 if (addr > e->size)
652 e = __bound_find_region(e, p);
653 if (e->start != (size_t)p)
654 return EMPTY_SIZE;
655 return e->size;
658 /* patched memory functions */
660 /* force compiler to perform stores coded up to this point */
661 #define barrier() __asm__ __volatile__ ("": : : "memory")
663 static void install_malloc_hooks(void)
665 #ifdef CONFIG_TCC_MALLOC_HOOKS
666 saved_malloc_hook = __malloc_hook;
667 saved_free_hook = __free_hook;
668 saved_realloc_hook = __realloc_hook;
669 saved_memalign_hook = __memalign_hook;
670 __malloc_hook = __bound_malloc;
671 __free_hook = __bound_free;
672 __realloc_hook = __bound_realloc;
673 __memalign_hook = __bound_memalign;
675 barrier();
676 #endif
679 static void restore_malloc_hooks(void)
681 #ifdef CONFIG_TCC_MALLOC_HOOKS
682 __malloc_hook = saved_malloc_hook;
683 __free_hook = saved_free_hook;
684 __realloc_hook = saved_realloc_hook;
685 __memalign_hook = saved_memalign_hook;
687 barrier();
688 #endif
691 static void *libc_malloc(size_t size)
693 void *ptr;
694 restore_malloc_hooks();
695 ptr = malloc(size);
696 install_malloc_hooks();
697 return ptr;
700 static void libc_free(void *ptr)
702 restore_malloc_hooks();
703 free(ptr);
704 install_malloc_hooks();
707 /* XXX: we should use a malloc which ensure that it is unlikely that
708 two malloc'ed data have the same address if 'free' are made in
709 between. */
710 void *__bound_malloc(size_t size, const void *caller)
712 void *ptr;
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 = libc_malloc(size + 1);
719 if (!ptr)
720 return NULL;
721 __bound_new_region(ptr, size);
722 return ptr;
725 void *__bound_memalign(size_t size, size_t align, const void *caller)
727 void *ptr;
729 restore_malloc_hooks();
731 #ifndef HAVE_MEMALIGN
732 if (align > 4) {
733 /* XXX: handle it ? */
734 ptr = NULL;
735 } else {
736 /* we suppose that malloc aligns to at least four bytes */
737 ptr = malloc(size + 1);
739 #else
740 /* we allocate one more byte to ensure the regions will be
741 separated by at least one byte. With the glibc malloc, it may
742 be in fact not necessary */
743 ptr = memalign(size + 1, align);
744 #endif
746 install_malloc_hooks();
748 if (!ptr)
749 return NULL;
750 __bound_new_region(ptr, size);
751 return ptr;
754 void __bound_free(void *ptr, const void *caller)
756 if (ptr == NULL)
757 return;
758 if (__bound_delete_region(ptr) != 0)
759 bound_error("freeing invalid region");
761 libc_free(ptr);
764 void *__bound_realloc(void *ptr, size_t size, const void *caller)
766 void *ptr1;
767 size_t old_size;
769 if (size == 0) {
770 __bound_free(ptr, caller);
771 return NULL;
772 } else {
773 ptr1 = __bound_malloc(size, caller);
774 if (ptr == NULL || ptr1 == NULL)
775 return ptr1;
776 old_size = get_region_size(ptr);
777 if (old_size == EMPTY_SIZE)
778 bound_error("realloc'ing invalid pointer");
779 memcpy(ptr1, ptr, old_size);
780 __bound_free(ptr, caller);
781 return ptr1;
785 #ifndef CONFIG_TCC_MALLOC_HOOKS
786 void *__bound_calloc(size_t nmemb, size_t size)
788 void *ptr;
789 size = size * nmemb;
790 ptr = __bound_malloc(size, NULL);
791 if (!ptr)
792 return NULL;
793 memset(ptr, 0, size);
794 return ptr;
796 #endif
798 #if 0
799 static void bound_dump(void)
801 BoundEntry *page, *e;
802 int i, j;
804 printf("region dump:\n");
805 for(i=0;i<BOUND_T1_SIZE;i++) {
806 page = __bound_t1[i];
807 for(j=0;j<BOUND_T2_SIZE;j++) {
808 e = page + j;
809 /* do not print invalid or empty entries */
810 if (e->size != EMPTY_SIZE && e->start != 0) {
811 printf("%08x:",
812 (i << (BOUND_T2_BITS + BOUND_T3_BITS)) +
813 (j << BOUND_T3_BITS));
814 do {
815 printf(" %08lx:%08lx", e->start, e->start + e->size);
816 e = e->next;
817 } while (e != NULL);
818 printf("\n");
823 #endif
825 /* some useful checked functions */
827 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
828 static void __bound_check(const void *p, size_t size)
830 if (size == 0)
831 return;
832 p = __bound_ptr_add((void *)p, size);
833 if (p == INVALID_POINTER)
834 bound_error("invalid pointer");
837 void *__bound_memcpy(void *dst, const void *src, size_t size)
839 __bound_check(dst, size);
840 __bound_check(src, size);
841 /* check also region overlap */
842 if (src >= dst && src < dst + size)
843 bound_error("overlapping regions in memcpy()");
844 return memcpy(dst, src, size);
847 void *__bound_memmove(void *dst, const void *src, size_t size)
849 __bound_check(dst, size);
850 __bound_check(src, size);
851 return memmove(dst, src, size);
854 void *__bound_memset(void *dst, int c, size_t size)
856 __bound_check(dst, size);
857 return memset(dst, c, size);
860 /* XXX: could be optimized */
861 int __bound_strlen(const char *s)
863 const char *p;
864 int len;
866 len = 0;
867 for(;;) {
868 p = __bound_ptr_indir1((char *)s, len);
869 if (p == INVALID_POINTER)
870 bound_error("bad pointer in strlen()");
871 if (*p == '\0')
872 break;
873 len++;
875 return len;
878 char *__bound_strcpy(char *dst, const char *src)
880 int len;
881 len = __bound_strlen(src);
882 return __bound_memcpy(dst, src, len + 1);