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
2315 2315
2316/* Subtract from the stack pointer, and push the resulting value onto the stack */2316/* Subtract from the stack pointer, and push the resulting value onto the stack */
2317ST_FUNC void gen_vla_alloc(CType *type, int align) {2317ST_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));
2319 o(0xE04D0000|(r<<12)|r); /* sub r, sp, r */2324 o(0xE04D0000|(r<<12)|r); /* sub r, sp, r */
2320#ifdef TCC_ARM_EABI2325#ifdef TCC_ARM_EABI
2321 if (align < 8)2326 if (align < 8)
@@ -2328,6 +2333,18 @@ ST_FUNC void gen_vla_alloc(CType *type, int align) { · 6 unmodified lines
2328 tcc_error("alignment is not a power of 2: %i", align);2333 tcc_error("alignment is not a power of 2: %i", align);
2329 o(stuff_const(0xE3C0D000|(r<<16), align - 1)); /* bic sp, r, #align-1 */2334 o(stuff_const(0xE3C0D000|(r<<16), align - 1)); /* bic sp, r, #align-1 */
2330 vpop();2335 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
2331}2348}
2332 2349
2333/* end of ARM code generator */2350/* 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
2049}2049}
2050 2050
2051ST_FUNC void gen_vla_alloc(CType *type, int align) {2051ST_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));
2053 o(0x91003c00 | r | r << 5); // add x(r),x(r),#152058 o(0x91003c00 | r | r << 5); // add x(r),x(r),#15
2054 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#152059 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15
2055 o(0xcb2063ff | r << 16); // sub sp,sp,x(r)2060 o(0xcb2063ff | r << 16); // sub sp,sp,x(r)
2056 vpop();2061 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
2057}2074}
2058 2075
2059/* end of A64 code generator */2076/* 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
1355 1355
1356ST_FUNC void gen_vla_alloc(CType *type, int align)1356ST_FUNC void gen_vla_alloc(CType *type, int align)
1357{1357{
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));
1359 EI(0x13, 0, rr, rr, 15); // addi RR, RR, 151364 EI(0x13, 0, rr, rr, 15); // addi RR, RR, 15
1360 EI(0x13, 7, rr, rr, -16); // andi, RR, RR, -161365 EI(0x13, 7, rr, rr, -16); // andi, RR, RR, -16
1361 ER(0x33, 0, 2, 2, rr, 0x20); // sub sp, sp, rr1366 ER(0x33, 0, 2, 2, rr, 0x20); // sub sp, sp, rr
1362 vpop();1367 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
1363}1380}
1364#endif1381#endif
tcctok.h+1−0
@@ -306,6 +306,7 @@ · 305 unmodified lines
306 DEF(TOK___bound_local_new, "__bound_local_new")306 DEF(TOK___bound_local_new, "__bound_local_new")
307 DEF(TOK___bound_local_delete, "__bound_local_delete")307 DEF(TOK___bound_local_delete, "__bound_local_delete")
308 DEF(TOK___bound_setjmp, "__bound_setjmp")308 DEF(TOK___bound_setjmp, "__bound_setjmp")
309 DEF(TOK___bound_new_region, "__bound_new_region")
309# ifdef TCC_TARGET_PE310# ifdef TCC_TARGET_PE
310# ifdef TCC_TARGET_X86_64311# ifdef TCC_TARGET_X86_64
311 DEF(TOK___bound_alloca_nr, "__bound_alloca_nr")312 DEF(TOK___bound_alloca_nr, "__bound_alloca_nr")
tests/Makefile+1−1
@@ -179,7 +179,7 @@ memtest: · 178 unmodified lines
179 179
180# memory and bound check auto test180# memory and bound check auto test
181BOUNDS_OK = 1 4 8 10 14 16181BOUNDS_OK = 1 4 8 10 14 16
182BOUNDS_FAIL= 2 5 6 7 9 11 12 13 15 17182BOUNDS_FAIL= 2 5 6 7 9 11 12 13 15 17 18
183 183
184btest: boundtest.c184btest: boundtest.c
185 @echo ------------ $@ ------------185 @echo ------------ $@ ------------
tests/boundtest.c+12−1
@@ -231,6 +231,16 @@ int test17() · 230 unmodified lines
231 return 0;231 return 0;
232}232}
233 233
234int 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
234int (*table_test[])(void) = {244int (*table_test[])(void) = {
235 test1,245 test1,
236 test2,246 test2,
@@ -248,7 +258,8 @@ int (*table_test[])(void) = { · 11 unmodified lines
248 test14,258 test14,
249 test15,259 test15,
250 test16,260 test16,
251 test17261 test17,
262 test18
252};263};
253 264
254int main(int argc, char **argv)265int 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