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
5454 /* external interface */
5555 void __bound_init(void);
5656 void __bound_new_region(void *p, unsigned long size);
57−void __bound_delete_region(void *p);
57+int __bound_delete_region(void *p);
5858
59−/* currently, tcc cannot compile that because we use unsupported GNUC
59+/* currently, tcc cannot compile that because we use unsupported GNU C
6060 extensions */
6161 #if !defined(__TINYC__)
6262 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
6969 void __bound_local_new(void *p) __attribute__((regparm(1)));
7070 void __bound_local_delete(void *p) __attribute__((regparm(1)));
7171 #endif
72+static void *get_caller_pc(int n);
7273
7374 void *__bound_malloc(size_t size, const void *caller);
7475 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
122123 return __bound_empty_t2;
123124 }
124125
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, ...)
126129 {
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);
134131 }
135132
136133 static void bound_alloc_error(void)
137134 {
138− bound_error("not enough memory for bound checking code\n");
135+ bound_error(NULL, "not enough memory for bound checking code\n");
139136 }
140137
141138 /* 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
190187 }
191188
192189 #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 */
193204 #define GET_CALLER_FP(fp)\
194205 {\
195206 unsigned long *fp1;\
@@ -251,6 +262,10 @@ void *__bound_ptr_indir ## dsize (void *p, int offset) \ · 55 unmodified lines
251262 return p + offset; \
252263 }
253264
265+static void *get_caller_pc(int n)
266+{
267+ return 0;
268+}
254269 #endif
255270
256271 BOUND_PTR_INDIR(1)
@@ -527,7 +542,8 @@ static inline void delete_region(BoundEntry *e, · 270 unmodified lines
527542 }
528543
529544 /* 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)
531547 {
532548 unsigned long start, end, addr, size, empty_size;
533549 BoundEntry *page, *e, *e2;
@@ -546,7 +562,7 @@ void __bound_delete_region(void *p) · 12 unmodified lines
546562 e = __bound_find_region(e, p);
547563 /* test if invalid region */
548564 if (e->size == EMPTY_SIZE || (unsigned long)p != e->start)
549− bound_error("freeing invalid region");
565+ return -1;
550566 /* compute the size we put in invalid regions */
551567 if (e->is_invalid)
552568 empty_size = INVALID_SIZE;
@@ -599,6 +615,7 @@ void __bound_delete_region(void *p) · 46 unmodified lines
599615 }
600616 delete_region(e, p, empty_size);
601617 }
618+ return 0;
602619 }
603620
604621 /* 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
699716 {
700717 if (ptr == NULL)
701718 return;
702− __bound_delete_region(ptr);
719+ if (__bound_delete_region(ptr) != 0)
720+ bound_error(caller, "freeing invalid region");
703721
704722 libc_free(ptr);
705723 }
@@ -718,7 +736,7 @@ void *__bound_realloc(void *ptr, size_t size, const void *caller) · 12 unmodified lines
718736 return ptr1;
719737 old_size = get_region_size(ptr);
720738 if (old_size == EMPTY_SIZE)
721− bound_error("realloc'ing invalid pointer");
739+ bound_error(caller, "realloc'ing invalid pointer");
722740 memcpy(ptr1, ptr, old_size);
723741 __bound_free(ptr, caller);
724742 return ptr1;
@@ -752,7 +770,41 @@ static void bound_dump(void) · 27 unmodified lines
752770 }
753771 #endif
754772
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+
756808 /* resolve check check syms */
757809 typedef struct BCSyms {
758810 char *str;
@@ -760,13 +812,14 @@ typedef struct BCSyms { · 1 unmodified line
760812 } BCSyms;
761813
762814 static BCSyms bcheck_syms[] = {
815+ { "memcpy", __bound_memcpy },
816+ { "memmove", __bound_memmove },
817+ { "memset", __bound_memset },
763818 { NULL, NULL },
764819 };
765−#endif
766820
767821 static void *bound_resolve_sym(const char *sym)
768822 {
769−#if 0
770823 BCSyms *p;
771824 p = bcheck_syms;
772825 while (p->str != NULL) {
@@ -774,6 +827,5 @@ static void *bound_resolve_sym(const char *sym) · 1 unmodified line
774827 return p->ptr;
775828 p++;
776829 }
777−#endif
778830 return NULL;
779831 }

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