| @@ -51,6 +51,10 @@ · 50 unmodified lines |
| 51 | 51 | #define TCC_TARGET_I386 |
| 52 | 52 | #endif |
| 53 | 53 | |
| 54 | +#if !defined(WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_IL) |
| 55 | +#define CONFIG_TCC_BCHECK /* enable bound checking code */ |
| 56 | +#endif |
| 57 | + |
| 54 | 58 | /* amount of virtual memory associated to a section (currently, we do |
| 55 | 59 | not realloc them) */ |
| 56 | 60 | #define SECTION_VSIZE (1024 * 1024) |
| @@ -454,6 +458,9 @@ char *tcc_keywords = · 397 unmodified lines |
| 454 | 458 | |
| 455 | 459 | #ifdef WIN32 |
| 456 | 460 | #define snprintf _snprintf |
| 461 | +#endif |
| 462 | + |
| 463 | +#if defined(WIN32) || defined(TCC_UCLIBC) |
| 457 | 464 | /* currently incorrect */ |
| 458 | 465 | long double strtold(const char *nptr, char **endptr) |
| 459 | 466 | { |
| @@ -534,7 +541,9 @@ static inline int is_float(int t) · 74 unmodified lines |
| 534 | 541 | return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT; |
| 535 | 542 | } |
| 536 | 543 | |
| 544 | +#ifdef CONFIG_TCC_BCHECK |
| 537 | 545 | #include "bcheck.c" |
| 546 | +#endif |
| 538 | 547 | |
| 539 | 548 | #ifdef TCC_TARGET_I386 |
| 540 | 549 | #include "i386-gen.c" |
| @@ -797,10 +806,14 @@ static inline int toup(int c) · 256 unmodified lines |
| 797 | 806 | void printline(void) |
| 798 | 807 | { |
| 799 | 808 | 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 | + } |
| 804 | 817 | } |
| 805 | 818 | |
| 806 | 819 | void error(const char *fmt, ...) |
| @@ -2516,6 +2529,7 @@ void gaddrof(void) · 1709 unmodified lines |
| 2516 | 2529 | vtop->r = (vtop->r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL; |
| 2517 | 2530 | } |
| 2518 | 2531 | |
| 2532 | +#ifdef CONFIG_TCC_BCHECK |
| 2519 | 2533 | /* generate lvalue bound code */ |
| 2520 | 2534 | void gbound(void) |
| 2521 | 2535 | { |
| @@ -2529,6 +2543,7 @@ void gbound(void) · 7 unmodified lines |
| 2529 | 2543 | vtop->r |= VT_LVAL; |
| 2530 | 2544 | } |
| 2531 | 2545 | } |
| 2546 | +#endif |
| 2532 | 2547 | |
| 2533 | 2548 | /* store vtop a register belonging to class 'rc'. lvalues are |
| 2534 | 2549 | converted to values. Cannot be used if cannot be converted to |
| @@ -2568,8 +2583,10 @@ int gv(int rc) · 33 unmodified lines |
| 2568 | 2583 | data_offset += size << 2; |
| 2569 | 2584 | data_section->data_ptr = (unsigned char *)data_offset; |
| 2570 | 2585 | } |
| 2586 | +#ifdef CONFIG_TCC_BCHECK |
| 2571 | 2587 | if (vtop->r & VT_MUSTBOUND) |
| 2572 | 2588 | gbound(); |
| 2589 | +#endif |
| 2573 | 2590 | |
| 2574 | 2591 | r = vtop->r & VT_VALMASK; |
| 2575 | 2592 | /* need to reload if: |
| @@ -3127,6 +3144,7 @@ void gen_op(int op) · 551 unmodified lines |
| 3127 | 3144 | /* XXX: cast to int ? (long long case) */ |
| 3128 | 3145 | vpushi(pointed_size(vtop[-1].t)); |
| 3129 | 3146 | gen_op('*'); |
| 3147 | +#ifdef CONFIG_TCC_BCHECK |
| 3130 | 3148 | /* if evaluating constant expression, no code should be |
| 3131 | 3149 | generated, so no bound check */ |
| 3132 | 3150 | if (do_bounds_check && !const_wanted) { |
| @@ -3139,7 +3157,9 @@ void gen_op(int op) · 6 unmodified lines |
| 3139 | 3157 | } |
| 3140 | 3158 | gen_bounded_ptr_add1(); |
| 3141 | 3159 | gen_bounded_ptr_add2(0); |
| 3142 | | − } else { |
| 3160 | + } else |
| 3161 | +#endif |
| 3162 | + { |
| 3143 | 3163 | gen_opc(op); |
| 3144 | 3164 | } |
| 3145 | 3165 | /* put again type if gen_opc() swaped operands */ |
| @@ -3747,12 +3767,14 @@ void vstore(void) · 601 unmodified lines |
| 3747 | 3767 | /* store result */ |
| 3748 | 3768 | vstore(); |
| 3749 | 3769 | } else { |
| 3770 | +#ifdef CONFIG_TCC_BCHECK |
| 3750 | 3771 | /* bound check case */ |
| 3751 | 3772 | if (vtop[-1].r & VT_MUSTBOUND) { |
| 3752 | 3773 | vswap(); |
| 3753 | 3774 | gbound(); |
| 3754 | 3775 | vswap(); |
| 3755 | 3776 | } |
| 3777 | +#endif |
| 3756 | 3778 | rc = RC_INT; |
| 3757 | 3779 | if (is_float(ft)) |
| 3758 | 3780 | rc = RC_FLOAT; |
| @@ -5854,12 +5876,14 @@ void open_dll(char *libname) · 2095 unmodified lines |
| 5854 | 5876 | |
| 5855 | 5877 | static void *resolve_sym(const char *sym) |
| 5856 | 5878 | { |
| 5857 | | − void *ptr; |
| 5879 | +#ifdef CONFIG_TCC_BCHECK |
| 5858 | 5880 | if (do_bounds_check) { |
| 5881 | + void *ptr; |
| 5859 | 5882 | ptr = bound_resolve_sym(sym); |
| 5860 | 5883 | if (ptr) |
| 5861 | 5884 | return ptr; |
| 5862 | 5885 | } |
| 5886 | +#endif |
| 5863 | 5887 | return dlsym(NULL, sym); |
| 5864 | 5888 | } |
| 5865 | 5889 | |
| @@ -6269,10 +6293,8 @@ int launch_exe(int argc, char **argv) · 403 unmodified lines |
| 6269 | 6293 | #endif |
| 6270 | 6294 | } |
| 6271 | 6295 | |
| 6296 | +#ifdef CONFIG_TCC_BCHECK |
| 6272 | 6297 | if (do_bounds_check) { |
| 6273 | | −#ifdef WIN32 |
| 6274 | | − error("bound checking currently not available for Windows"); |
| 6275 | | −#else |
| 6276 | 6298 | int *p, *p_end; |
| 6277 | 6299 | __bound_init(); |
| 6278 | 6300 | /* add all known static regions */ |
| @@ -6282,8 +6304,8 @@ int launch_exe(int argc, char **argv) · 3 unmodified lines |
| 6282 | 6304 | __bound_new_region((void *)p[0], p[1]); |
| 6283 | 6305 | p += 2; |
| 6284 | 6306 | } |
| 6285 | | −#endif |
| 6286 | 6307 | } |
| 6308 | +#endif |
| 6287 | 6309 | |
| 6288 | 6310 | t = (int (*)())s->c; |
| 6289 | 6311 | return (*t)(argc, argv); |
| @@ -6292,7 +6314,7 @@ int launch_exe(int argc, char **argv) · 2 unmodified lines |
| 6292 | 6314 | |
| 6293 | 6315 | void help(void) |
| 6294 | 6316 | { |
| 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" |
| 6296 | 6318 | "usage: tcc [-Idir] [-Dsym[=val]] [-Usym] [-llib] [-g] [-b]\n" |
| 6297 | 6319 | " [-i infile] infile [infile_args...]\n" |
| 6298 | 6320 | "\n" |
| @@ -6301,7 +6323,9 @@ void help(void) · 2 unmodified lines |
| 6301 | 6323 | "-Usym : undefine 'sym'\n" |
| 6302 | 6324 | "-llib : link with dynamic library 'lib'\n" |
| 6303 | 6325 | "-g : generate runtime debug info\n" |
| 6326 | +#ifdef CONFIG_TCC_BCHECK |
| 6304 | 6327 | "-b : compile with built-in memory and bounds checker (implies -g)\n" |
| 6328 | +#endif |
| 6305 | 6329 | "-i infile : compile infile\n" |
| 6306 | 6330 | ); |
| 6307 | 6331 | } |
| @@ -6368,6 +6392,7 @@ int main(int argc, char **argv) · 60 unmodified lines |
| 6368 | 6392 | tcc_compile_file(argv[optind++]); |
| 6369 | 6393 | } else if (!strcmp(r + 1, "bench")) { |
| 6370 | 6394 | do_bench = 1; |
| 6395 | +#ifdef CONFIG_TCC_BCHECK |
| 6371 | 6396 | } else if (r[1] == 'b') { |
| 6372 | 6397 | if (!do_bounds_check) { |
| 6373 | 6398 | do_bounds_check = 1; |
| @@ -6381,8 +6406,11 @@ int main(int argc, char **argv) · 7 unmodified lines |
| 6381 | 6406 | /* debug is implied */ |
| 6382 | 6407 | goto debug_opt; |
| 6383 | 6408 | } |
| 6409 | +#endif |
| 6384 | 6410 | } else if (r[1] == 'g') { |
| 6411 | +#ifdef CONFIG_TCC_BCHECK |
| 6385 | 6412 | debug_opt: |
| 6413 | +#endif |
| 6386 | 6414 | if (!do_debug) { |
| 6387 | 6415 | do_debug = 1; |
| 6388 | 6416 | |
| @@ -6409,8 +6437,7 @@ int main(int argc, char **argv) · 20 unmodified lines |
| 6409 | 6437 | goto show_help; |
| 6410 | 6438 | outfile = argv[optind++]; |
| 6411 | 6439 | } else { |
| 6412 | | − fprintf(stderr, "invalid option -- '%s'\n", r); |
| 6413 | | − exit(1); |
| 6440 | + error("invalid option -- '%s'", r); |
| 6414 | 6441 | } |
| 6415 | 6442 | } |
| 6416 | 6443 | |