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
This commit is contained in:
TianlongLiang 2024-11-07 13:38:42 +08:00 committed by GitHub
parent e352f0ab10
commit bf78863c56
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 9 deletions

View File

@ -3610,6 +3610,17 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
#endif #endif
} }
/* Code size in code entry can't be smaller than size of vec(locals)
* + expr(at least 1 for opcode end). And expressions are encoded by
* their instruction sequence terminated with an explicit 0x0B
* opcode for end. */
if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) {
set_error_buf(
error_buf, error_buf_size,
"section size mismatch: function body END opcode expected");
return false;
}
/* Alloc memory, layout: function structure + local types */ /* Alloc memory, layout: function structure + local types */
code_size = (uint32)(p_code_end - p_code); code_size = (uint32)(p_code_end - p_code);
@ -15837,15 +15848,12 @@ re_scan:
} }
if (loader_ctx->csp_num > 0) { if (loader_ctx->csp_num > 0) {
if (cur_func_idx < module->function_count - 1) /* unmatched end opcodes result from unbalanced control flow structures,
/* Function with missing end marker (between two functions) */ * for example, br_table with inconsistent target count (1 declared, 2
set_error_buf(error_buf, error_buf_size, "END opcode expected"); * given), or simply superfluous end opcodes */
else set_error_buf(
/* Function with missing end marker error_buf, error_buf_size,
(at EOF or end of code sections) */ "unexpected end opcodes from unbalanced control flow structures");
set_error_buf(error_buf, error_buf_size,
"unexpected end of section or function, "
"or section size mismatch");
goto fail; goto fail;
} }

View File

@ -1183,6 +1183,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
local_count += sub_local_count; local_count += sub_local_count;
} }
bh_assert(p_code_end > p_code && *(p_code_end - 1) == WASM_OP_END);
/* Alloc memory, layout: function structure + local types */ /* Alloc memory, layout: function structure + local types */
code_size = (uint32)(p_code_end - p_code); code_size = (uint32)(p_code_end - p_code);