History

better error display - added some string functions

bellard committed 24 years ago

Commit 4a922fa1 parent 3d22a2a1 file · +71 −19Browse files

Unified · Split

bcheck.c+71−19
@@ -54,9 +54,9 @@ typedef struct BoundEntry { · 53 unmodified lines
54/* external interface */54/* external interface */
55void __bound_init(void);55void __bound_init(void);
56void __bound_new_region(void *p, unsigned long size);56void __bound_new_region(void *p, unsigned long size);
57void __bound_delete_region(void *p);57int __bound_delete_region(void *p);
58 58
59/* currently, tcc cannot compile that because we use unsupported GNUC59/* currently, tcc cannot compile that because we use unsupported GNU C
60 extensions */60 extensions */
61#if !defined(__TINYC__)61#if !defined(__TINYC__)
62void *__bound_ptr_add(void *p, int offset) __attribute__((regparm(2)));62void *__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
69void __bound_local_new(void *p) __attribute__((regparm(1)));69void __bound_local_new(void *p) __attribute__((regparm(1)));
70void __bound_local_delete(void *p) __attribute__((regparm(1)));70void __bound_local_delete(void *p) __attribute__((regparm(1)));
71#endif71#endif
72static void *get_caller_pc(int n);
72 73
73void *__bound_malloc(size_t size, const void *caller);74void *__bound_malloc(size_t size, const void *caller);
74void *__bound_memalign(size_t size, size_t align, const void *caller);75void *__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
125static void bound_error(const char *fmt, ...)126/* print a bound error and recurse thru 'nb_callers' to get the error
127 position */
128static 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
136static void bound_alloc_error(void)133static 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) */
192static 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
265static void *get_caller_pc(int n)
266{
267 return 0;
268}
254#endif269#endif
255 270
256BOUND_PTR_INDIR(1)271BOUND_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. */
530void __bound_delete_region(void *p)545/* return non zero if error */
546int __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 non621/* 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#endif771#endif
754 772
755#if 0773/* some useful checked functions */
774
775/* check that (p ... p + size - 1) lies inside 'p' region, if any */
776static 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
785void *__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
795void *__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
802void *__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 */
757typedef struct BCSyms {809typedef struct BCSyms {
758 char *str;810 char *str;
@@ -760,13 +812,14 @@ typedef struct BCSyms { · 1 unmodified line
760} BCSyms;812} BCSyms;
761 813
762static BCSyms bcheck_syms[] = {814static 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
767static void *bound_resolve_sym(const char *sym)821static 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}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t