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)));
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 /* currently, tcc cannot compile that because we use GNUC extensions */
157 #if !defined(__TINYC__)
159 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
160 the end of a region in this case */
161 void *__bound_ptr_add(void *p
, int offset
)
163 unsigned long addr
= (unsigned long)p
;
165 #if defined(BOUND_DEBUG)
166 printf("add: 0x%x %d\n", (int)p
, offset
);
169 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
170 e
= (BoundEntry
*)((char *)e
+
171 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
172 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
174 if (addr
> e
->size
) {
175 e
= __bound_find_region(e
, p
);
176 addr
= (unsigned long)p
- e
->start
;
180 return INVALID_POINTER
; /* return an invalid pointer */
184 /* return '(p + offset)' for pointer indirection (the resulting must
185 be strictly inside the region */
186 #define BOUND_PTR_INDIR(dsize) \
187 void *__bound_ptr_indir ## dsize (void *p, int offset) \
189 unsigned long addr = (unsigned long)p; \
192 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
193 e = (BoundEntry *)((char *)e + \
194 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
195 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
197 if (addr > e->size) { \
198 e = __bound_find_region(e, p); \
199 addr = (unsigned long)p - e->start; \
201 addr += offset + dsize; \
202 if (addr > e->size) \
203 return INVALID_POINTER; /* return an invalid pointer */ \
208 /* return the frame pointer of the caller */
209 #define GET_CALLER_FP(fp)\
212 __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp1));\
216 #error put code to extract the calling frame pointer
219 /* called when entering a function to add all the local regions */
220 void __bound_local_new(void *p1
)
222 unsigned long addr
, size
, fp
, *p
= p1
;
231 __bound_new_region((void *)addr
, size
);
235 /* called when leaving a function to delete all the local regions */
236 void __bound_local_delete(void *p1
)
238 unsigned long addr
, fp
, *p
= p1
;
246 __bound_delete_region((void *)addr
);
252 void __bound_local_new(void *p
)
255 void __bound_local_delete(void *p
)
259 void *__bound_ptr_add(void *p
, int offset
)
264 #define BOUND_PTR_INDIR(dsize) \
265 void *__bound_ptr_indir ## dsize (void *p, int offset) \
278 static BoundEntry
*__bound_new_page(void)
283 page
= libc_malloc(sizeof(BoundEntry
) * BOUND_T2_SIZE
);
286 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
287 /* put empty entries */
289 page
[i
].size
= EMPTY_SIZE
;
291 page
[i
].is_invalid
= 0;
296 /* currently we use malloc(). Should use bound_new_page() */
297 static BoundEntry
*bound_new_entry(void)
300 e
= libc_malloc(sizeof(BoundEntry
));
304 static void bound_free_entry(BoundEntry
*e
)
309 static inline BoundEntry
*get_page(int index
)
312 page
= __bound_t1
[index
];
313 if (page
== __bound_empty_t2
|| page
== __bound_invalid_t2
) {
314 /* create a new page if necessary */
315 page
= __bound_new_page();
316 __bound_t1
[index
] = page
;
321 /* mark a region as being invalid (can only be used during init) */
322 static void mark_invalid(unsigned long addr
, unsigned long size
)
324 unsigned long start
, end
;
326 int t1_start
, t1_end
, i
, j
, t2_start
, t2_end
;
331 t2_start
= (start
+ BOUND_T3_SIZE
- 1) >> BOUND_T3_BITS
;
333 t2_end
= end
>> BOUND_T3_BITS
;
335 t2_end
= 1 << (BOUND_T1_BITS
+ BOUND_T2_BITS
);
338 printf("mark_invalid: start = %x %x\n", t2_start
, t2_end
);
341 /* first we handle full pages */
342 t1_start
= (t2_start
+ BOUND_T2_SIZE
- 1) >> BOUND_T2_BITS
;
343 t1_end
= t2_end
>> BOUND_T2_BITS
;
345 i
= t2_start
& (BOUND_T2_SIZE
- 1);
346 j
= t2_end
& (BOUND_T2_SIZE
- 1);
348 if (t1_start
== t1_end
) {
349 page
= get_page(t2_start
>> BOUND_T2_BITS
);
351 page
[i
].size
= INVALID_SIZE
;
352 page
[i
].is_invalid
= 1;
356 page
= get_page(t2_start
>> BOUND_T2_BITS
);
357 for(; i
< BOUND_T2_SIZE
; i
++) {
358 page
[i
].size
= INVALID_SIZE
;
359 page
[i
].is_invalid
= 1;
362 for(i
= t1_start
; i
< t1_end
; i
++) {
363 __bound_t1
[i
] = __bound_invalid_t2
;
366 page
= get_page(t1_end
);
367 for(i
= 0; i
< j
; i
++) {
368 page
[i
].size
= INVALID_SIZE
;
369 page
[i
].is_invalid
= 1;
375 void __bound_init(void)
379 unsigned long start
, size
;
382 /* save malloc hooks and install bound check hooks */
383 install_malloc_hooks();
386 __bound_t1
= libc_malloc(BOUND_T1_SIZE
* sizeof(BoundEntry
*));
390 __bound_empty_t2
= __bound_new_page();
391 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
392 __bound_t1
[i
] = __bound_empty_t2
;
395 page
= __bound_new_page();
396 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
397 /* put invalid entries */
399 page
[i
].size
= INVALID_SIZE
;
401 page
[i
].is_invalid
= 1;
403 __bound_invalid_t2
= page
;
405 /* invalid pointer zone */
406 start
= (unsigned long)INVALID_POINTER
& ~(BOUND_T23_SIZE
- 1);
407 size
= BOUND_T23_SIZE
;
408 mark_invalid(start
, size
);
410 #if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
411 /* malloc zone is also marked invalid. can only use that with
412 hooks because all libs should use the same malloc. The solution
413 would be to build a new malloc for tcc. */
414 start
= (unsigned long)&_end
;
415 size
= 128 * 0x100000;
416 mark_invalid(start
, size
);
419 /* add all static bound check values */
420 p
= (int *)&__bounds_start
;
422 __bound_new_region((void *)p
[0], p
[1]);
427 static inline void add_region(BoundEntry
*e
,
428 unsigned long start
, unsigned long size
)
432 /* no region : add it */
436 /* already regions in the list: add it at the head */
437 e1
= bound_new_entry();
438 e1
->start
= e
->start
;
447 /* create a new region. It should not already exist in the region list */
448 void __bound_new_region(void *p
, unsigned long size
)
450 unsigned long start
, end
;
451 BoundEntry
*page
, *e
, *e2
;
452 int t1_start
, t1_end
, i
, t2_start
, t2_end
;
454 start
= (unsigned long)p
;
456 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
457 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
460 page
= get_page(t1_start
);
461 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
462 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
463 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
464 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
466 printf("new %lx %lx %x %x %x %x\n",
467 start
, end
, t1_start
, t1_end
, t2_start
, t2_end
);
470 e
= (BoundEntry
*)((char *)page
+ t2_start
);
471 add_region(e
, start
, size
);
473 if (t1_end
== t1_start
) {
474 /* same ending page */
475 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
482 add_region(e
, start
, size
);
485 /* mark until end of page */
486 e2
= page
+ BOUND_T2_SIZE
;
492 /* mark intermediate pages, if any */
493 for(i
=t1_start
+1;i
<t1_end
;i
++) {
495 e2
= page
+ BOUND_T2_SIZE
;
496 for(e
=page
;e
<e2
;e
++) {
502 page
= get_page(t1_end
);
503 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
504 for(e
=page
;e
<e2
;e
++) {
508 add_region(e
, start
, size
);
512 /* delete a region */
513 static inline void delete_region(BoundEntry
*e
,
514 void *p
, unsigned long empty_size
)
519 addr
= (unsigned long)p
;
521 if (addr
<= e
->size
) {
522 /* region found is first one */
525 /* no more region: mark it empty */
527 e
->size
= empty_size
;
529 /* copy next region in head */
530 e
->start
= e1
->start
;
533 bound_free_entry(e1
);
536 /* find the matching region */
540 /* region not found: do nothing */
543 addr
= (unsigned long)p
- e
->start
;
544 if (addr
<= e
->size
) {
545 /* found: remove entry */
554 /* WARNING: 'p' must be the starting point of the region. */
555 /* return non zero if error */
556 int __bound_delete_region(void *p
)
558 unsigned long start
, end
, addr
, size
, empty_size
;
559 BoundEntry
*page
, *e
, *e2
;
560 int t1_start
, t1_end
, t2_start
, t2_end
, i
;
562 start
= (unsigned long)p
;
563 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
564 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
565 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
567 /* find region size */
568 page
= __bound_t1
[t1_start
];
569 e
= (BoundEntry
*)((char *)page
+ t2_start
);
570 addr
= start
- e
->start
;
572 e
= __bound_find_region(e
, p
);
573 /* test if invalid region */
574 if (e
->size
== EMPTY_SIZE
|| (unsigned long)p
!= e
->start
)
576 /* compute the size we put in invalid regions */
578 empty_size
= INVALID_SIZE
;
580 empty_size
= EMPTY_SIZE
;
584 /* now we can free each entry */
585 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
586 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
587 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
589 delete_region(e
, p
, empty_size
);
590 if (t1_end
== t1_start
) {
591 /* same ending page */
592 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
597 e
->size
= empty_size
;
599 delete_region(e
, p
, empty_size
);
602 /* mark until end of page */
603 e2
= page
+ BOUND_T2_SIZE
;
607 e
->size
= empty_size
;
609 /* mark intermediate pages, if any */
610 /* XXX: should free them */
611 for(i
=t1_start
+1;i
<t1_end
;i
++) {
613 e2
= page
+ BOUND_T2_SIZE
;
614 for(e
=page
;e
<e2
;e
++) {
616 e
->size
= empty_size
;
620 page
= get_page(t2_end
);
621 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
622 for(e
=page
;e
<e2
;e
++) {
624 e
->size
= empty_size
;
626 delete_region(e
, p
, empty_size
);
631 /* return the size of the region starting at p, or EMPTY_SIZE if non
633 static unsigned long get_region_size(void *p
)
635 unsigned long addr
= (unsigned long)p
;
638 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
639 e
= (BoundEntry
*)((char *)e
+
640 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
641 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
644 e
= __bound_find_region(e
, p
);
645 if (e
->start
!= (unsigned long)p
)
650 /* patched memory functions */
652 static void install_malloc_hooks(void)
654 #ifdef CONFIG_TCC_MALLOC_HOOKS
655 saved_malloc_hook
= __malloc_hook
;
656 saved_free_hook
= __free_hook
;
657 saved_realloc_hook
= __realloc_hook
;
658 saved_memalign_hook
= __memalign_hook
;
659 __malloc_hook
= __bound_malloc
;
660 __free_hook
= __bound_free
;
661 __realloc_hook
= __bound_realloc
;
662 __memalign_hook
= __bound_memalign
;
666 static void restore_malloc_hooks(void)
668 #ifdef CONFIG_TCC_MALLOC_HOOKS
669 __malloc_hook
= saved_malloc_hook
;
670 __free_hook
= saved_free_hook
;
671 __realloc_hook
= saved_realloc_hook
;
672 __memalign_hook
= saved_memalign_hook
;
676 static void *libc_malloc(size_t size
)
679 restore_malloc_hooks();
681 install_malloc_hooks();
685 static void libc_free(void *ptr
)
687 restore_malloc_hooks();
689 install_malloc_hooks();
692 /* XXX: we should use a malloc which ensure that it is unlikely that
693 two malloc'ed data have the same address if 'free' are made in
695 void *__bound_malloc(size_t size
, const void *caller
)
699 /* we allocate one more byte to ensure the regions will be
700 separated by at least one byte. With the glibc malloc, it may
701 be in fact not necessary */
702 ptr
= libc_malloc(size
+ 1);
706 __bound_new_region(ptr
, size
);
710 void *__bound_memalign(size_t size
, size_t align
, const void *caller
)
714 restore_malloc_hooks();
716 /* we allocate one more byte to ensure the regions will be
717 separated by at least one byte. With the glibc malloc, it may
718 be in fact not necessary */
719 ptr
= memalign(size
+ 1, align
);
721 install_malloc_hooks();
725 __bound_new_region(ptr
, size
);
729 void __bound_free(void *ptr
, const void *caller
)
733 if (__bound_delete_region(ptr
) != 0)
734 bound_error("freeing invalid region");
739 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
)
745 __bound_free(ptr
, caller
);
748 ptr1
= __bound_malloc(size
, caller
);
749 if (ptr
== NULL
|| ptr1
== NULL
)
751 old_size
= get_region_size(ptr
);
752 if (old_size
== EMPTY_SIZE
)
753 bound_error("realloc'ing invalid pointer");
754 memcpy(ptr1
, ptr
, old_size
);
755 __bound_free(ptr
, caller
);
760 #ifndef CONFIG_TCC_MALLOC_HOOKS
761 void *__bound_calloc(size_t nmemb
, size_t size
)
765 ptr
= __bound_malloc(size
);
768 memset(ptr
, 0, size
);
774 static void bound_dump(void)
776 BoundEntry
*page
, *e
;
779 printf("region dump:\n");
780 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
781 page
= __bound_t1
[i
];
782 for(j
=0;j
<BOUND_T2_SIZE
;j
++) {
784 /* do not print invalid or empty entries */
785 if (e
->size
!= EMPTY_SIZE
&& e
->start
!= 0) {
787 (i
<< (BOUND_T2_BITS
+ BOUND_T3_BITS
)) +
788 (j
<< BOUND_T3_BITS
));
790 printf(" %08lx:%08lx", e
->start
, e
->start
+ e
->size
);
800 /* some useful checked functions */
802 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
803 static void __bound_check(const void *p
, size_t size
)
807 p
= __bound_ptr_add((void *)p
, size
);
808 if (p
== INVALID_POINTER
)
809 bound_error("invalid pointer");
812 void *__bound_memcpy(void *dst
, const void *src
, size_t size
)
814 __bound_check(dst
, size
);
815 __bound_check(src
, size
);
816 /* check also region overlap */
817 if (src
>= dst
&& src
< dst
+ size
)
818 bound_error("overlapping regions in memcpy()");
819 return memcpy(dst
, src
, size
);
822 void *__bound_memmove(void *dst
, const void *src
, size_t size
)
824 __bound_check(dst
, size
);
825 __bound_check(src
, size
);
826 return memmove(dst
, src
, size
);
829 void *__bound_memset(void *dst
, int c
, size_t size
)
831 __bound_check(dst
, size
);
832 return memset(dst
, c
, size
);
835 /* XXX: could be optimized */
836 int __bound_strlen(const char *s
)
843 p
= __bound_ptr_indir1((char *)s
, len
);
844 if (p
== INVALID_POINTER
)
845 bound_error("bad pointer in strlen()");
853 char *__bound_strcpy(char *dst
, const char *src
)
856 len
= __bound_strlen(src
);
857 return __bound_memcpy(dst
, src
, len
+ 1);