History

Add vla bound support for arm,arm64 and riscv64

tcctok.h: - Add __bound_new_region arm-gen.c arm64-gen.c riscv64-gen.c: - Add bound checking support to gen_vla_alloc tests/Makefile boundtest.c - Add test18 vla bound checking test

herman ten brugge committed 6 years ago

Commit 8fb8d881 parent 44019e86 files · +68 −5Browse files

Unified · Split

6 files
  1. arm-gen.c +18 −1
  2. arm64-gen.c +18 −1
  3. riscv64-gen.c +18 −1
  4. tcctok.h +1 −0
  5. tests/Makefile +1 −1
  6. tests/boundtest.c +12 −1
arm-gen.c+18−1
@@ -2315,7 +2315,12 @@ ST_FUNC void gen_vla_sp_restore(int addr) { · 2314 unmodified lines
23152315
23162316 /* Subtract from the stack pointer, and push the resulting value onto the stack */
23172317 ST_FUNC void gen_vla_alloc(CType *type, int align) {
2318− int r = intr(gv(RC_INT));
2318+ int r;
2319+#if defined(CONFIG_TCC_BCHECK)
2320+ if (tcc_state->do_bounds_check)
2321+ vpushv(vtop);
2322+#endif
2323+ r = intr(gv(RC_INT));
23192324 o(0xE04D0000|(r<<12)|r); /* sub r, sp, r */
23202325 #ifdef TCC_ARM_EABI
23212326 if (align < 8)
@@ -2328,6 +2333,18 @@ ST_FUNC void gen_vla_alloc(CType *type, int align) { · 6 unmodified lines
23282333 tcc_error("alignment is not a power of 2: %i", align);
23292334 o(stuff_const(0xE3C0D000|(r<<16), align - 1)); /* bic sp, r, #align-1 */
23302335 vpop();
2336+#if defined(CONFIG_TCC_BCHECK)
2337+ if (tcc_state->do_bounds_check) {
2338+ vpushi(0);
2339+ vtop->r = TREG_R0;
2340+ o(0xe1a0000d | (vtop->r << 12)); // mov r0,sp
2341+ vswap();
2342+ vpush_global_sym(&func_old_type, TOK___bound_new_region);
2343+ vrott(3);
2344+ gfunc_call(2);
2345+ func_bound_add_epilog = 1;
2346+ }
2347+#endif
23312348 }
23322349
23332350 /* end of ARM code generator */
arm64-gen.c+18−1
@@ -2049,11 +2049,28 @@ ST_FUNC void gen_vla_sp_restore(int addr) { · 2048 unmodified lines
20492049 }
20502050
20512051 ST_FUNC void gen_vla_alloc(CType *type, int align) {
2052− uint32_t r = intr(gv(RC_INT));
2052+ uint32_t r;
2053+#if defined(CONFIG_TCC_BCHECK)
2054+ if (tcc_state->do_bounds_check)
2055+ vpushv(vtop);
2056+#endif
2057+ r = intr(gv(RC_INT));
20532058 o(0x91003c00 | r | r << 5); // add x(r),x(r),#15
20542059 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15
20552060 o(0xcb2063ff | r << 16); // sub sp,sp,x(r)
20562061 vpop();
2062+#if defined(CONFIG_TCC_BCHECK)
2063+ if (tcc_state->do_bounds_check) {
2064+ vpushi(0);
2065+ vtop->r = TREG_R(0);
2066+ o(0x910003e0 | vtop->r); // mov r0,sp
2067+ vswap();
2068+ vpush_global_sym(&func_old_type, TOK___bound_new_region);
2069+ vrott(3);
2070+ gfunc_call(2);
2071+ func_bound_add_epilog = 1;
2072+ }
2073+#endif
20572074 }
20582075
20592076 /* end of A64 code generator */
riscv64-gen.c+18−1
@@ -1355,10 +1355,27 @@ ST_FUNC void gen_vla_sp_restore(int addr) · 1354 unmodified lines
13551355
13561356 ST_FUNC void gen_vla_alloc(CType *type, int align)
13571357 {
1358− int rr = ireg(gv(RC_INT));
1358+ int rr;
1359+#if defined(CONFIG_TCC_BCHECK)
1360+ if (tcc_state->do_bounds_check)
1361+ vpushv(vtop);
1362+#endif
1363+ rr = ireg(gv(RC_INT));
13591364 EI(0x13, 0, rr, rr, 15); // addi RR, RR, 15
13601365 EI(0x13, 7, rr, rr, -16); // andi, RR, RR, -16
13611366 ER(0x33, 0, 2, 2, rr, 0x20); // sub sp, sp, rr
13621367 vpop();
1368+#if defined(CONFIG_TCC_BCHECK)
1369+ if (tcc_state->do_bounds_check) {
1370+ vpushi(0);
1371+ vtop->r = TREG_R(0);
1372+ o(0x00010513); /* mv a0,sp */
1373+ vswap();
1374+ vpush_global_sym(&func_old_type, TOK___bound_new_region);
1375+ vrott(3);
1376+ gfunc_call(2);
1377+ func_bound_add_epilog = 1;
1378+ }
1379+#endif
13631380 }
13641381 #endif
tcctok.h+1−0
@@ -306,6 +306,7 @@ · 305 unmodified lines
306306 DEF(TOK___bound_local_new, "__bound_local_new")
307307 DEF(TOK___bound_local_delete, "__bound_local_delete")
308308 DEF(TOK___bound_setjmp, "__bound_setjmp")
309+ DEF(TOK___bound_new_region, "__bound_new_region")
309310 # ifdef TCC_TARGET_PE
310311 # ifdef TCC_TARGET_X86_64
311312 DEF(TOK___bound_alloca_nr, "__bound_alloca_nr")
tests/Makefile+1−1
@@ -179,7 +179,7 @@ memtest: · 178 unmodified lines
179179
180180 # memory and bound check auto test
181181 BOUNDS_OK = 1 4 8 10 14 16
182−BOUNDS_FAIL= 2 5 6 7 9 11 12 13 15 17
182+BOUNDS_FAIL= 2 5 6 7 9 11 12 13 15 17 18
183183
184184 btest: boundtest.c
185185 @echo ------------ $@ ------------
tests/boundtest.c+12−1
@@ -231,6 +231,16 @@ int test17() · 230 unmodified lines
231231 return 0;
232232 }
233233
234+int test18(void)
235+{
236+ int i, sum = 0, n = TAB_SIZE;
237+ int tab[n];
238+ for(i=0;i<TAB_SIZE+1;i++) {
239+ sum += tab[i];
240+ }
241+ return sum;
242+}
243+
234244 int (*table_test[])(void) = {
235245 test1,
236246 test2,
@@ -248,7 +258,8 @@ int (*table_test[])(void) = { · 11 unmodified lines
248258 test14,
249259 test15,
250260 test16,
251− test17
261+ test17,
262+ test18
252263 };
253264
254265 int main(int argc, char **argv)

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