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));\
219 #elif defined(__x86_64__)
220 /* TCC always creates %rbp frames also on x86_64, so use them. */
221 #define GET_CALLER_FP(fp)\
224 __asm__ __volatile__ ("movq %%rbp,%0" :"=g" (fp1));\
228 #error put code to extract the calling frame pointer
231 /* called when entering a function to add all the local regions */
232 void FASTCALL
__bound_local_new(void *p1
)
234 unsigned long addr
, size
, fp
, *p
= p1
;
243 __bound_new_region((void *)addr
, size
);
247 /* called when leaving a function to delete all the local regions */
248 void FASTCALL
__bound_local_delete(void *p1
)
250 unsigned long addr
, fp
, *p
= p1
;
258 __bound_delete_region((void *)addr
);
262 static BoundEntry
*__bound_new_page(void)
267 page
= libc_malloc(sizeof(BoundEntry
) * BOUND_T2_SIZE
);
270 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
271 /* put empty entries */
273 page
[i
].size
= EMPTY_SIZE
;
275 page
[i
].is_invalid
= 0;
280 /* currently we use malloc(). Should use bound_new_page() */
281 static BoundEntry
*bound_new_entry(void)
284 e
= libc_malloc(sizeof(BoundEntry
));
288 static void bound_free_entry(BoundEntry
*e
)
293 static inline BoundEntry
*get_page(int index
)
296 page
= __bound_t1
[index
];
297 if (page
== __bound_empty_t2
|| page
== __bound_invalid_t2
) {
298 /* create a new page if necessary */
299 page
= __bound_new_page();
300 __bound_t1
[index
] = page
;
305 /* mark a region as being invalid (can only be used during init) */
306 static void mark_invalid(unsigned long addr
, unsigned long size
)
308 unsigned long start
, end
;
310 int t1_start
, t1_end
, i
, j
, t2_start
, t2_end
;
315 t2_start
= (start
+ BOUND_T3_SIZE
- 1) >> BOUND_T3_BITS
;
317 t2_end
= end
>> BOUND_T3_BITS
;
319 t2_end
= 1 << (BOUND_T1_BITS
+ BOUND_T2_BITS
);
322 printf("mark_invalid: start = %x %x\n", t2_start
, t2_end
);
325 /* first we handle full pages */
326 t1_start
= (t2_start
+ BOUND_T2_SIZE
- 1) >> BOUND_T2_BITS
;
327 t1_end
= t2_end
>> BOUND_T2_BITS
;
329 i
= t2_start
& (BOUND_T2_SIZE
- 1);
330 j
= t2_end
& (BOUND_T2_SIZE
- 1);
332 if (t1_start
== t1_end
) {
333 page
= get_page(t2_start
>> BOUND_T2_BITS
);
335 page
[i
].size
= INVALID_SIZE
;
336 page
[i
].is_invalid
= 1;
340 page
= get_page(t2_start
>> BOUND_T2_BITS
);
341 for(; i
< BOUND_T2_SIZE
; i
++) {
342 page
[i
].size
= INVALID_SIZE
;
343 page
[i
].is_invalid
= 1;
346 for(i
= t1_start
; i
< t1_end
; i
++) {
347 __bound_t1
[i
] = __bound_invalid_t2
;
350 page
= get_page(t1_end
);
351 for(i
= 0; i
< j
; i
++) {
352 page
[i
].size
= INVALID_SIZE
;
353 page
[i
].is_invalid
= 1;
359 void __bound_init(void)
363 unsigned long start
, size
;
366 /* save malloc hooks and install bound check hooks */
367 install_malloc_hooks();
370 __bound_t1
= libc_malloc(BOUND_T1_SIZE
* sizeof(BoundEntry
*));
374 __bound_empty_t2
= __bound_new_page();
375 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
376 __bound_t1
[i
] = __bound_empty_t2
;
379 page
= __bound_new_page();
380 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
381 /* put invalid entries */
383 page
[i
].size
= INVALID_SIZE
;
385 page
[i
].is_invalid
= 1;
387 __bound_invalid_t2
= page
;
389 /* invalid pointer zone */
390 start
= (unsigned long)INVALID_POINTER
& ~(BOUND_T23_SIZE
- 1);
391 size
= BOUND_T23_SIZE
;
392 mark_invalid(start
, size
);
394 #if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
395 /* malloc zone is also marked invalid. can only use that with
396 hooks because all libs should use the same malloc. The solution
397 would be to build a new malloc for tcc. */
398 start
= (unsigned long)&_end
;
399 size
= 128 * 0x100000;
400 mark_invalid(start
, size
);
403 /* add all static bound check values */
404 p
= (int *)&__bounds_start
;
406 __bound_new_region((void *)p
[0], p
[1]);
411 void __bound_exit(void)
413 restore_malloc_hooks();
416 static inline void add_region(BoundEntry
*e
,
417 unsigned long start
, unsigned long size
)
421 /* no region : add it */
425 /* already regions in the list: add it at the head */
426 e1
= bound_new_entry();
427 e1
->start
= e
->start
;
436 /* create a new region. It should not already exist in the region list */
437 void __bound_new_region(void *p
, unsigned long size
)
439 unsigned long start
, end
;
440 BoundEntry
*page
, *e
, *e2
;
441 int t1_start
, t1_end
, i
, t2_start
, t2_end
;
443 start
= (unsigned long)p
;
445 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
446 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
449 page
= get_page(t1_start
);
450 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
451 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
452 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
453 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
455 printf("new %lx %lx %x %x %x %x\n",
456 start
, end
, t1_start
, t1_end
, t2_start
, t2_end
);
459 e
= (BoundEntry
*)((char *)page
+ t2_start
);
460 add_region(e
, start
, size
);
462 if (t1_end
== t1_start
) {
463 /* same ending page */
464 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
471 add_region(e
, start
, size
);
474 /* mark until end of page */
475 e2
= page
+ BOUND_T2_SIZE
;
481 /* mark intermediate pages, if any */
482 for(i
=t1_start
+1;i
<t1_end
;i
++) {
484 e2
= page
+ BOUND_T2_SIZE
;
485 for(e
=page
;e
<e2
;e
++) {
491 page
= get_page(t1_end
);
492 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
493 for(e
=page
;e
<e2
;e
++) {
497 add_region(e
, start
, size
);
501 /* delete a region */
502 static inline void delete_region(BoundEntry
*e
,
503 void *p
, unsigned long empty_size
)
508 addr
= (unsigned long)p
;
510 if (addr
<= e
->size
) {
511 /* region found is first one */
514 /* no more region: mark it empty */
516 e
->size
= empty_size
;
518 /* copy next region in head */
519 e
->start
= e1
->start
;
522 bound_free_entry(e1
);
525 /* find the matching region */
529 /* region not found: do nothing */
532 addr
= (unsigned long)p
- e
->start
;
533 if (addr
<= e
->size
) {
534 /* found: remove entry */
543 /* WARNING: 'p' must be the starting point of the region. */
544 /* return non zero if error */
545 int __bound_delete_region(void *p
)
547 unsigned long start
, end
, addr
, size
, empty_size
;
548 BoundEntry
*page
, *e
, *e2
;
549 int t1_start
, t1_end
, t2_start
, t2_end
, i
;
551 start
= (unsigned long)p
;
552 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
553 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
554 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
556 /* find region size */
557 page
= __bound_t1
[t1_start
];
558 e
= (BoundEntry
*)((char *)page
+ t2_start
);
559 addr
= start
- e
->start
;
561 e
= __bound_find_region(e
, p
);
562 /* test if invalid region */
563 if (e
->size
== EMPTY_SIZE
|| (unsigned long)p
!= e
->start
)
565 /* compute the size we put in invalid regions */
567 empty_size
= INVALID_SIZE
;
569 empty_size
= EMPTY_SIZE
;
573 /* now we can free each entry */
574 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
575 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
576 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
578 delete_region(e
, p
, empty_size
);
579 if (t1_end
== t1_start
) {
580 /* same ending page */
581 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
586 e
->size
= empty_size
;
588 delete_region(e
, p
, empty_size
);
591 /* mark until end of page */
592 e2
= page
+ BOUND_T2_SIZE
;
596 e
->size
= empty_size
;
598 /* mark intermediate pages, if any */
599 /* XXX: should free them */
600 for(i
=t1_start
+1;i
<t1_end
;i
++) {
602 e2
= page
+ BOUND_T2_SIZE
;
603 for(e
=page
;e
<e2
;e
++) {
605 e
->size
= empty_size
;
609 page
= get_page(t2_end
);
610 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
611 for(e
=page
;e
<e2
;e
++) {
613 e
->size
= empty_size
;
615 delete_region(e
, p
, empty_size
);
620 /* return the size of the region starting at p, or EMPTY_SIZE if non
622 static unsigned long get_region_size(void *p
)
624 unsigned long addr
= (unsigned long)p
;
627 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
628 e
= (BoundEntry
*)((char *)e
+
629 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
630 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
633 e
= __bound_find_region(e
, p
);
634 if (e
->start
!= (unsigned long)p
)
639 /* patched memory functions */
641 /* force compiler to perform stores coded up to this point */
642 #define barrier() __asm__ __volatile__ ("": : : "memory")
644 static void install_malloc_hooks(void)
646 #ifdef CONFIG_TCC_MALLOC_HOOKS
647 saved_malloc_hook
= __malloc_hook
;
648 saved_free_hook
= __free_hook
;
649 saved_realloc_hook
= __realloc_hook
;
650 saved_memalign_hook
= __memalign_hook
;
651 __malloc_hook
= __bound_malloc
;
652 __free_hook
= __bound_free
;
653 __realloc_hook
= __bound_realloc
;
654 __memalign_hook
= __bound_memalign
;
660 static void restore_malloc_hooks(void)
662 #ifdef CONFIG_TCC_MALLOC_HOOKS
663 __malloc_hook
= saved_malloc_hook
;
664 __free_hook
= saved_free_hook
;
665 __realloc_hook
= saved_realloc_hook
;
666 __memalign_hook
= saved_memalign_hook
;
672 static void *libc_malloc(size_t size
)
675 restore_malloc_hooks();
677 install_malloc_hooks();
681 static void libc_free(void *ptr
)
683 restore_malloc_hooks();
685 install_malloc_hooks();
688 /* XXX: we should use a malloc which ensure that it is unlikely that
689 two malloc'ed data have the same address if 'free' are made in
691 void *__bound_malloc(size_t size
, const void *caller
)
695 /* we allocate one more byte to ensure the regions will be
696 separated by at least one byte. With the glibc malloc, it may
697 be in fact not necessary */
698 ptr
= libc_malloc(size
+ 1);
702 __bound_new_region(ptr
, size
);
706 void *__bound_memalign(size_t size
, size_t align
, const void *caller
)
710 restore_malloc_hooks();
712 #ifndef HAVE_MEMALIGN
714 /* XXX: handle it ? */
717 /* we suppose that malloc aligns to at least four bytes */
718 ptr
= malloc(size
+ 1);
721 /* we allocate one more byte to ensure the regions will be
722 separated by at least one byte. With the glibc malloc, it may
723 be in fact not necessary */
724 ptr
= memalign(size
+ 1, align
);
727 install_malloc_hooks();
731 __bound_new_region(ptr
, size
);
735 void __bound_free(void *ptr
, const void *caller
)
739 if (__bound_delete_region(ptr
) != 0)
740 bound_error("freeing invalid region");
745 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
)
751 __bound_free(ptr
, caller
);
754 ptr1
= __bound_malloc(size
, caller
);
755 if (ptr
== NULL
|| ptr1
== NULL
)
757 old_size
= get_region_size(ptr
);
758 if (old_size
== EMPTY_SIZE
)
759 bound_error("realloc'ing invalid pointer");
760 memcpy(ptr1
, ptr
, old_size
);
761 __bound_free(ptr
, caller
);
766 #ifndef CONFIG_TCC_MALLOC_HOOKS
767 void *__bound_calloc(size_t nmemb
, size_t size
)
771 ptr
= __bound_malloc(size
, NULL
);
774 memset(ptr
, 0, size
);
780 static void bound_dump(void)
782 BoundEntry
*page
, *e
;
785 printf("region dump:\n");
786 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
787 page
= __bound_t1
[i
];
788 for(j
=0;j
<BOUND_T2_SIZE
;j
++) {
790 /* do not print invalid or empty entries */
791 if (e
->size
!= EMPTY_SIZE
&& e
->start
!= 0) {
793 (i
<< (BOUND_T2_BITS
+ BOUND_T3_BITS
)) +
794 (j
<< BOUND_T3_BITS
));
796 printf(" %08lx:%08lx", e
->start
, e
->start
+ e
->size
);
806 /* some useful checked functions */
808 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
809 static void __bound_check(const void *p
, size_t size
)
813 p
= __bound_ptr_add((void *)p
, size
);
814 if (p
== INVALID_POINTER
)
815 bound_error("invalid pointer");
818 void *__bound_memcpy(void *dst
, const void *src
, size_t size
)
820 __bound_check(dst
, size
);
821 __bound_check(src
, size
);
822 /* check also region overlap */
823 if (src
>= dst
&& src
< dst
+ size
)
824 bound_error("overlapping regions in memcpy()");
825 return memcpy(dst
, src
, size
);
828 void *__bound_memmove(void *dst
, const void *src
, size_t size
)
830 __bound_check(dst
, size
);
831 __bound_check(src
, size
);
832 return memmove(dst
, src
, size
);
835 void *__bound_memset(void *dst
, int c
, size_t size
)
837 __bound_check(dst
, size
);
838 return memset(dst
, c
, size
);
841 /* XXX: could be optimized */
842 int __bound_strlen(const char *s
)
849 p
= __bound_ptr_indir1((char *)s
, len
);
850 if (p
== INVALID_POINTER
)
851 bound_error("bad pointer in strlen()");
859 char *__bound_strcpy(char *dst
, const char *src
)
862 len
= __bound_strlen(src
);
863 return __bound_memcpy(dst
, src
, len
+ 1);