@@ -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
0 commit comments