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
39 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__dietlibc__) \
40 || defined(__UCLIBC__) || defined(__OpenBSD__)
41 #warning Bound checking not fully supported in this environment.
42 #undef CONFIG_TCC_MALLOC_HOOKS
46 #define BOUND_T1_BITS 13
47 #define BOUND_T2_BITS 11
48 #define BOUND_T3_BITS (32 - BOUND_T1_BITS - BOUND_T2_BITS)
50 #define BOUND_T1_SIZE (1 << BOUND_T1_BITS)
51 #define BOUND_T2_SIZE (1 << BOUND_T2_BITS)
52 #define BOUND_T3_SIZE (1 << BOUND_T3_BITS)
53 #define BOUND_E_BITS 4
55 #define BOUND_T23_BITS (BOUND_T2_BITS + BOUND_T3_BITS)
56 #define BOUND_T23_SIZE (1 << BOUND_T23_BITS)
59 /* this pointer is generated when bound check is incorrect */
60 #define INVALID_POINTER ((void *)(-2))
61 /* size of an empty region */
62 #define EMPTY_SIZE 0xffffffff
63 /* size of an invalid region */
64 #define INVALID_SIZE 0
66 typedef struct BoundEntry
{
69 struct BoundEntry
*next
;
70 unsigned long is_invalid
; /* true if pointers outside region are invalid */
73 /* external interface */
74 void __bound_init(void);
75 void __bound_new_region(void *p
, unsigned long size
);
76 int __bound_delete_region(void *p
);
78 #define FASTCALL __attribute__((regparm(3)))
80 void *__bound_malloc(size_t size
, const void *caller
);
81 void *__bound_memalign(size_t size
, size_t align
, const void *caller
);
82 void __bound_free(void *ptr
, const void *caller
);
83 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
);
84 static void *libc_malloc(size_t size
);
85 static void libc_free(void *ptr
);
86 static void install_malloc_hooks(void);
87 static void restore_malloc_hooks(void);
89 #ifdef CONFIG_TCC_MALLOC_HOOKS
90 static void *saved_malloc_hook
;
91 static void *saved_free_hook
;
92 static void *saved_realloc_hook
;
93 static void *saved_memalign_hook
;
96 /* linker definitions */
100 extern char __bounds_start
; /* start of static bounds table */
101 /* error message, just for TCC */
102 const char *__bound_error_msg
;
104 /* runtime error output */
105 extern void rt_error(unsigned long pc
, const char *fmt
, ...);
108 static BoundEntry
*__bound_t1
[BOUND_T1_SIZE
]; /* page table */
110 static BoundEntry
**__bound_t1
; /* page table */
112 static BoundEntry
*__bound_empty_t2
; /* empty page, for unused pages */
113 static BoundEntry
*__bound_invalid_t2
; /* invalid page, for invalid pointers */
115 static BoundEntry
*__bound_find_region(BoundEntry
*e1
, void *p
)
117 unsigned long addr
, tmp
;
122 addr
= (unsigned long)p
;
124 if (addr
<= e
->size
) {
125 /* put region at the head */
127 e1
->start
= e
->start
;
136 /* no entry found: return empty entry or invalid entry */
138 return __bound_invalid_t2
;
140 return __bound_empty_t2
;
143 /* print a bound error message */
144 static void bound_error(const char *fmt
, ...)
146 __bound_error_msg
= fmt
;
147 *(int *)0 = 0; /* force a runtime error */
150 static void bound_alloc_error(void)
152 bound_error("not enough memory for bound checking code");
155 /* currently, tcc cannot compile that because we use GNUC extensions */
156 #if !defined(__TINYC__)
158 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
159 the end of a region in this case */
160 void * FASTCALL
__bound_ptr_add(void *p
, int offset
)
162 unsigned long addr
= (unsigned long)p
;
164 #if defined(BOUND_DEBUG)
165 printf("add: 0x%x %d\n", (int)p
, offset
);
168 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
169 e
= (BoundEntry
*)((char *)e
+
170 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
171 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
173 if (addr
> e
->size
) {
174 e
= __bound_find_region(e
, p
);
175 addr
= (unsigned long)p
- e
->start
;
179 return INVALID_POINTER
; /* return an invalid pointer */
183 /* return '(p + offset)' for pointer indirection (the resulting must
184 be strictly inside the region */
185 #define BOUND_PTR_INDIR(dsize) \
186 void * FASTCALL __bound_ptr_indir ## dsize (void *p, int offset) \
188 unsigned long addr = (unsigned long)p; \
191 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
192 e = (BoundEntry *)((char *)e + \
193 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
194 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
196 if (addr > e->size) { \
197 e = __bound_find_region(e, p); \
198 addr = (unsigned long)p - e->start; \
200 addr += offset + dsize; \
201 if (addr > e->size) \
202 return INVALID_POINTER; /* return an invalid pointer */ \
207 /* return the frame pointer of the caller */
208 #define GET_CALLER_FP(fp)\
211 __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp1));\
215 #error put code to extract the calling frame pointer
218 /* called when entering a function to add all the local regions */
219 void FASTCALL
__bound_local_new(void *p1
)
221 unsigned long addr
, size
, fp
, *p
= p1
;
230 __bound_new_region((void *)addr
, size
);
234 /* called when leaving a function to delete all the local regions */
235 void FASTCALL
__bound_local_delete(void *p1
)
237 unsigned long addr
, fp
, *p
= p1
;
245 __bound_delete_region((void *)addr
);
251 void __bound_local_new(void *p
)
254 void __bound_local_delete(void *p
)
258 void *__bound_ptr_add(void *p
, int offset
)
263 #define BOUND_PTR_INDIR(dsize) \
264 void *__bound_ptr_indir ## dsize (void *p, int offset) \
277 static BoundEntry
*__bound_new_page(void)
282 page
= libc_malloc(sizeof(BoundEntry
) * BOUND_T2_SIZE
);
285 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
286 /* put empty entries */
288 page
[i
].size
= EMPTY_SIZE
;
290 page
[i
].is_invalid
= 0;
295 /* currently we use malloc(). Should use bound_new_page() */
296 static BoundEntry
*bound_new_entry(void)
299 e
= libc_malloc(sizeof(BoundEntry
));
303 static void bound_free_entry(BoundEntry
*e
)
308 static inline BoundEntry
*get_page(int index
)
311 page
= __bound_t1
[index
];
312 if (page
== __bound_empty_t2
|| page
== __bound_invalid_t2
) {
313 /* create a new page if necessary */
314 page
= __bound_new_page();
315 __bound_t1
[index
] = page
;
320 /* mark a region as being invalid (can only be used during init) */
321 static void mark_invalid(unsigned long addr
, unsigned long size
)
323 unsigned long start
, end
;
325 int t1_start
, t1_end
, i
, j
, t2_start
, t2_end
;
330 t2_start
= (start
+ BOUND_T3_SIZE
- 1) >> BOUND_T3_BITS
;
332 t2_end
= end
>> BOUND_T3_BITS
;
334 t2_end
= 1 << (BOUND_T1_BITS
+ BOUND_T2_BITS
);
337 printf("mark_invalid: start = %x %x\n", t2_start
, t2_end
);
340 /* first we handle full pages */
341 t1_start
= (t2_start
+ BOUND_T2_SIZE
- 1) >> BOUND_T2_BITS
;
342 t1_end
= t2_end
>> BOUND_T2_BITS
;
344 i
= t2_start
& (BOUND_T2_SIZE
- 1);
345 j
= t2_end
& (BOUND_T2_SIZE
- 1);
347 if (t1_start
== t1_end
) {
348 page
= get_page(t2_start
>> BOUND_T2_BITS
);
350 page
[i
].size
= INVALID_SIZE
;
351 page
[i
].is_invalid
= 1;
355 page
= get_page(t2_start
>> BOUND_T2_BITS
);
356 for(; i
< BOUND_T2_SIZE
; i
++) {
357 page
[i
].size
= INVALID_SIZE
;
358 page
[i
].is_invalid
= 1;
361 for(i
= t1_start
; i
< t1_end
; i
++) {
362 __bound_t1
[i
] = __bound_invalid_t2
;
365 page
= get_page(t1_end
);
366 for(i
= 0; i
< j
; i
++) {
367 page
[i
].size
= INVALID_SIZE
;
368 page
[i
].is_invalid
= 1;
374 void __bound_init(void)
378 unsigned long start
, size
;
381 /* save malloc hooks and install bound check hooks */
382 install_malloc_hooks();
385 __bound_t1
= libc_malloc(BOUND_T1_SIZE
* sizeof(BoundEntry
*));
389 __bound_empty_t2
= __bound_new_page();
390 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
391 __bound_t1
[i
] = __bound_empty_t2
;
394 page
= __bound_new_page();
395 for(i
=0;i
<BOUND_T2_SIZE
;i
++) {
396 /* put invalid entries */
398 page
[i
].size
= INVALID_SIZE
;
400 page
[i
].is_invalid
= 1;
402 __bound_invalid_t2
= page
;
404 /* invalid pointer zone */
405 start
= (unsigned long)INVALID_POINTER
& ~(BOUND_T23_SIZE
- 1);
406 size
= BOUND_T23_SIZE
;
407 mark_invalid(start
, size
);
409 #if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
410 /* malloc zone is also marked invalid. can only use that with
411 hooks because all libs should use the same malloc. The solution
412 would be to build a new malloc for tcc. */
413 start
= (unsigned long)&_end
;
414 size
= 128 * 0x100000;
415 mark_invalid(start
, size
);
418 /* add all static bound check values */
419 p
= (int *)&__bounds_start
;
421 __bound_new_region((void *)p
[0], p
[1]);
426 void __bound_exit(void)
428 restore_malloc_hooks();
431 static inline void add_region(BoundEntry
*e
,
432 unsigned long start
, unsigned long size
)
436 /* no region : add it */
440 /* already regions in the list: add it at the head */
441 e1
= bound_new_entry();
442 e1
->start
= e
->start
;
451 /* create a new region. It should not already exist in the region list */
452 void __bound_new_region(void *p
, unsigned long size
)
454 unsigned long start
, end
;
455 BoundEntry
*page
, *e
, *e2
;
456 int t1_start
, t1_end
, i
, t2_start
, t2_end
;
458 start
= (unsigned long)p
;
460 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
461 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
464 page
= get_page(t1_start
);
465 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
466 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
467 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
468 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
470 printf("new %lx %lx %x %x %x %x\n",
471 start
, end
, t1_start
, t1_end
, t2_start
, t2_end
);
474 e
= (BoundEntry
*)((char *)page
+ t2_start
);
475 add_region(e
, start
, size
);
477 if (t1_end
== t1_start
) {
478 /* same ending page */
479 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
486 add_region(e
, start
, size
);
489 /* mark until end of page */
490 e2
= page
+ BOUND_T2_SIZE
;
496 /* mark intermediate pages, if any */
497 for(i
=t1_start
+1;i
<t1_end
;i
++) {
499 e2
= page
+ BOUND_T2_SIZE
;
500 for(e
=page
;e
<e2
;e
++) {
506 page
= get_page(t1_end
);
507 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
508 for(e
=page
;e
<e2
;e
++) {
512 add_region(e
, start
, size
);
516 /* delete a region */
517 static inline void delete_region(BoundEntry
*e
,
518 void *p
, unsigned long empty_size
)
523 addr
= (unsigned long)p
;
525 if (addr
<= e
->size
) {
526 /* region found is first one */
529 /* no more region: mark it empty */
531 e
->size
= empty_size
;
533 /* copy next region in head */
534 e
->start
= e1
->start
;
537 bound_free_entry(e1
);
540 /* find the matching region */
544 /* region not found: do nothing */
547 addr
= (unsigned long)p
- e
->start
;
548 if (addr
<= e
->size
) {
549 /* found: remove entry */
558 /* WARNING: 'p' must be the starting point of the region. */
559 /* return non zero if error */
560 int __bound_delete_region(void *p
)
562 unsigned long start
, end
, addr
, size
, empty_size
;
563 BoundEntry
*page
, *e
, *e2
;
564 int t1_start
, t1_end
, t2_start
, t2_end
, i
;
566 start
= (unsigned long)p
;
567 t1_start
= start
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
568 t2_start
= (start
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
569 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
571 /* find region size */
572 page
= __bound_t1
[t1_start
];
573 e
= (BoundEntry
*)((char *)page
+ t2_start
);
574 addr
= start
- e
->start
;
576 e
= __bound_find_region(e
, p
);
577 /* test if invalid region */
578 if (e
->size
== EMPTY_SIZE
|| (unsigned long)p
!= e
->start
)
580 /* compute the size we put in invalid regions */
582 empty_size
= INVALID_SIZE
;
584 empty_size
= EMPTY_SIZE
;
588 /* now we can free each entry */
589 t1_end
= end
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
);
590 t2_end
= (end
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
591 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
);
593 delete_region(e
, p
, empty_size
);
594 if (t1_end
== t1_start
) {
595 /* same ending page */
596 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
601 e
->size
= empty_size
;
603 delete_region(e
, p
, empty_size
);
606 /* mark until end of page */
607 e2
= page
+ BOUND_T2_SIZE
;
611 e
->size
= empty_size
;
613 /* mark intermediate pages, if any */
614 /* XXX: should free them */
615 for(i
=t1_start
+1;i
<t1_end
;i
++) {
617 e2
= page
+ BOUND_T2_SIZE
;
618 for(e
=page
;e
<e2
;e
++) {
620 e
->size
= empty_size
;
624 page
= get_page(t2_end
);
625 e2
= (BoundEntry
*)((char *)page
+ t2_end
);
626 for(e
=page
;e
<e2
;e
++) {
628 e
->size
= empty_size
;
630 delete_region(e
, p
, empty_size
);
635 /* return the size of the region starting at p, or EMPTY_SIZE if non
637 static unsigned long get_region_size(void *p
)
639 unsigned long addr
= (unsigned long)p
;
642 e
= __bound_t1
[addr
>> (BOUND_T2_BITS
+ BOUND_T3_BITS
)];
643 e
= (BoundEntry
*)((char *)e
+
644 ((addr
>> (BOUND_T3_BITS
- BOUND_E_BITS
)) &
645 ((BOUND_T2_SIZE
- 1) << BOUND_E_BITS
)));
648 e
= __bound_find_region(e
, p
);
649 if (e
->start
!= (unsigned long)p
)
654 /* patched memory functions */
656 static void install_malloc_hooks(void)
658 #ifdef CONFIG_TCC_MALLOC_HOOKS
659 saved_malloc_hook
= __malloc_hook
;
660 saved_free_hook
= __free_hook
;
661 saved_realloc_hook
= __realloc_hook
;
662 saved_memalign_hook
= __memalign_hook
;
663 __malloc_hook
= __bound_malloc
;
664 __free_hook
= __bound_free
;
665 __realloc_hook
= __bound_realloc
;
666 __memalign_hook
= __bound_memalign
;
670 static void restore_malloc_hooks(void)
672 #ifdef CONFIG_TCC_MALLOC_HOOKS
673 __malloc_hook
= saved_malloc_hook
;
674 __free_hook
= saved_free_hook
;
675 __realloc_hook
= saved_realloc_hook
;
676 __memalign_hook
= saved_memalign_hook
;
680 static void *libc_malloc(size_t size
)
683 restore_malloc_hooks();
685 install_malloc_hooks();
689 static void libc_free(void *ptr
)
691 restore_malloc_hooks();
693 install_malloc_hooks();
696 /* XXX: we should use a malloc which ensure that it is unlikely that
697 two malloc'ed data have the same address if 'free' are made in
699 void *__bound_malloc(size_t size
, const void *caller
)
703 /* we allocate one more byte to ensure the regions will be
704 separated by at least one byte. With the glibc malloc, it may
705 be in fact not necessary */
706 ptr
= libc_malloc(size
+ 1);
710 __bound_new_region(ptr
, size
);
714 void *__bound_memalign(size_t size
, size_t align
, const void *caller
)
718 restore_malloc_hooks();
720 #ifndef HAVE_MEMALIGN
722 /* XXX: handle it ? */
725 /* we suppose that malloc aligns to at least four bytes */
726 ptr
= malloc(size
+ 1);
729 /* we allocate one more byte to ensure the regions will be
730 separated by at least one byte. With the glibc malloc, it may
731 be in fact not necessary */
732 ptr
= memalign(size
+ 1, align
);
735 install_malloc_hooks();
739 __bound_new_region(ptr
, size
);
743 void __bound_free(void *ptr
, const void *caller
)
747 if (__bound_delete_region(ptr
) != 0)
748 bound_error("freeing invalid region");
753 void *__bound_realloc(void *ptr
, size_t size
, const void *caller
)
759 __bound_free(ptr
, caller
);
762 ptr1
= __bound_malloc(size
, caller
);
763 if (ptr
== NULL
|| ptr1
== NULL
)
765 old_size
= get_region_size(ptr
);
766 if (old_size
== EMPTY_SIZE
)
767 bound_error("realloc'ing invalid pointer");
768 memcpy(ptr1
, ptr
, old_size
);
769 __bound_free(ptr
, caller
);
774 #ifndef CONFIG_TCC_MALLOC_HOOKS
775 void *__bound_calloc(size_t nmemb
, size_t size
)
779 ptr
= __bound_malloc(size
, NULL
);
782 memset(ptr
, 0, size
);
788 static void bound_dump(void)
790 BoundEntry
*page
, *e
;
793 printf("region dump:\n");
794 for(i
=0;i
<BOUND_T1_SIZE
;i
++) {
795 page
= __bound_t1
[i
];
796 for(j
=0;j
<BOUND_T2_SIZE
;j
++) {
798 /* do not print invalid or empty entries */
799 if (e
->size
!= EMPTY_SIZE
&& e
->start
!= 0) {
801 (i
<< (BOUND_T2_BITS
+ BOUND_T3_BITS
)) +
802 (j
<< BOUND_T3_BITS
));
804 printf(" %08lx:%08lx", e
->start
, e
->start
+ e
->size
);
814 /* some useful checked functions */
816 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
817 static void __bound_check(const void *p
, size_t size
)
821 p
= __bound_ptr_add((void *)p
, size
);
822 if (p
== INVALID_POINTER
)
823 bound_error("invalid pointer");
826 void *__bound_memcpy(void *dst
, const void *src
, size_t size
)
828 __bound_check(dst
, size
);
829 __bound_check(src
, size
);
830 /* check also region overlap */
831 if (src
>= dst
&& src
< dst
+ size
)
832 bound_error("overlapping regions in memcpy()");
833 return memcpy(dst
, src
, size
);
836 void *__bound_memmove(void *dst
, const void *src
, size_t size
)
838 __bound_check(dst
, size
);
839 __bound_check(src
, size
);
840 return memmove(dst
, src
, size
);
843 void *__bound_memset(void *dst
, int c
, size_t size
)
845 __bound_check(dst
, size
);
846 return memset(dst
, c
, size
);
849 /* XXX: could be optimized */
850 int __bound_strlen(const char *s
)
857 p
= __bound_ptr_indir1((char *)s
, len
);
858 if (p
== INVALID_POINTER
)
859 bound_error("bad pointer in strlen()");
867 char *__bound_strcpy(char *dst
, const char *src
)
870 len
= __bound_strlen(src
);
871 return __bound_memcpy(dst
, src
, len
+ 1);