i386: We can change 'lea 0(%ebp),r' to 'mov %ebp,r'
[tinycc.git] / lib / bcheck.c
blob5ea2d0bf4da323b3220315754dcba403238e377b
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 /* return the frame pointer of the caller */
212 #define GET_CALLER_FP(fp)\
214 fp = (unsigned long)__builtin_frame_address(1);\
217 /* called when entering a function to add all the local regions */
218 void FASTCALL __bound_local_new(void *p1)
220 unsigned long addr, size, fp, *p = p1;
221 GET_CALLER_FP(fp);
222 for(;;) {
223 addr = p[0];
224 if (addr == 0)
225 break;
226 addr += fp;
227 size = p[1];
228 p += 2;
229 __bound_new_region((void *)addr, size);
233 /* called when leaving a function to delete all the local regions */
234 void FASTCALL __bound_local_delete(void *p1)
236 unsigned long addr, fp, *p = p1;
237 GET_CALLER_FP(fp);
238 for(;;) {
239 addr = p[0];
240 if (addr == 0)
241 break;
242 addr += fp;
243 p += 2;
244 __bound_delete_region((void *)addr);
248 static BoundEntry *__bound_new_page(void)
250 BoundEntry *page;
251 int i;
253 page = libc_malloc(sizeof(BoundEntry) * BOUND_T2_SIZE);
254 if (!page)
255 bound_alloc_error();
256 for(i=0;i<BOUND_T2_SIZE;i++) {
257 /* put empty entries */
258 page[i].start = 0;
259 page[i].size = EMPTY_SIZE;
260 page[i].next = NULL;
261 page[i].is_invalid = 0;
263 return page;
266 /* currently we use malloc(). Should use bound_new_page() */
267 static BoundEntry *bound_new_entry(void)
269 BoundEntry *e;
270 e = libc_malloc(sizeof(BoundEntry));
271 return e;
274 static void bound_free_entry(BoundEntry *e)
276 libc_free(e);
279 static inline BoundEntry *get_page(int index)
281 BoundEntry *page;
282 page = __bound_t1[index];
283 if (page == __bound_empty_t2 || page == __bound_invalid_t2) {
284 /* create a new page if necessary */
285 page = __bound_new_page();
286 __bound_t1[index] = page;
288 return page;
291 /* mark a region as being invalid (can only be used during init) */
292 static void mark_invalid(unsigned long addr, unsigned long size)
294 unsigned long start, end;
295 BoundEntry *page;
296 int t1_start, t1_end, i, j, t2_start, t2_end;
298 start = addr;
299 end = addr + size;
301 t2_start = (start + BOUND_T3_SIZE - 1) >> BOUND_T3_BITS;
302 if (end != 0)
303 t2_end = end >> BOUND_T3_BITS;
304 else
305 t2_end = 1 << (BOUND_T1_BITS + BOUND_T2_BITS);
307 #if 0
308 printf("mark_invalid: start = %x %x\n", t2_start, t2_end);
309 #endif
311 /* first we handle full pages */
312 t1_start = (t2_start + BOUND_T2_SIZE - 1) >> BOUND_T2_BITS;
313 t1_end = t2_end >> BOUND_T2_BITS;
315 i = t2_start & (BOUND_T2_SIZE - 1);
316 j = t2_end & (BOUND_T2_SIZE - 1);
318 if (t1_start == t1_end) {
319 page = get_page(t2_start >> BOUND_T2_BITS);
320 for(; i < j; i++) {
321 page[i].size = INVALID_SIZE;
322 page[i].is_invalid = 1;
324 } else {
325 if (i > 0) {
326 page = get_page(t2_start >> BOUND_T2_BITS);
327 for(; i < BOUND_T2_SIZE; i++) {
328 page[i].size = INVALID_SIZE;
329 page[i].is_invalid = 1;
332 for(i = t1_start; i < t1_end; i++) {
333 __bound_t1[i] = __bound_invalid_t2;
335 if (j != 0) {
336 page = get_page(t1_end);
337 for(i = 0; i < j; i++) {
338 page[i].size = INVALID_SIZE;
339 page[i].is_invalid = 1;
345 void __bound_init(void)
347 int i;
348 BoundEntry *page;
349 unsigned long start, size;
350 int *p;
352 /* save malloc hooks and install bound check hooks */
353 install_malloc_hooks();
355 #ifndef BOUND_STATIC
356 __bound_t1 = libc_malloc(BOUND_T1_SIZE * sizeof(BoundEntry *));
357 if (!__bound_t1)
358 bound_alloc_error();
359 #endif
360 __bound_empty_t2 = __bound_new_page();
361 for(i=0;i<BOUND_T1_SIZE;i++) {
362 __bound_t1[i] = __bound_empty_t2;
365 page = __bound_new_page();
366 for(i=0;i<BOUND_T2_SIZE;i++) {
367 /* put invalid entries */
368 page[i].start = 0;
369 page[i].size = INVALID_SIZE;
370 page[i].next = NULL;
371 page[i].is_invalid = 1;
373 __bound_invalid_t2 = page;
375 /* invalid pointer zone */
376 start = (unsigned long)INVALID_POINTER & ~(BOUND_T23_SIZE - 1);
377 size = BOUND_T23_SIZE;
378 mark_invalid(start, size);
380 #if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
381 /* malloc zone is also marked invalid. can only use that with
382 hooks because all libs should use the same malloc. The solution
383 would be to build a new malloc for tcc. */
384 start = (unsigned long)&_end;
385 size = 128 * 0x100000;
386 mark_invalid(start, size);
387 #endif
389 /* add all static bound check values */
390 p = (int *)&__bounds_start;
391 while (p[0] != 0) {
392 __bound_new_region((void *)p[0], p[1]);
393 p += 2;
397 void __bound_exit(void)
399 restore_malloc_hooks();
402 static inline void add_region(BoundEntry *e,
403 unsigned long start, unsigned long size)
405 BoundEntry *e1;
406 if (e->start == 0) {
407 /* no region : add it */
408 e->start = start;
409 e->size = size;
410 } else {
411 /* already regions in the list: add it at the head */
412 e1 = bound_new_entry();
413 e1->start = e->start;
414 e1->size = e->size;
415 e1->next = e->next;
416 e->start = start;
417 e->size = size;
418 e->next = e1;
422 /* create a new region. It should not already exist in the region list */
423 void __bound_new_region(void *p, unsigned long size)
425 unsigned long start, end;
426 BoundEntry *page, *e, *e2;
427 int t1_start, t1_end, i, t2_start, t2_end;
429 start = (unsigned long)p;
430 end = start + size;
431 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
432 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
434 /* start */
435 page = get_page(t1_start);
436 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
437 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
438 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
439 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
440 #ifdef BOUND_DEBUG
441 printf("new %lx %lx %x %x %x %x\n",
442 start, end, t1_start, t1_end, t2_start, t2_end);
443 #endif
445 e = (BoundEntry *)((char *)page + t2_start);
446 add_region(e, start, size);
448 if (t1_end == t1_start) {
449 /* same ending page */
450 e2 = (BoundEntry *)((char *)page + t2_end);
451 if (e2 > e) {
452 e++;
453 for(;e<e2;e++) {
454 e->start = start;
455 e->size = size;
457 add_region(e, start, size);
459 } else {
460 /* mark until end of page */
461 e2 = page + BOUND_T2_SIZE;
462 e++;
463 for(;e<e2;e++) {
464 e->start = start;
465 e->size = size;
467 /* mark intermediate pages, if any */
468 for(i=t1_start+1;i<t1_end;i++) {
469 page = get_page(i);
470 e2 = page + BOUND_T2_SIZE;
471 for(e=page;e<e2;e++) {
472 e->start = start;
473 e->size = size;
476 /* last page */
477 page = get_page(t1_end);
478 e2 = (BoundEntry *)((char *)page + t2_end);
479 for(e=page;e<e2;e++) {
480 e->start = start;
481 e->size = size;
483 add_region(e, start, size);
487 /* delete a region */
488 static inline void delete_region(BoundEntry *e,
489 void *p, unsigned long empty_size)
491 unsigned long addr;
492 BoundEntry *e1;
494 addr = (unsigned long)p;
495 addr -= e->start;
496 if (addr <= e->size) {
497 /* region found is first one */
498 e1 = e->next;
499 if (e1 == NULL) {
500 /* no more region: mark it empty */
501 e->start = 0;
502 e->size = empty_size;
503 } else {
504 /* copy next region in head */
505 e->start = e1->start;
506 e->size = e1->size;
507 e->next = e1->next;
508 bound_free_entry(e1);
510 } else {
511 /* find the matching region */
512 for(;;) {
513 e1 = e;
514 e = e->next;
515 /* region not found: do nothing */
516 if (e == NULL)
517 break;
518 addr = (unsigned long)p - e->start;
519 if (addr <= e->size) {
520 /* found: remove entry */
521 e1->next = e->next;
522 bound_free_entry(e);
523 break;
529 /* WARNING: 'p' must be the starting point of the region. */
530 /* return non zero if error */
531 int __bound_delete_region(void *p)
533 unsigned long start, end, addr, size, empty_size;
534 BoundEntry *page, *e, *e2;
535 int t1_start, t1_end, t2_start, t2_end, i;
537 start = (unsigned long)p;
538 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
539 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
540 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
542 /* find region size */
543 page = __bound_t1[t1_start];
544 e = (BoundEntry *)((char *)page + t2_start);
545 addr = start - e->start;
546 if (addr > e->size)
547 e = __bound_find_region(e, p);
548 /* test if invalid region */
549 if (e->size == EMPTY_SIZE || (unsigned long)p != e->start)
550 return -1;
551 /* compute the size we put in invalid regions */
552 if (e->is_invalid)
553 empty_size = INVALID_SIZE;
554 else
555 empty_size = EMPTY_SIZE;
556 size = e->size;
557 end = start + size;
559 /* now we can free each entry */
560 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
561 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
562 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
564 delete_region(e, p, empty_size);
565 if (t1_end == t1_start) {
566 /* same ending page */
567 e2 = (BoundEntry *)((char *)page + t2_end);
568 if (e2 > e) {
569 e++;
570 for(;e<e2;e++) {
571 e->start = 0;
572 e->size = empty_size;
574 delete_region(e, p, empty_size);
576 } else {
577 /* mark until end of page */
578 e2 = page + BOUND_T2_SIZE;
579 e++;
580 for(;e<e2;e++) {
581 e->start = 0;
582 e->size = empty_size;
584 /* mark intermediate pages, if any */
585 /* XXX: should free them */
586 for(i=t1_start+1;i<t1_end;i++) {
587 page = get_page(i);
588 e2 = page + BOUND_T2_SIZE;
589 for(e=page;e<e2;e++) {
590 e->start = 0;
591 e->size = empty_size;
594 /* last page */
595 page = get_page(t2_end);
596 e2 = (BoundEntry *)((char *)page + t2_end);
597 for(e=page;e<e2;e++) {
598 e->start = 0;
599 e->size = empty_size;
601 delete_region(e, p, empty_size);
603 return 0;
606 /* return the size of the region starting at p, or EMPTY_SIZE if non
607 existant region. */
608 static unsigned long get_region_size(void *p)
610 unsigned long addr = (unsigned long)p;
611 BoundEntry *e;
613 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
614 e = (BoundEntry *)((char *)e +
615 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
616 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
617 addr -= e->start;
618 if (addr > e->size)
619 e = __bound_find_region(e, p);
620 if (e->start != (unsigned long)p)
621 return EMPTY_SIZE;
622 return e->size;
625 /* patched memory functions */
627 /* force compiler to perform stores coded up to this point */
628 #define barrier() __asm__ __volatile__ ("": : : "memory")
630 static void install_malloc_hooks(void)
632 #ifdef CONFIG_TCC_MALLOC_HOOKS
633 saved_malloc_hook = __malloc_hook;
634 saved_free_hook = __free_hook;
635 saved_realloc_hook = __realloc_hook;
636 saved_memalign_hook = __memalign_hook;
637 __malloc_hook = __bound_malloc;
638 __free_hook = __bound_free;
639 __realloc_hook = __bound_realloc;
640 __memalign_hook = __bound_memalign;
642 barrier();
643 #endif
646 static void restore_malloc_hooks(void)
648 #ifdef CONFIG_TCC_MALLOC_HOOKS
649 __malloc_hook = saved_malloc_hook;
650 __free_hook = saved_free_hook;
651 __realloc_hook = saved_realloc_hook;
652 __memalign_hook = saved_memalign_hook;
654 barrier();
655 #endif
658 static void *libc_malloc(size_t size)
660 void *ptr;
661 restore_malloc_hooks();
662 ptr = malloc(size);
663 install_malloc_hooks();
664 return ptr;
667 static void libc_free(void *ptr)
669 restore_malloc_hooks();
670 free(ptr);
671 install_malloc_hooks();
674 /* XXX: we should use a malloc which ensure that it is unlikely that
675 two malloc'ed data have the same address if 'free' are made in
676 between. */
677 void *__bound_malloc(size_t size, const void *caller)
679 void *ptr;
681 /* we allocate one more byte to ensure the regions will be
682 separated by at least one byte. With the glibc malloc, it may
683 be in fact not necessary */
684 ptr = libc_malloc(size + 1);
686 if (!ptr)
687 return NULL;
688 __bound_new_region(ptr, size);
689 return ptr;
692 void *__bound_memalign(size_t size, size_t align, const void *caller)
694 void *ptr;
696 restore_malloc_hooks();
698 #ifndef HAVE_MEMALIGN
699 if (align > 4) {
700 /* XXX: handle it ? */
701 ptr = NULL;
702 } else {
703 /* we suppose that malloc aligns to at least four bytes */
704 ptr = malloc(size + 1);
706 #else
707 /* we allocate one more byte to ensure the regions will be
708 separated by at least one byte. With the glibc malloc, it may
709 be in fact not necessary */
710 ptr = memalign(size + 1, align);
711 #endif
713 install_malloc_hooks();
715 if (!ptr)
716 return NULL;
717 __bound_new_region(ptr, size);
718 return ptr;
721 void __bound_free(void *ptr, const void *caller)
723 if (ptr == NULL)
724 return;
725 if (__bound_delete_region(ptr) != 0)
726 bound_error("freeing invalid region");
728 libc_free(ptr);
731 void *__bound_realloc(void *ptr, size_t size, const void *caller)
733 void *ptr1;
734 int old_size;
736 if (size == 0) {
737 __bound_free(ptr, caller);
738 return NULL;
739 } else {
740 ptr1 = __bound_malloc(size, caller);
741 if (ptr == NULL || ptr1 == NULL)
742 return ptr1;
743 old_size = get_region_size(ptr);
744 if (old_size == EMPTY_SIZE)
745 bound_error("realloc'ing invalid pointer");
746 memcpy(ptr1, ptr, old_size);
747 __bound_free(ptr, caller);
748 return ptr1;
752 #ifndef CONFIG_TCC_MALLOC_HOOKS
753 void *__bound_calloc(size_t nmemb, size_t size)
755 void *ptr;
756 size = size * nmemb;
757 ptr = __bound_malloc(size, NULL);
758 if (!ptr)
759 return NULL;
760 memset(ptr, 0, size);
761 return ptr;
763 #endif
765 #if 0
766 static void bound_dump(void)
768 BoundEntry *page, *e;
769 int i, j;
771 printf("region dump:\n");
772 for(i=0;i<BOUND_T1_SIZE;i++) {
773 page = __bound_t1[i];
774 for(j=0;j<BOUND_T2_SIZE;j++) {
775 e = page + j;
776 /* do not print invalid or empty entries */
777 if (e->size != EMPTY_SIZE && e->start != 0) {
778 printf("%08x:",
779 (i << (BOUND_T2_BITS + BOUND_T3_BITS)) +
780 (j << BOUND_T3_BITS));
781 do {
782 printf(" %08lx:%08lx", e->start, e->start + e->size);
783 e = e->next;
784 } while (e != NULL);
785 printf("\n");
790 #endif
792 /* some useful checked functions */
794 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
795 static void __bound_check(const void *p, size_t size)
797 if (size == 0)
798 return;
799 p = __bound_ptr_add((void *)p, size);
800 if (p == INVALID_POINTER)
801 bound_error("invalid pointer");
804 void *__bound_memcpy(void *dst, const void *src, size_t size)
806 __bound_check(dst, size);
807 __bound_check(src, size);
808 /* check also region overlap */
809 if (src >= dst && src < dst + size)
810 bound_error("overlapping regions in memcpy()");
811 return memcpy(dst, src, size);
814 void *__bound_memmove(void *dst, const void *src, size_t size)
816 __bound_check(dst, size);
817 __bound_check(src, size);
818 return memmove(dst, src, size);
821 void *__bound_memset(void *dst, int c, size_t size)
823 __bound_check(dst, size);
824 return memset(dst, c, size);
827 /* XXX: could be optimized */
828 int __bound_strlen(const char *s)
830 const char *p;
831 int len;
833 len = 0;
834 for(;;) {
835 p = __bound_ptr_indir1((char *)s, len);
836 if (p == INVALID_POINTER)
837 bound_error("bad pointer in strlen()");
838 if (*p == '\0')
839 break;
840 len++;
842 return len;
845 char *__bound_strcpy(char *dst, const char *src)
847 int len;
848 len = __bound_strlen(src);
849 return __bound_memcpy(dst, src, len + 1);