Unified · Split
| @@ -163,7 +163,9 @@ int section_num; · 162 unmodified lines | |||
| 163 | Section *text_section, *data_section, *bss_section; /* predefined sections */ | 163 | Section *text_section, *data_section, *bss_section; /* predefined sections */ |
| 164 | Section *cur_text_section; /* current section where function code is | 164 | Section *cur_text_section; /* current section where function code is |
| 165 | generated */ | 165 | generated */ |
| 166 | /* bound check related sections */ | ||
| 166 | Section *bounds_section; /* contains global data bound description */ | 167 | Section *bounds_section; /* contains global data bound description */ |
| 168 | Section *lbounds_section; /* contains local data bound description */ | ||
| 167 | /* debug sections */ | 169 | /* debug sections */ |
| 168 | Section *stab_section, *stabstr_section, *symtab_section, *strtab_section; | 170 | Section *stab_section, *stabstr_section, *symtab_section, *strtab_section; |
| 169 | 171 | ||
| @@ -209,18 +211,20 @@ int gnu_ext = 1; · 39 unmodified lines | |||
| 209 | int tcc_ext = 1; | 211 | int tcc_ext = 1; |
| 210 | 212 | ||
| 211 | /* The current value can be: */ | 213 | /* The current value can be: */ |
| 212 | #define VT_VALMASK 0x00ff | 214 | #define VT_VALMASK 0x00ff |
| 213 | #define VT_CONST 0x00f0 /* constant in vc | 215 | #define VT_CONST 0x00f0 /* constant in vc |
| 214 | (must be first non register value) */ | 216 | (must be first non register value) */ |
| 215 | #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */ | 217 | #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */ |
| 216 | #define VT_LOCAL 0x00f2 /* offset on stack */ | 218 | #define VT_LOCAL 0x00f2 /* offset on stack */ |
| 217 | #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */ | 219 | #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */ |
| 218 | #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */ | 220 | #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */ |
| 219 | #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */ | 221 | #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */ |
| 220 | #define VT_LVAL 0x0100 /* var is an lvalue */ | 222 | #define VT_LVAL 0x0100 /* var is an lvalue */ |
| 221 | #define VT_FORWARD 0x0200 /* value is forward reference */ | 223 | #define VT_FORWARD 0x0200 /* value is forward reference */ |
| 222 | #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for | 224 | #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for |
| 223 | char/short stored in integer registers) */ | 225 | char/short stored in integer registers) */ |
| 226 | #define VT_MUSTBOUND 0x0800 /* bound checking must be done before | ||
| 227 | dereferencing value */ | ||
| 224 | 228 | ||
| 225 | /* types */ | 229 | /* types */ |
| 226 | #define VT_STRUCT_SHIFT 16 /* structure/enum name shift (16 bits left) */ | 230 | #define VT_STRUCT_SHIFT 16 /* structure/enum name shift (16 bits left) */ |
| @@ -402,6 +406,7 @@ void decl(int l); · 175 unmodified lines | |||
| 402 | void decl_initializer(int t, int r, int c, int first, int size_only); | 406 | void decl_initializer(int t, int r, int c, int first, int size_only); |
| 403 | int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init); | 407 | int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init); |
| 404 | int gv(int rc); | 408 | int gv(int rc); |
| 409 | void gv2(int rc1, int rc2); | ||
| 405 | void move_reg(int r, int s); | 410 | void move_reg(int r, int s); |
| 406 | void save_regs(void); | 411 | void save_regs(void); |
| 407 | void save_reg(int r); | 412 | void save_reg(int r); |
| @@ -2339,6 +2344,20 @@ void gaddrof(void) · 1931 unmodified lines | |||
| 2339 | vtop->r = (vtop->r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL; | 2344 | vtop->r = (vtop->r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL; |
| 2340 | } | 2345 | } |
| 2341 | 2346 | ||
| 2347 | /* generate lvalue bound code */ | ||
| 2348 | void gbound(void) | ||
| 2349 | { | ||
| 2350 | vtop->r &= ~VT_MUSTBOUND; | ||
| 2351 | /* if lvalue, then use checking code before dereferencing */ | ||
| 2352 | if (vtop->r & VT_LVAL) { | ||
| 2353 | gaddrof(); | ||
| 2354 | vpushi(0); | ||
| 2355 | gen_bounded_ptr_add1(); | ||
| 2356 | gen_bounded_ptr_add2(1); | ||
| 2357 | vtop->r |= VT_LVAL; | ||
| 2358 | } | ||
| 2359 | } | ||
| 2360 | |||
| 2342 | /* store vtop a register belonging to class 'rc'. lvalues are | 2361 | /* store vtop a register belonging to class 'rc'. lvalues are |
| 2343 | converted to values. Cannot be used if cannot be converted to | 2362 | converted to values. Cannot be used if cannot be converted to |
| 2344 | register value (such as structures). */ | 2363 | register value (such as structures). */ |
| @@ -2377,6 +2396,9 @@ int gv(int rc) · 32 unmodified lines | |||
| 2377 | data_offset += size << 2; | 2396 | data_offset += size << 2; |
| 2378 | data_section->data_ptr = (unsigned char *)data_offset; | 2397 | data_section->data_ptr = (unsigned char *)data_offset; |
| 2379 | } | 2398 | } |
| 2399 | if (vtop->r & VT_MUSTBOUND) | ||
| 2400 | gbound(); | ||
| 2401 | |||
| 2380 | r = vtop->r & VT_VALMASK; | 2402 | r = vtop->r & VT_VALMASK; |
| 2381 | /* need to reload if: | 2403 | /* need to reload if: |
| 2382 | - constant | 2404 | - constant |
| @@ -2436,6 +2458,33 @@ int gv(int rc) · 53 unmodified lines | |||
| 2436 | return r; | 2458 | return r; |
| 2437 | } | 2459 | } |
| 2438 | 2460 | ||
| 2461 | /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */ | ||
| 2462 | void gv2(int rc1, int rc2) | ||
| 2463 | { | ||
| 2464 | /* generate more generic register first */ | ||
| 2465 | if (rc1 <= rc2) { | ||
| 2466 | vswap(); | ||
| 2467 | gv(rc1); | ||
| 2468 | vswap(); | ||
| 2469 | gv(rc2); | ||
| 2470 | /* test if reload is needed for first register */ | ||
| 2471 | if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) { | ||
| 2472 | vswap(); | ||
| 2473 | gv(rc1); | ||
| 2474 | vswap(); | ||
| 2475 | } | ||
| 2476 | } else { | ||
| 2477 | gv(rc2); | ||
| 2478 | vswap(); | ||
| 2479 | gv(rc1); | ||
| 2480 | vswap(); | ||
| 2481 | /* test if reload is needed for first register */ | ||
| 2482 | if ((vtop[0].r & VT_VALMASK) >= VT_CONST) { | ||
| 2483 | gv(rc2); | ||
| 2484 | } | ||
| 2485 | } | ||
| 2486 | } | ||
| 2487 | |||
| 2439 | /* expand long long on stack in two int registers */ | 2488 | /* expand long long on stack in two int registers */ |
| 2440 | void lexpand(void) | 2489 | void lexpand(void) |
| 2441 | { | 2490 | { |
| @@ -2454,10 +2503,7 @@ void lexpand(void) · 12 unmodified lines | |||
| 2454 | /* build a long long from two ints */ | 2503 | /* build a long long from two ints */ |
| 2455 | void lbuild(int t) | 2504 | void lbuild(int t) |
| 2456 | { | 2505 | { |
| 2457 | vswap(); | 2506 | gv2(RC_INT, RC_INT); |
| 2458 | gv(RC_INT); | ||
| 2459 | vswap(); | ||
| 2460 | gv(RC_INT); | ||
| 2461 | vtop[-1].r2 = vtop[0].r; | 2507 | vtop[-1].r2 = vtop[0].r; |
| 2462 | vtop[-1].t = t; | 2508 | vtop[-1].t = t; |
| 2463 | vpop(); | 2509 | vpop(); |
| @@ -2890,13 +2936,15 @@ void gen_op(int op) · 426 unmodified lines | |||
| 2890 | vpushi(pointed_size(vtop[-1].t)); | 2936 | vpushi(pointed_size(vtop[-1].t)); |
| 2891 | gen_op('*'); | 2937 | gen_op('*'); |
| 2892 | if (do_bounds_check) { | 2938 | if (do_bounds_check) { |
| 2893 | /* if bounded pointers, we generate a special code to test bounds */ | 2939 | /* if bounded pointers, we generate a special code to |
| 2940 | test bounds */ | ||
| 2894 | if (op == '-') { | 2941 | if (op == '-') { |
| 2895 | vpushi(0); | 2942 | vpushi(0); |
| 2896 | vswap(); | 2943 | vswap(); |
| 2897 | gen_op('-'); | 2944 | gen_op('-'); |
| 2898 | } | 2945 | } |
| 2899 | gen_bounded_ptr_add(); | 2946 | gen_bounded_ptr_add1(); |
| 2947 | gen_bounded_ptr_add2(0); | ||
| 2900 | } else { | 2948 | } else { |
| 2901 | gen_opc(op); | 2949 | gen_opc(op); |
| 2902 | } | 2950 | } |
| @@ -3503,10 +3551,16 @@ void vstore(void) · 600 unmodified lines | |||
| 3503 | /* store result */ | 3551 | /* store result */ |
| 3504 | vstore(); | 3552 | vstore(); |
| 3505 | } else { | 3553 | } else { |
| 3554 | /* bound check case */ | ||
| 3555 | if (vtop[-1].r & VT_MUSTBOUND) { | ||
| 3556 | vswap(); | ||
| 3557 | gbound(); | ||
| 3558 | vswap(); | ||
| 3559 | } | ||
| 3506 | rc = RC_INT; | 3560 | rc = RC_INT; |
| 3507 | if (is_float(ft)) | 3561 | if (is_float(ft)) |
| 3508 | rc = RC_FLOAT; | 3562 | rc = RC_FLOAT; |
| 3509 | r = gv(rc); /* generate value (XXX: move that to store code) */ | 3563 | r = gv(rc); /* generate value */ |
| 3510 | /* if lvalue was saved on stack, must read it */ | 3564 | /* if lvalue was saved on stack, must read it */ |
| 3511 | if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) { | 3565 | if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) { |
| 3512 | SValue sv; | 3566 | SValue sv; |
| @@ -4043,8 +4097,13 @@ void indir(void) · 530 unmodified lines | |||
| 4043 | if (vtop->r & VT_LVAL) | 4097 | if (vtop->r & VT_LVAL) |
| 4044 | gv(RC_INT); | 4098 | gv(RC_INT); |
| 4045 | vtop->t = pointed_type(vtop->t); | 4099 | vtop->t = pointed_type(vtop->t); |
| 4046 | if (!(vtop->t & VT_ARRAY)) /* an array is never an lvalue */ | 4100 | /* an array is never an lvalue */ |
| 4101 | if (!(vtop->t & VT_ARRAY)) { | ||
| 4047 | vtop->r |= VT_LVAL; | 4102 | vtop->r |= VT_LVAL; |
| 4103 | /* if bound checking, the referenced pointer must be checked */ | ||
| 4104 | if (do_bounds_check) | ||
| 4105 | vtop->r |= VT_MUSTBOUND; | ||
| 4106 | } | ||
| 4048 | } | 4107 | } |
| 4049 | 4108 | ||
| 4050 | /* pass a parameter to a function and do type checking and casting */ | 4109 | /* pass a parameter to a function and do type checking and casting */ |
| @@ -4659,7 +4718,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 608 unmodified lines | |||
| 4659 | } else { | 4718 | } else { |
| 4660 | gv(RC_IRET); | 4719 | gv(RC_IRET); |
| 4661 | } | 4720 | } |
| 4662 | vpop(); | 4721 | vtop--; /* NOT vpop() because on x86 it would flush the fp stack */ |
| 4663 | } | 4722 | } |
| 4664 | skip(';'); | 4723 | skip(';'); |
| 4665 | rsym = gjmp(rsym); /* jmp */ | 4724 | rsym = gjmp(rsym); /* jmp */ |
| @@ -5195,8 +5254,23 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 529 unmodified lines | |||
| 5195 | if (ad->aligned > align) | 5254 | if (ad->aligned > align) |
| 5196 | align = ad->aligned; | 5255 | align = ad->aligned; |
| 5197 | if ((r & VT_VALMASK) == VT_LOCAL) { | 5256 | if ((r & VT_VALMASK) == VT_LOCAL) { |
| 5257 | if (do_bounds_check && (t & VT_ARRAY)) | ||
| 5258 | loc--; | ||
| 5198 | loc = (loc - size) & -align; | 5259 | loc = (loc - size) & -align; |
| 5199 | addr = loc; | 5260 | addr = loc; |
| 5261 | /* handles bounds */ | ||
| 5262 | /* XXX: currently, since we do only one pass, we cannot track | ||
| 5263 | '&' operators, so we add only arrays */ | ||
| 5264 | if (do_bounds_check && (t & VT_ARRAY)) { | ||
| 5265 | int *bounds_ptr; | ||
| 5266 | /* add padding between regions */ | ||
| 5267 | loc--; | ||
| 5268 | /* then add local bound info */ | ||
| 5269 | bounds_ptr = (int *)lbounds_section->data_ptr; | ||
| 5270 | *bounds_ptr++ = addr; | ||
| 5271 | *bounds_ptr++ = size; | ||
| 5272 | lbounds_section->data_ptr = (unsigned char *)bounds_ptr; | ||
| 5273 | } | ||
| 5200 | } else { | 5274 | } else { |
| 5201 | /* compute section */ | 5275 | /* compute section */ |
| 5202 | sec = ad->section; | 5276 | sec = ad->section; |
| @@ -5219,7 +5293,7 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 16 unmodified lines | |||
| 5219 | int *bounds_ptr; | 5293 | int *bounds_ptr; |
| 5220 | /* first, we need to add at least one byte between each region */ | 5294 | /* first, we need to add at least one byte between each region */ |
| 5221 | data_offset++; | 5295 | data_offset++; |
| 5222 | /* then add global data info */ | 5296 | /* then add global bound info */ |
| 5223 | bounds_ptr = (int *)bounds_section->data_ptr; | 5297 | bounds_ptr = (int *)bounds_section->data_ptr; |
| 5224 | *bounds_ptr++ = addr; | 5298 | *bounds_ptr++ = addr; |
| 5225 | *bounds_ptr++ = size; | 5299 | *bounds_ptr++ = size; |
| @@ -6059,9 +6133,11 @@ int main(int argc, char **argv) · 833 unmodified lines | |||
| 6059 | } else if (r[1] == 'b') { | 6133 | } else if (r[1] == 'b') { |
| 6060 | if (!do_bounds_check) { | 6134 | if (!do_bounds_check) { |
| 6061 | do_bounds_check = 1; | 6135 | do_bounds_check = 1; |
| 6062 | /* create bounds section for global data */ | 6136 | /* create bounds sections */ |
| 6063 | bounds_section = new_section(".bounds", | 6137 | bounds_section = new_section(".bounds", |
| 6064 | SHT_PROGBITS, SHF_ALLOC); | 6138 | SHT_PROGBITS, SHF_ALLOC); |
| 6139 | lbounds_section = new_section(".lbounds", | ||
| 6140 | SHT_PROGBITS, SHF_ALLOC); | ||
| 6065 | /* debug is implied */ | 6141 | /* debug is implied */ |
| 6066 | goto debug_opt; | 6142 | goto debug_opt; |
| 6067 | } | 6143 | } |