Skip to content

Commit bf78863

Browse files
Wasm loader enhancement: check code size in code entry (#3892)
add wasm loader check: in code entry, the code size should match the size of vec(locals) + expr, and expr should end with opcode end
1 parent e352f0a commit bf78863

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

core/iwasm/interpreter/wasm_loader.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3610,6 +3610,17 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
36103610
#endif
36113611
}
36123612

3613+
/* Code size in code entry can't be smaller than size of vec(locals)
3614+
* + expr(at least 1 for opcode end). And expressions are encoded by
3615+
* their instruction sequence terminated with an explicit 0x0B
3616+
* opcode for end. */
3617+
if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) {
3618+
set_error_buf(
3619+
error_buf, error_buf_size,
3620+
"section size mismatch: function body END opcode expected");
3621+
return false;
3622+
}
3623+
36133624
/* Alloc memory, layout: function structure + local types */
36143625
code_size = (uint32)(p_code_end - p_code);
36153626

@@ -15837,15 +15848,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1583715848
}
1583815849

1583915850
if (loader_ctx->csp_num > 0) {
15840-
if (cur_func_idx < module->function_count - 1)
15841-
/* Function with missing end marker (between two functions) */
15842-
set_error_buf(error_buf, error_buf_size, "END opcode expected");
15843-
else
15844-
/* Function with missing end marker
15845-
(at EOF or end of code sections) */
15846-
set_error_buf(error_buf, error_buf_size,
15847-
"unexpected end of section or function, "
15848-
"or section size mismatch");
15851+
/* unmatched end opcodes result from unbalanced control flow structures,
15852+
* for example, br_table with inconsistent target count (1 declared, 2
15853+
* given), or simply superfluous end opcodes */
15854+
set_error_buf(
15855+
error_buf, error_buf_size,
15856+
"unexpected end opcodes from unbalanced control flow structures");
1584915857
goto fail;
1585015858
}
1585115859

core/iwasm/interpreter/wasm_mini_loader.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
11831183
local_count += sub_local_count;
11841184
}
11851185

1186+
bh_assert(p_code_end > p_code && *(p_code_end - 1) == WASM_OP_END);
1187+
11861188
/* Alloc memory, layout: function structure + local types */
11871189
code_size = (uint32)(p_code_end - p_code);
11881190

0 commit comments

Comments
 (0)