History

simplified error handling

bellard committed 24 years ago

Commit bde35a81 parent b31d34f1 file · +11 −31Browse files

Unified · Split

bcheck.c+11−31
@@ -77,7 +77,6 @@ void *__bound_ptr_indir16(void *p, int offset) __attribute__((regparm(2))); · 76 unmodified lines
77void __bound_local_new(void *p) __attribute__((regparm(1)));77void __bound_local_new(void *p) __attribute__((regparm(1)));
78void __bound_local_delete(void *p) __attribute__((regparm(1)));78void __bound_local_delete(void *p) __attribute__((regparm(1)));
79#endif79#endif
80static void *get_caller_pc(int n);
81 80
82void *__bound_malloc(size_t size, const void *caller);81void *__bound_malloc(size_t size, const void *caller);
83void *__bound_memalign(size_t size, size_t align, const void *caller);82void *__bound_memalign(size_t size, size_t align, const void *caller);
@@ -100,8 +99,8 @@ extern char _end; · 16 unmodified lines
100 99
101/* TCC definitions */100/* TCC definitions */
102extern char __bounds_start; /* start of static bounds table */101extern char __bounds_start; /* start of static bounds table */
103/* error function. if NULL, simply do abort() */102/* error message, just for TCC */
104void (*__bound_error_func)(unsigned long caller, const char *msg);103const char *__bound_error_msg;
105 104
106/* runtime error output */105/* runtime error output */
107extern void rt_error(unsigned long pc, const char *fmt, ...);106extern void rt_error(unsigned long pc, const char *fmt, ...);
@@ -143,16 +142,15 @@ static BoundEntry *__bound_find_region(BoundEntry *e1, void *p) · 35 unmodified lines
143}142}
144 143
145/* print a bound error message */144/* print a bound error message */
146static void bound_error(const void *caller, const char *fmt, ...)145static void bound_error(const char *fmt, ...)
147{146{
148 if (!__bound_error_func)147 __bound_error_msg = fmt;
149 abort();148 *(int *)0 = 0; /* force a runtime error */
150 __bound_error_func((unsigned long)caller, fmt);
151}149}
152 150
153static void bound_alloc_error(void)151static void bound_alloc_error(void)
154{152{
155 bound_error(NULL, "not enough memory for bound checking code");153 bound_error("not enough memory for bound checking code");
156}154}
157 155
158/* currently, tcc cannot compile that because we use GNUC extensions */156/* currently, tcc cannot compile that because we use GNUC extensions */
@@ -207,19 +205,6 @@ void *__bound_ptr_indir ## dsize (void *p, int offset) \ · 48 unmodified lines
207}205}
208 206
209#ifdef __i386__207#ifdef __i386__
210
211/* return the PC of the N'th caller (N=1: first caller) */
212static void *get_caller_pc(int n)
213{
214 unsigned long fp;
215 int i;
216
217 __asm__ __volatile__ ("movl %%ebp,%0" :"=g" (fp));
218 for(i=0;i<n;i++)
219 fp = ((unsigned long *)fp)[0];
220 return ((void **)fp)[1];
221}
222
223/* return the frame pointer of the caller */208/* return the frame pointer of the caller */
224#define GET_CALLER_FP(fp)\209#define GET_CALLER_FP(fp)\
225{\210{\
@@ -281,11 +266,6 @@ void *__bound_ptr_indir ## dsize (void *p, int offset) \ · 55 unmodified lines
281{ \266{ \
282 return p + offset; \267 return p + offset; \
283}268}
284
285static void *get_caller_pc(int n)
286{
287 return 0;
288}
289#endif269#endif
290 270
291BOUND_PTR_INDIR(1)271BOUND_PTR_INDIR(1)
@@ -751,7 +731,7 @@ void __bound_free(void *ptr, const void *caller) · 459 unmodified lines
751 if (ptr == NULL)731 if (ptr == NULL)
752 return;732 return;
753 if (__bound_delete_region(ptr) != 0)733 if (__bound_delete_region(ptr) != 0)
754 bound_error(caller, "freeing invalid region");734 bound_error("freeing invalid region");
755 735
756 libc_free(ptr);736 libc_free(ptr);
757}737}
@@ -770,7 +750,7 @@ void *__bound_realloc(void *ptr, size_t size, const void *caller) · 12 unmodified lines
770 return ptr1;750 return ptr1;
771 old_size = get_region_size(ptr);751 old_size = get_region_size(ptr);
772 if (old_size == EMPTY_SIZE)752 if (old_size == EMPTY_SIZE)
773 bound_error(caller, "realloc'ing invalid pointer");753 bound_error("realloc'ing invalid pointer");
774 memcpy(ptr1, ptr, old_size);754 memcpy(ptr1, ptr, old_size);
775 __bound_free(ptr, caller);755 __bound_free(ptr, caller);
776 return ptr1;756 return ptr1;
@@ -826,7 +806,7 @@ static void __bound_check(const void *p, size_t size) · 49 unmodified lines
826 return;806 return;
827 p = __bound_ptr_add((void *)p, size);807 p = __bound_ptr_add((void *)p, size);
828 if (p == INVALID_POINTER)808 if (p == INVALID_POINTER)
829 bound_error(get_caller_pc(2), "invalid pointer");809 bound_error("invalid pointer");
830}810}
831 811
832void *__bound_memcpy(void *dst, const void *src, size_t size)812void *__bound_memcpy(void *dst, const void *src, size_t size)
@@ -835,7 +815,7 @@ void *__bound_memcpy(void *dst, const void *src, size_t size) · 2 unmodified lines
835 __bound_check(src, size);815 __bound_check(src, size);
836 /* check also region overlap */816 /* check also region overlap */
837 if (src >= dst && src < dst + size)817 if (src >= dst && src < dst + size)
838 bound_error(get_caller_pc(1), "overlapping regions in memcpy()");818 bound_error("overlapping regions in memcpy()");
839 return memcpy(dst, src, size);819 return memcpy(dst, src, size);
840}820}
841 821
@@ -862,7 +842,7 @@ int __bound_strlen(const char *s) · 20 unmodified lines
862 for(;;) {842 for(;;) {
863 p = __bound_ptr_indir1((char *)s, len);843 p = __bound_ptr_indir1((char *)s, len);
864 if (p == INVALID_POINTER)844 if (p == INVALID_POINTER)
865 bound_error(get_caller_pc(1), "bad pointer in strlen()");845 bound_error("bad pointer in strlen()");
866 if (*p == '\0')846 if (*p == '\0')
867 break;847 break;
868 len++;848 len++;

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