History

added optionnal bound check compile - fixed error reporting

bellard committed 24 years ago

Commit 42266811 parent 6933ac64 files · +75 −36Browse files

Unified · Split

4 files
  1. bcheck.c +30 −7
  2. i386-gen.c +4 −1
  3. il-gen.c +1 −15
  4. tcc.c +40 −13
bcheck.c+30−7
@@ -24,6 +24,9 @@ · 23 unmodified lines
2424 bound checking not used) */
2525 //#define BOUND_STATIC
2626
27+/* use malloc hooks. Currently the code cannot be reliable if no hooks */
28+#define CONFIG_TCC_MALLOC_HOOKS
29+
2730 #define BOUND_T1_BITS 13
2831 #define BOUND_T2_BITS 11
2932 #define BOUND_T3_BITS (32 - BOUND_T1_BITS - BOUND_T2_BITS)
@@ -80,7 +83,7 @@ static void libc_free(void *ptr); · 50 unmodified lines
8083 static void install_malloc_hooks(void);
8184 static void restore_malloc_hooks(void);
8285
83−#ifndef WIN32
86+#ifdef CONFIG_TCC_MALLOC_HOOKS
8487 static void *saved_malloc_hook;
8588 static void *saved_free_hook;
8689 static void *saved_realloc_hook;
@@ -407,8 +410,10 @@ void __bound_init(void) · 320 unmodified lines
407410 size = BOUND_T23_SIZE;
408411 mark_invalid(start, size);
409412
410−#if !defined(__TINYC__) && !defined(WIN32)
411− /* malloc zone is also marked invalid */
413+#if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
414+ /* malloc zone is also marked invalid. can only use that with
415+ hooks because all libs should use the same malloc. The solution
416+ would be to build a new malloc for tcc. */
412417 start = (unsigned long)&_end;
413418 size = 128 * 0x100000;
414419 mark_invalid(start, size);
@@ -642,7 +647,7 @@ static unsigned long get_region_size(void *p) · 227 unmodified lines
642647
643648 static void install_malloc_hooks(void)
644649 {
645−#ifndef WIN32
650+#ifdef CONFIG_TCC_MALLOC_HOOKS
646651 saved_malloc_hook = __malloc_hook;
647652 saved_free_hook = __free_hook;
648653 saved_realloc_hook = __realloc_hook;
@@ -656,7 +661,7 @@ static void install_malloc_hooks(void) · 7 unmodified lines
656661
657662 static void restore_malloc_hooks(void)
658663 {
659−#ifndef WIN32
664+#ifdef CONFIG_TCC_MALLOC_HOOKS
660665 __malloc_hook = saved_malloc_hook;
661666 __free_hook = saved_free_hook;
662667 __realloc_hook = saved_realloc_hook;
@@ -698,7 +703,6 @@ void *__bound_malloc(size_t size, const void *caller) · 35 unmodified lines
698703 return ptr;
699704 }
700705
701−#ifndef WIN32
702706 void *__bound_memalign(size_t size, size_t align, const void *caller)
703707 {
704708 void *ptr;
@@ -717,7 +721,6 @@ void *__bound_memalign(size_t size, size_t align, const void *caller) · 12 unmodified lines
717721 __bound_new_region(ptr, size);
718722 return ptr;
719723 }
720−#endif
721724
722725 void __bound_free(void *ptr, const void *caller)
723726 {
@@ -750,6 +753,19 @@ void *__bound_realloc(void *ptr, size_t size, const void *caller) · 26 unmodified lines
750753 }
751754 }
752755
756+#ifndef CONFIG_TCC_MALLOC_HOOKS
757+void *__bound_calloc(size_t nmemb, size_t size)
758+{
759+ void *ptr;
760+ size = size * nmemb;
761+ ptr = __bound_malloc(size);
762+ if (!ptr)
763+ return NULL;
764+ memset(ptr, 0, size);
765+ return ptr;
766+}
767+#endif
768+
753769 #if 0
754770 static void bound_dump(void)
755771 {
@@ -844,6 +860,13 @@ typedef struct BCSyms { · 88 unmodified lines
844860 } BCSyms;
845861
846862 static BCSyms bcheck_syms[] = {
863+#ifndef CONFIG_TCC_MALLOC_HOOKS
864+ { "malloc", __bound_malloc },
865+ { "free", __bound_free },
866+ { "realloc", __bound_realloc },
867+ { "memalign", __bound_memalign },
868+ { "calloc", __bound_calloc },
869+#endif
847870 { "memcpy", __bound_memcpy },
848871 { "memmove", __bound_memmove },
849872 { "memset", __bound_memset },
i386-gen.c+4−1
@@ -412,6 +412,7 @@ void gfunc_prolog(int t) · 411 unmodified lines
412412 /* generate function epilog */
413413 void gfunc_epilog(void)
414414 {
415+#ifdef CONFIG_TCC_BCHECK
415416 if (do_bounds_check && func_bound_ptr != lbounds_section->data_ptr) {
416417 int saved_ind;
417418 int *bounds_ptr;
@@ -431,6 +432,7 @@ void gfunc_epilog(void) · 13 unmodified lines
431432 oad(0xe8, (int)__bound_local_delete - ind - 5);
432433 o(0x585a); /* restore returned value, if any */
433434 }
435+#endif
434436 o(0xc9); /* leave */
435437 if (func_ret_sub == 0) {
436438 o(0xc3); /* ret */
@@ -818,7 +820,7 @@ void gen_cvt_ftof(int t) · 381 unmodified lines
818820 }
819821
820822 /* bound check support functions */
821−
823+#ifdef CONFIG_TCC_BCHECK
822824 /* generate first part of bounded pointer addition */
823825 void gen_bounded_ptr_add1(void)
824826 {
@@ -859,6 +861,7 @@ void gen_bounded_ptr_add2(int deref) · 34 unmodified lines
859861 /* return pointer is there */
860862 vtop->r = REG_EAX;
861863 }
864+#endif
862865
863866 /* end of X86 code generator */
864867 /*************************************************************/
il-gen.c+1−15
@@ -662,20 +662,6 @@ void gen_cvt_ftof(int t) · 661 unmodified lines
662662 }
663663 }
664664
665−/* bound check support functions */
666−
667−/* generate first part of bounded pointer addition */
668−void gen_bounded_ptr_add1(void)
669−{
670− /* not handled */
671−}
672−
673−/* if deref is true, then also test dereferencing */
674−void gen_bounded_ptr_add2(int deref)
675−{
676− /* not handled */
677−}
678−
679−/* end of X86 code generator */
665+/* end of CIL code generator */
680666 /*************************************************************/
681667
tcc.c+40−13
@@ -51,6 +51,10 @@ · 50 unmodified lines
5151 #define TCC_TARGET_I386
5252 #endif
5353
54+#if !defined(WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_IL)
55+#define CONFIG_TCC_BCHECK /* enable bound checking code */
56+#endif
57+
5458 /* amount of virtual memory associated to a section (currently, we do
5559 not realloc them) */
5660 #define SECTION_VSIZE (1024 * 1024)
@@ -454,6 +458,9 @@ char *tcc_keywords = · 397 unmodified lines
454458
455459 #ifdef WIN32
456460 #define snprintf _snprintf
461+#endif
462+
463+#if defined(WIN32) || defined(TCC_UCLIBC)
457464 /* currently incorrect */
458465 long double strtold(const char *nptr, char **endptr)
459466 {
@@ -534,7 +541,9 @@ static inline int is_float(int t) · 74 unmodified lines
534541 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
535542 }
536543
544+#ifdef CONFIG_TCC_BCHECK
537545 #include "bcheck.c"
546+#endif
538547
539548 #ifdef TCC_TARGET_I386
540549 #include "i386-gen.c"
@@ -797,10 +806,14 @@ static inline int toup(int c) · 256 unmodified lines
797806 void printline(void)
798807 {
799808 BufferedFile **f;
800− for(f = include_stack; f < include_stack_ptr; f++)
801− fprintf(stderr, "In file included from %s:%d:\n",
802− (*f)->filename, (*f)->line_num);
803− fprintf(stderr, "%s:%d: ", file->filename, file->line_num);
809+ if (file) {
810+ for(f = include_stack; f < include_stack_ptr; f++)
811+ fprintf(stderr, "In file included from %s:%d:\n",
812+ (*f)->filename, (*f)->line_num);
813+ fprintf(stderr, "%s:%d: ", file->filename, file->line_num);
814+ } else {
815+ fprintf(stderr, "tcc: ");
816+ }
804817 }
805818
806819 void error(const char *fmt, ...)
@@ -2516,6 +2529,7 @@ void gaddrof(void) · 1709 unmodified lines
25162529 vtop->r = (vtop->r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL;
25172530 }
25182531
2532+#ifdef CONFIG_TCC_BCHECK
25192533 /* generate lvalue bound code */
25202534 void gbound(void)
25212535 {
@@ -2529,6 +2543,7 @@ void gbound(void) · 7 unmodified lines
25292543 vtop->r |= VT_LVAL;
25302544 }
25312545 }
2546+#endif
25322547
25332548 /* store vtop a register belonging to class 'rc'. lvalues are
25342549 converted to values. Cannot be used if cannot be converted to
@@ -2568,8 +2583,10 @@ int gv(int rc) · 33 unmodified lines
25682583 data_offset += size << 2;
25692584 data_section->data_ptr = (unsigned char *)data_offset;
25702585 }
2586+#ifdef CONFIG_TCC_BCHECK
25712587 if (vtop->r & VT_MUSTBOUND)
25722588 gbound();
2589+#endif
25732590
25742591 r = vtop->r & VT_VALMASK;
25752592 /* need to reload if:
@@ -3127,6 +3144,7 @@ void gen_op(int op) · 551 unmodified lines
31273144 /* XXX: cast to int ? (long long case) */
31283145 vpushi(pointed_size(vtop[-1].t));
31293146 gen_op('*');
3147+#ifdef CONFIG_TCC_BCHECK
31303148 /* if evaluating constant expression, no code should be
31313149 generated, so no bound check */
31323150 if (do_bounds_check && !const_wanted) {
@@ -3139,7 +3157,9 @@ void gen_op(int op) · 6 unmodified lines
31393157 }
31403158 gen_bounded_ptr_add1();
31413159 gen_bounded_ptr_add2(0);
3142− } else {
3160+ } else
3161+#endif
3162+ {
31433163 gen_opc(op);
31443164 }
31453165 /* put again type if gen_opc() swaped operands */
@@ -3747,12 +3767,14 @@ void vstore(void) · 601 unmodified lines
37473767 /* store result */
37483768 vstore();
37493769 } else {
3770+#ifdef CONFIG_TCC_BCHECK
37503771 /* bound check case */
37513772 if (vtop[-1].r & VT_MUSTBOUND) {
37523773 vswap();
37533774 gbound();
37543775 vswap();
37553776 }
3777+#endif
37563778 rc = RC_INT;
37573779 if (is_float(ft))
37583780 rc = RC_FLOAT;
@@ -5854,12 +5876,14 @@ void open_dll(char *libname) · 2095 unmodified lines
58545876
58555877 static void *resolve_sym(const char *sym)
58565878 {
5857− void *ptr;
5879+#ifdef CONFIG_TCC_BCHECK
58585880 if (do_bounds_check) {
5881+ void *ptr;
58595882 ptr = bound_resolve_sym(sym);
58605883 if (ptr)
58615884 return ptr;
58625885 }
5886+#endif
58635887 return dlsym(NULL, sym);
58645888 }
58655889
@@ -6269,10 +6293,8 @@ int launch_exe(int argc, char **argv) · 403 unmodified lines
62696293 #endif
62706294 }
62716295
6296+#ifdef CONFIG_TCC_BCHECK
62726297 if (do_bounds_check) {
6273−#ifdef WIN32
6274− error("bound checking currently not available for Windows");
6275−#else
62766298 int *p, *p_end;
62776299 __bound_init();
62786300 /* add all known static regions */
@@ -6282,8 +6304,8 @@ int launch_exe(int argc, char **argv) · 3 unmodified lines
62826304 __bound_new_region((void *)p[0], p[1]);
62836305 p += 2;
62846306 }
6285−#endif
62866307 }
6308+#endif
62876309
62886310 t = (int (*)())s->c;
62896311 return (*t)(argc, argv);
@@ -6292,7 +6314,7 @@ int launch_exe(int argc, char **argv) · 2 unmodified lines
62926314
62936315 void help(void)
62946316 {
6295− printf("tcc version 0.9.4 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
6317+ printf("tcc version 0.9.5 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
62966318 "usage: tcc [-Idir] [-Dsym[=val]] [-Usym] [-llib] [-g] [-b]\n"
62976319 " [-i infile] infile [infile_args...]\n"
62986320 "\n"
@@ -6301,7 +6323,9 @@ void help(void) · 2 unmodified lines
63016323 "-Usym : undefine 'sym'\n"
63026324 "-llib : link with dynamic library 'lib'\n"
63036325 "-g : generate runtime debug info\n"
6326+#ifdef CONFIG_TCC_BCHECK
63046327 "-b : compile with built-in memory and bounds checker (implies -g)\n"
6328+#endif
63056329 "-i infile : compile infile\n"
63066330 );
63076331 }
@@ -6368,6 +6392,7 @@ int main(int argc, char **argv) · 60 unmodified lines
63686392 tcc_compile_file(argv[optind++]);
63696393 } else if (!strcmp(r + 1, "bench")) {
63706394 do_bench = 1;
6395+#ifdef CONFIG_TCC_BCHECK
63716396 } else if (r[1] == 'b') {
63726397 if (!do_bounds_check) {
63736398 do_bounds_check = 1;
@@ -6381,8 +6406,11 @@ int main(int argc, char **argv) · 7 unmodified lines
63816406 /* debug is implied */
63826407 goto debug_opt;
63836408 }
6409+#endif
63846410 } else if (r[1] == 'g') {
6411+#ifdef CONFIG_TCC_BCHECK
63856412 debug_opt:
6413+#endif
63866414 if (!do_debug) {
63876415 do_debug = 1;
63886416
@@ -6409,8 +6437,7 @@ int main(int argc, char **argv) · 20 unmodified lines
64096437 goto show_help;
64106438 outfile = argv[optind++];
64116439 } else {
6412− fprintf(stderr, "invalid option -- '%s'\n", r);
6413− exit(1);
6440+ error("invalid option -- '%s'", r);
64146441 }
64156442 }
64166443

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