Fix issues of handling op block/if/loop/else (#1049)

Since `basic_block_else` is NULL, it meets a crash if there is a
IF block without a else branch. Like:

``` wat
(func (export "params-id") (param i32) (result i32)
  (i32.const 1)
  (if (param i32) (result i32) (local.get 0)
    (then)
  )
)
```

Consider the ELSE block will be created lazily, focus on
`basic_block_entry" here.
This commit is contained in:
liang.he 2022-03-21 14:00:58 +08:00 committed by GitHub
parent 9fd3d53bc9
commit 0f2885cd66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 19 deletions

View File

@ -128,8 +128,7 @@ push_jit_block_to_stack_and_pass_params(JitCompContext *cc, JitBlock *block,
JitReg value; JitReg value;
uint32 i, param_index, cell_num; uint32 i, param_index, cell_num;
if (block->label_type == LABEL_TYPE_BLOCK if (cc->cur_basic_block == basic_block) {
|| (block->label_type == LABEL_TYPE_IF && !cond)) {
/* Reuse the current basic block and no need to commit values, /* Reuse the current basic block and no need to commit values,
we just move param values from current block's value stack to we just move param values from current block's value stack to
the new block's value stack */ the new block's value stack */
@ -178,10 +177,11 @@ push_jit_block_to_stack_and_pass_params(JitCompContext *cc, JitBlock *block,
/* Push the new block to block stack */ /* Push the new block to block stack */
jit_block_stack_push(&cc->block_stack, block); jit_block_stack_push(&cc->block_stack, block);
if (!cond) { /* LOOP block */ if (block->label_type == LABEL_TYPE_LOOP) {
BUILD_BR(basic_block); BUILD_BR(basic_block);
} }
else { /* IF block with condition br insn */ else {
/* IF block with condition br insn */
if (!GEN_INSN(CMP, cc->cmp_reg, cond, NEW_CONST(I32, 0)) if (!GEN_INSN(CMP, cc->cmp_reg, cond, NEW_CONST(I32, 0))
|| !(insn = GEN_INSN(BNE, cc->cmp_reg, || !(insn = GEN_INSN(BNE, cc->cmp_reg,
jit_basic_block_label(basic_block), 0))) { jit_basic_block_label(basic_block), 0))) {
@ -192,7 +192,7 @@ push_jit_block_to_stack_and_pass_params(JitCompContext *cc, JitBlock *block,
/* Don't create else basic block or end basic block now, just /* Don't create else basic block or end basic block now, just
save its incoming BNE insn, and patch the insn's else label save its incoming BNE insn, and patch the insn's else label
when the basic block is lazily created */ when the basic block is lazily created */
if (basic_block == block->basic_block_entry) { if (block->wasm_code_else) {
block->incoming_insn_for_else_bb = insn; block->incoming_insn_for_else_bb = insn;
} }
else { else {
@ -584,16 +584,9 @@ jit_compile_op_block(JitCompContext *cc, uint8 **p_frame_ip,
SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1); SET_BB_END_BCIP(cc->cur_basic_block, *p_frame_ip - 1);
SET_BB_BEGIN_BCIP(block->basic_block_entry, *p_frame_ip); SET_BB_BEGIN_BCIP(block->basic_block_entry, *p_frame_ip);
if (else_addr) { if (!push_jit_block_to_stack_and_pass_params(
if (!push_jit_block_to_stack_and_pass_params( cc, block, block->basic_block_entry, value))
cc, block, block->basic_block_entry, value)) goto fail;
goto fail;
}
else {
if (!push_jit_block_to_stack_and_pass_params(
cc, block, block->basic_block_else, value))
goto fail;
}
} }
else { else {
if (jit_cc_get_const_I32(cc, value) != 0) { if (jit_cc_get_const_I32(cc, value) != 0) {
@ -601,7 +594,7 @@ jit_compile_op_block(JitCompContext *cc, uint8 **p_frame_ip,
BASIC_BLOCK if cannot be reached, we treat it same as BASIC_BLOCK if cannot be reached, we treat it same as
LABEL_TYPE_BLOCK and start to translate if branch */ LABEL_TYPE_BLOCK and start to translate if branch */
if (!push_jit_block_to_stack_and_pass_params( if (!push_jit_block_to_stack_and_pass_params(
cc, block, block->basic_block_entry, 0)) cc, block, cc->cur_basic_block, 0))
goto fail; goto fail;
} }
else { else {
@ -610,7 +603,7 @@ jit_compile_op_block(JitCompContext *cc, uint8 **p_frame_ip,
BASIC_BLOCK if cannot be reached, we treat it same as BASIC_BLOCK if cannot be reached, we treat it same as
LABEL_TYPE_BLOCK and start to translate else branch */ LABEL_TYPE_BLOCK and start to translate else branch */
if (!push_jit_block_to_stack_and_pass_params( if (!push_jit_block_to_stack_and_pass_params(
cc, block, block->basic_block_else, 0)) cc, block, cc->cur_basic_block, 0))
goto fail; goto fail;
*p_frame_ip = else_addr + 1; *p_frame_ip = else_addr + 1;
} }

View File

@ -185,8 +185,7 @@ jit_dump_basic_block(JitCompContext *cc, JitBasicBlock *block)
: cc->jitted_addr_end); : cc->jitted_addr_end);
else else
/* Dump IR. */ /* Dump IR. */
JIT_FOREACH_INSN(block, insn) JIT_FOREACH_INSN(block, insn) jit_dump_insn(cc, insn);
jit_dump_insn(cc, insn);
os_printf(" ; SUCCS("); os_printf(" ; SUCCS(");