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(__DragonFly__) && !defined(__OpenBSD__)
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
38 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__dietlibc__) \
39 || defined(__UCLIBC__) || defined(__OpenBSD__) || defined(_WIN32)
40 #warning Bound checking does not support malloc (etc.) in this environment.
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 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
155 the end of a region in this case */
156 void * FASTCALL
__bound_ptr_add(void *p
, int offset
)
158 unsigned long addr
= (unsigned long)p
;
160 #if defined(BOUND_DEBUG)
161 printf("add: 0x%x %d\n", (int)p
, offset
);
164 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
165 e
= (BoundEntry
*)((char *)e
+
166 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
167 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
169 if (addr
> e
->size
) {
170 e
= __bound_find_region(e
, p
);
171 addr
= (unsigned long)p
- e
->start
;
175 return INVALID_POINTER
; /* return an invalid pointer */
179 /* return '(p + offset)' for pointer indirection (the resulting must
180 be strictly inside the region */
181 #define BOUND_PTR_INDIR(dsize) \
182 void * FASTCALL __bound_ptr_indir ## dsize (void *p, int offset) \
184 unsigned long addr = (unsigned long)p; \
187 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
188 e = (BoundEntry *)((char *)e + \
189 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
190 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
192 if (addr > e->size) { \
193 e = __bound_find_region(e, p); \
194 addr = (unsigned long)p - e->start; \
196 addr += offset + dsize; \
197 if (addr > e->size) \
198 return INVALID_POINTER; /* return an invalid pointer */ \
210 /* return the frame pointer of the caller */
211 #define GET_CALLER_FP(fp)\
214 __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp1));\
218 #error put code to extract the calling frame pointer
221 /* called when entering a function to add all the local regions */
222 void FASTCALL
__bound_local_new(void *p1
)
224 unsigned long addr
, size
, fp
, *p
= p1
;
233 __bound_new_region((void *)addr
, size
);
237 /* called when leaving a function to delete all the local regions */
238 void FASTCALL
__bound_local_delete(void *p1
)
240 unsigned long addr
, fp
, *p
= p1
;
248 __bound_delete_region((void *)addr
);
252 static BoundEntry
*__bound_new_page(void)
257 page
= libc_malloc(sizeof(BoundEntry
) * BOUND_T2_SIZE
);
260 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
261 /* put empty entries */
263 page
[i
].size
= EMPTY_SIZE
;
265 page
[i
].is_invalid
= 0;
270 /* currently we use malloc(). Should use bound_new_page() */
271 static BoundEntry
*bound_new_entry(void)
274 e
= libc_malloc(sizeof(BoundEntry
));
278 static void bound_free_entry(BoundEntry
*e
)
283 static inline BoundEntry
*get_page(int index
)
286 page
= __bound_t1
[index
];
287 if (page
== __bound_empty_t2
|| page
== __bound_invalid_t2
) {
288 /* create a new page if necessary */
289 page
= __bound_new_page();
290 __bound_t1
[index
] = page
;
295 /* mark a region as being invalid (can only be used during init) */
296 static void mark_invalid(unsigned long addr
, unsigned long size
)
298 unsigned long start
, end
;
300 int t1_start
, t1_end
, i
, j
, t2_start
, t2_end
;
305 t2_start
= (start
+ BOUND_T3_SIZE
- 1) >> BOUND_T3_BITS
;
307 t2_end
= end
>> BOUND_T3_BITS
;
309 t2_end
= 1 << (BOUND_T1_BITS
+ BOUND_T2_BITS
);
312 printf("mark_invalid: start = %x %x\n", t2_start
, t2_end
);
315 /* first we handle full pages */
316 t1_start
= (t2_start
+ BOUND_T2_SIZE
- 1) >> BOUND_T2_BITS
;
317 t1_end
= t2_end
>> BOUND_T2_BITS
;
319 i
= t2_start
& (BOUND_T2_SIZE
- 1);
320 j
= t2_end
& (BOUND_T2_SIZE
- 1);
322 if (t1_start
== t1_end
) {
323 page
= get_page(t2_start
>> BOUND_T2_BITS
);
325 page
[i
].size
= INVALID_SIZE
;
326 page
[i
].is_invalid
= 1;
330 page
= get_page(t2_start
>> BOUND_T2_BITS
);
331 for(; i
< BOUND_T2_SIZE
; i
++) {
332 page
[i
].size
= INVALID_SIZE
;
333 page
[i
].is_invalid
= 1;
336 for(i
= t1_start
; i
< t1_end
; i
++) {
337 __bound_t1
[i
] = __bound_invalid_t2
;
340 page
= get_page(t1_end
);
341 for(i
= 0; i
< j
; i
++) {
342 page
[i
].size
= INVALID_SIZE
;
343 page
[i
].is_invalid
= 1;
349 void __bound_init(void)
353 unsigned long start
, size
;
356 /* save malloc hooks and install bound check hooks */
357 install_malloc_hooks();
360 __bound_t1
= libc_malloc(BOUND_T1_SIZE
* sizeof(BoundEntry
*));
364 __bound_empty_t2
= __bound_new_page();
365 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
366 __bound_t1
[i
] = __bound_empty_t2
;
369 page
= __bound_new_page();
370 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
371 /* put invalid entries */
373 page
[i
].size
= INVALID_SIZE
;
375 page
[i
].is_invalid
= 1;
377 __bound_invalid_t2
= page
;
379 /* invalid pointer zone */
380 start
= (unsigned long)INVALID_POINTER
& ~(BOUND_T23_SIZE
- 1);
381 size
= BOUND_T23_SIZE
;
382 mark_invalid(start
, size
);
384 #if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
385 /* malloc zone is also marked invalid. can only use that with
386 hooks because all libs should use the same malloc. The solution
387 would be to build a new malloc for tcc. */
388 start
= (unsigned long)&_end
;
389 size
= 128 * 0x100000;
390 mark_invalid(start
, size
);
393 /* add all static bound check values */
394 p
= (int *)&__bounds_start
;
396 __bound_new_region((void *)p
[0], p
[1]);
401 void __bound_exit(void)
403 restore_malloc_hooks();
406 static inline void add_region(BoundEntry
*e
,
407 unsigned long start
, unsigned long size
)
411 /* no region : add it */
415 /* already regions in the list: add it at the head */
416 e1
= bound_new_entry();
417 e1
->start
= e
->start
;
426 /* create a new region. It should not already exist in the region list */
427 void __bound_new_region(void *p
, unsigned long size
)
429 unsigned long start
, end
;
430 BoundEntry
*page
, *e
, *e2
;
431 int t1_start
, t1_end
, i
, t2_start
, t2_end
;
433 start
= (unsigned long)p
;
435 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
436 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
439 page
= get_page(t1_start
);
440 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
441 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
442 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
443 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
445 printf("new %lx %lx %x %x %x %x\n",
446 start
, end
, t1_start
, t1_end
, t2_start
, t2_end
);
449 e
= (BoundEntry
*)((char *)page
+ t2_start
);
450 add_region(e
, start
, size
);
452 if (t1_end
== t1_start
) {
453 /* same ending page */
454 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
461 add_region(e
, start
, size
);
464 /* mark until end of page */
465 e2
= page
+ BOUND_T2_SIZE
;
471 /* mark intermediate pages, if any */
472 for(i
=t1_start
+1;i
<t1_end
;i
++) {
474 e2
= page
+ BOUND_T2_SIZE
;
475 for(e
=page
;e
<e2
;e
++) {
481 page
= get_page(t1_end
);
482 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
483 for(e
=page
;e
<e2
;e
++) {
487 add_region(e
, start
, size
);
491 /* delete a region */
492 static inline void delete_region(BoundEntry
*e
,
493 void *p
, unsigned long empty_size
)
498 addr
= (unsigned long)p
;
500 if (addr
<= e
->size
) {
501 /* region found is first one */
504 /* no more region: mark it empty */
506 e
->size
= empty_size
;
508 /* copy next region in head */
509 e
->start
= e1
->start
;
512 bound_free_entry(e1
);
515 /* find the matching region */
519 /* region not found: do nothing */
522 addr
= (unsigned long)p
- e
->start
;
523 if (addr
<= e
->size
) {
524 /* found: remove entry */
533 /* WARNING: 'p' must be the starting point of the region. */
534 /* return non zero if error */
535 int __bound_delete_region(void *p
)
537 unsigned long start
, end
, addr
, size
, empty_size
;
538 BoundEntry
*page
, *e
, *e2
;
539 int t1_start
, t1_end
, t2_start
, t2_end
, i
;
541 start
= (unsigned long)p
;
542 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
543 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
544 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
546 /* find region size */
547 page
= __bound_t1
[t1_start
];
548 e
= (BoundEntry
*)((char *)page
+ t2_start
);
549 addr
= start
- e
->start
;
551 e
= __bound_find_region(e
, p
);
552 /* test if invalid region */
553 if (e
->size
== EMPTY_SIZE
|| (unsigned long)p
!= e
->start
)
555 /* compute the size we put in invalid regions */
557 empty_size
= INVALID_SIZE
;
559 empty_size
= EMPTY_SIZE
;
563 /* now we can free each entry */
564 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
565 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
566 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
568 delete_region(e
, p
, empty_size
);
569 if (t1_end
== t1_start
) {
570 /* same ending page */
571 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
576 e
->size
= empty_size
;
578 delete_region(e
, p
, empty_size
);
581 /* mark until end of page */
582 e2
= page
+ BOUND_T2_SIZE
;
586 e
->size
= empty_size
;
588 /* mark intermediate pages, if any */
589 /* XXX: should free them */
590 for(i
=t1_start
+1;i
<t1_end
;i
++) {
592 e2
= page
+ BOUND_T2_SIZE
;
593 for(e
=page
;e
<e2
;e
++) {
595 e
->size
= empty_size
;
599 page
= get_page(t2_end
);
600 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
601 for(e
=page
;e
<e2
;e
++) {
603 e
->size
= empty_size
;
605 delete_region(e
, p
, empty_size
);
610 /* return the size of the region starting at p, or EMPTY_SIZE if non
612 static unsigned long get_region_size(void *p
)
614 unsigned long addr
= (unsigned long)p
;
617 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
618 e
= (BoundEntry
*)((char *)e
+
619 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
620 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
623 e
= __bound_find_region(e
, p
);
624 if (e
->start
!= (unsigned long)p
)
629 /* patched memory functions */
631 static void install_malloc_hooks(void)
633 #ifdef CONFIG_TCC_MALLOC_HOOKS
634 saved_malloc_hook
= __malloc_hook
;
635 saved_free_hook
= __free_hook
;
636 saved_realloc_hook
= __realloc_hook
;
637 saved_memalign_hook
= __memalign_hook
;
638 __malloc_hook
= __bound_malloc
;
639 __free_hook
= __bound_free
;
640 __realloc_hook
= __bound_realloc
;
641 __memalign_hook
= __bound_memalign
;
645 static void restore_malloc_hooks(void)
647 #ifdef CONFIG_TCC_MALLOC_HOOKS
648 __malloc_hook
= saved_malloc_hook
;
649 __free_hook
= saved_free_hook
;
650 __realloc_hook
= saved_realloc_hook
;
651 __memalign_hook
= saved_memalign_hook
;
655 static void *libc_malloc(size_t size
)
658 restore_malloc_hooks();
660 install_malloc_hooks();
664 static void libc_free(void *ptr
)
666 restore_malloc_hooks();
668 install_malloc_hooks();
671 /* XXX: we should use a malloc which ensure that it is unlikely that
672 two malloc'ed data have the same address if 'free' are made in
674 void *__bound_malloc(size_t size
, const void *caller
)
678 /* we allocate one more byte to ensure the regions will be
679 separated by at least one byte. With the glibc malloc, it may
680 be in fact not necessary */
681 ptr
= libc_malloc(size
+ 1);
685 __bound_new_region(ptr
, size
);
689 void *__bound_memalign(size_t size
, size_t align
, const void *caller
)
693 restore_malloc_hooks();
695 #ifndef HAVE_MEMALIGN
697 /* XXX: handle it ? */
700 /* we suppose that malloc aligns to at least four bytes */
701 ptr
= malloc(size
+ 1);
704 /* we allocate one more byte to ensure the regions will be
705 separated by at least one byte. With the glibc malloc, it may
706 be in fact not necessary */
707 ptr
= memalign(size
+ 1, align
);
710 install_malloc_hooks();
714 __bound_new_region(ptr
, size
);
718 void __bound_free(void *ptr
, const void *caller
)
722 if (__bound_delete_region(ptr
) != 0)
723 bound_error("freeing invalid region");
728 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
)
734 __bound_free(ptr
, caller
);
737 ptr1
= __bound_malloc(size
, caller
);
738 if (ptr
== NULL
|| ptr1
== NULL
)
740 old_size
= get_region_size(ptr
);
741 if (old_size
== EMPTY_SIZE
)
742 bound_error("realloc'ing invalid pointer");
743 memcpy(ptr1
, ptr
, old_size
);
744 __bound_free(ptr
, caller
);
749 #ifndef CONFIG_TCC_MALLOC_HOOKS
750 void *__bound_calloc(size_t nmemb
, size_t size
)
754 ptr
= __bound_malloc(size
, NULL
);
757 memset(ptr
, 0, size
);
763 static void bound_dump(void)
765 BoundEntry
*page
, *e
;
768 printf("region dump:\n");
769 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
770 page
= __bound_t1
[i
];
771 for(j
=0;j
<BOUND_T2_SIZE
;j
++) {
773 /* do not print invalid or empty entries */
774 if (e
->size
!= EMPTY_SIZE
&& e
->start
!= 0) {
776 (i
<< (BOUND_T2_BITS
+ BOUND_T3_BITS
)) +
777 (j
<< BOUND_T3_BITS
));
779 printf(" %08lx:%08lx", e
->start
, e
->start
+ e
->size
);
789 /* some useful checked functions */
791 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
792 static void __bound_check(const void *p
, size_t size
)
796 p
= __bound_ptr_add((void *)p
, size
);
797 if (p
== INVALID_POINTER
)
798 bound_error("invalid pointer");
801 void *__bound_memcpy(void *dst
, const void *src
, size_t size
)
803 __bound_check(dst
, size
);
804 __bound_check(src
, size
);
805 /* check also region overlap */
806 if (src
>= dst
&& src
< dst
+ size
)
807 bound_error("overlapping regions in memcpy()");
808 return memcpy(dst
, src
, size
);
811 void *__bound_memmove(void *dst
, const void *src
, size_t size
)
813 __bound_check(dst
, size
);
814 __bound_check(src
, size
);
815 return memmove(dst
, src
, size
);
818 void *__bound_memset(void *dst
, int c
, size_t size
)
820 __bound_check(dst
, size
);
821 return memset(dst
, c
, size
);
824 /* XXX: could be optimized */
825 int __bound_strlen(const char *s
)
832 p
= __bound_ptr_indir1((char *)s
, len
);
833 if (p
== INVALID_POINTER
)
834 bound_error("bad pointer in strlen()");
842 char *__bound_strcpy(char *dst
, const char *src
)
845 len
= __bound_strlen(src
);
846 return __bound_memcpy(dst
, src
, len
+ 1);