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
25 #if !defined(__FreeBSD__) \
26 && !defined(__FreeBSD_kernel__) \
27 && !defined(__DragonFly__) \
28 && !defined(__OpenBSD__) \
29 && !defined(__NetBSD__)
37 /* #define BOUND_DEBUG */
40 #define dprintf(a...) fprintf(a)
45 /* define so that bound array is static (faster, but use memory if
46 bound checking not used) */
47 /* #define BOUND_STATIC */
49 /* use malloc hooks. Currently the code cannot be reliable if no hooks */
50 #define CONFIG_TCC_MALLOC_HOOKS
53 #if defined(__FreeBSD__) \
54 || defined(__FreeBSD_kernel__) \
55 || defined(__DragonFly__) \
56 || defined(__OpenBSD__) \
57 || defined(__NetBSD__) \
58 || defined(__dietlibc__) \
60 //#warning Bound checking does not support malloc (etc.) in this environment.
61 #undef CONFIG_TCC_MALLOC_HOOKS
65 #define BOUND_T1_BITS 13
66 #define BOUND_T2_BITS 11
67 #define BOUND_T3_BITS (sizeof(size_t)*8 - BOUND_T1_BITS - BOUND_T2_BITS)
68 #define BOUND_E_BITS (sizeof(size_t))
70 #define BOUND_T1_SIZE ((size_t)1 << BOUND_T1_BITS)
71 #define BOUND_T2_SIZE ((size_t)1 << BOUND_T2_BITS)
72 #define BOUND_T3_SIZE ((size_t)1 << BOUND_T3_BITS)
74 #define BOUND_T23_BITS (BOUND_T2_BITS + BOUND_T3_BITS)
75 #define BOUND_T23_SIZE ((size_t)1 << BOUND_T23_BITS)
78 /* this pointer is generated when bound check is incorrect */
79 #define INVALID_POINTER ((void *)(-2))
80 /* size of an empty region */
81 #define EMPTY_SIZE ((size_t)(-1))
82 /* size of an invalid region */
83 #define INVALID_SIZE 0
85 typedef struct BoundEntry
{
88 struct BoundEntry
*next
;
89 size_t is_invalid
; /* true if pointers outside region are invalid */
92 /* external interface */
93 void __bound_init(void);
94 void __bound_new_region(void *p
, size_t size
);
95 int __bound_delete_region(void *p
);
98 /* an __attribute__ macro is defined in the system headers */
101 #define FASTCALL __attribute__((regparm(3)))
103 void *__bound_malloc(size_t size
, const void *caller
);
104 void *__bound_memalign(size_t size
, size_t align
, const void *caller
);
105 void __bound_free(void *ptr
, const void *caller
);
106 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
);
107 static void *libc_malloc(size_t size
);
108 static void libc_free(void *ptr
);
109 static void install_malloc_hooks(void);
110 static void restore_malloc_hooks(void);
112 #ifdef CONFIG_TCC_MALLOC_HOOKS
113 static void *saved_malloc_hook
;
114 static void *saved_free_hook
;
115 static void *saved_realloc_hook
;
116 static void *saved_memalign_hook
;
119 /* TCC definitions */
120 extern char __bounds_start
; /* start of static bounds table */
121 /* error message, just for TCC */
122 const char *__bound_error_msg
;
124 /* runtime error output */
125 extern void rt_error(size_t pc
, const char *fmt
, ...);
128 static BoundEntry
*__bound_t1
[BOUND_T1_SIZE
]; /* page table */
130 static BoundEntry
**__bound_t1
; /* page table */
132 static BoundEntry
*__bound_empty_t2
; /* empty page, for unused pages */
133 static BoundEntry
*__bound_invalid_t2
; /* invalid page, for invalid pointers */
135 static BoundEntry
*__bound_find_region(BoundEntry
*e1
, void *p
)
144 if (addr
<= e
->size
) {
145 /* put region at the head */
147 e1
->start
= e
->start
;
156 /* no entry found: return empty entry or invalid entry */
158 return __bound_invalid_t2
;
160 return __bound_empty_t2
;
163 /* print a bound error message */
164 static void bound_error(const char *fmt
, ...)
166 __bound_error_msg
= fmt
;
167 fprintf(stderr
,"%s %s: %s\n", __FILE__
, __FUNCTION__
, fmt
);
168 *(void **)0 = 0; /* force a runtime error */
171 static void bound_alloc_error(void)
173 bound_error("not enough memory for bound checking code");
176 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
177 the end of a region in this case */
178 void * FASTCALL
__bound_ptr_add(void *p
, size_t offset
)
180 size_t addr
= (size_t)p
;
183 dprintf(stderr
, "%s %s: %p %x\n",
184 __FILE__
, __FUNCTION__
, p
, (unsigned)offset
);
188 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
189 e
= (BoundEntry
*)((char *)e
+
190 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
191 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
193 if (addr
> e
->size
) {
194 e
= __bound_find_region(e
, p
);
195 addr
= (size_t)p
- e
->start
;
198 if (addr
>= e
->size
) {
199 fprintf(stderr
,"%s %s: %p is outside of the region\n",
200 __FILE__
, __FUNCTION__
, p
+ offset
);
201 return INVALID_POINTER
; /* return an invalid pointer */
206 /* return '(p + offset)' for pointer indirection (the resulting must
207 be strictly inside the region */
208 #define BOUND_PTR_INDIR(dsize) \
209 void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \
211 size_t addr = (size_t)p; \
214 dprintf(stderr, "%s %s: %p %x start\n", \
215 __FILE__, __FUNCTION__, p, (unsigned)offset); \
218 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
219 e = (BoundEntry *)((char *)e + \
220 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
221 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
223 if (addr > e->size) { \
224 e = __bound_find_region(e, p); \
225 addr = (size_t)p - e->start; \
227 addr += offset + dsize; \
228 if (addr > e->size) { \
229 fprintf(stderr,"%s %s: %p is outside of the region\n", \
230 __FILE__, __FUNCTION__, p + offset); \
231 return INVALID_POINTER; /* return an invalid pointer */ \
233 dprintf(stderr, "%s %s: return p+offset = %p\n", \
234 __FILE__, __FUNCTION__, p + offset); \
245 #if defined(__GNUC__) && (__GNUC__ >= 6)
247 * At least gcc 6.2 complains when __builtin_frame_address is used with
250 #pragma GCC diagnostic push
251 #pragma GCC diagnostic ignored "-Wframe-address"
254 /* return the frame pointer of the caller */
255 #define GET_CALLER_FP(fp)\
257 fp = (size_t)__builtin_frame_address(1);\
260 /* called when entering a function to add all the local regions */
261 void FASTCALL
__bound_local_new(void *p1
)
263 size_t addr
, size
, fp
, *p
= p1
;
265 dprintf(stderr
, "%s, %s start p1=%p\n", __FILE__
, __FUNCTION__
, p
);
274 __bound_new_region((void *)addr
, size
);
276 dprintf(stderr
, "%s, %s end\n", __FILE__
, __FUNCTION__
);
279 /* called when leaving a function to delete all the local regions */
280 void FASTCALL
__bound_local_delete(void *p1
)
282 size_t addr
, fp
, *p
= p1
;
290 __bound_delete_region((void *)addr
);
294 #if defined(__GNUC__) && (__GNUC__ >= 6)
295 #pragma GCC diagnostic pop
298 static BoundEntry
*__bound_new_page(void)
303 page
= libc_malloc(sizeof(BoundEntry
) * BOUND_T2_SIZE
);
306 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
307 /* put empty entries */
309 page
[i
].size
= EMPTY_SIZE
;
311 page
[i
].is_invalid
= 0;
316 /* currently we use malloc(). Should use bound_new_page() */
317 static BoundEntry
*bound_new_entry(void)
320 e
= libc_malloc(sizeof(BoundEntry
));
324 static void bound_free_entry(BoundEntry
*e
)
329 static BoundEntry
*get_page(size_t index
)
332 page
= __bound_t1
[index
];
333 if (!page
|| page
== __bound_empty_t2
|| page
== __bound_invalid_t2
) {
334 /* create a new page if necessary */
335 page
= __bound_new_page();
336 __bound_t1
[index
] = page
;
341 /* mark a region as being invalid (can only be used during init) */
342 static void mark_invalid(size_t addr
, size_t size
)
346 size_t t1_start
, t1_end
, i
, j
, t2_start
, t2_end
;
351 t2_start
= (start
+ BOUND_T3_SIZE
- 1) >> BOUND_T3_BITS
;
353 t2_end
= end
>> BOUND_T3_BITS
;
355 t2_end
= 1 << (BOUND_T1_BITS
+ BOUND_T2_BITS
);
358 dprintf(stderr
, "mark_invalid: start = %x %x\n", t2_start
, t2_end
);
361 /* first we handle full pages */
362 t1_start
= (t2_start
+ BOUND_T2_SIZE
- 1) >> BOUND_T2_BITS
;
363 t1_end
= t2_end
>> BOUND_T2_BITS
;
365 i
= t2_start
& (BOUND_T2_SIZE
- 1);
366 j
= t2_end
& (BOUND_T2_SIZE
- 1);
368 if (t1_start
== t1_end
) {
369 page
= get_page(t2_start
>> BOUND_T2_BITS
);
371 page
[i
].size
= INVALID_SIZE
;
372 page
[i
].is_invalid
= 1;
376 page
= get_page(t2_start
>> BOUND_T2_BITS
);
377 for(; i
< BOUND_T2_SIZE
; i
++) {
378 page
[i
].size
= INVALID_SIZE
;
379 page
[i
].is_invalid
= 1;
382 for(i
= t1_start
; i
< t1_end
; i
++) {
383 __bound_t1
[i
] = __bound_invalid_t2
;
386 page
= get_page(t1_end
);
387 for(i
= 0; i
< j
; i
++) {
388 page
[i
].size
= INVALID_SIZE
;
389 page
[i
].is_invalid
= 1;
395 void __bound_init(void)
408 dprintf(stderr
, "%s, %s() start\n", __FILE__
, __FUNCTION__
);
410 /* save malloc hooks and install bound check hooks */
411 install_malloc_hooks();
414 __bound_t1
= libc_malloc(BOUND_T1_SIZE
* sizeof(BoundEntry
*));
418 __bound_empty_t2
= __bound_new_page();
419 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
420 __bound_t1
[i
] = __bound_empty_t2
;
423 page
= __bound_new_page();
424 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
425 /* put invalid entries */
427 page
[i
].size
= INVALID_SIZE
;
429 page
[i
].is_invalid
= 1;
431 __bound_invalid_t2
= page
;
433 /* invalid pointer zone */
434 start
= (size_t)INVALID_POINTER
& ~(BOUND_T23_SIZE
- 1);
435 size
= BOUND_T23_SIZE
;
436 mark_invalid(start
, size
);
438 #if defined(CONFIG_TCC_MALLOC_HOOKS)
439 /* malloc zone is also marked invalid. can only use that with
440 * hooks because all libs should use the same malloc. The solution
441 * would be to build a new malloc for tcc.
443 * usually heap (= malloc zone) comes right after bss, i.e. after _end, but
444 * not always - either if we are running from under `tcc -b -run`, or if
445 * address space randomization is turned on(a), heap start will be separated
448 * So sbrk(0) will be a good approximation for start_brk:
450 * - if we are a separately compiled program, __bound_init() runs early,
451 * and sbrk(0) should be equal or very near to start_brk(b) (in case other
452 * constructors malloc something), or
454 * - if we are running from under `tcc -b -run`, sbrk(0) will return
455 * start of heap portion which is under this program control, and not
456 * mark as invalid earlier allocated memory.
459 * (a) /proc/sys/kernel/randomize_va_space = 2, on Linux;
460 * usually turned on by default.
462 * (b) on Linux >= v3.3, the alternative is to read
463 * start_brk from /proc/self/stat
465 start
= (size_t)sbrk(0);
466 size
= 128 * 0x100000;
467 mark_invalid(start
, size
);
470 /* add all static bound check values */
471 p
= (size_t *)&__bounds_start
;
473 __bound_new_region((void *)p
[0], p
[1]);
477 dprintf(stderr
, "%s, %s() end\n\n", __FILE__
, __FUNCTION__
);
480 void __bound_main_arg(void **p
)
485 dprintf(stderr
, "%s, %s calling __bound_new_region(%p %x)\n",
486 __FILE__
, __FUNCTION__
, start
, (unsigned)((void *)p
- start
));
488 __bound_new_region(start
, (void *) p
- start
);
491 void __bound_exit(void)
493 dprintf(stderr
, "%s, %s()\n", __FILE__
, __FUNCTION__
);
494 restore_malloc_hooks();
497 static inline void add_region(BoundEntry
*e
,
498 size_t start
, size_t size
)
502 /* no region : add it */
506 /* already regions in the list: add it at the head */
507 e1
= bound_new_entry();
508 e1
->start
= e
->start
;
517 /* create a new region. It should not already exist in the region list */
518 void __bound_new_region(void *p
, size_t size
)
521 BoundEntry
*page
, *e
, *e2
;
522 size_t t1_start
, t1_end
, i
, t2_start
, t2_end
;
524 dprintf(stderr
, "%s, %s(%p, %x) start\n",
525 __FILE__
, __FUNCTION__
, p
, (unsigned)size
);
531 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
532 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
535 page
= get_page(t1_start
);
536 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
537 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
538 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
539 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
542 e
= (BoundEntry
*)((char *)page
+ t2_start
);
543 add_region(e
, start
, size
);
545 if (t1_end
== t1_start
) {
546 /* same ending page */
547 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
554 add_region(e
, start
, size
);
557 /* mark until end of page */
558 e2
= page
+ BOUND_T2_SIZE
;
564 /* mark intermediate pages, if any */
565 for(i
=t1_start
+1;i
<t1_end
;i
++) {
567 e2
= page
+ BOUND_T2_SIZE
;
568 for(e
=page
;e
<e2
;e
++) {
574 page
= get_page(t1_end
);
575 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
576 for(e
=page
;e
<e2
;e
++) {
580 add_region(e
, start
, size
);
583 dprintf(stderr
, "%s, %s end\n", __FILE__
, __FUNCTION__
);
586 /* delete a region */
587 static inline void delete_region(BoundEntry
*e
, void *p
, size_t empty_size
)
594 if (addr
<= e
->size
) {
595 /* region found is first one */
598 /* no more region: mark it empty */
600 e
->size
= empty_size
;
602 /* copy next region in head */
603 e
->start
= e1
->start
;
606 bound_free_entry(e1
);
609 /* find the matching region */
613 /* region not found: do nothing */
616 addr
= (size_t)p
- e
->start
;
617 if (addr
<= e
->size
) {
618 /* found: remove entry */
627 /* WARNING: 'p' must be the starting point of the region. */
628 /* return non zero if error */
629 int __bound_delete_region(void *p
)
631 size_t start
, end
, addr
, size
, empty_size
;
632 BoundEntry
*page
, *e
, *e2
;
633 size_t t1_start
, t1_end
, t2_start
, t2_end
, i
;
635 dprintf(stderr
, "%s %s() start\n", __FILE__
, __FUNCTION__
);
640 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
641 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
642 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
644 /* find region size */
645 page
= __bound_t1
[t1_start
];
646 e
= (BoundEntry
*)((char *)page
+ t2_start
);
647 addr
= start
- e
->start
;
649 e
= __bound_find_region(e
, p
);
650 /* test if invalid region */
651 if (e
->size
== EMPTY_SIZE
|| (size_t)p
!= e
->start
)
653 /* compute the size we put in invalid regions */
655 empty_size
= INVALID_SIZE
;
657 empty_size
= EMPTY_SIZE
;
661 /* now we can free each entry */
662 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
663 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
664 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
666 delete_region(e
, p
, empty_size
);
667 if (t1_end
== t1_start
) {
668 /* same ending page */
669 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
674 e
->size
= empty_size
;
676 delete_region(e
, p
, empty_size
);
679 /* mark until end of page */
680 e2
= page
+ BOUND_T2_SIZE
;
684 e
->size
= empty_size
;
686 /* mark intermediate pages, if any */
687 /* XXX: should free them */
688 for(i
=t1_start
+1;i
<t1_end
;i
++) {
690 e2
= page
+ BOUND_T2_SIZE
;
691 for(e
=page
;e
<e2
;e
++) {
693 e
->size
= empty_size
;
697 page
= get_page(t1_end
);
698 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
699 for(e
=page
;e
<e2
;e
++) {
701 e
->size
= empty_size
;
703 delete_region(e
, p
, empty_size
);
706 dprintf(stderr
, "%s %s() end\n", __FILE__
, __FUNCTION__
);
711 /* return the size of the region starting at p, or EMPTY_SIZE if non
713 static size_t get_region_size(void *p
)
715 size_t addr
= (size_t)p
;
718 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
719 e
= (BoundEntry
*)((char *)e
+
720 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
721 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
724 e
= __bound_find_region(e
, p
);
725 if (e
->start
!= (size_t)p
)
730 /* patched memory functions */
732 /* force compiler to perform stores coded up to this point */
733 #define barrier() __asm__ __volatile__ ("": : : "memory")
735 static void install_malloc_hooks(void)
737 #ifdef CONFIG_TCC_MALLOC_HOOKS
738 saved_malloc_hook
= __malloc_hook
;
739 saved_free_hook
= __free_hook
;
740 saved_realloc_hook
= __realloc_hook
;
741 saved_memalign_hook
= __memalign_hook
;
742 __malloc_hook
= __bound_malloc
;
743 __free_hook
= __bound_free
;
744 __realloc_hook
= __bound_realloc
;
745 __memalign_hook
= __bound_memalign
;
751 static void restore_malloc_hooks(void)
753 #ifdef CONFIG_TCC_MALLOC_HOOKS
754 __malloc_hook
= saved_malloc_hook
;
755 __free_hook
= saved_free_hook
;
756 __realloc_hook
= saved_realloc_hook
;
757 __memalign_hook
= saved_memalign_hook
;
763 static void *libc_malloc(size_t size
)
766 restore_malloc_hooks();
768 install_malloc_hooks();
772 static void libc_free(void *ptr
)
774 restore_malloc_hooks();
776 install_malloc_hooks();
779 /* XXX: we should use a malloc which ensure that it is unlikely that
780 two malloc'ed data have the same address if 'free' are made in
782 void *__bound_malloc(size_t size
, const void *caller
)
786 /* we allocate one more byte to ensure the regions will be
787 separated by at least one byte. With the glibc malloc, it may
788 be in fact not necessary */
789 ptr
= libc_malloc(size
+ 1);
794 dprintf(stderr
, "%s, %s calling __bound_new_region(%p, %x)\n",
795 __FILE__
, __FUNCTION__
, ptr
, (unsigned)size
);
797 __bound_new_region(ptr
, size
);
801 void *__bound_memalign(size_t size
, size_t align
, const void *caller
)
805 restore_malloc_hooks();
807 #ifndef HAVE_MEMALIGN
809 /* XXX: handle it ? */
812 /* we suppose that malloc aligns to at least four bytes */
813 ptr
= malloc(size
+ 1);
816 /* we allocate one more byte to ensure the regions will be
817 separated by at least one byte. With the glibc malloc, it may
818 be in fact not necessary */
819 ptr
= memalign(size
+ 1, align
);
822 install_malloc_hooks();
827 dprintf(stderr
, "%s, %s calling __bound_new_region(%p, %x)\n",
828 __FILE__
, __FUNCTION__
, ptr
, (unsigned)size
);
830 __bound_new_region(ptr
, size
);
834 void __bound_free(void *ptr
, const void *caller
)
838 if (__bound_delete_region(ptr
) != 0)
839 bound_error("freeing invalid region");
844 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
)
850 __bound_free(ptr
, caller
);
853 ptr1
= __bound_malloc(size
, caller
);
854 if (ptr
== NULL
|| ptr1
== NULL
)
856 old_size
= get_region_size(ptr
);
857 if (old_size
== EMPTY_SIZE
)
858 bound_error("realloc'ing invalid pointer");
859 memcpy(ptr1
, ptr
, old_size
);
860 __bound_free(ptr
, caller
);
865 #ifndef CONFIG_TCC_MALLOC_HOOKS
866 void *__bound_calloc(size_t nmemb
, size_t size
)
870 ptr
= __bound_malloc(size
, NULL
);
873 memset(ptr
, 0, size
);
879 static void bound_dump(void)
881 BoundEntry
*page
, *e
;
884 fprintf(stderr
, "region dump:\n");
885 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
886 page
= __bound_t1
[i
];
887 for(j
=0;j
<BOUND_T2_SIZE
;j
++) {
889 /* do not print invalid or empty entries */
890 if (e
->size
!= EMPTY_SIZE
&& e
->start
!= 0) {
891 fprintf(stderr
, "%08x:",
892 (i
<< (BOUND_T2_BITS
+ BOUND_T3_BITS
)) +
893 (j
<< BOUND_T3_BITS
));
895 fprintf(stderr
, " %08lx:%08lx", e
->start
, e
->start
+ e
->size
);
898 fprintf(stderr
, "\n");
905 /* some useful checked functions */
907 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
908 static void __bound_check(const void *p
, size_t size
)
912 p
= __bound_ptr_add((void *)p
, size
- 1);
913 if (p
== INVALID_POINTER
)
914 bound_error("invalid pointer");
917 void *__bound_memcpy(void *dst
, const void *src
, size_t size
)
921 dprintf(stderr
, "%s %s: start, dst=%p src=%p size=%x\n",
922 __FILE__
, __FUNCTION__
, dst
, src
, (unsigned)size
);
924 __bound_check(dst
, size
);
925 __bound_check(src
, size
);
926 /* check also region overlap */
927 if (src
>= dst
&& src
< dst
+ size
)
928 bound_error("overlapping regions in memcpy()");
930 p
= memcpy(dst
, src
, size
);
932 dprintf(stderr
, "%s %s: end, p=%p\n", __FILE__
, __FUNCTION__
, p
);
936 void *__bound_memmove(void *dst
, const void *src
, size_t size
)
938 __bound_check(dst
, size
);
939 __bound_check(src
, size
);
940 return memmove(dst
, src
, size
);
943 void *__bound_memset(void *dst
, int c
, size_t size
)
945 __bound_check(dst
, size
);
946 return memset(dst
, c
, size
);
949 /* XXX: could be optimized */
950 int __bound_strlen(const char *s
)
957 p
= __bound_ptr_indir1((char *)s
, len
);
958 if (p
== INVALID_POINTER
)
959 bound_error("bad pointer in strlen()");
967 char *__bound_strcpy(char *dst
, const char *src
)
972 dprintf(stderr
, "%s %s: strcpy start, dst=%p src=%p\n",
973 __FILE__
, __FUNCTION__
, dst
, src
);
974 len
= __bound_strlen(src
);
975 p
= __bound_memcpy(dst
, src
, len
+ 1);
976 dprintf(stderr
, "%s %s: strcpy end, p = %p\n",
977 __FILE__
, __FUNCTION__
, p
);