Skip to content

Commit

Permalink
EH: fix validation of delegate opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Feb 1, 2024
1 parent 3111a86 commit 9fcedfd
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -7129,6 +7129,40 @@ check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
return NULL;
}

#if WASM_ENABLE_EXCE_HANDLING != 0
static BranchBlock *
check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
uint8 *buf_end, char *error_buf,
uint32 error_buf_size)
{
uint8 *p = *p_buf, *p_end = buf_end;
BranchBlock *frame_csp_tmp;
uint32 depth;

read_leb_uint32(p, p_end, depth);
/*
* Note: "delegate 0" means the surrounding block, not the
* try-delegate block itself.
*
* Note: the caller hasn't popped the try-delegate frame yet.
*/
bh_assert(loader_ctx->csp_num > 0);
if (loader_ctx->csp_num - 1 <= depth) {
set_error_buf(error_buf, error_buf_size, "unknown delegate label");
goto fail;
}
frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
#if WASM_ENABLE_FAST_INTERP != 0
emit_br_info(frame_csp_tmp);
#endif

*p_buf = p;
return frame_csp_tmp;
fail:
return NULL;
}
#endif

static bool
check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
char *error_buf, uint32 error_buf_size)
Expand Down Expand Up @@ -7831,16 +7865,10 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
case WASM_OP_DELEGATE:
{
/* check target block is valid */
if (!(frame_csp_tmp = check_branch_block(
if (!(frame_csp_tmp = check_branch_block_for_delegate(
loader_ctx, &p, p_end, error_buf, error_buf_size)))
goto fail;

/* valid types */
if (LABEL_TYPE_TRY != frame_csp_tmp->label_type) {
snprintf(error_buf, error_buf_size, "unknown label");
goto fail;
}

BranchBlock *cur_block = loader_ctx->frame_csp - 1;
uint8 label_type = cur_block->label_type;

Expand Down

0 comments on commit 9fcedfd

Please sign in to comment.