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.
30 /* define so that bound array is static (faster, but use memory if
31 bound checking not used) */
32 //#define BOUND_STATIC
34 /* use malloc hooks. Currently the code cannot be reliable if no hooks */
35 #define CONFIG_TCC_MALLOC_HOOKS
40 #warning Bound checking not fully supported on FreeBSD
41 #undef CONFIG_TCC_MALLOC_HOOKS
45 #define BOUND_T1_BITS 13
46 #define BOUND_T2_BITS 11
47 #define BOUND_T3_BITS (32 - BOUND_T1_BITS - BOUND_T2_BITS)
49 #define BOUND_T1_SIZE (1 << BOUND_T1_BITS)
50 #define BOUND_T2_SIZE (1 << BOUND_T2_BITS)
51 #define BOUND_T3_SIZE (1 << BOUND_T3_BITS)
52 #define BOUND_E_BITS 4
54 #define BOUND_T23_BITS (BOUND_T2_BITS + BOUND_T3_BITS)
55 #define BOUND_T23_SIZE (1 << BOUND_T23_BITS)
58 /* this pointer is generated when bound check is incorrect */
59 #define INVALID_POINTER ((void *)(-2))
60 /* size of an empty region */
61 #define EMPTY_SIZE 0xffffffff
62 /* size of an invalid region */
63 #define INVALID_SIZE 0
65 typedef struct BoundEntry
{
68 struct BoundEntry
*next
;
69 unsigned long is_invalid
; /* true if pointers outside region are invalid */
72 /* external interface */
73 void __bound_init(void);
74 void __bound_new_region(void *p
, unsigned long size
);
75 int __bound_delete_region(void *p
);
77 /* currently, tcc cannot compile that because we use unsupported GNU C
79 #if !defined(__TINYC__)
80 void *__bound_ptr_add(void *p
, int offset
) __attribute__((regparm(2)));
81 void *__bound_ptr_indir1(void *p
, int offset
) __attribute__((regparm(2)));
82 void *__bound_ptr_indir2(void *p
, int offset
) __attribute__((regparm(2)));
83 void *__bound_ptr_indir4(void *p
, int offset
) __attribute__((regparm(2)));
84 void *__bound_ptr_indir8(void *p
, int offset
) __attribute__((regparm(2)));
85 void *__bound_ptr_indir12(void *p
, int offset
) __attribute__((regparm(2)));
86 void *__bound_ptr_indir16(void *p
, int offset
) __attribute__((regparm(2)));
87 void __bound_local_new(void *p
) __attribute__((regparm(1)));
88 void __bound_local_delete(void *p
) __attribute__((regparm(1)));
91 void *__bound_malloc(size_t size
, const void *caller
);
92 void *__bound_memalign(size_t size
, size_t align
, const void *caller
);
93 void __bound_free(void *ptr
, const void *caller
);
94 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
);
95 static void *libc_malloc(size_t size
);
96 static void libc_free(void *ptr
);
97 static void install_malloc_hooks(void);
98 static void restore_malloc_hooks(void);
100 #ifdef CONFIG_TCC_MALLOC_HOOKS
101 static void *saved_malloc_hook
;
102 static void *saved_free_hook
;
103 static void *saved_realloc_hook
;
104 static void *saved_memalign_hook
;
107 /* linker definitions */
110 /* TCC definitions */
111 extern char __bounds_start
; /* start of static bounds table */
112 /* error message, just for TCC */
113 const char *__bound_error_msg
;
115 /* runtime error output */
116 extern void rt_error(unsigned long pc
, const char *fmt
, ...);
119 static BoundEntry
*__bound_t1
[BOUND_T1_SIZE
]; /* page table */
121 static BoundEntry
**__bound_t1
; /* page table */
123 static BoundEntry
*__bound_empty_t2
; /* empty page, for unused pages */
124 static BoundEntry
*__bound_invalid_t2
; /* invalid page, for invalid pointers */
126 static BoundEntry
*__bound_find_region(BoundEntry
*e1
, void *p
)
128 unsigned long addr
, tmp
;
133 addr
= (unsigned long)p
;
135 if (addr
<= e
->size
) {
136 /* put region at the head */
138 e1
->start
= e
->start
;
147 /* no entry found: return empty entry or invalid entry */
149 return __bound_invalid_t2
;
151 return __bound_empty_t2
;
154 /* print a bound error message */
155 static void bound_error(const char *fmt
, ...)
157 __bound_error_msg
= fmt
;
158 *(int *)0 = 0; /* force a runtime error */
161 static void bound_alloc_error(void)
163 bound_error("not enough memory for bound checking code");
166 /* currently, tcc cannot compile that because we use GNUC extensions */
167 #if !defined(__TINYC__)
169 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
170 the end of a region in this case */
171 void *__bound_ptr_add(void *p
, int offset
)
173 unsigned long addr
= (unsigned long)p
;
175 #if defined(BOUND_DEBUG)
176 printf("add: 0x%x %d\n", (int)p
, offset
);
179 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
180 e
= (BoundEntry
*)((char *)e
+
181 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
182 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
184 if (addr
> e
->size
) {
185 e
= __bound_find_region(e
, p
);
186 addr
= (unsigned long)p
- e
->start
;
190 return INVALID_POINTER
; /* return an invalid pointer */
194 /* return '(p + offset)' for pointer indirection (the resulting must
195 be strictly inside the region */
196 #define BOUND_PTR_INDIR(dsize) \
197 void *__bound_ptr_indir ## dsize (void *p, int offset) \
199 unsigned long addr = (unsigned long)p; \
202 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
203 e = (BoundEntry *)((char *)e + \
204 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
205 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
207 if (addr > e->size) { \
208 e = __bound_find_region(e, p); \
209 addr = (unsigned long)p - e->start; \
211 addr += offset + dsize; \
212 if (addr > e->size) \
213 return INVALID_POINTER; /* return an invalid pointer */ \
218 /* return the frame pointer of the caller */
219 #define GET_CALLER_FP(fp)\
222 __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp1));\
226 #error put code to extract the calling frame pointer
229 /* called when entering a function to add all the local regions */
230 void __bound_local_new(void *p1
)
232 unsigned long addr
, size
, fp
, *p
= p1
;
241 __bound_new_region((void *)addr
, size
);
245 /* called when leaving a function to delete all the local regions */
246 void __bound_local_delete(void *p1
)
248 unsigned long addr
, fp
, *p
= p1
;
256 __bound_delete_region((void *)addr
);
262 void __bound_local_new(void *p
)
265 void __bound_local_delete(void *p
)
269 void *__bound_ptr_add(void *p
, int offset
)
274 #define BOUND_PTR_INDIR(dsize) \
275 void *__bound_ptr_indir ## dsize (void *p, int offset) \
288 static BoundEntry
*__bound_new_page(void)
293 page
= libc_malloc(sizeof(BoundEntry
) * BOUND_T2_SIZE
);
296 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
297 /* put empty entries */
299 page
[i
].size
= EMPTY_SIZE
;
301 page
[i
].is_invalid
= 0;
306 /* currently we use malloc(). Should use bound_new_page() */
307 static BoundEntry
*bound_new_entry(void)
310 e
= libc_malloc(sizeof(BoundEntry
));
314 static void bound_free_entry(BoundEntry
*e
)
319 static inline BoundEntry
*get_page(int index
)
322 page
= __bound_t1
[index
];
323 if (page
== __bound_empty_t2
|| page
== __bound_invalid_t2
) {
324 /* create a new page if necessary */
325 page
= __bound_new_page();
326 __bound_t1
[index
] = page
;
331 /* mark a region as being invalid (can only be used during init) */
332 static void mark_invalid(unsigned long addr
, unsigned long size
)
334 unsigned long start
, end
;
336 int t1_start
, t1_end
, i
, j
, t2_start
, t2_end
;
341 t2_start
= (start
+ BOUND_T3_SIZE
- 1) >> BOUND_T3_BITS
;
343 t2_end
= end
>> BOUND_T3_BITS
;
345 t2_end
= 1 << (BOUND_T1_BITS
+ BOUND_T2_BITS
);
348 printf("mark_invalid: start = %x %x\n", t2_start
, t2_end
);
351 /* first we handle full pages */
352 t1_start
= (t2_start
+ BOUND_T2_SIZE
- 1) >> BOUND_T2_BITS
;
353 t1_end
= t2_end
>> BOUND_T2_BITS
;
355 i
= t2_start
& (BOUND_T2_SIZE
- 1);
356 j
= t2_end
& (BOUND_T2_SIZE
- 1);
358 if (t1_start
== t1_end
) {
359 page
= get_page(t2_start
>> BOUND_T2_BITS
);
361 page
[i
].size
= INVALID_SIZE
;
362 page
[i
].is_invalid
= 1;
366 page
= get_page(t2_start
>> BOUND_T2_BITS
);
367 for(; i
< BOUND_T2_SIZE
; i
++) {
368 page
[i
].size
= INVALID_SIZE
;
369 page
[i
].is_invalid
= 1;
372 for(i
= t1_start
; i
< t1_end
; i
++) {
373 __bound_t1
[i
] = __bound_invalid_t2
;
376 page
= get_page(t1_end
);
377 for(i
= 0; i
< j
; i
++) {
378 page
[i
].size
= INVALID_SIZE
;
379 page
[i
].is_invalid
= 1;
385 void __bound_init(void)
389 unsigned long start
, size
;
392 /* save malloc hooks and install bound check hooks */
393 install_malloc_hooks();
396 __bound_t1
= libc_malloc(BOUND_T1_SIZE
* sizeof(BoundEntry
*));
400 __bound_empty_t2
= __bound_new_page();
401 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
402 __bound_t1
[i
] = __bound_empty_t2
;
405 page
= __bound_new_page();
406 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
407 /* put invalid entries */
409 page
[i
].size
= INVALID_SIZE
;
411 page
[i
].is_invalid
= 1;
413 __bound_invalid_t2
= page
;
415 /* invalid pointer zone */
416 start
= (unsigned long)INVALID_POINTER
& ~(BOUND_T23_SIZE
- 1);
417 size
= BOUND_T23_SIZE
;
418 mark_invalid(start
, size
);
420 #if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
421 /* malloc zone is also marked invalid. can only use that with
422 hooks because all libs should use the same malloc. The solution
423 would be to build a new malloc for tcc. */
424 start
= (unsigned long)&_end
;
425 size
= 128 * 0x100000;
426 mark_invalid(start
, size
);
429 /* add all static bound check values */
430 p
= (int *)&__bounds_start
;
432 __bound_new_region((void *)p
[0], p
[1]);
437 static inline void add_region(BoundEntry
*e
,
438 unsigned long start
, unsigned long size
)
442 /* no region : add it */
446 /* already regions in the list: add it at the head */
447 e1
= bound_new_entry();
448 e1
->start
= e
->start
;
457 /* create a new region. It should not already exist in the region list */
458 void __bound_new_region(void *p
, unsigned long size
)
460 unsigned long start
, end
;
461 BoundEntry
*page
, *e
, *e2
;
462 int t1_start
, t1_end
, i
, t2_start
, t2_end
;
464 start
= (unsigned long)p
;
466 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
467 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
470 page
= get_page(t1_start
);
471 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
472 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
473 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
474 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
476 printf("new %lx %lx %x %x %x %x\n",
477 start
, end
, t1_start
, t1_end
, t2_start
, t2_end
);
480 e
= (BoundEntry
*)((char *)page
+ t2_start
);
481 add_region(e
, start
, size
);
483 if (t1_end
== t1_start
) {
484 /* same ending page */
485 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
492 add_region(e
, start
, size
);
495 /* mark until end of page */
496 e2
= page
+ BOUND_T2_SIZE
;
502 /* mark intermediate pages, if any */
503 for(i
=t1_start
+1;i
<t1_end
;i
++) {
505 e2
= page
+ BOUND_T2_SIZE
;
506 for(e
=page
;e
<e2
;e
++) {
512 page
= get_page(t1_end
);
513 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
514 for(e
=page
;e
<e2
;e
++) {
518 add_region(e
, start
, size
);
522 /* delete a region */
523 static inline void delete_region(BoundEntry
*e
,
524 void *p
, unsigned long empty_size
)
529 addr
= (unsigned long)p
;
531 if (addr
<= e
->size
) {
532 /* region found is first one */
535 /* no more region: mark it empty */
537 e
->size
= empty_size
;
539 /* copy next region in head */
540 e
->start
= e1
->start
;
543 bound_free_entry(e1
);
546 /* find the matching region */
550 /* region not found: do nothing */
553 addr
= (unsigned long)p
- e
->start
;
554 if (addr
<= e
->size
) {
555 /* found: remove entry */
564 /* WARNING: 'p' must be the starting point of the region. */
565 /* return non zero if error */
566 int __bound_delete_region(void *p
)
568 unsigned long start
, end
, addr
, size
, empty_size
;
569 BoundEntry
*page
, *e
, *e2
;
570 int t1_start
, t1_end
, t2_start
, t2_end
, i
;
572 start
= (unsigned long)p
;
573 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
574 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
575 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
577 /* find region size */
578 page
= __bound_t1
[t1_start
];
579 e
= (BoundEntry
*)((char *)page
+ t2_start
);
580 addr
= start
- e
->start
;
582 e
= __bound_find_region(e
, p
);
583 /* test if invalid region */
584 if (e
->size
== EMPTY_SIZE
|| (unsigned long)p
!= e
->start
)
586 /* compute the size we put in invalid regions */
588 empty_size
= INVALID_SIZE
;
590 empty_size
= EMPTY_SIZE
;
594 /* now we can free each entry */
595 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
596 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
597 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
599 delete_region(e
, p
, empty_size
);
600 if (t1_end
== t1_start
) {
601 /* same ending page */
602 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
607 e
->size
= empty_size
;
609 delete_region(e
, p
, empty_size
);
612 /* mark until end of page */
613 e2
= page
+ BOUND_T2_SIZE
;
617 e
->size
= empty_size
;
619 /* mark intermediate pages, if any */
620 /* XXX: should free them */
621 for(i
=t1_start
+1;i
<t1_end
;i
++) {
623 e2
= page
+ BOUND_T2_SIZE
;
624 for(e
=page
;e
<e2
;e
++) {
626 e
->size
= empty_size
;
630 page
= get_page(t2_end
);
631 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
632 for(e
=page
;e
<e2
;e
++) {
634 e
->size
= empty_size
;
636 delete_region(e
, p
, empty_size
);
641 /* return the size of the region starting at p, or EMPTY_SIZE if non
643 static unsigned long get_region_size(void *p
)
645 unsigned long addr
= (unsigned long)p
;
648 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
649 e
= (BoundEntry
*)((char *)e
+
650 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
651 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
654 e
= __bound_find_region(e
, p
);
655 if (e
->start
!= (unsigned long)p
)
660 /* patched memory functions */
662 static void install_malloc_hooks(void)
664 #ifdef CONFIG_TCC_MALLOC_HOOKS
665 saved_malloc_hook
= __malloc_hook
;
666 saved_free_hook
= __free_hook
;
667 saved_realloc_hook
= __realloc_hook
;
668 saved_memalign_hook
= __memalign_hook
;
669 __malloc_hook
= __bound_malloc
;
670 __free_hook
= __bound_free
;
671 __realloc_hook
= __bound_realloc
;
672 __memalign_hook
= __bound_memalign
;
676 static void restore_malloc_hooks(void)
678 #ifdef CONFIG_TCC_MALLOC_HOOKS
679 __malloc_hook
= saved_malloc_hook
;
680 __free_hook
= saved_free_hook
;
681 __realloc_hook
= saved_realloc_hook
;
682 __memalign_hook
= saved_memalign_hook
;
686 static void *libc_malloc(size_t size
)
689 restore_malloc_hooks();
691 install_malloc_hooks();
695 static void libc_free(void *ptr
)
697 restore_malloc_hooks();
699 install_malloc_hooks();
702 /* XXX: we should use a malloc which ensure that it is unlikely that
703 two malloc'ed data have the same address if 'free' are made in
705 void *__bound_malloc(size_t size
, const void *caller
)
709 /* we allocate one more byte to ensure the regions will be
710 separated by at least one byte. With the glibc malloc, it may
711 be in fact not necessary */
712 ptr
= libc_malloc(size
+ 1);
716 __bound_new_region(ptr
, size
);
720 void *__bound_memalign(size_t size
, size_t align
, const void *caller
)
724 restore_malloc_hooks();
726 #ifndef HAVE_MEMALIGN
728 /* XXX: handle it ? */
731 /* we suppose that malloc aligns to at least four bytes */
732 ptr
= malloc(size
+ 1);
735 /* we allocate one more byte to ensure the regions will be
736 separated by at least one byte. With the glibc malloc, it may
737 be in fact not necessary */
738 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("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("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("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("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("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);