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 */
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
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
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
{
74 struct BoundEntry
*next
;
75 unsigned long is_invalid
; /* true if pointers outside region are invalid */
78 /* external interface */
79 void __bound_init(void);
80 void __bound_new_region(void *p
, unsigned long 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
;
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(unsigned long pc
, const char *fmt
, ...);
110 static BoundEntry
*__bound_t1
[BOUND_T1_SIZE
]; /* page table */
112 static BoundEntry
**__bound_t1
; /* page table */
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 unsigned long addr
, tmp
;
124 addr
= (unsigned long)p
;
126 if (addr
<= e
->size
) {
127 /* put region at the head */
129 e1
->start
= e
->start
;
138 /* no entry found: return empty entry or invalid entry */
140 return __bound_invalid_t2
;
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
, int offset
)
161 unsigned long addr
= (unsigned long)p
;
163 #if defined(BOUND_DEBUG)
164 printf("add: 0x%x %d\n", (int)p
, offset
);
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
)));
172 if (addr
> e
->size
) {
173 e
= __bound_find_region(e
, p
);
174 addr
= (unsigned long)p
- e
->start
;
178 return INVALID_POINTER
; /* return an invalid pointer */
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 unsigned long addr = (unsigned long)p; \
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))); \
195 if (addr > e->size) { \
196 e = __bound_find_region(e, p); \
197 addr = (unsigned long)p - e->start; \
199 addr += offset + dsize; \
200 if (addr > e->size) \
201 return INVALID_POINTER; /* return an invalid pointer */ \
212 /* return the frame pointer of the caller */
213 #define GET_CALLER_FP(fp)\
215 fp = (unsigned long)__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 unsigned long addr
, size
, fp
, *p
= p1
;
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 unsigned long addr
, fp
, *p
= p1
;
245 __bound_delete_region((void *)addr
);
249 static BoundEntry
*__bound_new_page(void)
254 page
= libc_malloc(sizeof(BoundEntry
) * BOUND_T2_SIZE
);
257 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
258 /* put empty entries */
260 page
[i
].size
= EMPTY_SIZE
;
262 page
[i
].is_invalid
= 0;
267 /* currently we use malloc(). Should use bound_new_page() */
268 static BoundEntry
*bound_new_entry(void)
271 e
= libc_malloc(sizeof(BoundEntry
));
275 static void bound_free_entry(BoundEntry
*e
)
280 static inline BoundEntry
*get_page(int index
)
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
;
292 /* mark a region as being invalid (can only be used during init) */
293 static void mark_invalid(unsigned long addr
, unsigned long size
)
295 unsigned long start
, end
;
297 int t1_start
, t1_end
, i
, j
, t2_start
, t2_end
;
302 t2_start
= (start
+ BOUND_T3_SIZE
- 1) >> BOUND_T3_BITS
;
304 t2_end
= end
>> BOUND_T3_BITS
;
306 t2_end
= 1 << (BOUND_T1_BITS
+ BOUND_T2_BITS
);
309 printf("mark_invalid: start = %x %x\n", t2_start
, t2_end
);
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
);
322 page
[i
].size
= INVALID_SIZE
;
323 page
[i
].is_invalid
= 1;
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
;
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)
350 unsigned long start
, size
;
353 /* save malloc hooks and install bound check hooks */
354 install_malloc_hooks();
357 __bound_t1
= libc_malloc(BOUND_T1_SIZE
* sizeof(BoundEntry
*));
361 __bound_empty_t2
= __bound_new_page();
362 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
363 __bound_t1
[i
] = __bound_empty_t2
;
366 page
= __bound_new_page();
367 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
368 /* put invalid entries */
370 page
[i
].size
= INVALID_SIZE
;
372 page
[i
].is_invalid
= 1;
374 __bound_invalid_t2
= page
;
376 /* invalid pointer zone */
377 start
= (unsigned long)INVALID_POINTER
& ~(BOUND_T23_SIZE
- 1);
378 size
= BOUND_T23_SIZE
;
379 mark_invalid(start
, size
);
381 #if defined(CONFIG_TCC_MALLOC_HOOKS)
382 /* malloc zone is also marked invalid. can only use that with
383 * hooks because all libs should use the same malloc. The solution
384 * would be to build a new malloc for tcc.
386 * usually heap (= malloc zone) comes right after bss, i.e. after _end, but
387 * not always - either if we are running from under `tcc -b -run`, or if
388 * address space randomization is turned on(a), heap start will be separated
391 * So sbrk(0) will be a good approximation for start_brk:
393 * - if we are a separately compiled program, __bound_init() runs early,
394 * and sbrk(0) should be equal or very near to start_brk(b) (in case other
395 * constructors malloc something), or
397 * - if we are running from under `tcc -b -run`, sbrk(0) will return
398 * start of heap portion which is under this program control, and not
399 * mark as invalid earlier allocated memory.
402 * (a) /proc/sys/kernel/randomize_va_space = 2, on Linux;
403 * usually turned on by default.
405 * (b) on Linux >= v3.3, the alternative is to read
406 * start_brk from /proc/self/stat
408 start
= (unsigned long)sbrk(0);
409 size
= 128 * 0x100000;
410 mark_invalid(start
, size
);
413 /* add all static bound check values */
414 p
= (int *)&__bounds_start
;
416 __bound_new_region((void *)p
[0], p
[1]);
421 void __bound_main_arg(void **p
)
425 __bound_new_region(start
, (void *) p
- start
);
428 void __bound_exit(void)
430 restore_malloc_hooks();
433 static inline void add_region(BoundEntry
*e
,
434 unsigned long start
, unsigned long size
)
438 /* no region : add it */
442 /* already regions in the list: add it at the head */
443 e1
= bound_new_entry();
444 e1
->start
= e
->start
;
453 /* create a new region. It should not already exist in the region list */
454 void __bound_new_region(void *p
, unsigned long size
)
456 unsigned long start
, end
;
457 BoundEntry
*page
, *e
, *e2
;
458 int t1_start
, t1_end
, i
, t2_start
, t2_end
;
460 start
= (unsigned long)p
;
462 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
463 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
466 page
= get_page(t1_start
);
467 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
468 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
469 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
470 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
472 printf("new %lx %lx %x %x %x %x\n",
473 start
, end
, t1_start
, t1_end
, t2_start
, t2_end
);
476 e
= (BoundEntry
*)((char *)page
+ t2_start
);
477 add_region(e
, start
, size
);
479 if (t1_end
== t1_start
) {
480 /* same ending page */
481 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
488 add_region(e
, start
, size
);
491 /* mark until end of page */
492 e2
= page
+ BOUND_T2_SIZE
;
498 /* mark intermediate pages, if any */
499 for(i
=t1_start
+1;i
<t1_end
;i
++) {
501 e2
= page
+ BOUND_T2_SIZE
;
502 for(e
=page
;e
<e2
;e
++) {
508 page
= get_page(t1_end
);
509 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
510 for(e
=page
;e
<e2
;e
++) {
514 add_region(e
, start
, size
);
518 /* delete a region */
519 static inline void delete_region(BoundEntry
*e
,
520 void *p
, unsigned long empty_size
)
525 addr
= (unsigned long)p
;
527 if (addr
<= e
->size
) {
528 /* region found is first one */
531 /* no more region: mark it empty */
533 e
->size
= empty_size
;
535 /* copy next region in head */
536 e
->start
= e1
->start
;
539 bound_free_entry(e1
);
542 /* find the matching region */
546 /* region not found: do nothing */
549 addr
= (unsigned long)p
- e
->start
;
550 if (addr
<= e
->size
) {
551 /* found: remove entry */
560 /* WARNING: 'p' must be the starting point of the region. */
561 /* return non zero if error */
562 int __bound_delete_region(void *p
)
564 unsigned long start
, end
, addr
, size
, empty_size
;
565 BoundEntry
*page
, *e
, *e2
;
566 int t1_start
, t1_end
, t2_start
, t2_end
, i
;
568 start
= (unsigned long)p
;
569 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
570 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
571 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
573 /* find region size */
574 page
= __bound_t1
[t1_start
];
575 e
= (BoundEntry
*)((char *)page
+ t2_start
);
576 addr
= start
- e
->start
;
578 e
= __bound_find_region(e
, p
);
579 /* test if invalid region */
580 if (e
->size
== EMPTY_SIZE
|| (unsigned long)p
!= e
->start
)
582 /* compute the size we put in invalid regions */
584 empty_size
= INVALID_SIZE
;
586 empty_size
= EMPTY_SIZE
;
590 /* now we can free each entry */
591 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
592 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
593 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
595 delete_region(e
, p
, empty_size
);
596 if (t1_end
== t1_start
) {
597 /* same ending page */
598 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
603 e
->size
= empty_size
;
605 delete_region(e
, p
, empty_size
);
608 /* mark until end of page */
609 e2
= page
+ BOUND_T2_SIZE
;
613 e
->size
= empty_size
;
615 /* mark intermediate pages, if any */
616 /* XXX: should free them */
617 for(i
=t1_start
+1;i
<t1_end
;i
++) {
619 e2
= page
+ BOUND_T2_SIZE
;
620 for(e
=page
;e
<e2
;e
++) {
622 e
->size
= empty_size
;
626 page
= get_page(t1_end
);
627 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
628 for(e
=page
;e
<e2
;e
++) {
630 e
->size
= empty_size
;
632 delete_region(e
, p
, empty_size
);
637 /* return the size of the region starting at p, or EMPTY_SIZE if non
639 static unsigned long get_region_size(void *p
)
641 unsigned long addr
= (unsigned long)p
;
644 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
645 e
= (BoundEntry
*)((char *)e
+
646 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
647 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
650 e
= __bound_find_region(e
, p
);
651 if (e
->start
!= (unsigned long)p
)
656 /* patched memory functions */
658 /* force compiler to perform stores coded up to this point */
659 #define barrier() __asm__ __volatile__ ("": : : "memory")
661 static void install_malloc_hooks(void)
663 #ifdef CONFIG_TCC_MALLOC_HOOKS
664 saved_malloc_hook
= __malloc_hook
;
665 saved_free_hook
= __free_hook
;
666 saved_realloc_hook
= __realloc_hook
;
667 saved_memalign_hook
= __memalign_hook
;
668 __malloc_hook
= __bound_malloc
;
669 __free_hook
= __bound_free
;
670 __realloc_hook
= __bound_realloc
;
671 __memalign_hook
= __bound_memalign
;
677 static void restore_malloc_hooks(void)
679 #ifdef CONFIG_TCC_MALLOC_HOOKS
680 __malloc_hook
= saved_malloc_hook
;
681 __free_hook
= saved_free_hook
;
682 __realloc_hook
= saved_realloc_hook
;
683 __memalign_hook
= saved_memalign_hook
;
689 static void *libc_malloc(size_t size
)
692 restore_malloc_hooks();
694 install_malloc_hooks();
698 static void libc_free(void *ptr
)
700 restore_malloc_hooks();
702 install_malloc_hooks();
705 /* XXX: we should use a malloc which ensure that it is unlikely that
706 two malloc'ed data have the same address if 'free' are made in
708 void *__bound_malloc(size_t size
, const void *caller
)
712 /* we allocate one more byte to ensure the regions will be
713 separated by at least one byte. With the glibc malloc, it may
714 be in fact not necessary */
715 ptr
= libc_malloc(size
+ 1);
719 __bound_new_region(ptr
, size
);
723 void *__bound_memalign(size_t size
, size_t align
, const void *caller
)
727 restore_malloc_hooks();
729 #ifndef HAVE_MEMALIGN
731 /* XXX: handle it ? */
734 /* we suppose that malloc aligns to at least four bytes */
735 ptr
= malloc(size
+ 1);
738 /* we allocate one more byte to ensure the regions will be
739 separated by at least one byte. With the glibc malloc, it may
740 be in fact not necessary */
741 ptr
= memalign(size
+ 1, align
);
744 install_malloc_hooks();
748 __bound_new_region(ptr
, size
);
752 void __bound_free(void *ptr
, const void *caller
)
756 if (__bound_delete_region(ptr
) != 0)
757 bound_error("freeing invalid region");
762 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
)
768 __bound_free(ptr
, caller
);
771 ptr1
= __bound_malloc(size
, caller
);
772 if (ptr
== NULL
|| ptr1
== NULL
)
774 old_size
= get_region_size(ptr
);
775 if (old_size
== EMPTY_SIZE
)
776 bound_error("realloc'ing invalid pointer");
777 memcpy(ptr1
, ptr
, old_size
);
778 __bound_free(ptr
, caller
);
783 #ifndef CONFIG_TCC_MALLOC_HOOKS
784 void *__bound_calloc(size_t nmemb
, size_t size
)
788 ptr
= __bound_malloc(size
, NULL
);
791 memset(ptr
, 0, size
);
797 static void bound_dump(void)
799 BoundEntry
*page
, *e
;
802 printf("region dump:\n");
803 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
804 page
= __bound_t1
[i
];
805 for(j
=0;j
<BOUND_T2_SIZE
;j
++) {
807 /* do not print invalid or empty entries */
808 if (e
->size
!= EMPTY_SIZE
&& e
->start
!= 0) {
810 (i
<< (BOUND_T2_BITS
+ BOUND_T3_BITS
)) +
811 (j
<< BOUND_T3_BITS
));
813 printf(" %08lx:%08lx", e
->start
, e
->start
+ e
->size
);
823 /* some useful checked functions */
825 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
826 static void __bound_check(const void *p
, size_t size
)
830 p
= __bound_ptr_add((void *)p
, size
);
831 if (p
== INVALID_POINTER
)
832 bound_error("invalid pointer");
835 void *__bound_memcpy(void *dst
, const void *src
, size_t size
)
837 __bound_check(dst
, size
);
838 __bound_check(src
, size
);
839 /* check also region overlap */
840 if (src
>= dst
&& src
< dst
+ size
)
841 bound_error("overlapping regions in memcpy()");
842 return memcpy(dst
, src
, size
);
845 void *__bound_memmove(void *dst
, const void *src
, size_t size
)
847 __bound_check(dst
, size
);
848 __bound_check(src
, size
);
849 return memmove(dst
, src
, size
);
852 void *__bound_memset(void *dst
, int c
, size_t size
)
854 __bound_check(dst
, size
);
855 return memset(dst
, c
, size
);
858 /* XXX: could be optimized */
859 int __bound_strlen(const char *s
)
866 p
= __bound_ptr_indir1((char *)s
, len
);
867 if (p
== INVALID_POINTER
)
868 bound_error("bad pointer in strlen()");
876 char *__bound_strcpy(char *dst
, const char *src
)
879 len
= __bound_strlen(src
);
880 return __bound_memcpy(dst
, src
, len
+ 1);