Unified · Split
| @@ -54,9 +54,9 @@ typedef struct BoundEntry { · 53 unmodified lines | |||
| 54 | /* external interface */ | 54 | /* external interface */ |
| 55 | void __bound_init(void); | 55 | void __bound_init(void); |
| 56 | void __bound_new_region(void *p, unsigned long size); | 56 | void __bound_new_region(void *p, unsigned long size); |
| 57 | void __bound_delete_region(void *p); | 57 | int __bound_delete_region(void *p); |
| 58 | 58 | ||
| 59 | /* currently, tcc cannot compile that because we use unsupported GNUC | 59 | /* currently, tcc cannot compile that because we use unsupported GNU C |
| 60 | extensions */ | 60 | extensions */ |
| 61 | #if !defined(__TINYC__) | 61 | #if !defined(__TINYC__) |
| 62 | void *__bound_ptr_add(void *p, int offset) __attribute__((regparm(2))); | 62 | void *__bound_ptr_add(void *p, int offset) __attribute__((regparm(2))); |
| @@ -69,6 +69,7 @@ void *__bound_ptr_indir16(void *p, int offset) __attribute__((regparm(2))); · 6 unmodified lines | |||
| 69 | void __bound_local_new(void *p) __attribute__((regparm(1))); | 69 | void __bound_local_new(void *p) __attribute__((regparm(1))); |
| 70 | void __bound_local_delete(void *p) __attribute__((regparm(1))); | 70 | void __bound_local_delete(void *p) __attribute__((regparm(1))); |
| 71 | #endif | 71 | #endif |
| 72 | static void *get_caller_pc(int n); | ||
| 72 | 73 | ||
| 73 | void *__bound_malloc(size_t size, const void *caller); | 74 | void *__bound_malloc(size_t size, const void *caller); |
| 74 | void *__bound_memalign(size_t size, size_t align, const void *caller); | 75 | void *__bound_memalign(size_t size, size_t align, const void *caller); |
| @@ -122,20 +123,16 @@ static BoundEntry *__bound_find_region(BoundEntry *e1, void *p) · 47 unmodified lines | |||
| 122 | return __bound_empty_t2; | 123 | return __bound_empty_t2; |
| 123 | } | 124 | } |
| 124 | 125 | ||
| 125 | static void bound_error(const char *fmt, ...) | 126 | /* print a bound error and recurse thru 'nb_callers' to get the error |
| 127 | position */ | ||
| 128 | static void bound_error(const void *caller, const char *fmt, ...) | ||
| 126 | { | 129 | { |
| 127 | va_list ap; | 130 | rt_error((unsigned long)caller, "%s", fmt); |
| 128 | va_start(ap, fmt); | ||
| 129 | fprintf(stderr, "bounds check: "); | ||
| 130 | vfprintf(stderr, fmt, ap); | ||
| 131 | fprintf(stderr, "\n"); | ||
| 132 | exit(1); | ||
| 133 | va_end(ap); | ||
| 134 | } | 131 | } |
| 135 | 132 | ||
| 136 | static void bound_alloc_error(void) | 133 | static void bound_alloc_error(void) |
| 137 | { | 134 | { |
| 138 | bound_error("not enough memory for bound checking code\n"); | 135 | bound_error(NULL, "not enough memory for bound checking code\n"); |
| 139 | } | 136 | } |
| 140 | 137 | ||
| 141 | /* currently, tcc cannot compile that because we use GNUC extensions */ | 138 | /* currently, tcc cannot compile that because we use GNUC extensions */ |
| @@ -190,6 +187,20 @@ void *__bound_ptr_indir ## dsize (void *p, int offset) \ · 48 unmodified lines | |||
| 190 | } | 187 | } |
| 191 | 188 | ||
| 192 | #ifdef __i386__ | 189 | #ifdef __i386__ |
| 190 | |||
| 191 | /* return the PC of the N'th caller (N=1: first caller) */ | ||
| 192 | static void *get_caller_pc(int n) | ||
| 193 | { | ||
| 194 | unsigned long fp; | ||
| 195 | int i; | ||
| 196 | |||
| 197 | __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp)); | ||
| 198 | for(i=0;i<n;i++) | ||
| 199 | fp = ((unsigned long *)fp)[0]; | ||
| 200 | return ((void **)fp)[1]; | ||
| 201 | } | ||
| 202 | |||
| 203 | /* return the frame pointer of the caller */ | ||
| 193 | #define GET_CALLER_FP(fp)\ | 204 | #define GET_CALLER_FP(fp)\ |
| 194 | {\ | 205 | {\ |
| 195 | unsigned long *fp1;\ | 206 | unsigned long *fp1;\ |
| @@ -251,6 +262,10 @@ void *__bound_ptr_indir ## dsize (void *p, int offset) \ · 55 unmodified lines | |||
| 251 | return p + offset; \ | 262 | return p + offset; \ |
| 252 | } | 263 | } |
| 253 | 264 | ||
| 265 | static void *get_caller_pc(int n) | ||
| 266 | { | ||
| 267 | return 0; | ||
| 268 | } | ||
| 254 | #endif | 269 | #endif |
| 255 | 270 | ||
| 256 | BOUND_PTR_INDIR(1) | 271 | BOUND_PTR_INDIR(1) |
| @@ -527,7 +542,8 @@ static inline void delete_region(BoundEntry *e, · 270 unmodified lines | |||
| 527 | } | 542 | } |
| 528 | 543 | ||
| 529 | /* WARNING: 'p' must be the starting point of the region. */ | 544 | /* WARNING: 'p' must be the starting point of the region. */ |
| 530 | void __bound_delete_region(void *p) | 545 | /* return non zero if error */ |
| 546 | int __bound_delete_region(void *p) | ||
| 531 | { | 547 | { |
| 532 | unsigned long start, end, addr, size, empty_size; | 548 | unsigned long start, end, addr, size, empty_size; |
| 533 | BoundEntry *page, *e, *e2; | 549 | BoundEntry *page, *e, *e2; |
| @@ -546,7 +562,7 @@ void __bound_delete_region(void *p) · 12 unmodified lines | |||
| 546 | e = __bound_find_region(e, p); | 562 | e = __bound_find_region(e, p); |
| 547 | /* test if invalid region */ | 563 | /* test if invalid region */ |
| 548 | if (e->size == EMPTY_SIZE || (unsigned long)p != e->start) | 564 | if (e->size == EMPTY_SIZE || (unsigned long)p != e->start) |
| 549 | bound_error("freeing invalid region"); | 565 | return -1; |
| 550 | /* compute the size we put in invalid regions */ | 566 | /* compute the size we put in invalid regions */ |
| 551 | if (e->is_invalid) | 567 | if (e->is_invalid) |
| 552 | empty_size = INVALID_SIZE; | 568 | empty_size = INVALID_SIZE; |
| @@ -599,6 +615,7 @@ void __bound_delete_region(void *p) · 46 unmodified lines | |||
| 599 | } | 615 | } |
| 600 | delete_region(e, p, empty_size); | 616 | delete_region(e, p, empty_size); |
| 601 | } | 617 | } |
| 618 | return 0; | ||
| 602 | } | 619 | } |
| 603 | 620 | ||
| 604 | /* return the size of the region starting at p, or EMPTY_SIZE if non | 621 | /* return the size of the region starting at p, or EMPTY_SIZE if non |
| @@ -699,7 +716,8 @@ void __bound_free(void *ptr, const void *caller) · 94 unmodified lines | |||
| 699 | { | 716 | { |
| 700 | if (ptr == NULL) | 717 | if (ptr == NULL) |
| 701 | return; | 718 | return; |
| 702 | __bound_delete_region(ptr); | 719 | if (__bound_delete_region(ptr) != 0) |
| 720 | bound_error(caller, "freeing invalid region"); | ||
| 703 | 721 | ||
| 704 | libc_free(ptr); | 722 | libc_free(ptr); |
| 705 | } | 723 | } |
| @@ -718,7 +736,7 @@ void *__bound_realloc(void *ptr, size_t size, const void *caller) · 12 unmodified lines | |||
| 718 | return ptr1; | 736 | return ptr1; |
| 719 | old_size = get_region_size(ptr); | 737 | old_size = get_region_size(ptr); |
| 720 | if (old_size == EMPTY_SIZE) | 738 | if (old_size == EMPTY_SIZE) |
| 721 | bound_error("realloc'ing invalid pointer"); | 739 | bound_error(caller, "realloc'ing invalid pointer"); |
| 722 | memcpy(ptr1, ptr, old_size); | 740 | memcpy(ptr1, ptr, old_size); |
| 723 | __bound_free(ptr, caller); | 741 | __bound_free(ptr, caller); |
| 724 | return ptr1; | 742 | return ptr1; |
| @@ -752,7 +770,41 @@ static void bound_dump(void) · 27 unmodified lines | |||
| 752 | } | 770 | } |
| 753 | #endif | 771 | #endif |
| 754 | 772 | ||
| 755 | #if 0 | 773 | /* some useful checked functions */ |
| 774 | |||
| 775 | /* check that (p ... p + size - 1) lies inside 'p' region, if any */ | ||
| 776 | static void __bound_check(const void *p, ssize_t size) | ||
| 777 | { | ||
| 778 | if (size == 0) | ||
| 779 | return; | ||
| 780 | p = __bound_ptr_add((void *)p, size); | ||
| 781 | if (p == INVALID_POINTER) | ||
| 782 | bound_error(get_caller_pc(2), "invalid pointer"); | ||
| 783 | } | ||
| 784 | |||
| 785 | void *__bound_memcpy(void *dst, const void *src, size_t size) | ||
| 786 | { | ||
| 787 | __bound_check(dst, size); | ||
| 788 | __bound_check(src, size); | ||
| 789 | /* check also region overlap */ | ||
| 790 | if (src >= dst && src < dst + size) | ||
| 791 | bound_error(get_caller_pc(1), "memcpy: overlapping regions"); | ||
| 792 | return memcpy(dst, src, size); | ||
| 793 | } | ||
| 794 | |||
| 795 | void *__bound_memmove(void *dst, const void *src, size_t size) | ||
| 796 | { | ||
| 797 | __bound_check(dst, size); | ||
| 798 | __bound_check(src, size); | ||
| 799 | return memmove(dst, src, size); | ||
| 800 | } | ||
| 801 | |||
| 802 | void *__bound_memset(void *dst, int c, size_t size) | ||
| 803 | { | ||
| 804 | __bound_check(dst, size); | ||
| 805 | return memset(dst, 0, size); | ||
| 806 | } | ||
| 807 | |||
| 756 | /* resolve check check syms */ | 808 | /* resolve check check syms */ |
| 757 | typedef struct BCSyms { | 809 | typedef struct BCSyms { |
| 758 | char *str; | 810 | char *str; |
| @@ -760,13 +812,14 @@ typedef struct BCSyms { · 1 unmodified line | |||
| 760 | } BCSyms; | 812 | } BCSyms; |
| 761 | 813 | ||
| 762 | static BCSyms bcheck_syms[] = { | 814 | static BCSyms bcheck_syms[] = { |
| 815 | { "memcpy", __bound_memcpy }, | ||
| 816 | { "memmove", __bound_memmove }, | ||
| 817 | { "memset", __bound_memset }, | ||
| 763 | { NULL, NULL }, | 818 | { NULL, NULL }, |
| 764 | }; | 819 | }; |
| 765 | #endif | ||
| 766 | 820 | ||
| 767 | static void *bound_resolve_sym(const char *sym) | 821 | static void *bound_resolve_sym(const char *sym) |
| 768 | { | 822 | { |
| 769 | #if 0 | ||
| 770 | BCSyms *p; | 823 | BCSyms *p; |
| 771 | p = bcheck_syms; | 824 | p = bcheck_syms; |
| 772 | while (p->str != NULL) { | 825 | while (p->str != NULL) { |
| @@ -774,6 +827,5 @@ static void *bound_resolve_sym(const char *sym) · 1 unmodified line | |||
| 774 | return p->ptr; | 827 | return p->ptr; |
| 775 | p++; | 828 | p++; |
| 776 | } | 829 | } |
| 777 | #endif | ||
| 778 | return NULL; | 830 | return NULL; |
| 779 | } | 831 | } |