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
39 #if defined(__FreeBSD__) || defined(__dietlibc__)
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 #define FASTCALL __attribute__((regparm(3)))
79 void *__bound_malloc(size_t size
, const void *caller
);
80 void *__bound_memalign(size_t size
, size_t align
, const void *caller
);
81 void __bound_free(void *ptr
, const void *caller
);
82 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
);
83 static void *libc_malloc(size_t size
);
84 static void libc_free(void *ptr
);
85 static void install_malloc_hooks(void);
86 static void restore_malloc_hooks(void);
88 #ifdef CONFIG_TCC_MALLOC_HOOKS
89 static void *saved_malloc_hook
;
90 static void *saved_free_hook
;
91 static void *saved_realloc_hook
;
92 static void *saved_memalign_hook
;
95 /* linker definitions */
99 extern char __bounds_start
; /* start of static bounds table */
100 /* error message, just for TCC */
101 const char *__bound_error_msg
;
103 /* runtime error output */
104 extern void rt_error(unsigned long pc
, const char *fmt
, ...);
107 static BoundEntry
*__bound_t1
[BOUND_T1_SIZE
]; /* page table */
109 static BoundEntry
**__bound_t1
; /* page table */
111 static BoundEntry
*__bound_empty_t2
; /* empty page, for unused pages */
112 static BoundEntry
*__bound_invalid_t2
; /* invalid page, for invalid pointers */
114 static BoundEntry
*__bound_find_region(BoundEntry
*e1
, void *p
)
116 unsigned long addr
, tmp
;
121 addr
= (unsigned long)p
;
123 if (addr
<= e
->size
) {
124 /* put region at the head */
126 e1
->start
= e
->start
;
135 /* no entry found: return empty entry or invalid entry */
137 return __bound_invalid_t2
;
139 return __bound_empty_t2
;
142 /* print a bound error message */
143 static void bound_error(const char *fmt
, ...)
145 __bound_error_msg
= fmt
;
146 *(int *)0 = 0; /* force a runtime error */
149 static void bound_alloc_error(void)
151 bound_error("not enough memory for bound checking code");
154 /* currently, tcc cannot compile that because we use GNUC extensions */
155 #if !defined(__TINYC__)
157 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
158 the end of a region in this case */
159 void * FASTCALL
__bound_ptr_add(void *p
, int offset
)
161 unsigned long addr
= (unsigned long)p
;
163 #if defined(BOUND_DEBUG)
164 printf("add: 0x%x %d\n", (int)p
, offset
);
167 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
168 e
= (BoundEntry
*)((char *)e
+
169 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
170 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
172 if (addr
> e
->size
) {
173 e
= __bound_find_region(e
, p
);
174 addr
= (unsigned long)p
- e
->start
;
178 return INVALID_POINTER
; /* return an invalid pointer */
182 /* return '(p + offset)' for pointer indirection (the resulting must
183 be strictly inside the region */
184 #define BOUND_PTR_INDIR(dsize) \
185 void * FASTCALL __bound_ptr_indir ## dsize (void *p, int offset) \
187 unsigned long addr = (unsigned long)p; \
190 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
191 e = (BoundEntry *)((char *)e + \
192 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
193 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
195 if (addr > e->size) { \
196 e = __bound_find_region(e, p); \
197 addr = (unsigned long)p - e->start; \
199 addr += offset + dsize; \
200 if (addr > e->size) \
201 return INVALID_POINTER; /* return an invalid pointer */ \
206 /* return the frame pointer of the caller */
207 #define GET_CALLER_FP(fp)\
210 __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp1));\
214 #error put code to extract the calling frame pointer
217 /* called when entering a function to add all the local regions */
218 void FASTCALL
__bound_local_new(void *p1
)
220 unsigned long addr
, size
, fp
, *p
= p1
;
229 __bound_new_region((void *)addr
, size
);
233 /* called when leaving a function to delete all the local regions */
234 void FASTCALL
__bound_local_delete(void *p1
)
236 unsigned long addr
, fp
, *p
= p1
;
244 __bound_delete_region((void *)addr
);
250 void __bound_local_new(void *p
)
253 void __bound_local_delete(void *p
)
257 void *__bound_ptr_add(void *p
, int offset
)
262 #define BOUND_PTR_INDIR(dsize) \
263 void *__bound_ptr_indir ## dsize (void *p, int offset) \
276 static BoundEntry
*__bound_new_page(void)
281 page
= libc_malloc(sizeof(BoundEntry
) * BOUND_T2_SIZE
);
284 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
285 /* put empty entries */
287 page
[i
].size
= EMPTY_SIZE
;
289 page
[i
].is_invalid
= 0;
294 /* currently we use malloc(). Should use bound_new_page() */
295 static BoundEntry
*bound_new_entry(void)
298 e
= libc_malloc(sizeof(BoundEntry
));
302 static void bound_free_entry(BoundEntry
*e
)
307 static inline BoundEntry
*get_page(int index
)
310 page
= __bound_t1
[index
];
311 if (page
== __bound_empty_t2
|| page
== __bound_invalid_t2
) {
312 /* create a new page if necessary */
313 page
= __bound_new_page();
314 __bound_t1
[index
] = page
;
319 /* mark a region as being invalid (can only be used during init) */
320 static void mark_invalid(unsigned long addr
, unsigned long size
)
322 unsigned long start
, end
;
324 int t1_start
, t1_end
, i
, j
, t2_start
, t2_end
;
329 t2_start
= (start
+ BOUND_T3_SIZE
- 1) >> BOUND_T3_BITS
;
331 t2_end
= end
>> BOUND_T3_BITS
;
333 t2_end
= 1 << (BOUND_T1_BITS
+ BOUND_T2_BITS
);
336 printf("mark_invalid: start = %x %x\n", t2_start
, t2_end
);
339 /* first we handle full pages */
340 t1_start
= (t2_start
+ BOUND_T2_SIZE
- 1) >> BOUND_T2_BITS
;
341 t1_end
= t2_end
>> BOUND_T2_BITS
;
343 i
= t2_start
& (BOUND_T2_SIZE
- 1);
344 j
= t2_end
& (BOUND_T2_SIZE
- 1);
346 if (t1_start
== t1_end
) {
347 page
= get_page(t2_start
>> BOUND_T2_BITS
);
349 page
[i
].size
= INVALID_SIZE
;
350 page
[i
].is_invalid
= 1;
354 page
= get_page(t2_start
>> BOUND_T2_BITS
);
355 for(; i
< BOUND_T2_SIZE
; i
++) {
356 page
[i
].size
= INVALID_SIZE
;
357 page
[i
].is_invalid
= 1;
360 for(i
= t1_start
; i
< t1_end
; i
++) {
361 __bound_t1
[i
] = __bound_invalid_t2
;
364 page
= get_page(t1_end
);
365 for(i
= 0; i
< j
; i
++) {
366 page
[i
].size
= INVALID_SIZE
;
367 page
[i
].is_invalid
= 1;
373 void __bound_init(void)
377 unsigned long start
, size
;
380 /* save malloc hooks and install bound check hooks */
381 install_malloc_hooks();
384 __bound_t1
= libc_malloc(BOUND_T1_SIZE
* sizeof(BoundEntry
*));
388 __bound_empty_t2
= __bound_new_page();
389 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
390 __bound_t1
[i
] = __bound_empty_t2
;
393 page
= __bound_new_page();
394 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
395 /* put invalid entries */
397 page
[i
].size
= INVALID_SIZE
;
399 page
[i
].is_invalid
= 1;
401 __bound_invalid_t2
= page
;
403 /* invalid pointer zone */
404 start
= (unsigned long)INVALID_POINTER
& ~(BOUND_T23_SIZE
- 1);
405 size
= BOUND_T23_SIZE
;
406 mark_invalid(start
, size
);
408 #if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
409 /* malloc zone is also marked invalid. can only use that with
410 hooks because all libs should use the same malloc. The solution
411 would be to build a new malloc for tcc. */
412 start
= (unsigned long)&_end
;
413 size
= 128 * 0x100000;
414 mark_invalid(start
, size
);
417 /* add all static bound check values */
418 p
= (int *)&__bounds_start
;
420 __bound_new_region((void *)p
[0], p
[1]);
425 static inline void add_region(BoundEntry
*e
,
426 unsigned long start
, unsigned long size
)
430 /* no region : add it */
434 /* already regions in the list: add it at the head */
435 e1
= bound_new_entry();
436 e1
->start
= e
->start
;
445 /* create a new region. It should not already exist in the region list */
446 void __bound_new_region(void *p
, unsigned long size
)
448 unsigned long start
, end
;
449 BoundEntry
*page
, *e
, *e2
;
450 int t1_start
, t1_end
, i
, t2_start
, t2_end
;
452 start
= (unsigned long)p
;
454 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
455 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
458 page
= get_page(t1_start
);
459 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
460 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
461 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
462 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
464 printf("new %lx %lx %x %x %x %x\n",
465 start
, end
, t1_start
, t1_end
, t2_start
, t2_end
);
468 e
= (BoundEntry
*)((char *)page
+ t2_start
);
469 add_region(e
, start
, size
);
471 if (t1_end
== t1_start
) {
472 /* same ending page */
473 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
480 add_region(e
, start
, size
);
483 /* mark until end of page */
484 e2
= page
+ BOUND_T2_SIZE
;
490 /* mark intermediate pages, if any */
491 for(i
=t1_start
+1;i
<t1_end
;i
++) {
493 e2
= page
+ BOUND_T2_SIZE
;
494 for(e
=page
;e
<e2
;e
++) {
500 page
= get_page(t1_end
);
501 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
502 for(e
=page
;e
<e2
;e
++) {
506 add_region(e
, start
, size
);
510 /* delete a region */
511 static inline void delete_region(BoundEntry
*e
,
512 void *p
, unsigned long empty_size
)
517 addr
= (unsigned long)p
;
519 if (addr
<= e
->size
) {
520 /* region found is first one */
523 /* no more region: mark it empty */
525 e
->size
= empty_size
;
527 /* copy next region in head */
528 e
->start
= e1
->start
;
531 bound_free_entry(e1
);
534 /* find the matching region */
538 /* region not found: do nothing */
541 addr
= (unsigned long)p
- e
->start
;
542 if (addr
<= e
->size
) {
543 /* found: remove entry */
552 /* WARNING: 'p' must be the starting point of the region. */
553 /* return non zero if error */
554 int __bound_delete_region(void *p
)
556 unsigned long start
, end
, addr
, size
, empty_size
;
557 BoundEntry
*page
, *e
, *e2
;
558 int t1_start
, t1_end
, t2_start
, t2_end
, i
;
560 start
= (unsigned long)p
;
561 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
562 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
563 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
565 /* find region size */
566 page
= __bound_t1
[t1_start
];
567 e
= (BoundEntry
*)((char *)page
+ t2_start
);
568 addr
= start
- e
->start
;
570 e
= __bound_find_region(e
, p
);
571 /* test if invalid region */
572 if (e
->size
== EMPTY_SIZE
|| (unsigned long)p
!= e
->start
)
574 /* compute the size we put in invalid regions */
576 empty_size
= INVALID_SIZE
;
578 empty_size
= EMPTY_SIZE
;
582 /* now we can free each entry */
583 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
584 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
585 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
587 delete_region(e
, p
, empty_size
);
588 if (t1_end
== t1_start
) {
589 /* same ending page */
590 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
595 e
->size
= empty_size
;
597 delete_region(e
, p
, empty_size
);
600 /* mark until end of page */
601 e2
= page
+ BOUND_T2_SIZE
;
605 e
->size
= empty_size
;
607 /* mark intermediate pages, if any */
608 /* XXX: should free them */
609 for(i
=t1_start
+1;i
<t1_end
;i
++) {
611 e2
= page
+ BOUND_T2_SIZE
;
612 for(e
=page
;e
<e2
;e
++) {
614 e
->size
= empty_size
;
618 page
= get_page(t2_end
);
619 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
620 for(e
=page
;e
<e2
;e
++) {
622 e
->size
= empty_size
;
624 delete_region(e
, p
, empty_size
);
629 /* return the size of the region starting at p, or EMPTY_SIZE if non
631 static unsigned long get_region_size(void *p
)
633 unsigned long addr
= (unsigned long)p
;
636 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
637 e
= (BoundEntry
*)((char *)e
+
638 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
639 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
642 e
= __bound_find_region(e
, p
);
643 if (e
->start
!= (unsigned long)p
)
648 /* patched memory functions */
650 static void install_malloc_hooks(void)
652 #ifdef CONFIG_TCC_MALLOC_HOOKS
653 saved_malloc_hook
= __malloc_hook
;
654 saved_free_hook
= __free_hook
;
655 saved_realloc_hook
= __realloc_hook
;
656 saved_memalign_hook
= __memalign_hook
;
657 __malloc_hook
= __bound_malloc
;
658 __free_hook
= __bound_free
;
659 __realloc_hook
= __bound_realloc
;
660 __memalign_hook
= __bound_memalign
;
664 static void restore_malloc_hooks(void)
666 #ifdef CONFIG_TCC_MALLOC_HOOKS
667 __malloc_hook
= saved_malloc_hook
;
668 __free_hook
= saved_free_hook
;
669 __realloc_hook
= saved_realloc_hook
;
670 __memalign_hook
= saved_memalign_hook
;
674 static void *libc_malloc(size_t size
)
677 restore_malloc_hooks();
679 install_malloc_hooks();
683 static void libc_free(void *ptr
)
685 restore_malloc_hooks();
687 install_malloc_hooks();
690 /* XXX: we should use a malloc which ensure that it is unlikely that
691 two malloc'ed data have the same address if 'free' are made in
693 void *__bound_malloc(size_t size
, const void *caller
)
697 /* we allocate one more byte to ensure the regions will be
698 separated by at least one byte. With the glibc malloc, it may
699 be in fact not necessary */
700 ptr
= libc_malloc(size
+ 1);
704 __bound_new_region(ptr
, size
);
708 void *__bound_memalign(size_t size
, size_t align
, const void *caller
)
712 restore_malloc_hooks();
714 #ifndef HAVE_MEMALIGN
716 /* XXX: handle it ? */
719 /* we suppose that malloc aligns to at least four bytes */
720 ptr
= malloc(size
+ 1);
723 /* we allocate one more byte to ensure the regions will be
724 separated by at least one byte. With the glibc malloc, it may
725 be in fact not necessary */
726 ptr
= memalign(size
+ 1, align
);
729 install_malloc_hooks();
733 __bound_new_region(ptr
, size
);
737 void __bound_free(void *ptr
, const void *caller
)
741 if (__bound_delete_region(ptr
) != 0)
742 bound_error("freeing invalid region");
747 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
)
753 __bound_free(ptr
, caller
);
756 ptr1
= __bound_malloc(size
, caller
);
757 if (ptr
== NULL
|| ptr1
== NULL
)
759 old_size
= get_region_size(ptr
);
760 if (old_size
== EMPTY_SIZE
)
761 bound_error("realloc'ing invalid pointer");
762 memcpy(ptr1
, ptr
, old_size
);
763 __bound_free(ptr
, caller
);
768 #ifndef CONFIG_TCC_MALLOC_HOOKS
769 void *__bound_calloc(size_t nmemb
, size_t size
)
773 ptr
= __bound_malloc(size
, NULL
);
776 memset(ptr
, 0, size
);
782 static void bound_dump(void)
784 BoundEntry
*page
, *e
;
787 printf("region dump:\n");
788 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
789 page
= __bound_t1
[i
];
790 for(j
=0;j
<BOUND_T2_SIZE
;j
++) {
792 /* do not print invalid or empty entries */
793 if (e
->size
!= EMPTY_SIZE
&& e
->start
!= 0) {
795 (i
<< (BOUND_T2_BITS
+ BOUND_T3_BITS
)) +
796 (j
<< BOUND_T3_BITS
));
798 printf(" %08lx:%08lx", e
->start
, e
->start
+ e
->size
);
808 /* some useful checked functions */
810 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
811 static void __bound_check(const void *p
, size_t size
)
815 p
= __bound_ptr_add((void *)p
, size
);
816 if (p
== INVALID_POINTER
)
817 bound_error("invalid pointer");
820 void *__bound_memcpy(void *dst
, const void *src
, size_t size
)
822 __bound_check(dst
, size
);
823 __bound_check(src
, size
);
824 /* check also region overlap */
825 if (src
>= dst
&& src
< dst
+ size
)
826 bound_error("overlapping regions in memcpy()");
827 return memcpy(dst
, src
, size
);
830 void *__bound_memmove(void *dst
, const void *src
, size_t size
)
832 __bound_check(dst
, size
);
833 __bound_check(src
, size
);
834 return memmove(dst
, src
, size
);
837 void *__bound_memset(void *dst
, int c
, size_t size
)
839 __bound_check(dst
, size
);
840 return memset(dst
, c
, size
);
843 /* XXX: could be optimized */
844 int __bound_strlen(const char *s
)
851 p
= __bound_ptr_indir1((char *)s
, len
);
852 if (p
== INVALID_POINTER
)
853 bound_error("bad pointer in strlen()");
861 char *__bound_strcpy(char *dst
, const char *src
)
864 len
= __bound_strlen(src
);
865 return __bound_memcpy(dst
, src
, len
+ 1);