2 * Tiny C Memory and bounds checker
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
24 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) \
25 && !defined(__DragonFly__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
32 /* #define BOUND_DEBUG */
35 #define dprintf(a...) fprintf(a)
40 /* define so that bound array is static (faster, but use memory if
41 bound checking not used) */
42 /* #define BOUND_STATIC */
44 /* use malloc hooks. Currently the code cannot be reliable if no hooks */
45 #define CONFIG_TCC_MALLOC_HOOKS
48 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
49 || defined(__DragonFly__) || defined(__dietlibc__) \
50 || defined(__UCLIBC__) || defined(__OpenBSD__) || defined(__NetBSD__) \
51 || defined(_WIN32) || defined(TCC_UCLIBC) || defined(TCC_MUSL)
52 //#warning Bound checking does not support malloc (etc.) in this environment.
53 #undef CONFIG_TCC_MALLOC_HOOKS
57 #define BOUND_T1_BITS 13
58 #define BOUND_T2_BITS 11
59 #define BOUND_T3_BITS (sizeof(size_t)*8 - BOUND_T1_BITS - BOUND_T2_BITS)
60 #define BOUND_E_BITS (sizeof(size_t))
62 #define BOUND_T1_SIZE ((size_t)1 << BOUND_T1_BITS)
63 #define BOUND_T2_SIZE ((size_t)1 << BOUND_T2_BITS)
64 #define BOUND_T3_SIZE ((size_t)1 << BOUND_T3_BITS)
66 #define BOUND_T23_BITS (BOUND_T2_BITS + BOUND_T3_BITS)
67 #define BOUND_T23_SIZE ((size_t)1 << BOUND_T23_BITS)
70 /* this pointer is generated when bound check is incorrect */
71 #define INVALID_POINTER ((void *)(-2))
72 /* size of an empty region */
73 #define EMPTY_SIZE ((size_t)(-1))
74 /* size of an invalid region */
75 #define INVALID_SIZE 0
77 typedef struct BoundEntry
{
80 struct BoundEntry
*next
;
81 size_t is_invalid
; /* true if pointers outside region are invalid */
84 /* external interface */
85 void __bound_init(void);
86 void __bound_new_region(void *p
, size_t size
);
87 int __bound_delete_region(void *p
);
90 /* an __attribute__ macro is defined in the system headers */
93 #define FASTCALL __attribute__((regparm(3)))
95 void *__bound_malloc(size_t size
, const void *caller
);
96 void *__bound_memalign(size_t size
, size_t align
, const void *caller
);
97 void __bound_free(void *ptr
, const void *caller
);
98 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
);
99 static void *libc_malloc(size_t size
);
100 static void libc_free(void *ptr
);
101 static void install_malloc_hooks(void);
102 static void restore_malloc_hooks(void);
104 #ifdef CONFIG_TCC_MALLOC_HOOKS
105 static void *saved_malloc_hook
;
106 static void *saved_free_hook
;
107 static void *saved_realloc_hook
;
108 static void *saved_memalign_hook
;
111 /* TCC definitions */
112 extern char __bounds_start
; /* start of static bounds table */
113 /* error message, just for TCC */
114 const char *__bound_error_msg
;
116 /* runtime error output */
117 extern void rt_error(size_t pc
, const char *fmt
, ...);
120 static BoundEntry
*__bound_t1
[BOUND_T1_SIZE
]; /* page table */
122 static BoundEntry
**__bound_t1
; /* page table */
124 static BoundEntry
*__bound_empty_t2
; /* empty page, for unused pages */
125 static BoundEntry
*__bound_invalid_t2
; /* invalid page, for invalid pointers */
127 static BoundEntry
*__bound_find_region(BoundEntry
*e1
, void *p
)
136 if (addr
<= e
->size
) {
137 /* put region at the head */
139 e1
->start
= e
->start
;
148 /* no entry found: return empty entry or invalid entry */
150 return __bound_invalid_t2
;
152 return __bound_empty_t2
;
155 /* print a bound error message */
156 static void bound_error(const char *fmt
, ...)
158 __bound_error_msg
= fmt
;
159 fprintf(stderr
,"%s %s: %s\n", __FILE__
, __FUNCTION__
, fmt
);
160 *(void **)0 = 0; /* force a runtime error */
163 static void bound_alloc_error(void)
165 bound_error("not enough memory for bound checking code");
168 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
169 the end of a region in this case */
170 void * FASTCALL
__bound_ptr_add(void *p
, size_t offset
)
172 size_t addr
= (size_t)p
;
175 dprintf(stderr
, "%s %s: %p %x\n",
176 __FILE__
, __FUNCTION__
, p
, (unsigned)offset
);
180 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
181 e
= (BoundEntry
*)((char *)e
+
182 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
183 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
185 if (addr
> e
->size
) {
186 e
= __bound_find_region(e
, p
);
187 addr
= (size_t)p
- e
->start
;
190 if (addr
>= e
->size
) {
191 fprintf(stderr
,"%s %s: %p is outside of the region\n",
192 __FILE__
, __FUNCTION__
, p
+ offset
);
193 return INVALID_POINTER
; /* return an invalid pointer */
198 /* return '(p + offset)' for pointer indirection (the resulting must
199 be strictly inside the region */
200 #define BOUND_PTR_INDIR(dsize) \
201 void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \
203 size_t addr = (size_t)p; \
206 dprintf(stderr, "%s %s: %p %x start\n", \
207 __FILE__, __FUNCTION__, p, (unsigned)offset); \
210 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
211 e = (BoundEntry *)((char *)e + \
212 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
213 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
215 if (addr > e->size) { \
216 e = __bound_find_region(e, p); \
217 addr = (size_t)p - e->start; \
219 addr += offset + dsize; \
220 if (addr > e->size) { \
221 fprintf(stderr,"%s %s: %p is outside of the region\n", \
222 __FILE__, __FUNCTION__, p + offset); \
223 return INVALID_POINTER; /* return an invalid pointer */ \
225 dprintf(stderr, "%s %s: return p+offset = %p\n", \
226 __FILE__, __FUNCTION__, p + offset); \
237 #if defined(__GNUC__) && (__GNUC__ >= 6)
239 * At least gcc 6.2 complains when __builtin_frame_address is used whith
242 #pragma GCC diagnostic push
243 #pragma GCC diagnostic ignored "-Wframe-address"
246 /* return the frame pointer of the caller */
247 #define GET_CALLER_FP(fp)\
249 fp = (size_t)__builtin_frame_address(1);\
252 /* called when entering a function to add all the local regions */
253 void FASTCALL
__bound_local_new(void *p1
)
255 size_t addr
, size
, fp
, *p
= p1
;
257 dprintf(stderr
, "%s, %s start p1=%p\n", __FILE__
, __FUNCTION__
, p
);
266 __bound_new_region((void *)addr
, size
);
268 dprintf(stderr
, "%s, %s end\n", __FILE__
, __FUNCTION__
);
271 /* called when leaving a function to delete all the local regions */
272 void FASTCALL
__bound_local_delete(void *p1
)
274 size_t addr
, fp
, *p
= p1
;
282 __bound_delete_region((void *)addr
);
286 #if defined(__GNUC__) && (__GNUC__ >= 6)
287 #pragma GCC diagnostic pop
290 static BoundEntry
*__bound_new_page(void)
295 page
= libc_malloc(sizeof(BoundEntry
) * BOUND_T2_SIZE
);
298 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
299 /* put empty entries */
301 page
[i
].size
= EMPTY_SIZE
;
303 page
[i
].is_invalid
= 0;
308 /* currently we use malloc(). Should use bound_new_page() */
309 static BoundEntry
*bound_new_entry(void)
312 e
= libc_malloc(sizeof(BoundEntry
));
316 static void bound_free_entry(BoundEntry
*e
)
321 static BoundEntry
*get_page(size_t index
)
324 page
= __bound_t1
[index
];
325 if (!page
|| page
== __bound_empty_t2
|| page
== __bound_invalid_t2
) {
326 /* create a new page if necessary */
327 page
= __bound_new_page();
328 __bound_t1
[index
] = page
;
333 /* mark a region as being invalid (can only be used during init) */
334 static void mark_invalid(size_t addr
, size_t size
)
338 size_t t1_start
, t1_end
, i
, j
, t2_start
, t2_end
;
343 t2_start
= (start
+ BOUND_T3_SIZE
- 1) >> BOUND_T3_BITS
;
345 t2_end
= end
>> BOUND_T3_BITS
;
347 t2_end
= 1 << (BOUND_T1_BITS
+ BOUND_T2_BITS
);
350 dprintf(stderr
, "mark_invalid: start = %x %x\n", t2_start
, t2_end
);
353 /* first we handle full pages */
354 t1_start
= (t2_start
+ BOUND_T2_SIZE
- 1) >> BOUND_T2_BITS
;
355 t1_end
= t2_end
>> BOUND_T2_BITS
;
357 i
= t2_start
& (BOUND_T2_SIZE
- 1);
358 j
= t2_end
& (BOUND_T2_SIZE
- 1);
360 if (t1_start
== t1_end
) {
361 page
= get_page(t2_start
>> BOUND_T2_BITS
);
363 page
[i
].size
= INVALID_SIZE
;
364 page
[i
].is_invalid
= 1;
368 page
= get_page(t2_start
>> BOUND_T2_BITS
);
369 for(; i
< BOUND_T2_SIZE
; i
++) {
370 page
[i
].size
= INVALID_SIZE
;
371 page
[i
].is_invalid
= 1;
374 for(i
= t1_start
; i
< t1_end
; i
++) {
375 __bound_t1
[i
] = __bound_invalid_t2
;
378 page
= get_page(t1_end
);
379 for(i
= 0; i
< j
; i
++) {
380 page
[i
].size
= INVALID_SIZE
;
381 page
[i
].is_invalid
= 1;
387 void __bound_init(void)
400 dprintf(stderr
, "%s, %s() start\n", __FILE__
, __FUNCTION__
);
402 /* save malloc hooks and install bound check hooks */
403 install_malloc_hooks();
406 __bound_t1
= libc_malloc(BOUND_T1_SIZE
* sizeof(BoundEntry
*));
410 __bound_empty_t2
= __bound_new_page();
411 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
412 __bound_t1
[i
] = __bound_empty_t2
;
415 page
= __bound_new_page();
416 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
417 /* put invalid entries */
419 page
[i
].size
= INVALID_SIZE
;
421 page
[i
].is_invalid
= 1;
423 __bound_invalid_t2
= page
;
425 /* invalid pointer zone */
426 start
= (size_t)INVALID_POINTER
& ~(BOUND_T23_SIZE
- 1);
427 size
= BOUND_T23_SIZE
;
428 mark_invalid(start
, size
);
430 #if defined(CONFIG_TCC_MALLOC_HOOKS)
431 /* malloc zone is also marked invalid. can only use that with
432 * hooks because all libs should use the same malloc. The solution
433 * would be to build a new malloc for tcc.
435 * usually heap (= malloc zone) comes right after bss, i.e. after _end, but
436 * not always - either if we are running from under `tcc -b -run`, or if
437 * address space randomization is turned on(a), heap start will be separated
440 * So sbrk(0) will be a good approximation for start_brk:
442 * - if we are a separately compiled program, __bound_init() runs early,
443 * and sbrk(0) should be equal or very near to start_brk(b) (in case other
444 * constructors malloc something), or
446 * - if we are running from under `tcc -b -run`, sbrk(0) will return
447 * start of heap portion which is under this program control, and not
448 * mark as invalid earlier allocated memory.
451 * (a) /proc/sys/kernel/randomize_va_space = 2, on Linux;
452 * usually turned on by default.
454 * (b) on Linux >= v3.3, the alternative is to read
455 * start_brk from /proc/self/stat
457 start
= (size_t)sbrk(0);
458 size
= 128 * 0x100000;
459 mark_invalid(start
, size
);
462 /* add all static bound check values */
463 p
= (size_t *)&__bounds_start
;
465 __bound_new_region((void *)p
[0], p
[1]);
469 dprintf(stderr
, "%s, %s() end\n\n", __FILE__
, __FUNCTION__
);
472 void __bound_main_arg(void **p
)
477 dprintf(stderr
, "%s, %s calling __bound_new_region(%p %x)\n",
478 __FILE__
, __FUNCTION__
, start
, (unsigned)((void *)p
- start
));
480 __bound_new_region(start
, (void *) p
- start
);
483 void __bound_exit(void)
485 dprintf(stderr
, "%s, %s()\n", __FILE__
, __FUNCTION__
);
486 restore_malloc_hooks();
489 static inline void add_region(BoundEntry
*e
,
490 size_t start
, size_t size
)
494 /* no region : add it */
498 /* already regions in the list: add it at the head */
499 e1
= bound_new_entry();
500 e1
->start
= e
->start
;
509 /* create a new region. It should not already exist in the region list */
510 void __bound_new_region(void *p
, size_t size
)
513 BoundEntry
*page
, *e
, *e2
;
514 size_t t1_start
, t1_end
, i
, t2_start
, t2_end
;
516 dprintf(stderr
, "%s, %s(%p, %x) start\n",
517 __FILE__
, __FUNCTION__
, p
, (unsigned)size
);
523 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
524 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
527 page
= get_page(t1_start
);
528 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
529 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
530 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
531 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
534 e
= (BoundEntry
*)((char *)page
+ t2_start
);
535 add_region(e
, start
, size
);
537 if (t1_end
== t1_start
) {
538 /* same ending page */
539 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
546 add_region(e
, start
, size
);
549 /* mark until end of page */
550 e2
= page
+ BOUND_T2_SIZE
;
556 /* mark intermediate pages, if any */
557 for(i
=t1_start
+1;i
<t1_end
;i
++) {
559 e2
= page
+ BOUND_T2_SIZE
;
560 for(e
=page
;e
<e2
;e
++) {
566 page
= get_page(t1_end
);
567 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
568 for(e
=page
;e
<e2
;e
++) {
572 add_region(e
, start
, size
);
575 dprintf(stderr
, "%s, %s end\n", __FILE__
, __FUNCTION__
);
578 /* delete a region */
579 static inline void delete_region(BoundEntry
*e
, void *p
, size_t empty_size
)
586 if (addr
<= e
->size
) {
587 /* region found is first one */
590 /* no more region: mark it empty */
592 e
->size
= empty_size
;
594 /* copy next region in head */
595 e
->start
= e1
->start
;
598 bound_free_entry(e1
);
601 /* find the matching region */
605 /* region not found: do nothing */
608 addr
= (size_t)p
- e
->start
;
609 if (addr
<= e
->size
) {
610 /* found: remove entry */
619 /* WARNING: 'p' must be the starting point of the region. */
620 /* return non zero if error */
621 int __bound_delete_region(void *p
)
623 size_t start
, end
, addr
, size
, empty_size
;
624 BoundEntry
*page
, *e
, *e2
;
625 size_t t1_start
, t1_end
, t2_start
, t2_end
, i
;
627 dprintf(stderr
, "%s %s() start\n", __FILE__
, __FUNCTION__
);
632 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
633 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
634 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
636 /* find region size */
637 page
= __bound_t1
[t1_start
];
638 e
= (BoundEntry
*)((char *)page
+ t2_start
);
639 addr
= start
- e
->start
;
641 e
= __bound_find_region(e
, p
);
642 /* test if invalid region */
643 if (e
->size
== EMPTY_SIZE
|| (size_t)p
!= e
->start
)
645 /* compute the size we put in invalid regions */
647 empty_size
= INVALID_SIZE
;
649 empty_size
= EMPTY_SIZE
;
653 /* now we can free each entry */
654 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
655 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
656 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
658 delete_region(e
, p
, empty_size
);
659 if (t1_end
== t1_start
) {
660 /* same ending page */
661 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
666 e
->size
= empty_size
;
668 delete_region(e
, p
, empty_size
);
671 /* mark until end of page */
672 e2
= page
+ BOUND_T2_SIZE
;
676 e
->size
= empty_size
;
678 /* mark intermediate pages, if any */
679 /* XXX: should free them */
680 for(i
=t1_start
+1;i
<t1_end
;i
++) {
682 e2
= page
+ BOUND_T2_SIZE
;
683 for(e
=page
;e
<e2
;e
++) {
685 e
->size
= empty_size
;
689 page
= get_page(t1_end
);
690 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
691 for(e
=page
;e
<e2
;e
++) {
693 e
->size
= empty_size
;
695 delete_region(e
, p
, empty_size
);
698 dprintf(stderr
, "%s %s() end\n", __FILE__
, __FUNCTION__
);
703 /* return the size of the region starting at p, or EMPTY_SIZE if non
705 static size_t get_region_size(void *p
)
707 size_t addr
= (size_t)p
;
710 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
711 e
= (BoundEntry
*)((char *)e
+
712 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
713 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
716 e
= __bound_find_region(e
, p
);
717 if (e
->start
!= (size_t)p
)
722 /* patched memory functions */
724 /* force compiler to perform stores coded up to this point */
725 #define barrier() __asm__ __volatile__ ("": : : "memory")
727 static void install_malloc_hooks(void)
729 #ifdef CONFIG_TCC_MALLOC_HOOKS
730 saved_malloc_hook
= __malloc_hook
;
731 saved_free_hook
= __free_hook
;
732 saved_realloc_hook
= __realloc_hook
;
733 saved_memalign_hook
= __memalign_hook
;
734 __malloc_hook
= __bound_malloc
;
735 __free_hook
= __bound_free
;
736 __realloc_hook
= __bound_realloc
;
737 __memalign_hook
= __bound_memalign
;
743 static void restore_malloc_hooks(void)
745 #ifdef CONFIG_TCC_MALLOC_HOOKS
746 __malloc_hook
= saved_malloc_hook
;
747 __free_hook
= saved_free_hook
;
748 __realloc_hook
= saved_realloc_hook
;
749 __memalign_hook
= saved_memalign_hook
;
755 static void *libc_malloc(size_t size
)
758 restore_malloc_hooks();
760 install_malloc_hooks();
764 static void libc_free(void *ptr
)
766 restore_malloc_hooks();
768 install_malloc_hooks();
771 /* XXX: we should use a malloc which ensure that it is unlikely that
772 two malloc'ed data have the same address if 'free' are made in
774 void *__bound_malloc(size_t size
, const void *caller
)
778 /* we allocate one more byte to ensure the regions will be
779 separated by at least one byte. With the glibc malloc, it may
780 be in fact not necessary */
781 ptr
= libc_malloc(size
+ 1);
786 dprintf(stderr
, "%s, %s calling __bound_new_region(%p, %x)\n",
787 __FILE__
, __FUNCTION__
, ptr
, (unsigned)size
);
789 __bound_new_region(ptr
, size
);
793 void *__bound_memalign(size_t size
, size_t align
, const void *caller
)
797 restore_malloc_hooks();
799 #ifndef HAVE_MEMALIGN
801 /* XXX: handle it ? */
804 /* we suppose that malloc aligns to at least four bytes */
805 ptr
= malloc(size
+ 1);
808 /* we allocate one more byte to ensure the regions will be
809 separated by at least one byte. With the glibc malloc, it may
810 be in fact not necessary */
811 ptr
= memalign(size
+ 1, align
);
814 install_malloc_hooks();
819 dprintf(stderr
, "%s, %s calling __bound_new_region(%p, %x)\n",
820 __FILE__
, __FUNCTION__
, ptr
, (unsigned)size
);
822 __bound_new_region(ptr
, size
);
826 void __bound_free(void *ptr
, const void *caller
)
830 if (__bound_delete_region(ptr
) != 0)
831 bound_error("freeing invalid region");
836 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
)
842 __bound_free(ptr
, caller
);
845 ptr1
= __bound_malloc(size
, caller
);
846 if (ptr
== NULL
|| ptr1
== NULL
)
848 old_size
= get_region_size(ptr
);
849 if (old_size
== EMPTY_SIZE
)
850 bound_error("realloc'ing invalid pointer");
851 memcpy(ptr1
, ptr
, old_size
);
852 __bound_free(ptr
, caller
);
857 #ifndef CONFIG_TCC_MALLOC_HOOKS
858 void *__bound_calloc(size_t nmemb
, size_t size
)
862 ptr
= __bound_malloc(size
, NULL
);
865 memset(ptr
, 0, size
);
871 static void bound_dump(void)
873 BoundEntry
*page
, *e
;
876 fprintf(stderr
, "region dump:\n");
877 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
878 page
= __bound_t1
[i
];
879 for(j
=0;j
<BOUND_T2_SIZE
;j
++) {
881 /* do not print invalid or empty entries */
882 if (e
->size
!= EMPTY_SIZE
&& e
->start
!= 0) {
883 fprintf(stderr
, "%08x:",
884 (i
<< (BOUND_T2_BITS
+ BOUND_T3_BITS
)) +
885 (j
<< BOUND_T3_BITS
));
887 fprintf(stderr
, " %08lx:%08lx", e
->start
, e
->start
+ e
->size
);
890 fprintf(stderr
, "\n");
897 /* some useful checked functions */
899 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
900 static void __bound_check(const void *p
, size_t size
)
904 p
= __bound_ptr_add((void *)p
, size
- 1);
905 if (p
== INVALID_POINTER
)
906 bound_error("invalid pointer");
909 void *__bound_memcpy(void *dst
, const void *src
, size_t size
)
913 dprintf(stderr
, "%s %s: start, dst=%p src=%p size=%x\n",
914 __FILE__
, __FUNCTION__
, dst
, src
, (unsigned)size
);
916 __bound_check(dst
, size
);
917 __bound_check(src
, size
);
918 /* check also region overlap */
919 if (src
>= dst
&& src
< dst
+ size
)
920 bound_error("overlapping regions in memcpy()");
922 p
= memcpy(dst
, src
, size
);
924 dprintf(stderr
, "%s %s: end, p=%p\n", __FILE__
, __FUNCTION__
, p
);
928 void *__bound_memmove(void *dst
, const void *src
, size_t size
)
930 __bound_check(dst
, size
);
931 __bound_check(src
, size
);
932 return memmove(dst
, src
, size
);
935 void *__bound_memset(void *dst
, int c
, size_t size
)
937 __bound_check(dst
, size
);
938 return memset(dst
, c
, size
);
941 /* XXX: could be optimized */
942 int __bound_strlen(const char *s
)
949 p
= __bound_ptr_indir1((char *)s
, len
);
950 if (p
== INVALID_POINTER
)
951 bound_error("bad pointer in strlen()");
959 char *__bound_strcpy(char *dst
, const char *src
)
964 dprintf(stderr
, "%s %s: strcpy start, dst=%p src=%p\n",
965 __FILE__
, __FUNCTION__
, dst
, src
);
966 len
= __bound_strlen(src
);
967 p
= __bound_memcpy(dst
, src
, len
+ 1);
968 dprintf(stderr
, "%s %s: strcpy end, p = %p\n",
969 __FILE__
, __FUNCTION__
, p
);