Unified · Split
| @@ -17,6 +17,11 @@ · 16 unmodified lines | |||
| 17 | * along with this program; if not, write to the Free Software | 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ | 19 | */ |
| 20 | #include <stdlib.h> | ||
| 21 | #include <stdio.h> | ||
| 22 | #include <stdarg.h> | ||
| 23 | #include <string.h> | ||
| 24 | #include <malloc.h> | ||
| 20 | 25 | ||
| 21 | //#define BOUND_DEBUG | 26 | //#define BOUND_DEBUG |
| 22 | 27 | ||
| @@ -92,6 +97,11 @@ static void *saved_memalign_hook; · 69 unmodified lines | |||
| 92 | 97 | ||
| 93 | extern char _end; | 98 | extern char _end; |
| 94 | 99 | ||
| 100 | /* TCC defines: */ | ||
| 101 | extern char __bounds_start; /* start of static bounds table */ | ||
| 102 | /* runtime error output */ | ||
| 103 | extern void rt_error(unsigned long pc, const char *fmt, ...); | ||
| 104 | |||
| 95 | #ifdef BOUND_STATIC | 105 | #ifdef BOUND_STATIC |
| 96 | static BoundEntry *__bound_t1[BOUND_T1_SIZE]; /* page table */ | 106 | static BoundEntry *__bound_t1[BOUND_T1_SIZE]; /* page table */ |
| 97 | #else | 107 | #else |
| @@ -381,6 +391,7 @@ void __bound_init(void) · 283 unmodified lines | |||
| 381 | int i; | 391 | int i; |
| 382 | BoundEntry *page; | 392 | BoundEntry *page; |
| 383 | unsigned long start, size; | 393 | unsigned long start, size; |
| 394 | int *p; | ||
| 384 | 395 | ||
| 385 | /* save malloc hooks and install bound check hooks */ | 396 | /* save malloc hooks and install bound check hooks */ |
| 386 | install_malloc_hooks(); | 397 | install_malloc_hooks(); |
| @@ -418,6 +429,13 @@ void __bound_init(void) · 31 unmodified lines | |||
| 418 | size = 128 * 0x100000; | 429 | size = 128 * 0x100000; |
| 419 | mark_invalid(start, size); | 430 | mark_invalid(start, size); |
| 420 | #endif | 431 | #endif |
| 432 | |||
| 433 | /* add all static bound check values */ | ||
| 434 | p = (int *)&__bounds_start; | ||
| 435 | while (p[0] != 0) { | ||
| 436 | __bound_new_region((void *)p[0], p[1]); | ||
| 437 | p += 2; | ||
| 438 | } | ||
| 421 | } | 439 | } |
| 422 | 440 | ||
| 423 | static inline void add_region(BoundEntry *e, | 441 | static inline void add_region(BoundEntry *e, |
| @@ -853,36 +871,3 @@ char *__bound_strcpy(char *dst, const char *src) · 429 unmodified lines | |||
| 853 | return __bound_memcpy(dst, src, len + 1); | 871 | return __bound_memcpy(dst, src, len + 1); |
| 854 | } | 872 | } |
| 855 | 873 | ||
| 856 | /* resolve bound check syms */ | ||
| 857 | typedef struct BCSyms { | ||
| 858 | char *str; | ||
| 859 | void *ptr; | ||
| 860 | } BCSyms; | ||
| 861 | |||
| 862 | static BCSyms bcheck_syms[] = { | ||
| 863 | #ifndef CONFIG_TCC_MALLOC_HOOKS | ||
| 864 | { "malloc", __bound_malloc }, | ||
| 865 | { "free", __bound_free }, | ||
| 866 | { "realloc", __bound_realloc }, | ||
| 867 | { "memalign", __bound_memalign }, | ||
| 868 | { "calloc", __bound_calloc }, | ||
| 869 | #endif | ||
| 870 | { "memcpy", __bound_memcpy }, | ||
| 871 | { "memmove", __bound_memmove }, | ||
| 872 | { "memset", __bound_memset }, | ||
| 873 | { "strlen", __bound_strlen }, | ||
| 874 | { "strcpy", __bound_strcpy }, | ||
| 875 | { NULL, NULL }, | ||
| 876 | }; | ||
| 877 | |||
| 878 | static void *bound_resolve_sym(const char *sym) | ||
| 879 | { | ||
| 880 | BCSyms *p; | ||
| 881 | p = bcheck_syms; | ||
| 882 | while (p->str != NULL) { | ||
| 883 | if (!strcmp(p->str, sym)) | ||
| 884 | return p->ptr; | ||
| 885 | p++; | ||
| 886 | } | ||
| 887 | return NULL; | ||
| 888 | } | ||