a small revers for bcheck.o changes (d80593bc4d43)
[tinycc.git] / lib / bcheck.c
blob6f47f808a81892f850f56a56a05fc48338fafa10
1 /*
2 * Tiny C Memory and bounds checker
3 *
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
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) \
25 && !defined(__DragonFly__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
26 #include <malloc.h>
27 #endif
28 #if !defined(_WIN32)
29 #include <unistd.h>
30 #endif
32 /* #define BOUND_DEBUG */
34 /* define so that bound array is static (faster, but use memory if
35 bound checking not used) */
36 /* #define BOUND_STATIC */
38 /* use malloc hooks. Currently the code cannot be reliable if no hooks */
39 #define CONFIG_TCC_MALLOC_HOOKS
40 #define HAVE_MEMALIGN
42 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
43 || defined(__DragonFly__) || defined(__dietlibc__) \
44 || defined(__UCLIBC__) || defined(__OpenBSD__) || defined(__NetBSD__) \
45 || defined(_WIN32) || defined(TCC_UCLIBC)
46 #warning Bound checking does not support malloc (etc.) in this environment.
47 #undef CONFIG_TCC_MALLOC_HOOKS
48 #undef HAVE_MEMALIGN
49 #endif
51 #define BOUND_T1_BITS 13
52 #define BOUND_T2_BITS 11
53 #define BOUND_T3_BITS (32 - BOUND_T1_BITS - BOUND_T2_BITS)
55 #define BOUND_T1_SIZE (1 << BOUND_T1_BITS)
56 #define BOUND_T2_SIZE (1 << BOUND_T2_BITS)
57 #define BOUND_T3_SIZE (1 << BOUND_T3_BITS)
58 #define BOUND_E_BITS 4
60 #define BOUND_T23_BITS (BOUND_T2_BITS + BOUND_T3_BITS)
61 #define BOUND_T23_SIZE (1 << BOUND_T23_BITS)
64 /* this pointer is generated when bound check is incorrect */
65 #define INVALID_POINTER ((void *)(-2))
66 /* size of an empty region */
67 #define EMPTY_SIZE 0xffffffff
68 /* size of an invalid region */
69 #define INVALID_SIZE 0
71 typedef struct BoundEntry {
72 size_t start;
73 size_t size;
74 struct BoundEntry *next;
75 size_t is_invalid; /* true if pointers outside region are invalid */
76 } BoundEntry;
78 /* external interface */
79 void __bound_init(void);
80 void __bound_new_region(void *p, size_t size);
81 int __bound_delete_region(void *p);
83 #ifdef __attribute__
84 /* an __attribute__ macro is defined in the system headers */
85 #undef __attribute__
86 #endif
87 #define FASTCALL __attribute__((regparm(3)))
89 void *__bound_malloc(size_t size, const void *caller);
90 void *__bound_memalign(size_t size, size_t align, const void *caller);
91 void __bound_free(void *ptr, const void *caller);
92 void *__bound_realloc(void *ptr, size_t size, const void *caller);
93 static void *libc_malloc(size_t size);
94 static void libc_free(void *ptr);
95 static void install_malloc_hooks(void);
96 static void restore_malloc_hooks(void);
98 #ifdef CONFIG_TCC_MALLOC_HOOKS
99 static void *saved_malloc_hook;
100 static void *saved_free_hook;
101 static void *saved_realloc_hook;
102 static void *saved_memalign_hook;
103 #endif
105 /* TCC definitions */
106 extern char __bounds_start; /* start of static bounds table */
107 /* error message, just for TCC */
108 const char *__bound_error_msg;
110 /* runtime error output */
111 extern void rt_error(size_t pc, const char *fmt, ...);
113 #ifdef BOUND_STATIC
114 static BoundEntry *__bound_t1[BOUND_T1_SIZE]; /* page table */
115 #else
116 static BoundEntry **__bound_t1; /* page table */
117 #endif
118 static BoundEntry *__bound_empty_t2; /* empty page, for unused pages */
119 static BoundEntry *__bound_invalid_t2; /* invalid page, for invalid pointers */
121 static BoundEntry *__bound_find_region(BoundEntry *e1, void *p)
123 size_t addr, tmp;
124 BoundEntry *e;
126 e = e1;
127 while (e != NULL) {
128 addr = (size_t)p;
129 addr -= e->start;
130 if (addr <= e->size) {
131 /* put region at the head */
132 tmp = e1->start;
133 e1->start = e->start;
134 e->start = tmp;
135 tmp = e1->size;
136 e1->size = e->size;
137 e->size = tmp;
138 return e1;
140 e = e->next;
142 /* no entry found: return empty entry or invalid entry */
143 if (e1->is_invalid)
144 return __bound_invalid_t2;
145 else
146 return __bound_empty_t2;
149 /* print a bound error message */
150 static void bound_error(const char *fmt, ...)
152 __bound_error_msg = fmt;
153 *(int *)0 = 0; /* force a runtime error */
156 static void bound_alloc_error(void)
158 bound_error("not enough memory for bound checking code");
161 /* return '(p + offset)' for pointer arithmetic (a pointer can reach
162 the end of a region in this case */
163 void * FASTCALL __bound_ptr_add(void *p, size_t offset)
165 size_t addr = (size_t)p;
166 BoundEntry *e;
167 #if defined(BOUND_DEBUG)
168 printf("%s %s: 0x%x %d\n", __FILE__, __FUNCTION__, (int)p, offset);
169 #endif
171 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
172 e = (BoundEntry *)((char *)e +
173 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
174 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
175 addr -= e->start;
176 if (addr > e->size) {
177 e = __bound_find_region(e, p);
178 addr = (size_t)p - e->start;
180 addr += offset;
181 if (addr >= e->size) {
182 #if defined(BOUND_DEBUG)
183 printf("%s %s: 0x%zx is outside of the region\n", __FILE__, __FUNCTION__, p + offset);
184 #endif
185 return INVALID_POINTER; /* return an invalid pointer */
187 return p + offset;
190 /* return '(p + offset)' for pointer indirection (the resulting must
191 be strictly inside the region */
192 #define BOUND_PTR_INDIR(dsize) \
193 void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \
195 size_t addr = (size_t)p; \
196 BoundEntry *e; \
198 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
199 e = (BoundEntry *)((char *)e + \
200 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
201 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \
202 addr -= e->start; \
203 if (addr > e->size) { \
204 e = __bound_find_region(e, p); \
205 addr = (size_t)p - e->start; \
207 addr += offset + dsize; \
208 if (addr > e->size) \
209 return INVALID_POINTER; /* return an invalid pointer */ \
210 return p + offset; \
213 BOUND_PTR_INDIR(1)
214 BOUND_PTR_INDIR(2)
215 BOUND_PTR_INDIR(4)
216 BOUND_PTR_INDIR(8)
217 BOUND_PTR_INDIR(12)
218 BOUND_PTR_INDIR(16)
220 /* return the frame pointer of the caller */
221 #define GET_CALLER_FP(fp)\
223 fp = (size_t)__builtin_frame_address(1);\
226 /* called when entering a function to add all the local regions */
227 void FASTCALL __bound_local_new(void *p1)
229 size_t addr, size, fp, *p = p1;
230 #ifdef BOUND_DEBUG
231 fprintf(stderr, "%s, %s start p1=%zx *p1=%zx\n", __FILE__, __FUNCTION__, p, *p);
232 #endif
233 GET_CALLER_FP(fp);
234 for(;;) {
235 addr = p[0];
236 if (addr == 0)
237 break;
238 addr += fp;
239 size = p[1];
240 p += 2;
241 __bound_new_region((void *)addr, size);
243 #ifdef BOUND_DEBUG
244 fprintf(stderr, "%s, %s end\n", __FILE__, __FUNCTION__);
245 #endif
248 /* called when leaving a function to delete all the local regions */
249 void FASTCALL __bound_local_delete(void *p1)
251 size_t addr, fp, *p = p1;
252 GET_CALLER_FP(fp);
253 for(;;) {
254 addr = p[0];
255 if (addr == 0)
256 break;
257 addr += fp;
258 p += 2;
259 __bound_delete_region((void *)addr);
263 static BoundEntry *__bound_new_page(void)
265 BoundEntry *page;
266 int i;
268 page = libc_malloc(sizeof(BoundEntry) * BOUND_T2_SIZE);
269 if (!page)
270 bound_alloc_error();
271 for(i=0;i<BOUND_T2_SIZE;i++) {
272 /* put empty entries */
273 page[i].start = 0;
274 page[i].size = EMPTY_SIZE;
275 page[i].next = NULL;
276 page[i].is_invalid = 0;
278 return page;
281 /* currently we use malloc(). Should use bound_new_page() */
282 static BoundEntry *bound_new_entry(void)
284 BoundEntry *e;
285 e = libc_malloc(sizeof(BoundEntry));
286 return e;
289 static void bound_free_entry(BoundEntry *e)
291 libc_free(e);
294 static inline BoundEntry *get_page(int index)
296 BoundEntry *page;
297 page = __bound_t1[index];
298 if (page == __bound_empty_t2 || page == __bound_invalid_t2) {
299 /* create a new page if necessary */
300 page = __bound_new_page();
301 __bound_t1[index] = page;
303 return page;
306 /* mark a region as being invalid (can only be used during init) */
307 static void mark_invalid(size_t addr, size_t size)
309 size_t start, end;
310 BoundEntry *page;
311 size_t t1_start, t1_end, i, j, t2_start, t2_end;
313 start = addr;
314 end = addr + size;
316 t2_start = (start + BOUND_T3_SIZE - 1) >> BOUND_T3_BITS;
317 if (end != 0)
318 t2_end = end >> BOUND_T3_BITS;
319 else
320 t2_end = 1 << (BOUND_T1_BITS + BOUND_T2_BITS);
322 #if 0
323 printf("mark_invalid: start = %x %x\n", t2_start, t2_end);
324 #endif
326 /* first we handle full pages */
327 t1_start = (t2_start + BOUND_T2_SIZE - 1) >> BOUND_T2_BITS;
328 t1_end = t2_end >> BOUND_T2_BITS;
330 i = t2_start & (BOUND_T2_SIZE - 1);
331 j = t2_end & (BOUND_T2_SIZE - 1);
333 if (t1_start == t1_end) {
334 page = get_page(t2_start >> BOUND_T2_BITS);
335 for(; i < j; i++) {
336 page[i].size = INVALID_SIZE;
337 page[i].is_invalid = 1;
339 } else {
340 if (i > 0) {
341 page = get_page(t2_start >> BOUND_T2_BITS);
342 for(; i < BOUND_T2_SIZE; i++) {
343 page[i].size = INVALID_SIZE;
344 page[i].is_invalid = 1;
347 for(i = t1_start; i < t1_end; i++) {
348 __bound_t1[i] = __bound_invalid_t2;
350 if (j != 0) {
351 page = get_page(t1_end);
352 for(i = 0; i < j; i++) {
353 page[i].size = INVALID_SIZE;
354 page[i].is_invalid = 1;
360 void __bound_init(void)
362 int i;
363 BoundEntry *page;
364 size_t start, size;
365 size_t *p;
367 #ifdef BOUND_DEBUG
368 fprintf(stderr, "%s, %s() start\n", __FILE__, __FUNCTION__);
369 #endif
371 /* save malloc hooks and install bound check hooks */
372 install_malloc_hooks();
374 #ifndef BOUND_STATIC
375 __bound_t1 = libc_malloc(BOUND_T1_SIZE * sizeof(BoundEntry *));
376 if (!__bound_t1)
377 bound_alloc_error();
378 #endif
379 __bound_empty_t2 = __bound_new_page();
380 for(i=0;i<BOUND_T1_SIZE;i++) {
381 __bound_t1[i] = __bound_empty_t2;
384 page = __bound_new_page();
385 for(i=0;i<BOUND_T2_SIZE;i++) {
386 /* put invalid entries */
387 page[i].start = 0;
388 page[i].size = INVALID_SIZE;
389 page[i].next = NULL;
390 page[i].is_invalid = 1;
392 __bound_invalid_t2 = page;
394 /* invalid pointer zone */
395 start = (size_t)INVALID_POINTER & ~(BOUND_T23_SIZE - 1);
396 size = BOUND_T23_SIZE;
397 mark_invalid(start, size);
399 #if defined(CONFIG_TCC_MALLOC_HOOKS)
400 /* malloc zone is also marked invalid. can only use that with
401 * hooks because all libs should use the same malloc. The solution
402 * would be to build a new malloc for tcc.
404 * usually heap (= malloc zone) comes right after bss, i.e. after _end, but
405 * not always - either if we are running from under `tcc -b -run`, or if
406 * address space randomization is turned on(a), heap start will be separated
407 * from bss end.
409 * So sbrk(0) will be a good approximation for start_brk:
411 * - if we are a separately compiled program, __bound_init() runs early,
412 * and sbrk(0) should be equal or very near to start_brk(b) (in case other
413 * constructors malloc something), or
415 * - if we are running from under `tcc -b -run`, sbrk(0) will return
416 * start of heap portion which is under this program control, and not
417 * mark as invalid earlier allocated memory.
420 * (a) /proc/sys/kernel/randomize_va_space = 2, on Linux;
421 * usually turned on by default.
423 * (b) on Linux >= v3.3, the alternative is to read
424 * start_brk from /proc/self/stat
426 start = (size_t)sbrk(0);
427 size = 128 * 0x100000;
428 mark_invalid(start, size);
429 #endif
431 /* add all static bound check values */
432 p = (size_t *)&__bounds_start;
433 while (p[0] != 0) {
434 __bound_new_region((void *)p[0], p[1]);
435 p += 2;
437 #ifdef BOUND_DEBUG
438 fprintf(stderr, "%s, %s() end\n\n", __FILE__, __FUNCTION__);
439 #endif
442 void __bound_main_arg(void **p)
444 void *start = p;
445 while (*p++);
446 #ifdef BOUND_DEBUG
447 fprintf(stderr, "%s, %s calling __bound_new_region(%p, %llx)\n",
448 __FILE__, __FUNCTION__, (void *) p - start);
449 #endif
450 __bound_new_region(start, (void *) p - start);
453 void __bound_exit(void)
455 restore_malloc_hooks();
458 static inline void add_region(BoundEntry *e,
459 size_t start, size_t size)
461 BoundEntry *e1;
462 if (e->start == 0) {
463 /* no region : add it */
464 e->start = start;
465 e->size = size;
466 } else {
467 /* already regions in the list: add it at the head */
468 e1 = bound_new_entry();
469 e1->start = e->start;
470 e1->size = e->size;
471 e1->next = e->next;
472 e->start = start;
473 e->size = size;
474 e->next = e1;
478 /* create a new region. It should not already exist in the region list */
479 void __bound_new_region(void *p, size_t size)
481 size_t start, end;
482 BoundEntry *page, *e, *e2;
483 size_t t1_start, t1_end, i, t2_start, t2_end;
485 #ifdef BOUND_DEBUG
486 fprintf(stderr, "%s, %s(%p, %zx) start\n",
487 __FILE__, __FUNCTION__, p, size);
488 #endif
490 start = (size_t)p;
491 end = start + size;
492 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
493 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
495 /* start */
496 page = get_page(t1_start);
497 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
498 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
499 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
500 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
503 e = (BoundEntry *)((char *)page + t2_start);
504 add_region(e, start, size);
506 if (t1_end == t1_start) {
507 /* same ending page */
508 e2 = (BoundEntry *)((char *)page + t2_end);
509 if (e2 > e) {
510 e++;
511 for(;e<e2;e++) {
512 e->start = start;
513 e->size = size;
515 add_region(e, start, size);
517 } else {
518 /* mark until end of page */
519 e2 = page + BOUND_T2_SIZE;
520 e++;
521 for(;e<e2;e++) {
522 e->start = start;
523 e->size = size;
525 /* mark intermediate pages, if any */
526 for(i=t1_start+1;i<t1_end;i++) {
527 page = get_page(i);
528 e2 = page + BOUND_T2_SIZE;
529 for(e=page;e<e2;e++) {
530 e->start = start;
531 e->size = size;
534 /* last page */
535 page = get_page(t1_end);
536 e2 = (BoundEntry *)((char *)page + t2_end);
537 for(e=page;e<e2;e++) {
538 e->start = start;
539 e->size = size;
541 add_region(e, start, size);
543 #ifdef BOUND_DEBUG
544 fprintf(stderr, "%s, %s end\n", __FILE__, __FUNCTION__);
545 #endif
548 /* delete a region */
549 static inline void delete_region(BoundEntry *e,
550 void *p, size_t empty_size)
552 size_t addr;
553 BoundEntry *e1;
555 addr = (size_t)p;
556 addr -= e->start;
557 if (addr <= e->size) {
558 /* region found is first one */
559 e1 = e->next;
560 if (e1 == NULL) {
561 /* no more region: mark it empty */
562 e->start = 0;
563 e->size = empty_size;
564 } else {
565 /* copy next region in head */
566 e->start = e1->start;
567 e->size = e1->size;
568 e->next = e1->next;
569 bound_free_entry(e1);
571 } else {
572 /* find the matching region */
573 for(;;) {
574 e1 = e;
575 e = e->next;
576 /* region not found: do nothing */
577 if (e == NULL)
578 break;
579 addr = (size_t)p - e->start;
580 if (addr <= e->size) {
581 /* found: remove entry */
582 e1->next = e->next;
583 bound_free_entry(e);
584 break;
590 /* WARNING: 'p' must be the starting point of the region. */
591 /* return non zero if error */
592 int __bound_delete_region(void *p)
594 size_t start, end, addr, size, empty_size;
595 BoundEntry *page, *e, *e2;
596 size_t t1_start, t1_end, t2_start, t2_end, i;
598 #ifdef BOUND_DEBUG
599 fprintf(stderr, "%s %s() start\n", __FILE__, __FUNCTION__);
600 #endif
602 start = (size_t)p;
603 t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS);
604 t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) &
605 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
607 /* find region size */
608 page = __bound_t1[t1_start];
609 e = (BoundEntry *)((char *)page + t2_start);
610 addr = start - e->start;
611 if (addr > e->size)
612 e = __bound_find_region(e, p);
613 /* test if invalid region */
614 if (e->size == EMPTY_SIZE || (size_t)p != e->start)
615 return -1;
616 /* compute the size we put in invalid regions */
617 if (e->is_invalid)
618 empty_size = INVALID_SIZE;
619 else
620 empty_size = EMPTY_SIZE;
621 size = e->size;
622 end = start + size;
624 /* now we can free each entry */
625 t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS);
626 t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) &
627 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS);
629 delete_region(e, p, empty_size);
630 if (t1_end == t1_start) {
631 /* same ending page */
632 e2 = (BoundEntry *)((char *)page + t2_end);
633 if (e2 > e) {
634 e++;
635 for(;e<e2;e++) {
636 e->start = 0;
637 e->size = empty_size;
639 delete_region(e, p, empty_size);
641 } else {
642 /* mark until end of page */
643 e2 = page + BOUND_T2_SIZE;
644 e++;
645 for(;e<e2;e++) {
646 e->start = 0;
647 e->size = empty_size;
649 /* mark intermediate pages, if any */
650 /* XXX: should free them */
651 for(i=t1_start+1;i<t1_end;i++) {
652 page = get_page(i);
653 e2 = page + BOUND_T2_SIZE;
654 for(e=page;e<e2;e++) {
655 e->start = 0;
656 e->size = empty_size;
659 /* last page */
660 page = get_page(t1_end);
661 e2 = (BoundEntry *)((char *)page + t2_end);
662 for(e=page;e<e2;e++) {
663 e->start = 0;
664 e->size = empty_size;
666 delete_region(e, p, empty_size);
669 #ifdef BOUND_DEBUG
670 fprintf(stderr, "%s %s() end\n", __FILE__, __FUNCTION__);
671 #endif
673 return 0;
676 /* return the size of the region starting at p, or EMPTY_SIZE if non
677 existent region. */
678 static size_t get_region_size(void *p)
680 size_t addr = (size_t)p;
681 BoundEntry *e;
683 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)];
684 e = (BoundEntry *)((char *)e +
685 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) &
686 ((BOUND_T2_SIZE - 1) << BOUND_E_BITS)));
687 addr -= e->start;
688 if (addr > e->size)
689 e = __bound_find_region(e, p);
690 if (e->start != (size_t)p)
691 return EMPTY_SIZE;
692 return e->size;
695 /* patched memory functions */
697 /* force compiler to perform stores coded up to this point */
698 #define barrier() __asm__ __volatile__ ("": : : "memory")
700 static void install_malloc_hooks(void)
702 #ifdef CONFIG_TCC_MALLOC_HOOKS
703 saved_malloc_hook = __malloc_hook;
704 saved_free_hook = __free_hook;
705 saved_realloc_hook = __realloc_hook;
706 saved_memalign_hook = __memalign_hook;
707 __malloc_hook = __bound_malloc;
708 __free_hook = __bound_free;
709 __realloc_hook = __bound_realloc;
710 __memalign_hook = __bound_memalign;
712 barrier();
713 #endif
716 static void restore_malloc_hooks(void)
718 #ifdef CONFIG_TCC_MALLOC_HOOKS
719 __malloc_hook = saved_malloc_hook;
720 __free_hook = saved_free_hook;
721 __realloc_hook = saved_realloc_hook;
722 __memalign_hook = saved_memalign_hook;
724 barrier();
725 #endif
728 static void *libc_malloc(size_t size)
730 void *ptr;
731 restore_malloc_hooks();
732 ptr = malloc(size);
733 install_malloc_hooks();
734 return ptr;
737 static void libc_free(void *ptr)
739 restore_malloc_hooks();
740 free(ptr);
741 install_malloc_hooks();
744 /* XXX: we should use a malloc which ensure that it is unlikely that
745 two malloc'ed data have the same address if 'free' are made in
746 between. */
747 void *__bound_malloc(size_t size, const void *caller)
749 void *ptr;
751 /* we allocate one more byte to ensure the regions will be
752 separated by at least one byte. With the glibc malloc, it may
753 be in fact not necessary */
754 ptr = libc_malloc(size + 1);
756 if (!ptr)
757 return NULL;
759 #ifdef BOUND_DEBUG
760 fprintf(stderr, "%s, %s calling __bound_new_region(%p, %llx)\n",
761 __FILE__, __FUNCTION__, ptr, size);
762 #endif
763 __bound_new_region(ptr, size);
764 return ptr;
767 void *__bound_memalign(size_t size, size_t align, const void *caller)
769 void *ptr;
771 restore_malloc_hooks();
773 #ifndef HAVE_MEMALIGN
774 if (align > 4) {
775 /* XXX: handle it ? */
776 ptr = NULL;
777 } else {
778 /* we suppose that malloc aligns to at least four bytes */
779 ptr = malloc(size + 1);
781 #else
782 /* we allocate one more byte to ensure the regions will be
783 separated by at least one byte. With the glibc malloc, it may
784 be in fact not necessary */
785 ptr = memalign(size + 1, align);
786 #endif
788 install_malloc_hooks();
790 if (!ptr)
791 return NULL;
793 #ifdef BOUND_DEBUG
794 fprintf(stderr, "%s, %s calling __bound_new_region(%p, %llx)\n",
795 __FILE__, __FUNCTION__, ptr, size);
796 #endif
797 __bound_new_region(ptr, size);
798 return ptr;
801 void __bound_free(void *ptr, const void *caller)
803 if (ptr == NULL)
804 return;
805 if (__bound_delete_region(ptr) != 0)
806 bound_error("freeing invalid region");
808 libc_free(ptr);
811 void *__bound_realloc(void *ptr, size_t size, const void *caller)
813 void *ptr1;
814 size_t old_size;
816 if (size == 0) {
817 __bound_free(ptr, caller);
818 return NULL;
819 } else {
820 ptr1 = __bound_malloc(size, caller);
821 if (ptr == NULL || ptr1 == NULL)
822 return ptr1;
823 old_size = get_region_size(ptr);
824 if (old_size == EMPTY_SIZE)
825 bound_error("realloc'ing invalid pointer");
826 memcpy(ptr1, ptr, old_size);
827 __bound_free(ptr, caller);
828 return ptr1;
832 #ifndef CONFIG_TCC_MALLOC_HOOKS
833 void *__bound_calloc(size_t nmemb, size_t size)
835 void *ptr;
836 size = size * nmemb;
837 ptr = __bound_malloc(size, NULL);
838 if (!ptr)
839 return NULL;
840 memset(ptr, 0, size);
841 return ptr;
843 #endif
845 #if 0
846 static void bound_dump(void)
848 BoundEntry *page, *e;
849 int i, j;
851 printf("region dump:\n");
852 for(i=0;i<BOUND_T1_SIZE;i++) {
853 page = __bound_t1[i];
854 for(j=0;j<BOUND_T2_SIZE;j++) {
855 e = page + j;
856 /* do not print invalid or empty entries */
857 if (e->size != EMPTY_SIZE && e->start != 0) {
858 printf("%08x:",
859 (i << (BOUND_T2_BITS + BOUND_T3_BITS)) +
860 (j << BOUND_T3_BITS));
861 do {
862 printf(" %08lx:%08lx", e->start, e->start + e->size);
863 e = e->next;
864 } while (e != NULL);
865 printf("\n");
870 #endif
872 /* some useful checked functions */
874 /* check that (p ... p + size - 1) lies inside 'p' region, if any */
875 static void __bound_check(const void *p, size_t size)
877 if (size == 0)
878 return;
879 p = __bound_ptr_add((void *)p, size);
880 if (p == INVALID_POINTER)
881 bound_error("invalid pointer");
884 void *__bound_memcpy(void *dst, const void *src, size_t size)
886 __bound_check(dst, size);
887 __bound_check(src, size);
888 /* check also region overlap */
889 if (src >= dst && src < dst + size)
890 bound_error("overlapping regions in memcpy()");
891 return memcpy(dst, src, size);
894 void *__bound_memmove(void *dst, const void *src, size_t size)
896 __bound_check(dst, size);
897 __bound_check(src, size);
898 return memmove(dst, src, size);
901 void *__bound_memset(void *dst, int c, size_t size)
903 __bound_check(dst, size);
904 return memset(dst, c, size);
907 /* XXX: could be optimized */
908 int __bound_strlen(const char *s)
910 const char *p;
911 size_t len;
913 len = 0;
914 for(;;) {
915 p = __bound_ptr_indir1((char *)s, len);
916 if (p == INVALID_POINTER)
917 bound_error("bad pointer in strlen()");
918 if (*p == '\0')
919 break;
920 len++;
922 return len;
925 char *__bound_strcpy(char *dst, const char *src)
927 int len;
928 len = __bound_strlen(src);
929 return __bound_memcpy(dst, src, len + 1);