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
24 bound checking not used) */24 bound checking not used) */
25//#define BOUND_STATIC25//#define BOUND_STATIC
26 26
27/* use malloc hooks. Currently the code cannot be reliable if no hooks */
28#define CONFIG_TCC_MALLOC_HOOKS
29
27#define BOUND_T1_BITS 1330#define BOUND_T1_BITS 13
28#define BOUND_T2_BITS 1131#define BOUND_T2_BITS 11
29#define BOUND_T3_BITS (32 - BOUND_T1_BITS - BOUND_T2_BITS)32#define BOUND_T3_BITS (32 - BOUND_T1_BITS - BOUND_T2_BITS)
@@ -80,7 +83,7 @@ static void libc_free(void *ptr); · 50 unmodified lines
80static void install_malloc_hooks(void);83static void install_malloc_hooks(void);
81static void restore_malloc_hooks(void);84static void restore_malloc_hooks(void);
82 85
83#ifndef WIN3286#ifdef CONFIG_TCC_MALLOC_HOOKS
84static void *saved_malloc_hook;87static void *saved_malloc_hook;
85static void *saved_free_hook;88static void *saved_free_hook;
86static void *saved_realloc_hook;89static void *saved_realloc_hook;
@@ -407,8 +410,10 @@ void __bound_init(void) · 320 unmodified lines
407 size = BOUND_T23_SIZE;410 size = BOUND_T23_SIZE;
408 mark_invalid(start, size);411 mark_invalid(start, size);
409 412
410#if !defined(__TINYC__) && !defined(WIN32)413#if !defined(__TINYC__) && defined(CONFIG_TCC_MALLOC_HOOKS)
411 /* malloc zone is also marked invalid */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. */
412 start = (unsigned long)&_end;417 start = (unsigned long)&_end;
413 size = 128 * 0x100000;418 size = 128 * 0x100000;
414 mark_invalid(start, size);419 mark_invalid(start, size);
@@ -642,7 +647,7 @@ static unsigned long get_region_size(void *p) · 227 unmodified lines
642 647
643static void install_malloc_hooks(void)648static void install_malloc_hooks(void)
644{649{
645#ifndef WIN32650#ifdef CONFIG_TCC_MALLOC_HOOKS
646 saved_malloc_hook = __malloc_hook;651 saved_malloc_hook = __malloc_hook;
647 saved_free_hook = __free_hook;652 saved_free_hook = __free_hook;
648 saved_realloc_hook = __realloc_hook;653 saved_realloc_hook = __realloc_hook;
@@ -656,7 +661,7 @@ static void install_malloc_hooks(void) · 7 unmodified lines
656 661
657static void restore_malloc_hooks(void)662static void restore_malloc_hooks(void)
658{663{
659#ifndef WIN32664#ifdef CONFIG_TCC_MALLOC_HOOKS
660 __malloc_hook = saved_malloc_hook;665 __malloc_hook = saved_malloc_hook;
661 __free_hook = saved_free_hook;666 __free_hook = saved_free_hook;
662 __realloc_hook = saved_realloc_hook;667 __realloc_hook = saved_realloc_hook;
@@ -698,7 +703,6 @@ void *__bound_malloc(size_t size, const void *caller) · 35 unmodified lines
698 return ptr;703 return ptr;
699}704}
700 705
701#ifndef WIN32
702void *__bound_memalign(size_t size, size_t align, const void *caller)706void *__bound_memalign(size_t size, size_t align, const void *caller)
703{707{
704 void *ptr;708 void *ptr;
@@ -717,7 +721,6 @@ void *__bound_memalign(size_t size, size_t align, const void *caller) · 12 unmodified lines
717 __bound_new_region(ptr, size);721 __bound_new_region(ptr, size);
718 return ptr;722 return ptr;
719}723}
720#endif
721 724
722void __bound_free(void *ptr, const void *caller)725void __bound_free(void *ptr, const void *caller)
723{726{
@@ -750,6 +753,19 @@ void *__bound_realloc(void *ptr, size_t size, const void *caller) · 26 unmodified lines
750 }753 }
751}754}
752 755
756#ifndef CONFIG_TCC_MALLOC_HOOKS
757void *__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
753#if 0769#if 0
754static void bound_dump(void)770static void bound_dump(void)
755{771{
@@ -844,6 +860,13 @@ typedef struct BCSyms { · 88 unmodified lines
844} BCSyms;860} BCSyms;
845 861
846static BCSyms bcheck_syms[] = {862static 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
847 { "memcpy", __bound_memcpy },870 { "memcpy", __bound_memcpy },
848 { "memmove", __bound_memmove },871 { "memmove", __bound_memmove },
849 { "memset", __bound_memset },872 { "memset", __bound_memset },
i386-gen.c+4−1
@@ -412,6 +412,7 @@ void gfunc_prolog(int t) · 411 unmodified lines
412/* generate function epilog */412/* generate function epilog */
413void gfunc_epilog(void)413void gfunc_epilog(void)
414{414{
415#ifdef CONFIG_TCC_BCHECK
415 if (do_bounds_check && func_bound_ptr != lbounds_section->data_ptr) {416 if (do_bounds_check && func_bound_ptr != lbounds_section->data_ptr) {
416 int saved_ind;417 int saved_ind;
417 int *bounds_ptr;418 int *bounds_ptr;
@@ -431,6 +432,7 @@ void gfunc_epilog(void) · 13 unmodified lines
431 oad(0xe8, (int)__bound_local_delete - ind - 5);432 oad(0xe8, (int)__bound_local_delete - ind - 5);
432 o(0x585a); /* restore returned value, if any */433 o(0x585a); /* restore returned value, if any */
433 }434 }
435#endif
434 o(0xc9); /* leave */436 o(0xc9); /* leave */
435 if (func_ret_sub == 0) {437 if (func_ret_sub == 0) {
436 o(0xc3); /* ret */438 o(0xc3); /* ret */
@@ -818,7 +820,7 @@ void gen_cvt_ftof(int t) · 381 unmodified lines
818}820}
819 821
820/* bound check support functions */822/* bound check support functions */
821 823#ifdef CONFIG_TCC_BCHECK
822/* generate first part of bounded pointer addition */824/* generate first part of bounded pointer addition */
823void gen_bounded_ptr_add1(void)825void gen_bounded_ptr_add1(void)
824{826{
@@ -859,6 +861,7 @@ void gen_bounded_ptr_add2(int deref) · 34 unmodified lines
859 /* return pointer is there */861 /* return pointer is there */
860 vtop->r = REG_EAX;862 vtop->r = REG_EAX;
861}863}
864#endif
862 865
863/* end of X86 code generator */866/* end of X86 code generator */
864/*************************************************************/867/*************************************************************/
il-gen.c+1−15
@@ -662,20 +662,6 @@ void gen_cvt_ftof(int t) · 661 unmodified lines
662 }662 }
663}663}
664 664
665/* bound check support functions */665/* end of CIL code generator */
666
667/* generate first part of bounded pointer addition */
668void gen_bounded_ptr_add1(void)
669{
670 /* not handled */
671}
672
673/* if deref is true, then also test dereferencing */
674void gen_bounded_ptr_add2(int deref)
675{
676 /* not handled */
677}
678
679/* end of X86 code generator */
680/*************************************************************/666/*************************************************************/
681 667
tcc.c+40−13
@@ -51,6 +51,10 @@ · 50 unmodified lines
51#define TCC_TARGET_I38651#define TCC_TARGET_I386
52#endif52#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/* amount of virtual memory associated to a section (currently, we do58/* amount of virtual memory associated to a section (currently, we do
55 not realloc them) */59 not realloc them) */
56#define SECTION_VSIZE (1024 * 1024)60#define SECTION_VSIZE (1024 * 1024)
@@ -454,6 +458,9 @@ char *tcc_keywords = · 397 unmodified lines
454 458
455#ifdef WIN32459#ifdef WIN32
456#define snprintf _snprintf460#define snprintf _snprintf
461#endif
462
463#if defined(WIN32) || defined(TCC_UCLIBC)
457/* currently incorrect */464/* currently incorrect */
458long double strtold(const char *nptr, char **endptr)465long double strtold(const char *nptr, char **endptr)
459{466{
@@ -534,7 +541,9 @@ static inline int is_float(int t) · 74 unmodified lines
534 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;541 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
535}542}
536 543
544#ifdef CONFIG_TCC_BCHECK
537#include "bcheck.c"545#include "bcheck.c"
546#endif
538 547
539#ifdef TCC_TARGET_I386548#ifdef TCC_TARGET_I386
540#include "i386-gen.c"549#include "i386-gen.c"
@@ -797,10 +806,14 @@ static inline int toup(int c) · 256 unmodified lines
797void printline(void)806void printline(void)
798{807{
799 BufferedFile **f;808 BufferedFile **f;
800 for(f = include_stack; f < include_stack_ptr; f++)809 if (file) {
801 fprintf(stderr, "In file included from %s:%d:\n", 810 for(f = include_stack; f < include_stack_ptr; f++)
802 (*f)->filename, (*f)->line_num);811 fprintf(stderr, "In file included from %s:%d:\n",
803 fprintf(stderr, "%s:%d: ", file->filename, file->line_num);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
806void error(const char *fmt, ...)819void error(const char *fmt, ...)
@@ -2516,6 +2529,7 @@ void gaddrof(void) · 1709 unmodified lines
2516 vtop->r = (vtop->r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL;2529 vtop->r = (vtop->r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL;
2517}2530}
2518 2531
2532#ifdef CONFIG_TCC_BCHECK
2519/* generate lvalue bound code */2533/* generate lvalue bound code */
2520void gbound(void)2534void gbound(void)
2521{2535{
@@ -2529,6 +2543,7 @@ void gbound(void) · 7 unmodified lines
2529 vtop->r |= VT_LVAL;2543 vtop->r |= VT_LVAL;
2530 }2544 }
2531}2545}
2546#endif
2532 2547
2533/* store vtop a register belonging to class 'rc'. lvalues are2548/* store vtop a register belonging to class 'rc'. lvalues are
2534 converted to values. Cannot be used if cannot be converted to2549 converted to values. Cannot be used if cannot be converted to
@@ -2568,8 +2583,10 @@ int gv(int rc) · 33 unmodified lines
2568 data_offset += size << 2;2583 data_offset += size << 2;
2569 data_section->data_ptr = (unsigned char *)data_offset;2584 data_section->data_ptr = (unsigned char *)data_offset;
2570 }2585 }
2586#ifdef CONFIG_TCC_BCHECK
2571 if (vtop->r & VT_MUSTBOUND) 2587 if (vtop->r & VT_MUSTBOUND)
2572 gbound();2588 gbound();
2589#endif
2573 2590
2574 r = vtop->r & VT_VALMASK;2591 r = vtop->r & VT_VALMASK;
2575 /* need to reload if:2592 /* need to reload if:
@@ -3127,6 +3144,7 @@ void gen_op(int op) · 551 unmodified lines
3127 /* XXX: cast to int ? (long long case) */3144 /* XXX: cast to int ? (long long case) */
3128 vpushi(pointed_size(vtop[-1].t));3145 vpushi(pointed_size(vtop[-1].t));
3129 gen_op('*');3146 gen_op('*');
3147#ifdef CONFIG_TCC_BCHECK
3130 /* if evaluating constant expression, no code should be3148 /* if evaluating constant expression, no code should be
3131 generated, so no bound check */3149 generated, so no bound check */
3132 if (do_bounds_check && !const_wanted) {3150 if (do_bounds_check && !const_wanted) {
@@ -3139,7 +3157,9 @@ void gen_op(int op) · 6 unmodified lines
3139 }3157 }
3140 gen_bounded_ptr_add1();3158 gen_bounded_ptr_add1();
3141 gen_bounded_ptr_add2(0);3159 gen_bounded_ptr_add2(0);
3142 } else {3160 } else
3161#endif
3162 {
3143 gen_opc(op);3163 gen_opc(op);
3144 }3164 }
3145 /* put again type if gen_opc() swaped operands */3165 /* put again type if gen_opc() swaped operands */
@@ -3747,12 +3767,14 @@ void vstore(void) · 601 unmodified lines
3747 /* store result */3767 /* store result */
3748 vstore();3768 vstore();
3749 } else {3769 } else {
3770#ifdef CONFIG_TCC_BCHECK
3750 /* bound check case */3771 /* bound check case */
3751 if (vtop[-1].r & VT_MUSTBOUND) {3772 if (vtop[-1].r & VT_MUSTBOUND) {
3752 vswap();3773 vswap();
3753 gbound();3774 gbound();
3754 vswap();3775 vswap();
3755 }3776 }
3777#endif
3756 rc = RC_INT;3778 rc = RC_INT;
3757 if (is_float(ft))3779 if (is_float(ft))
3758 rc = RC_FLOAT;3780 rc = RC_FLOAT;
@@ -5854,12 +5876,14 @@ void open_dll(char *libname) · 2095 unmodified lines
5854 5876
5855static void *resolve_sym(const char *sym)5877static void *resolve_sym(const char *sym)
5856{5878{
5857 void *ptr;5879#ifdef CONFIG_TCC_BCHECK
5858 if (do_bounds_check) {5880 if (do_bounds_check) {
5881 void *ptr;
5859 ptr = bound_resolve_sym(sym);5882 ptr = bound_resolve_sym(sym);
5860 if (ptr)5883 if (ptr)
5861 return ptr;5884 return ptr;
5862 }5885 }
5886#endif
5863 return dlsym(NULL, sym);5887 return dlsym(NULL, sym);
5864}5888}
5865 5889
@@ -6269,10 +6293,8 @@ int launch_exe(int argc, char **argv) · 403 unmodified lines
6269#endif6293#endif
6270 }6294 }
6271 6295
6296#ifdef CONFIG_TCC_BCHECK
6272 if (do_bounds_check) {6297 if (do_bounds_check) {
6273#ifdef WIN32
6274 error("bound checking currently not available for Windows");
6275#else
6276 int *p, *p_end;6298 int *p, *p_end;
6277 __bound_init();6299 __bound_init();
6278 /* add all known static regions */6300 /* add all known static regions */
@@ -6282,8 +6304,8 @@ int launch_exe(int argc, char **argv) · 3 unmodified lines
6282 __bound_new_region((void *)p[0], p[1]);6304 __bound_new_region((void *)p[0], p[1]);
6283 p += 2;6305 p += 2;
6284 }6306 }
6285#endif
6286 }6307 }
6308#endif
6287 6309
6288 t = (int (*)())s->c;6310 t = (int (*)())s->c;
6289 return (*t)(argc, argv);6311 return (*t)(argc, argv);
@@ -6292,7 +6314,7 @@ int launch_exe(int argc, char **argv) · 2 unmodified lines
6292 6314
6293void help(void)6315void 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 "usage: tcc [-Idir] [-Dsym[=val]] [-Usym] [-llib] [-g] [-b]\n"6318 "usage: tcc [-Idir] [-Dsym[=val]] [-Usym] [-llib] [-g] [-b]\n"
6297 " [-i infile] infile [infile_args...]\n"6319 " [-i infile] infile [infile_args...]\n"
6298 "\n"6320 "\n"
@@ -6301,7 +6323,9 @@ void help(void) · 2 unmodified lines
6301 "-Usym : undefine 'sym'\n"6323 "-Usym : undefine 'sym'\n"
6302 "-llib : link with dynamic library 'lib'\n"6324 "-llib : link with dynamic library 'lib'\n"
6303 "-g : generate runtime debug info\n"6325 "-g : generate runtime debug info\n"
6326#ifdef CONFIG_TCC_BCHECK
6304 "-b : compile with built-in memory and bounds checker (implies -g)\n"6327 "-b : compile with built-in memory and bounds checker (implies -g)\n"
6328#endif
6305 "-i infile : compile infile\n"6329 "-i infile : compile infile\n"
6306 );6330 );
6307}6331}
@@ -6368,6 +6392,7 @@ int main(int argc, char **argv) · 60 unmodified lines
6368 tcc_compile_file(argv[optind++]);6392 tcc_compile_file(argv[optind++]);
6369 } else if (!strcmp(r + 1, "bench")) {6393 } else if (!strcmp(r + 1, "bench")) {
6370 do_bench = 1;6394 do_bench = 1;
6395#ifdef CONFIG_TCC_BCHECK
6371 } else if (r[1] == 'b') {6396 } else if (r[1] == 'b') {
6372 if (!do_bounds_check) {6397 if (!do_bounds_check) {
6373 do_bounds_check = 1;6398 do_bounds_check = 1;
@@ -6381,8 +6406,11 @@ int main(int argc, char **argv) · 7 unmodified lines
6381 /* debug is implied */6406 /* debug is implied */
6382 goto debug_opt;6407 goto debug_opt;
6383 }6408 }
6409#endif
6384 } else if (r[1] == 'g') {6410 } else if (r[1] == 'g') {
6411#ifdef CONFIG_TCC_BCHECK
6385 debug_opt:6412 debug_opt:
6413#endif
6386 if (!do_debug) {6414 if (!do_debug) {
6387 do_debug = 1;6415 do_debug = 1;
6388 6416
@@ -6409,8 +6437,7 @@ int main(int argc, char **argv) · 20 unmodified lines
6409 goto show_help;6437 goto show_help;
6410 outfile = argv[optind++];6438 outfile = argv[optind++];
6411 } else {6439 } else {
6412 fprintf(stderr, "invalid option -- '%s'\n", r);6440 error("invalid option -- '%s'", r);
6413 exit(1);
6414 }6441 }
6415 }6442 }
6416 6443

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