Add input files/libs and reloc_output switch to TCCState
[tinycc.git] / lib / bcheck.c
blob09f2a3aa0e6c7f9a53c585f3c12ee44e12df20f8
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
36 #define HAVE_MEMALIGN
38 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__dietlibc__) \
39 || defined(__UCLIBC__) || defined(__OpenBSD__) || defined(_WIN32)
40 #warning Bound checking does not support malloc (etc.) in this environment.
41 #undef CONFIG_TCC_MALLOC_HOOKS
42 #undef HAVE_MEMALIGN
43 #endif
45 #define BOUND_T1_BITS 13
46 #define BOUND_T2_BITS 11
47 #define BOUND_T3_BITS (32 - BOUND_T1_BITS - BOUND_T2_BITS)
49 #define BOUND_T1_SIZE (1 << BOUND_T1_BITS)
50 #define BOUND_T2_SIZE (1 << BOUND_T2_BITS)
51 #define BOUND_T3_SIZE (1 << BOUND_T3_BITS)
52 #define BOUND_E_BITS 4
54 #define BOUND_T23_BITS (BOUND_T2_BITS + BOUND_T3_BITS)
55 #define BOUND_T23_SIZE (1 << BOUND_T23_BITS)
58 /* this pointer is generated when bound check is incorrect */
59 #define INVALID_POINTER ((void *)(-2))
60 /* size of an empty region */
61 #define EMPTY_SIZE 0xffffffff
62 /* size of an invalid region */
63 #define INVALID_SIZE 0
65 typedef struct BoundEntry {
66 unsigned long start;
67 unsigned long size;
68 struct BoundEntry *next;
69 unsigned long is_invalid; /* true if pointers outside region are invalid */
70 } BoundEntry;
72 /* external interface */
73 void __bound_init(void);
74 void __bound_new_region(void *p, unsigned long size);
75 int __bound_delete_region(void *p);
77 #define FASTCALL __attribute__((regparm(3)))
79 void *__bound_malloc(size_t size, const void *caller);
80 void *__bound_memalign(size_t size, size_t align, const void *caller);
81 void __bound_free(void *ptr, const void *caller);
82 void *__bound_realloc(void *ptr, size_t size, const void *caller);
83 static void *libc_malloc(size_t size);
84 static void libc_free(void *ptr);
85 static void install_malloc_hooks(void);
86 static void restore_malloc_hooks(void);
88 #ifdef CONFIG_TCC_MALLOC_HOOKS
89 static void *saved_malloc_hook;
90 static void *saved_free_hook;
91 static void *saved_realloc_hook;
92 static void *saved_memalign_hook;
93 #endif
95 /* linker definitions */
96 extern char _end;
98 /* TCC definitions */
99 extern char __bounds_start; /* start of static bounds table */
100 /* error message, just for TCC */
101 const char *__bound_error_msg;
103 /* runtime error output */
104 extern void rt_error(unsigned long pc, const char *fmt, ...);
106 #ifdef BOUND_STATIC
107 static BoundEntry *__bound_t1[BOUND_T1_SIZE]; /* page table */
108 #else
109 static BoundEntry **__bound_t1; /* page table */
110 #endif
111 static BoundEntry *__bound_empty_t2; /* empty page, for unused pages */
112 static BoundEntry *__bound_invalid_t2; /* invalid page, for invalid pointers */
114 static BoundEntry *__bound_find_region(BoundEntry *e1, void *p)
116 unsigned long addr, tmp;
117 BoundEntry *e;
119 e = e1;
120 while (e != NULL) {
121 addr = (unsigned long)p;
122 addr -= e->start;
123 if (addr <= e->size) {
124 /* put region at the head */
125 tmp = e1->start;
126 e1->start = e->start;
127 e->start = tmp;
128 tmp = e1->size;
129 e1->size = e->size;
130 e->size = tmp;
131 return e1;
133 e = e->next;
135 /* no entry found: return empty entry or invalid entry */
136 if (e1->is_invalid)
137 return __bound_invalid_t2;
138 else
139 return __bound_empty_t2;
142 /* print a bound error message */
143 static void bound_error(const char *fmt, ...)
145 __bound_error_msg = fmt;
146 *(int *)0 = 0; /* force a runtime error */
149 static void bound_alloc_error(void)
151 bound_error("not enough memory for bound checking code");
154 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
155 the end of a region in this case */
156 void * FASTCALL __bound_ptr_add(void *p, int offset)
158 unsigned long addr = (unsigned long)p;
159 BoundEntry *e;
160 #if defined(BOUND_DEBUG)
161 printf("add: 0x%x %d\n", (int)p, offset);
162 #endif
164 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
165 e = (BoundEntry *)((char *)e +
166 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
167 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
168 addr -= e->start;
169 if (addr > e->size) {
170 e = __bound_find_region(e, p);
171 addr = (unsigned long)p - e->start;
173 addr += offset;
174 if (addr > e->size)
175 return INVALID_POINTER; /* return an invalid pointer */
176 return p + offset;
179 /* return '(p + offset)' for pointer indirection (the resulting must
180 be strictly inside the region */
181 #define BOUND_PTR_INDIR(dsize) \
182 void * FASTCALL __bound_ptr_indir ## dsize (void *p, int offset) \
184 unsigned long addr = (unsigned long)p; \
185 BoundEntry *e; \
187 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
188 e = (BoundEntry *)((char *)e + \
189 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
190 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
191 addr -= e->start; \
192 if (addr > e->size) { \
193 e = __bound_find_region(e, p); \
194 addr = (unsigned long)p - e->start; \
196 addr += offset + dsize; \
197 if (addr > e->size) \
198 return INVALID_POINTER; /* return an invalid pointer */ \
199 return p + offset; \
202 BOUND_PTR_INDIR(1)
203 BOUND_PTR_INDIR(2)
204 BOUND_PTR_INDIR(4)
205 BOUND_PTR_INDIR(8)
206 BOUND_PTR_INDIR(12)
207 BOUND_PTR_INDIR(16)
209 #ifdef __i386__
210 /* return the frame pointer of the caller */
211 #define GET_CALLER_FP(fp)\
213 unsigned long *fp1;\
214 __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp1));\
215 fp = fp1[0];\
217 #else
218 #error put code to extract the calling frame pointer
219 #endif
221 /* called when entering a function to add all the local regions */
222 void FASTCALL __bound_local_new(void *p1)
224 unsigned long addr, size, fp, *p = p1;
225 GET_CALLER_FP(fp);
226 for(;;) {
227 addr = p[0];
228 if (addr == 0)
229 break;
230 addr += fp;
231 size = p[1];
232 p += 2;
233 __bound_new_region((void *)addr, size);
237 /* called when leaving a function to delete all the local regions */
238 void FASTCALL __bound_local_delete(void *p1)
240 unsigned long addr, fp, *p = p1;
241 GET_CALLER_FP(fp);
242 for(;;) {
243 addr = p[0];
244 if (addr == 0)
245 break;
246 addr += fp;
247 p += 2;
248 __bound_delete_region((void *)addr);
252 static BoundEntry *__bound_new_page(void)
254 BoundEntry *page;
255 int i;
257 page = libc_malloc(sizeof(BoundEntry) * BOUND_T2_SIZE);
258 if (!page)
259 bound_alloc_error();
260 for(i=0;i<BOUND_T2_SIZE;i++) {
261 /* put empty entries */
262 page[i].start = 0;
263 page[i].size = EMPTY_SIZE;
264 page[i].next = NULL;
265 page[i].is_invalid = 0;
267 return page;
270 /* currently we use malloc(). Should use bound_new_page() */
271 static BoundEntry *bound_new_entry(void)
273 BoundEntry *e;
274 e = libc_malloc(sizeof(BoundEntry));
275 return e;
278 static void bound_free_entry(BoundEntry *e)
280 libc_free(e);
283 static inline BoundEntry *get_page(int index)
285 BoundEntry *page;
286 page = __bound_t1[index];
287 if (page == __bound_empty_t2 || page == __bound_invalid_t2) {
288 /* create a new page if necessary */
289 page = __bound_new_page();
290 __bound_t1[index] = page;
292 return page;
295 /* mark a region as being invalid (can only be used during init) */
296 static void mark_invalid(unsigned long addr, unsigned long size)
298 unsigned long start, end;
299 BoundEntry *page;
300 int t1_start, t1_end, i, j, t2_start, t2_end;
302 start = addr;
303 end = addr + size;
305 t2_start = (start + BOUND_T3_SIZE - 1) >> BOUND_T3_BITS;
306 if (end != 0)
307 t2_end = end >> BOUND_T3_BITS;
308 else
309 t2_end = 1 << (BOUND_T1_BITS + BOUND_T2_BITS);
311 #if 0
312 printf("mark_invalid: start = %x %x\n", t2_start, t2_end);
313 #endif
315 /* first we handle full pages */
316 t1_start = (t2_start + BOUND_T2_SIZE - 1) >> BOUND_T2_BITS;
317 t1_end = t2_end >> BOUND_T2_BITS;
319 i = t2_start & (BOUND_T2_SIZE - 1);
320 j = t2_end & (BOUND_T2_SIZE - 1);
322 if (t1_start == t1_end) {
323 page = get_page(t2_start >> BOUND_T2_BITS);
324 for(; i < j; i++) {
325 page[i].size = INVALID_SIZE;
326 page[i].is_invalid = 1;
328 } else {
329 if (i > 0) {
330 page = get_page(t2_start >> BOUND_T2_BITS);
331 for(; i < BOUND_T2_SIZE; i++) {
332 page[i].size = INVALID_SIZE;
333 page[i].is_invalid = 1;
336 for(i = t1_start; i < t1_end; i++) {
337 __bound_t1[i] = __bound_invalid_t2;
339 if (j != 0) {
340 page = get_page(t1_end);
341 for(i = 0; i < j; i++) {
342 page[i].size = INVALID_SIZE;
343 page[i].is_invalid = 1;
349 void __bound_init(void)
351 int i;
352 BoundEntry *page;
353 unsigned long start, size;
354 int *p;
356 /* save malloc hooks and install bound check hooks */
357 install_malloc_hooks();
359 #ifndef BOUND_STATIC
360 __bound_t1 = libc_malloc(BOUND_T1_SIZE * sizeof(BoundEntry *));
361 if (!__bound_t1)
362 bound_alloc_error();
363 #endif
364 __bound_empty_t2 = __bound_new_page();
365 for(i=0;i<BOUND_T1_SIZE;i++) {
366 __bound_t1[i] = __bound_empty_t2;
369 page = __bound_new_page();
370 for(i=0;i<BOUND_T2_SIZE;i++) {
371 /* put invalid entries */
372 page[i].start = 0;
373 page[i].size = INVALID_SIZE;
374 page[i].next = NULL;
375 page[i].is_invalid = 1;
377 __bound_invalid_t2 = page;
379 /* invalid pointer zone */
380 start = (unsigned long)INVALID_POINTER & ~(BOUND_T23_SIZE - 1);
381 size = BOUND_T23_SIZE;
382 mark_invalid(start, size);
384 #if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
385 /* malloc zone is also marked invalid. can only use that with
386 hooks because all libs should use the same malloc. The solution
387 would be to build a new malloc for tcc. */
388 start = (unsigned long)&_end;
389 size = 128 * 0x100000;
390 mark_invalid(start, size);
391 #endif
393 /* add all static bound check values */
394 p = (int *)&__bounds_start;
395 while (p[0] != 0) {
396 __bound_new_region((void *)p[0], p[1]);
397 p += 2;
401 void __bound_exit(void)
403 restore_malloc_hooks();
406 static inline void add_region(BoundEntry *e,
407 unsigned long start, unsigned long size)
409 BoundEntry *e1;
410 if (e->start == 0) {
411 /* no region : add it */
412 e->start = start;
413 e->size = size;
414 } else {
415 /* already regions in the list: add it at the head */
416 e1 = bound_new_entry();
417 e1->start = e->start;
418 e1->size = e->size;
419 e1->next = e->next;
420 e->start = start;
421 e->size = size;
422 e->next = e1;
426 /* create a new region. It should not already exist in the region list */
427 void __bound_new_region(void *p, unsigned long size)
429 unsigned long start, end;
430 BoundEntry *page, *e, *e2;
431 int t1_start, t1_end, i, t2_start, t2_end;
433 start = (unsigned long)p;
434 end = start + size;
435 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
436 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
438 /* start */
439 page = get_page(t1_start);
440 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
441 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
442 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
443 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
444 #ifdef BOUND_DEBUG
445 printf("new %lx %lx %x %x %x %x\n",
446 start, end, t1_start, t1_end, t2_start, t2_end);
447 #endif
449 e = (BoundEntry *)((char *)page + t2_start);
450 add_region(e, start, size);
452 if (t1_end == t1_start) {
453 /* same ending page */
454 e2 = (BoundEntry *)((char *)page + t2_end);
455 if (e2 > e) {
456 e++;
457 for(;e<e2;e++) {
458 e->start = start;
459 e->size = size;
461 add_region(e, start, size);
463 } else {
464 /* mark until end of page */
465 e2 = page + BOUND_T2_SIZE;
466 e++;
467 for(;e<e2;e++) {
468 e->start = start;
469 e->size = size;
471 /* mark intermediate pages, if any */
472 for(i=t1_start+1;i<t1_end;i++) {
473 page = get_page(i);
474 e2 = page + BOUND_T2_SIZE;
475 for(e=page;e<e2;e++) {
476 e->start = start;
477 e->size = size;
480 /* last page */
481 page = get_page(t1_end);
482 e2 = (BoundEntry *)((char *)page + t2_end);
483 for(e=page;e<e2;e++) {
484 e->start = start;
485 e->size = size;
487 add_region(e, start, size);
491 /* delete a region */
492 static inline void delete_region(BoundEntry *e,
493 void *p, unsigned long empty_size)
495 unsigned long addr;
496 BoundEntry *e1;
498 addr = (unsigned long)p;
499 addr -= e->start;
500 if (addr <= e->size) {
501 /* region found is first one */
502 e1 = e->next;
503 if (e1 == NULL) {
504 /* no more region: mark it empty */
505 e->start = 0;
506 e->size = empty_size;
507 } else {
508 /* copy next region in head */
509 e->start = e1->start;
510 e->size = e1->size;
511 e->next = e1->next;
512 bound_free_entry(e1);
514 } else {
515 /* find the matching region */
516 for(;;) {
517 e1 = e;
518 e = e->next;
519 /* region not found: do nothing */
520 if (e == NULL)
521 break;
522 addr = (unsigned long)p - e->start;
523 if (addr <= e->size) {
524 /* found: remove entry */
525 e1->next = e->next;
526 bound_free_entry(e);
527 break;
533 /* WARNING: 'p' must be the starting point of the region. */
534 /* return non zero if error */
535 int __bound_delete_region(void *p)
537 unsigned long start, end, addr, size, empty_size;
538 BoundEntry *page, *e, *e2;
539 int t1_start, t1_end, t2_start, t2_end, i;
541 start = (unsigned long)p;
542 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
543 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
544 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
546 /* find region size */
547 page = __bound_t1[t1_start];
548 e = (BoundEntry *)((char *)page + t2_start);
549 addr = start - e->start;
550 if (addr > e->size)
551 e = __bound_find_region(e, p);
552 /* test if invalid region */
553 if (e->size == EMPTY_SIZE || (unsigned long)p != e->start)
554 return -1;
555 /* compute the size we put in invalid regions */
556 if (e->is_invalid)
557 empty_size = INVALID_SIZE;
558 else
559 empty_size = EMPTY_SIZE;
560 size = e->size;
561 end = start + size;
563 /* now we can free each entry */
564 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
565 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
566 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
568 delete_region(e, p, empty_size);
569 if (t1_end == t1_start) {
570 /* same ending page */
571 e2 = (BoundEntry *)((char *)page + t2_end);
572 if (e2 > e) {
573 e++;
574 for(;e<e2;e++) {
575 e->start = 0;
576 e->size = empty_size;
578 delete_region(e, p, empty_size);
580 } else {
581 /* mark until end of page */
582 e2 = page + BOUND_T2_SIZE;
583 e++;
584 for(;e<e2;e++) {
585 e->start = 0;
586 e->size = empty_size;
588 /* mark intermediate pages, if any */
589 /* XXX: should free them */
590 for(i=t1_start+1;i<t1_end;i++) {
591 page = get_page(i);
592 e2 = page + BOUND_T2_SIZE;
593 for(e=page;e<e2;e++) {
594 e->start = 0;
595 e->size = empty_size;
598 /* last page */
599 page = get_page(t2_end);
600 e2 = (BoundEntry *)((char *)page + t2_end);
601 for(e=page;e<e2;e++) {
602 e->start = 0;
603 e->size = empty_size;
605 delete_region(e, p, empty_size);
607 return 0;
610 /* return the size of the region starting at p, or EMPTY_SIZE if non
611 existant region. */
612 static unsigned long get_region_size(void *p)
614 unsigned long addr = (unsigned long)p;
615 BoundEntry *e;
617 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
618 e = (BoundEntry *)((char *)e +
619 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
620 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
621 addr -= e->start;
622 if (addr > e->size)
623 e = __bound_find_region(e, p);
624 if (e->start != (unsigned long)p)
625 return EMPTY_SIZE;
626 return e->size;
629 /* patched memory functions */
631 static void install_malloc_hooks(void)
633 #ifdef CONFIG_TCC_MALLOC_HOOKS
634 saved_malloc_hook = __malloc_hook;
635 saved_free_hook = __free_hook;
636 saved_realloc_hook = __realloc_hook;
637 saved_memalign_hook = __memalign_hook;
638 __malloc_hook = __bound_malloc;
639 __free_hook = __bound_free;
640 __realloc_hook = __bound_realloc;
641 __memalign_hook = __bound_memalign;
642 #endif
645 static void restore_malloc_hooks(void)
647 #ifdef CONFIG_TCC_MALLOC_HOOKS
648 __malloc_hook = saved_malloc_hook;
649 __free_hook = saved_free_hook;
650 __realloc_hook = saved_realloc_hook;
651 __memalign_hook = saved_memalign_hook;
652 #endif
655 static void *libc_malloc(size_t size)
657 void *ptr;
658 restore_malloc_hooks();
659 ptr = malloc(size);
660 install_malloc_hooks();
661 return ptr;
664 static void libc_free(void *ptr)
666 restore_malloc_hooks();
667 free(ptr);
668 install_malloc_hooks();
671 /* XXX: we should use a malloc which ensure that it is unlikely that
672 two malloc'ed data have the same address if 'free' are made in
673 between. */
674 void *__bound_malloc(size_t size, const void *caller)
676 void *ptr;
678 /* we allocate one more byte to ensure the regions will be
679 separated by at least one byte. With the glibc malloc, it may
680 be in fact not necessary */
681 ptr = libc_malloc(size + 1);
683 if (!ptr)
684 return NULL;
685 __bound_new_region(ptr, size);
686 return ptr;
689 void *__bound_memalign(size_t size, size_t align, const void *caller)
691 void *ptr;
693 restore_malloc_hooks();
695 #ifndef HAVE_MEMALIGN
696 if (align > 4) {
697 /* XXX: handle it ? */
698 ptr = NULL;
699 } else {
700 /* we suppose that malloc aligns to at least four bytes */
701 ptr = malloc(size + 1);
703 #else
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 = memalign(size + 1, align);
708 #endif
710 install_malloc_hooks();
712 if (!ptr)
713 return NULL;
714 __bound_new_region(ptr, size);
715 return ptr;
718 void __bound_free(void *ptr, const void *caller)
720 if (ptr == NULL)
721 return;
722 if (__bound_delete_region(ptr) != 0)
723 bound_error("freeing invalid region");
725 libc_free(ptr);
728 void *__bound_realloc(void *ptr, size_t size, const void *caller)
730 void *ptr1;
731 int old_size;
733 if (size == 0) {
734 __bound_free(ptr, caller);
735 return NULL;
736 } else {
737 ptr1 = __bound_malloc(size, caller);
738 if (ptr == NULL || ptr1 == NULL)
739 return ptr1;
740 old_size = get_region_size(ptr);
741 if (old_size == EMPTY_SIZE)
742 bound_error("realloc'ing invalid pointer");
743 memcpy(ptr1, ptr, old_size);
744 __bound_free(ptr, caller);
745 return ptr1;
749 #ifndef CONFIG_TCC_MALLOC_HOOKS
750 void *__bound_calloc(size_t nmemb, size_t size)
752 void *ptr;
753 size = size * nmemb;
754 ptr = __bound_malloc(size, NULL);
755 if (!ptr)
756 return NULL;
757 memset(ptr, 0, size);
758 return ptr;
760 #endif
762 #if 0
763 static void bound_dump(void)
765 BoundEntry *page, *e;
766 int i, j;
768 printf("region dump:\n");
769 for(i=0;i<BOUND_T1_SIZE;i++) {
770 page = __bound_t1[i];
771 for(j=0;j<BOUND_T2_SIZE;j++) {
772 e = page + j;
773 /* do not print invalid or empty entries */
774 if (e->size != EMPTY_SIZE && e->start != 0) {
775 printf("%08x:",
776 (i << (BOUND_T2_BITS + BOUND_T3_BITS)) +
777 (j << BOUND_T3_BITS));
778 do {
779 printf(" %08lx:%08lx", e->start, e->start + e->size);
780 e = e->next;
781 } while (e != NULL);
782 printf("\n");
787 #endif
789 /* some useful checked functions */
791 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
792 static void __bound_check(const void *p, size_t size)
794 if (size == 0)
795 return;
796 p = __bound_ptr_add((void *)p, size);
797 if (p == INVALID_POINTER)
798 bound_error("invalid pointer");
801 void *__bound_memcpy(void *dst, const void *src, size_t size)
803 __bound_check(dst, size);
804 __bound_check(src, size);
805 /* check also region overlap */
806 if (src >= dst && src < dst + size)
807 bound_error("overlapping regions in memcpy()");
808 return memcpy(dst, src, size);
811 void *__bound_memmove(void *dst, const void *src, size_t size)
813 __bound_check(dst, size);
814 __bound_check(src, size);
815 return memmove(dst, src, size);
818 void *__bound_memset(void *dst, int c, size_t size)
820 __bound_check(dst, size);
821 return memset(dst, c, size);
824 /* XXX: could be optimized */
825 int __bound_strlen(const char *s)
827 const char *p;
828 int len;
830 len = 0;
831 for(;;) {
832 p = __bound_ptr_indir1((char *)s, len);
833 if (p == INVALID_POINTER)
834 bound_error("bad pointer in strlen()");
835 if (*p == '\0')
836 break;
837 len++;
839 return len;
842 char *__bound_strcpy(char *dst, const char *src)
844 int len;
845 len = __bound_strlen(src);
846 return __bound_memcpy(dst, src, len + 1);