2 * Tiny C Memory and bounds checker
4 * Copyright (c) 2002 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 /* define so that bound array is static (faster, but use memory if
29 bound checking not used) */
30 //#define BOUND_STATIC
32 /* use malloc hooks. Currently the code cannot be reliable if no hooks */
33 #define CONFIG_TCC_MALLOC_HOOKS
35 #define BOUND_T1_BITS 13
36 #define BOUND_T2_BITS 11
37 #define BOUND_T3_BITS (32 - BOUND_T1_BITS - BOUND_T2_BITS)
39 #define BOUND_T1_SIZE (1 << BOUND_T1_BITS)
40 #define BOUND_T2_SIZE (1 << BOUND_T2_BITS)
41 #define BOUND_T3_SIZE (1 << BOUND_T3_BITS)
42 #define BOUND_E_BITS 4
44 #define BOUND_T23_BITS (BOUND_T2_BITS + BOUND_T3_BITS)
45 #define BOUND_T23_SIZE (1 << BOUND_T23_BITS)
48 /* this pointer is generated when bound check is incorrect */
49 #define INVALID_POINTER ((void *)(-2))
50 /* size of an empty region */
51 #define EMPTY_SIZE 0xffffffff
52 /* size of an invalid region */
53 #define INVALID_SIZE 0
55 typedef struct BoundEntry
{
58 struct BoundEntry
*next
;
59 unsigned long is_invalid
; /* true if pointers outside region are invalid */
62 /* external interface */
63 void __bound_init(void);
64 void __bound_new_region(void *p
, unsigned long size
);
65 int __bound_delete_region(void *p
);
67 /* currently, tcc cannot compile that because we use unsupported GNU C
69 #if !defined(__TINYC__)
70 void *__bound_ptr_add(void *p
, int offset
) __attribute__((regparm(2)));
71 void *__bound_ptr_indir1(void *p
, int offset
) __attribute__((regparm(2)));
72 void *__bound_ptr_indir2(void *p
, int offset
) __attribute__((regparm(2)));
73 void *__bound_ptr_indir4(void *p
, int offset
) __attribute__((regparm(2)));
74 void *__bound_ptr_indir8(void *p
, int offset
) __attribute__((regparm(2)));
75 void *__bound_ptr_indir12(void *p
, int offset
) __attribute__((regparm(2)));
76 void *__bound_ptr_indir16(void *p
, int offset
) __attribute__((regparm(2)));
77 void __bound_local_new(void *p
) __attribute__((regparm(1)));
78 void __bound_local_delete(void *p
) __attribute__((regparm(1)));
80 static void *get_caller_pc(int n
);
82 void *__bound_malloc(size_t size
, const void *caller
);
83 void *__bound_memalign(size_t size
, size_t align
, const void *caller
);
84 void __bound_free(void *ptr
, const void *caller
);
85 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
);
86 static void *libc_malloc(size_t size
);
87 static void libc_free(void *ptr
);
88 static void install_malloc_hooks(void);
89 static void restore_malloc_hooks(void);
91 #ifdef CONFIG_TCC_MALLOC_HOOKS
92 static void *saved_malloc_hook
;
93 static void *saved_free_hook
;
94 static void *saved_realloc_hook
;
95 static void *saved_memalign_hook
;
98 /* linker definitions */
101 /* TCC definitions */
102 extern char __bounds_start
; /* start of static bounds table */
103 /* error function. if NULL, simply do abort() */
104 void (*__bound_error_func
)(unsigned long caller
, const char *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 void *caller
, const char *fmt
, ...)
148 if (!__bound_error_func
)
150 __bound_error_func((unsigned long)caller
, fmt
);
153 static void bound_alloc_error(void)
155 bound_error(NULL
, "not enough memory for bound checking code");
158 /* currently, tcc cannot compile that because we use GNUC extensions */
159 #if !defined(__TINYC__)
161 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
162 the end of a region in this case */
163 void *__bound_ptr_add(void *p
, int offset
)
165 unsigned long addr
= (unsigned long)p
;
167 #if defined(BOUND_DEBUG)
168 printf("add: 0x%x %d\n", (int)p
, offset
);
171 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
172 e
= (BoundEntry
*)((char *)e
+
173 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
174 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
176 if (addr
> e
->size
) {
177 e
= __bound_find_region(e
, p
);
178 addr
= (unsigned long)p
- e
->start
;
182 return INVALID_POINTER
; /* return an invalid pointer */
186 /* return '(p + offset)' for pointer indirection (the resulting must
187 be strictly inside the region */
188 #define BOUND_PTR_INDIR(dsize) \
189 void *__bound_ptr_indir ## dsize (void *p, int offset) \
191 unsigned long addr = (unsigned long)p; \
194 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
195 e = (BoundEntry *)((char *)e + \
196 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
197 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
199 if (addr > e->size) { \
200 e = __bound_find_region(e, p); \
201 addr = (unsigned long)p - e->start; \
203 addr += offset + dsize; \
204 if (addr > e->size) \
205 return INVALID_POINTER; /* return an invalid pointer */ \
211 /* return the PC of the N'th caller (N=1: first caller) */
212 static void *get_caller_pc(int n
)
217 __asm__
__volatile__ ("movl %%ebp,%0" :"=g" (fp
));
219 fp
= ((unsigned long *)fp
)[0];
220 return ((void **)fp
)[1];
223 /* return the frame pointer of the caller */
224 #define GET_CALLER_FP(fp)\
227 __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp1));\
231 #error put code to extract the calling frame pointer
234 /* called when entering a function to add all the local regions */
235 void __bound_local_new(void *p1
)
237 unsigned long addr
, size
, fp
, *p
= p1
;
246 __bound_new_region((void *)addr
, size
);
250 /* called when leaving a function to delete all the local regions */
251 void __bound_local_delete(void *p1
)
253 unsigned long addr
, fp
, *p
= p1
;
261 __bound_delete_region((void *)addr
);
267 void __bound_local_new(void *p
)
270 void __bound_local_delete(void *p
)
274 void *__bound_ptr_add(void *p
, int offset
)
279 #define BOUND_PTR_INDIR(dsize) \
280 void *__bound_ptr_indir ## dsize (void *p, int offset) \
285 static void *get_caller_pc(int n
)
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 inline BoundEntry
*get_page(int index
)
332 page
= __bound_t1
[index
];
333 if (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(unsigned long addr
, unsigned long size
)
344 unsigned long start
, end
;
346 int 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 printf("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)
399 unsigned long start
, size
;
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
= (unsigned long)INVALID_POINTER
& ~(BOUND_T23_SIZE
- 1);
427 size
= BOUND_T23_SIZE
;
428 mark_invalid(start
, size
);
430 #if !defined(__TINYC__) && 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. */
434 start
= (unsigned long)&_end
;
435 size
= 128 * 0x100000;
436 mark_invalid(start
, size
);
439 /* add all static bound check values */
440 p
= (int *)&__bounds_start
;
442 __bound_new_region((void *)p
[0], p
[1]);
447 static inline void add_region(BoundEntry
*e
,
448 unsigned long start
, unsigned long size
)
452 /* no region : add it */
456 /* already regions in the list: add it at the head */
457 e1
= bound_new_entry();
458 e1
->start
= e
->start
;
467 /* create a new region. It should not already exist in the region list */
468 void __bound_new_region(void *p
, unsigned long size
)
470 unsigned long start
, end
;
471 BoundEntry
*page
, *e
, *e2
;
472 int t1_start
, t1_end
, i
, t2_start
, t2_end
;
474 start
= (unsigned long)p
;
476 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
477 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
480 page
= get_page(t1_start
);
481 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
482 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
483 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
484 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
486 printf("new %lx %lx %x %x %x %x\n",
487 start
, end
, t1_start
, t1_end
, t2_start
, t2_end
);
490 e
= (BoundEntry
*)((char *)page
+ t2_start
);
491 add_region(e
, start
, size
);
493 if (t1_end
== t1_start
) {
494 /* same ending page */
495 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
502 add_region(e
, start
, size
);
505 /* mark until end of page */
506 e2
= page
+ BOUND_T2_SIZE
;
512 /* mark intermediate pages, if any */
513 for(i
=t1_start
+1;i
<t1_end
;i
++) {
515 e2
= page
+ BOUND_T2_SIZE
;
516 for(e
=page
;e
<e2
;e
++) {
522 page
= get_page(t1_end
);
523 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
524 for(e
=page
;e
<e2
;e
++) {
528 add_region(e
, start
, size
);
532 /* delete a region */
533 static inline void delete_region(BoundEntry
*e
,
534 void *p
, unsigned long empty_size
)
539 addr
= (unsigned long)p
;
541 if (addr
<= e
->size
) {
542 /* region found is first one */
545 /* no more region: mark it empty */
547 e
->size
= empty_size
;
549 /* copy next region in head */
550 e
->start
= e1
->start
;
553 bound_free_entry(e1
);
556 /* find the matching region */
560 /* region not found: do nothing */
563 addr
= (unsigned long)p
- e
->start
;
564 if (addr
<= e
->size
) {
565 /* found: remove entry */
574 /* WARNING: 'p' must be the starting point of the region. */
575 /* return non zero if error */
576 int __bound_delete_region(void *p
)
578 unsigned long start
, end
, addr
, size
, empty_size
;
579 BoundEntry
*page
, *e
, *e2
;
580 int t1_start
, t1_end
, t2_start
, t2_end
, i
;
582 start
= (unsigned long)p
;
583 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
584 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
585 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
587 /* find region size */
588 page
= __bound_t1
[t1_start
];
589 e
= (BoundEntry
*)((char *)page
+ t2_start
);
590 addr
= start
- e
->start
;
592 e
= __bound_find_region(e
, p
);
593 /* test if invalid region */
594 if (e
->size
== EMPTY_SIZE
|| (unsigned long)p
!= e
->start
)
596 /* compute the size we put in invalid regions */
598 empty_size
= INVALID_SIZE
;
600 empty_size
= EMPTY_SIZE
;
604 /* now we can free each entry */
605 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
606 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
607 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
609 delete_region(e
, p
, empty_size
);
610 if (t1_end
== t1_start
) {
611 /* same ending page */
612 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
617 e
->size
= empty_size
;
619 delete_region(e
, p
, empty_size
);
622 /* mark until end of page */
623 e2
= page
+ BOUND_T2_SIZE
;
627 e
->size
= empty_size
;
629 /* mark intermediate pages, if any */
630 /* XXX: should free them */
631 for(i
=t1_start
+1;i
<t1_end
;i
++) {
633 e2
= page
+ BOUND_T2_SIZE
;
634 for(e
=page
;e
<e2
;e
++) {
636 e
->size
= empty_size
;
640 page
= get_page(t2_end
);
641 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
642 for(e
=page
;e
<e2
;e
++) {
644 e
->size
= empty_size
;
646 delete_region(e
, p
, empty_size
);
651 /* return the size of the region starting at p, or EMPTY_SIZE if non
653 static unsigned long get_region_size(void *p
)
655 unsigned long addr
= (unsigned long)p
;
658 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
659 e
= (BoundEntry
*)((char *)e
+
660 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
661 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
664 e
= __bound_find_region(e
, p
);
665 if (e
->start
!= (unsigned long)p
)
670 /* patched memory functions */
672 static void install_malloc_hooks(void)
674 #ifdef CONFIG_TCC_MALLOC_HOOKS
675 saved_malloc_hook
= __malloc_hook
;
676 saved_free_hook
= __free_hook
;
677 saved_realloc_hook
= __realloc_hook
;
678 saved_memalign_hook
= __memalign_hook
;
679 __malloc_hook
= __bound_malloc
;
680 __free_hook
= __bound_free
;
681 __realloc_hook
= __bound_realloc
;
682 __memalign_hook
= __bound_memalign
;
686 static void restore_malloc_hooks(void)
688 #ifdef CONFIG_TCC_MALLOC_HOOKS
689 __malloc_hook
= saved_malloc_hook
;
690 __free_hook
= saved_free_hook
;
691 __realloc_hook
= saved_realloc_hook
;
692 __memalign_hook
= saved_memalign_hook
;
696 static void *libc_malloc(size_t size
)
699 restore_malloc_hooks();
701 install_malloc_hooks();
705 static void libc_free(void *ptr
)
707 restore_malloc_hooks();
709 install_malloc_hooks();
712 /* XXX: we should use a malloc which ensure that it is unlikely that
713 two malloc'ed data have the same address if 'free' are made in
715 void *__bound_malloc(size_t size
, const void *caller
)
719 /* we allocate one more byte to ensure the regions will be
720 separated by at least one byte. With the glibc malloc, it may
721 be in fact not necessary */
722 ptr
= libc_malloc(size
+ 1);
726 __bound_new_region(ptr
, size
);
730 void *__bound_memalign(size_t size
, size_t align
, const void *caller
)
734 restore_malloc_hooks();
736 /* we allocate one more byte to ensure the regions will be
737 separated by at least one byte. With the glibc malloc, it may
738 be in fact not necessary */
739 ptr
= memalign(size
+ 1, align
);
741 install_malloc_hooks();
745 __bound_new_region(ptr
, size
);
749 void __bound_free(void *ptr
, const void *caller
)
753 if (__bound_delete_region(ptr
) != 0)
754 bound_error(caller
, "freeing invalid region");
759 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
)
765 __bound_free(ptr
, caller
);
768 ptr1
= __bound_malloc(size
, caller
);
769 if (ptr
== NULL
|| ptr1
== NULL
)
771 old_size
= get_region_size(ptr
);
772 if (old_size
== EMPTY_SIZE
)
773 bound_error(caller
, "realloc'ing invalid pointer");
774 memcpy(ptr1
, ptr
, old_size
);
775 __bound_free(ptr
, caller
);
780 #ifndef CONFIG_TCC_MALLOC_HOOKS
781 void *__bound_calloc(size_t nmemb
, size_t size
)
785 ptr
= __bound_malloc(size
);
788 memset(ptr
, 0, size
);
794 static void bound_dump(void)
796 BoundEntry
*page
, *e
;
799 printf("region dump:\n");
800 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
801 page
= __bound_t1
[i
];
802 for(j
=0;j
<BOUND_T2_SIZE
;j
++) {
804 /* do not print invalid or empty entries */
805 if (e
->size
!= EMPTY_SIZE
&& e
->start
!= 0) {
807 (i
<< (BOUND_T2_BITS
+ BOUND_T3_BITS
)) +
808 (j
<< BOUND_T3_BITS
));
810 printf(" %08lx:%08lx", e
->start
, e
->start
+ e
->size
);
820 /* some useful checked functions */
822 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
823 static void __bound_check(const void *p
, size_t size
)
827 p
= __bound_ptr_add((void *)p
, size
);
828 if (p
== INVALID_POINTER
)
829 bound_error(get_caller_pc(2), "invalid pointer");
832 void *__bound_memcpy(void *dst
, const void *src
, size_t size
)
834 __bound_check(dst
, size
);
835 __bound_check(src
, size
);
836 /* check also region overlap */
837 if (src
>= dst
&& src
< dst
+ size
)
838 bound_error(get_caller_pc(1), "overlapping regions in memcpy()");
839 return memcpy(dst
, src
, size
);
842 void *__bound_memmove(void *dst
, const void *src
, size_t size
)
844 __bound_check(dst
, size
);
845 __bound_check(src
, size
);
846 return memmove(dst
, src
, size
);
849 void *__bound_memset(void *dst
, int c
, size_t size
)
851 __bound_check(dst
, size
);
852 return memset(dst
, c
, size
);
855 /* XXX: could be optimized */
856 int __bound_strlen(const char *s
)
863 p
= __bound_ptr_indir1((char *)s
, len
);
864 if (p
== INVALID_POINTER
)
865 bound_error(get_caller_pc(1), "bad pointer in strlen()");
873 char *__bound_strcpy(char *dst
, const char *src
)
876 len
= __bound_strlen(src
);
877 return __bound_memcpy(dst
, src
, len
+ 1);