Unified · Split
| @@ -163,7 +163,9 @@ int section_num; · 162 unmodified lines | ||
| 163 | 163 | Section *text_section, *data_section, *bss_section; /* predefined sections */ |
| 164 | 164 | Section *cur_text_section; /* current section where function code is |
| 165 | 165 | generated */ |
| 166 | +/* bound check related sections */ | |
| 166 | 167 | Section *bounds_section; /* contains global data bound description */ |
| 168 | +Section *lbounds_section; /* contains local data bound description */ | |
| 167 | 169 | /* debug sections */ |
| 168 | 170 | Section *stab_section, *stabstr_section, *symtab_section, *strtab_section; |
| 169 | 171 | |
| @@ -209,18 +211,20 @@ int gnu_ext = 1; · 39 unmodified lines | ||
| 209 | 211 | int tcc_ext = 1; |
| 210 | 212 | |
| 211 | 213 | /* The current value can be: */ |
| 212 | −#define VT_VALMASK 0x00ff | |
| 213 | −#define VT_CONST 0x00f0 /* constant in vc | |
| 214 | +#define VT_VALMASK 0x00ff | |
| 215 | +#define VT_CONST 0x00f0 /* constant in vc | |
| 214 | 216 | (must be first non register value) */ |
| 215 | −#define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */ | |
| 216 | −#define VT_LOCAL 0x00f2 /* offset on stack */ | |
| 217 | −#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) */ | |
| 219 | −#define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */ | |
| 220 | −#define VT_LVAL 0x0100 /* var is an lvalue */ | |
| 221 | −#define VT_FORWARD 0x0200 /* value is forward reference */ | |
| 222 | −#define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for | |
| 217 | +#define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */ | |
| 218 | +#define VT_LOCAL 0x00f2 /* offset on stack */ | |
| 219 | +#define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */ | |
| 220 | +#define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */ | |
| 221 | +#define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */ | |
| 222 | +#define VT_LVAL 0x0100 /* var is an lvalue */ | |
| 223 | +#define VT_FORWARD 0x0200 /* value is forward reference */ | |
| 224 | +#define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for | |
| 223 | 225 | char/short stored in integer registers) */ |
| 226 | +#define VT_MUSTBOUND 0x0800 /* bound checking must be done before | |
| 227 | + dereferencing value */ | |
| 224 | 228 | |
| 225 | 229 | /* types */ |
| 226 | 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 | 406 | void decl_initializer(int t, int r, int c, int first, int size_only); |
| 403 | 407 | int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init); |
| 404 | 408 | int gv(int rc); |
| 409 | +void gv2(int rc1, int rc2); | |
| 405 | 410 | void move_reg(int r, int s); |
| 406 | 411 | void save_regs(void); |
| 407 | 412 | void save_reg(int r); |
| @@ -2339,6 +2344,20 @@ void gaddrof(void) · 1931 unmodified lines | ||
| 2339 | 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 | 2361 | /* store vtop a register belonging to class 'rc'. lvalues are |
| 2343 | 2362 | converted to values. Cannot be used if cannot be converted to |
| 2344 | 2363 | register value (such as structures). */ |
| @@ -2377,6 +2396,9 @@ int gv(int rc) · 32 unmodified lines | ||
| 2377 | 2396 | data_offset += size << 2; |
| 2378 | 2397 | data_section->data_ptr = (unsigned char *)data_offset; |
| 2379 | 2398 | } |
| 2399 | + if (vtop->r & VT_MUSTBOUND) | |
| 2400 | + gbound(); | |
| 2401 | + | |
| 2380 | 2402 | r = vtop->r & VT_VALMASK; |
| 2381 | 2403 | /* need to reload if: |
| 2382 | 2404 | - constant |
| @@ -2436,6 +2458,33 @@ int gv(int rc) · 53 unmodified lines | ||
| 2436 | 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 | 2488 | /* expand long long on stack in two int registers */ |
| 2440 | 2489 | void lexpand(void) |
| 2441 | 2490 | { |
| @@ -2454,10 +2503,7 @@ void lexpand(void) · 12 unmodified lines | ||
| 2454 | 2503 | /* build a long long from two ints */ |
| 2455 | 2504 | void lbuild(int t) |
| 2456 | 2505 | { |
| 2457 | − vswap(); | |
| 2458 | − gv(RC_INT); | |
| 2459 | − vswap(); | |
| 2460 | − gv(RC_INT); | |
| 2506 | + gv2(RC_INT, RC_INT); | |
| 2461 | 2507 | vtop[-1].r2 = vtop[0].r; |
| 2462 | 2508 | vtop[-1].t = t; |
| 2463 | 2509 | vpop(); |
| @@ -2890,13 +2936,15 @@ void gen_op(int op) · 426 unmodified lines | ||
| 2890 | 2936 | vpushi(pointed_size(vtop[-1].t)); |
| 2891 | 2937 | gen_op('*'); |
| 2892 | 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 | 2941 | if (op == '-') { |
| 2895 | 2942 | vpushi(0); |
| 2896 | 2943 | vswap(); |
| 2897 | 2944 | gen_op('-'); |
| 2898 | 2945 | } |
| 2899 | − gen_bounded_ptr_add(); | |
| 2946 | + gen_bounded_ptr_add1(); | |
| 2947 | + gen_bounded_ptr_add2(0); | |
| 2900 | 2948 | } else { |
| 2901 | 2949 | gen_opc(op); |
| 2902 | 2950 | } |
| @@ -3503,10 +3551,16 @@ void vstore(void) · 600 unmodified lines | ||
| 3503 | 3551 | /* store result */ |
| 3504 | 3552 | vstore(); |
| 3505 | 3553 | } else { |
| 3554 | + /* bound check case */ | |
| 3555 | + if (vtop[-1].r & VT_MUSTBOUND) { | |
| 3556 | + vswap(); | |
| 3557 | + gbound(); | |
| 3558 | + vswap(); | |
| 3559 | + } | |
| 3506 | 3560 | rc = RC_INT; |
| 3507 | 3561 | if (is_float(ft)) |
| 3508 | 3562 | rc = RC_FLOAT; |
| 3509 | − r = gv(rc); /* generate value (XXX: move that to store code) */ | |
| 3563 | + r = gv(rc); /* generate value */ | |
| 3510 | 3564 | /* if lvalue was saved on stack, must read it */ |
| 3511 | 3565 | if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) { |
| 3512 | 3566 | SValue sv; |
| @@ -4043,8 +4097,13 @@ void indir(void) · 530 unmodified lines | ||
| 4043 | 4097 | if (vtop->r & VT_LVAL) |
| 4044 | 4098 | gv(RC_INT); |
| 4045 | 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 | 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 | 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 | 4718 | } else { |
| 4660 | 4719 | gv(RC_IRET); |
| 4661 | 4720 | } |
| 4662 | − vpop(); | |
| 4721 | + vtop--; /* NOT vpop() because on x86 it would flush the fp stack */ | |
| 4663 | 4722 | } |
| 4664 | 4723 | skip(';'); |
| 4665 | 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 | 5254 | if (ad->aligned > align) |
| 5196 | 5255 | align = ad->aligned; |
| 5197 | 5256 | if ((r & VT_VALMASK) == VT_LOCAL) { |
| 5257 | + if (do_bounds_check && (t & VT_ARRAY)) | |
| 5258 | + loc--; | |
| 5198 | 5259 | loc = (loc - size) & -align; |
| 5199 | 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 | 5274 | } else { |
| 5201 | 5275 | /* compute section */ |
| 5202 | 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 | 5293 | int *bounds_ptr; |
| 5220 | 5294 | /* first, we need to add at least one byte between each region */ |
| 5221 | 5295 | data_offset++; |
| 5222 | − /* then add global data info */ | |
| 5296 | + /* then add global bound info */ | |
| 5223 | 5297 | bounds_ptr = (int *)bounds_section->data_ptr; |
| 5224 | 5298 | *bounds_ptr++ = addr; |
| 5225 | 5299 | *bounds_ptr++ = size; |
| @@ -6059,9 +6133,11 @@ int main(int argc, char **argv) · 833 unmodified lines | ||
| 6059 | 6133 | } else if (r[1] == 'b') { |
| 6060 | 6134 | if (!do_bounds_check) { |
| 6061 | 6135 | do_bounds_check = 1; |
| 6062 | − /* create bounds section for global data */ | |
| 6136 | + /* create bounds sections */ | |
| 6063 | 6137 | bounds_section = new_section(".bounds", |
| 6064 | 6138 | SHT_PROGBITS, SHF_ALLOC); |
| 6139 | + lbounds_section = new_section(".lbounds", | |
| 6140 | + SHT_PROGBITS, SHF_ALLOC); | |
| 6065 | 6141 | /* debug is implied */ |
| 6066 | 6142 | goto debug_opt; |
| 6067 | 6143 | } |