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__)
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
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
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
{
70 struct BoundEntry
*next
;
71 unsigned long is_invalid
; /* true if pointers outside region are invalid */
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
;
97 /* linker definitions */
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
, ...);
109 static BoundEntry
*__bound_t1
[BOUND_T1_SIZE
]; /* page table */
111 static BoundEntry
**__bound_t1
; /* page table */
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
;
123 addr
= (unsigned long)p
;
125 if (addr
<= e
->size
) {
126 /* put region at the head */
128 e1
->start
= e
->start
;
137 /* no entry found: return empty entry or invalid entry */
139 return __bound_invalid_t2
;
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
;
162 #if defined(BOUND_DEBUG)
163 printf("add: 0x%x %d\n", (int)p
, offset
);
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
)));
171 if (addr
> e
->size
) {
172 e
= __bound_find_region(e
, p
);
173 addr
= (unsigned long)p
- e
->start
;
177 return INVALID_POINTER
; /* return an invalid pointer */
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; \
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))); \
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 */ \
212 /* return the frame pointer of the caller */
213 #define GET_CALLER_FP(fp)\
216 __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp1));\
220 #error put code to extract the calling frame pointer
223 /* called when entering a function to add all the local regions */
224 void FASTCALL
__bound_local_new(void *p1
)
226 unsigned long addr
, size
, fp
, *p
= p1
;
235 __bound_new_region((void *)addr
, size
);
239 /* called when leaving a function to delete all the local regions */
240 void FASTCALL
__bound_local_delete(void *p1
)
242 unsigned long addr
, fp
, *p
= p1
;
250 __bound_delete_region((void *)addr
);
254 static BoundEntry
*__bound_new_page(void)
259 page
= libc_malloc(sizeof(BoundEntry
) * BOUND_T2_SIZE
);
262 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
263 /* put empty entries */
265 page
[i
].size
= EMPTY_SIZE
;
267 page
[i
].is_invalid
= 0;
272 /* currently we use malloc(). Should use bound_new_page() */
273 static BoundEntry
*bound_new_entry(void)
276 e
= libc_malloc(sizeof(BoundEntry
));
280 static void bound_free_entry(BoundEntry
*e
)
285 static inline BoundEntry
*get_page(int index
)
288 page
= __bound_t1
[index
];
289 if (page
== __bound_empty_t2
|| page
== __bound_invalid_t2
) {
290 /* create a new page if necessary */
291 page
= __bound_new_page();
292 __bound_t1
[index
] = page
;
297 /* mark a region as being invalid (can only be used during init) */
298 static void mark_invalid(unsigned long addr
, unsigned long size
)
300 unsigned long start
, end
;
302 int t1_start
, t1_end
, i
, j
, t2_start
, t2_end
;
307 t2_start
= (start
+ BOUND_T3_SIZE
- 1) >> BOUND_T3_BITS
;
309 t2_end
= end
>> BOUND_T3_BITS
;
311 t2_end
= 1 << (BOUND_T1_BITS
+ BOUND_T2_BITS
);
314 printf("mark_invalid: start = %x %x\n", t2_start
, t2_end
);
317 /* first we handle full pages */
318 t1_start
= (t2_start
+ BOUND_T2_SIZE
- 1) >> BOUND_T2_BITS
;
319 t1_end
= t2_end
>> BOUND_T2_BITS
;
321 i
= t2_start
& (BOUND_T2_SIZE
- 1);
322 j
= t2_end
& (BOUND_T2_SIZE
- 1);
324 if (t1_start
== t1_end
) {
325 page
= get_page(t2_start
>> BOUND_T2_BITS
);
327 page
[i
].size
= INVALID_SIZE
;
328 page
[i
].is_invalid
= 1;
332 page
= get_page(t2_start
>> BOUND_T2_BITS
);
333 for(; i
< BOUND_T2_SIZE
; i
++) {
334 page
[i
].size
= INVALID_SIZE
;
335 page
[i
].is_invalid
= 1;
338 for(i
= t1_start
; i
< t1_end
; i
++) {
339 __bound_t1
[i
] = __bound_invalid_t2
;
342 page
= get_page(t1_end
);
343 for(i
= 0; i
< j
; i
++) {
344 page
[i
].size
= INVALID_SIZE
;
345 page
[i
].is_invalid
= 1;
351 void __bound_init(void)
355 unsigned long start
, size
;
358 /* save malloc hooks and install bound check hooks */
359 install_malloc_hooks();
362 __bound_t1
= libc_malloc(BOUND_T1_SIZE
* sizeof(BoundEntry
*));
366 __bound_empty_t2
= __bound_new_page();
367 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
368 __bound_t1
[i
] = __bound_empty_t2
;
371 page
= __bound_new_page();
372 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
373 /* put invalid entries */
375 page
[i
].size
= INVALID_SIZE
;
377 page
[i
].is_invalid
= 1;
379 __bound_invalid_t2
= page
;
381 /* invalid pointer zone */
382 start
= (unsigned long)INVALID_POINTER
& ~(BOUND_T23_SIZE
- 1);
383 size
= BOUND_T23_SIZE
;
384 mark_invalid(start
, size
);
386 #if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
387 /* malloc zone is also marked invalid. can only use that with
388 hooks because all libs should use the same malloc. The solution
389 would be to build a new malloc for tcc. */
390 start
= (unsigned long)&_end
;
391 size
= 128 * 0x100000;
392 mark_invalid(start
, size
);
395 /* add all static bound check values */
396 p
= (int *)&__bounds_start
;
398 __bound_new_region((void *)p
[0], p
[1]);
403 void __bound_exit(void)
405 restore_malloc_hooks();
408 static inline void add_region(BoundEntry
*e
,
409 unsigned long start
, unsigned long size
)
413 /* no region : add it */
417 /* already regions in the list: add it at the head */
418 e1
= bound_new_entry();
419 e1
->start
= e
->start
;
428 /* create a new region. It should not already exist in the region list */
429 void __bound_new_region(void *p
, unsigned long size
)
431 unsigned long start
, end
;
432 BoundEntry
*page
, *e
, *e2
;
433 int t1_start
, t1_end
, i
, t2_start
, t2_end
;
435 start
= (unsigned long)p
;
437 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
438 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
441 page
= get_page(t1_start
);
442 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
443 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
444 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
445 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
447 printf("new %lx %lx %x %x %x %x\n",
448 start
, end
, t1_start
, t1_end
, t2_start
, t2_end
);
451 e
= (BoundEntry
*)((char *)page
+ t2_start
);
452 add_region(e
, start
, size
);
454 if (t1_end
== t1_start
) {
455 /* same ending page */
456 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
463 add_region(e
, start
, size
);
466 /* mark until end of page */
467 e2
= page
+ BOUND_T2_SIZE
;
473 /* mark intermediate pages, if any */
474 for(i
=t1_start
+1;i
<t1_end
;i
++) {
476 e2
= page
+ BOUND_T2_SIZE
;
477 for(e
=page
;e
<e2
;e
++) {
483 page
= get_page(t1_end
);
484 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
485 for(e
=page
;e
<e2
;e
++) {
489 add_region(e
, start
, size
);
493 /* delete a region */
494 static inline void delete_region(BoundEntry
*e
,
495 void *p
, unsigned long empty_size
)
500 addr
= (unsigned long)p
;
502 if (addr
<= e
->size
) {
503 /* region found is first one */
506 /* no more region: mark it empty */
508 e
->size
= empty_size
;
510 /* copy next region in head */
511 e
->start
= e1
->start
;
514 bound_free_entry(e1
);
517 /* find the matching region */
521 /* region not found: do nothing */
524 addr
= (unsigned long)p
- e
->start
;
525 if (addr
<= e
->size
) {
526 /* found: remove entry */
535 /* WARNING: 'p' must be the starting point of the region. */
536 /* return non zero if error */
537 int __bound_delete_region(void *p
)
539 unsigned long start
, end
, addr
, size
, empty_size
;
540 BoundEntry
*page
, *e
, *e2
;
541 int t1_start
, t1_end
, t2_start
, t2_end
, i
;
543 start
= (unsigned long)p
;
544 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
545 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
546 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
548 /* find region size */
549 page
= __bound_t1
[t1_start
];
550 e
= (BoundEntry
*)((char *)page
+ t2_start
);
551 addr
= start
- e
->start
;
553 e
= __bound_find_region(e
, p
);
554 /* test if invalid region */
555 if (e
->size
== EMPTY_SIZE
|| (unsigned long)p
!= e
->start
)
557 /* compute the size we put in invalid regions */
559 empty_size
= INVALID_SIZE
;
561 empty_size
= EMPTY_SIZE
;
565 /* now we can free each entry */
566 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
567 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
568 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
570 delete_region(e
, p
, empty_size
);
571 if (t1_end
== t1_start
) {
572 /* same ending page */
573 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
578 e
->size
= empty_size
;
580 delete_region(e
, p
, empty_size
);
583 /* mark until end of page */
584 e2
= page
+ BOUND_T2_SIZE
;
588 e
->size
= empty_size
;
590 /* mark intermediate pages, if any */
591 /* XXX: should free them */
592 for(i
=t1_start
+1;i
<t1_end
;i
++) {
594 e2
= page
+ BOUND_T2_SIZE
;
595 for(e
=page
;e
<e2
;e
++) {
597 e
->size
= empty_size
;
601 page
= get_page(t2_end
);
602 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
603 for(e
=page
;e
<e2
;e
++) {
605 e
->size
= empty_size
;
607 delete_region(e
, p
, empty_size
);
612 /* return the size of the region starting at p, or EMPTY_SIZE if non
614 static unsigned long get_region_size(void *p
)
616 unsigned long addr
= (unsigned long)p
;
619 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
620 e
= (BoundEntry
*)((char *)e
+
621 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
622 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
625 e
= __bound_find_region(e
, p
);
626 if (e
->start
!= (unsigned long)p
)
631 /* patched memory functions */
633 static void install_malloc_hooks(void)
635 #ifdef CONFIG_TCC_MALLOC_HOOKS
636 saved_malloc_hook
= __malloc_hook
;
637 saved_free_hook
= __free_hook
;
638 saved_realloc_hook
= __realloc_hook
;
639 saved_memalign_hook
= __memalign_hook
;
640 __malloc_hook
= __bound_malloc
;
641 __free_hook
= __bound_free
;
642 __realloc_hook
= __bound_realloc
;
643 __memalign_hook
= __bound_memalign
;
647 static void restore_malloc_hooks(void)
649 #ifdef CONFIG_TCC_MALLOC_HOOKS
650 __malloc_hook
= saved_malloc_hook
;
651 __free_hook
= saved_free_hook
;
652 __realloc_hook
= saved_realloc_hook
;
653 __memalign_hook
= saved_memalign_hook
;
657 static void *libc_malloc(size_t size
)
660 restore_malloc_hooks();
662 install_malloc_hooks();
666 static void libc_free(void *ptr
)
668 restore_malloc_hooks();
670 install_malloc_hooks();
673 /* XXX: we should use a malloc which ensure that it is unlikely that
674 two malloc'ed data have the same address if 'free' are made in
676 void *__bound_malloc(size_t size
, const void *caller
)
680 /* we allocate one more byte to ensure the regions will be
681 separated by at least one byte. With the glibc malloc, it may
682 be in fact not necessary */
683 ptr
= libc_malloc(size
+ 1);
687 __bound_new_region(ptr
, size
);
691 void *__bound_memalign(size_t size
, size_t align
, const void *caller
)
695 restore_malloc_hooks();
697 #ifndef HAVE_MEMALIGN
699 /* XXX: handle it ? */
702 /* we suppose that malloc aligns to at least four bytes */
703 ptr
= malloc(size
+ 1);
706 /* we allocate one more byte to ensure the regions will be
707 separated by at least one byte. With the glibc malloc, it may
708 be in fact not necessary */
709 ptr
= memalign(size
+ 1, align
);
712 install_malloc_hooks();
716 __bound_new_region(ptr
, size
);
720 void __bound_free(void *ptr
, const void *caller
)
724 if (__bound_delete_region(ptr
) != 0)
725 bound_error("freeing invalid region");
730 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
)
736 __bound_free(ptr
, caller
);
739 ptr1
= __bound_malloc(size
, caller
);
740 if (ptr
== NULL
|| ptr1
== NULL
)
742 old_size
= get_region_size(ptr
);
743 if (old_size
== EMPTY_SIZE
)
744 bound_error("realloc'ing invalid pointer");
745 memcpy(ptr1
, ptr
, old_size
);
746 __bound_free(ptr
, caller
);
751 #ifndef CONFIG_TCC_MALLOC_HOOKS
752 void *__bound_calloc(size_t nmemb
, size_t size
)
756 ptr
= __bound_malloc(size
, NULL
);
759 memset(ptr
, 0, size
);
765 static void bound_dump(void)
767 BoundEntry
*page
, *e
;
770 printf("region dump:\n");
771 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
772 page
= __bound_t1
[i
];
773 for(j
=0;j
<BOUND_T2_SIZE
;j
++) {
775 /* do not print invalid or empty entries */
776 if (e
->size
!= EMPTY_SIZE
&& e
->start
!= 0) {
778 (i
<< (BOUND_T2_BITS
+ BOUND_T3_BITS
)) +
779 (j
<< BOUND_T3_BITS
));
781 printf(" %08lx:%08lx", e
->start
, e
->start
+ e
->size
);
791 /* some useful checked functions */
793 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
794 static void __bound_check(const void *p
, size_t size
)
798 p
= __bound_ptr_add((void *)p
, size
);
799 if (p
== INVALID_POINTER
)
800 bound_error("invalid pointer");
803 void *__bound_memcpy(void *dst
, const void *src
, size_t size
)
805 __bound_check(dst
, size
);
806 __bound_check(src
, size
);
807 /* check also region overlap */
808 if (src
>= dst
&& src
< dst
+ size
)
809 bound_error("overlapping regions in memcpy()");
810 return memcpy(dst
, src
, size
);
813 void *__bound_memmove(void *dst
, const void *src
, size_t size
)
815 __bound_check(dst
, size
);
816 __bound_check(src
, size
);
817 return memmove(dst
, src
, size
);
820 void *__bound_memset(void *dst
, int c
, size_t size
)
822 __bound_check(dst
, size
);
823 return memset(dst
, c
, size
);
826 /* XXX: could be optimized */
827 int __bound_strlen(const char *s
)
834 p
= __bound_ptr_indir1((char *)s
, len
);
835 if (p
== INVALID_POINTER
)
836 bound_error("bad pointer in strlen()");
844 char *__bound_strcpy(char *dst
, const char *src
)
847 len
= __bound_strlen(src
);
848 return __bound_memcpy(dst
, src
, len
+ 1);