Unified · Split
| @@ -54,9 +54,9 @@ typedef struct BoundEntry { · 53 unmodified lines | ||
| 54 | 54 | /* external interface */ |
| 55 | 55 | void __bound_init(void); |
| 56 | 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 | 60 | extensions */ |
| 61 | 61 | #if !defined(__TINYC__) |
| 62 | 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 | 69 | void __bound_local_new(void *p) __attribute__((regparm(1))); |
| 70 | 70 | void __bound_local_delete(void *p) __attribute__((regparm(1))); |
| 71 | 71 | #endif |
| 72 | +static void *get_caller_pc(int n); | |
| 72 | 73 | |
| 73 | 74 | void *__bound_malloc(size_t size, const void *caller); |
| 74 | 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 | 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; | |
| 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); | |
| 130 | + rt_error((unsigned long)caller, "%s", fmt); | |
| 134 | 131 | } |
| 135 | 132 | |
| 136 | 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 | 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 | 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 | 204 | #define GET_CALLER_FP(fp)\ |
| 194 | 205 | {\ |
| 195 | 206 | unsigned long *fp1;\ |
| @@ -251,6 +262,10 @@ void *__bound_ptr_indir ## dsize (void *p, int offset) \ · 55 unmodified lines | ||
| 251 | 262 | return p + offset; \ |
| 252 | 263 | } |
| 253 | 264 | |
| 265 | +static void *get_caller_pc(int n) | |
| 266 | +{ | |
| 267 | + return 0; | |
| 268 | +} | |
| 254 | 269 | #endif |
| 255 | 270 | |
| 256 | 271 | BOUND_PTR_INDIR(1) |
| @@ -527,7 +542,8 @@ static inline void delete_region(BoundEntry *e, · 270 unmodified lines | ||
| 527 | 542 | } |
| 528 | 543 | |
| 529 | 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 | 548 | unsigned long start, end, addr, size, empty_size; |
| 533 | 549 | BoundEntry *page, *e, *e2; |
| @@ -546,7 +562,7 @@ void __bound_delete_region(void *p) · 12 unmodified lines | ||
| 546 | 562 | e = __bound_find_region(e, p); |
| 547 | 563 | /* test if invalid region */ |
| 548 | 564 | if (e->size == EMPTY_SIZE || (unsigned long)p != e->start) |
| 549 | − bound_error("freeing invalid region"); | |
| 565 | + return -1; | |
| 550 | 566 | /* compute the size we put in invalid regions */ |
| 551 | 567 | if (e->is_invalid) |
| 552 | 568 | empty_size = INVALID_SIZE; |
| @@ -599,6 +615,7 @@ void __bound_delete_region(void *p) · 46 unmodified lines | ||
| 599 | 615 | } |
| 600 | 616 | delete_region(e, p, empty_size); |
| 601 | 617 | } |
| 618 | + return 0; | |
| 602 | 619 | } |
| 603 | 620 | |
| 604 | 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 | 717 | if (ptr == NULL) |
| 701 | 718 | return; |
| 702 | − __bound_delete_region(ptr); | |
| 719 | + if (__bound_delete_region(ptr) != 0) | |
| 720 | + bound_error(caller, "freeing invalid region"); | |
| 703 | 721 | |
| 704 | 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 | 736 | return ptr1; |
| 719 | 737 | old_size = get_region_size(ptr); |
| 720 | 738 | if (old_size == EMPTY_SIZE) |
| 721 | − bound_error("realloc'ing invalid pointer"); | |
| 739 | + bound_error(caller, "realloc'ing invalid pointer"); | |
| 722 | 740 | memcpy(ptr1, ptr, old_size); |
| 723 | 741 | __bound_free(ptr, caller); |
| 724 | 742 | return ptr1; |
| @@ -752,7 +770,41 @@ static void bound_dump(void) · 27 unmodified lines | ||
| 752 | 770 | } |
| 753 | 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 | 808 | /* resolve check check syms */ |
| 757 | 809 | typedef struct BCSyms { |
| 758 | 810 | char *str; |
| @@ -760,13 +812,14 @@ typedef struct BCSyms { · 1 unmodified line | ||
| 760 | 812 | } BCSyms; |
| 761 | 813 | |
| 762 | 814 | static BCSyms bcheck_syms[] = { |
| 815 | + { "memcpy", __bound_memcpy }, | |
| 816 | + { "memmove", __bound_memmove }, | |
| 817 | + { "memset", __bound_memset }, | |
| 763 | 818 | { NULL, NULL }, |
| 764 | 819 | }; |
| 765 | −#endif | |
| 766 | 820 | |
| 767 | 821 | static void *bound_resolve_sym(const char *sym) |
| 768 | 822 | { |
| 769 | −#if 0 | |
| 770 | 823 | BCSyms *p; |
| 771 | 824 | p = bcheck_syms; |
| 772 | 825 | while (p->str != NULL) { |
| @@ -774,6 +827,5 @@ static void *bound_resolve_sym(const char *sym) · 1 unmodified line | ||
| 774 | 827 | return p->ptr; |
| 775 | 828 | p++; |
| 776 | 829 | } |
| 777 | −#endif | |
| 778 | 830 | return NULL; |
| 779 | 831 | } |