Skip to content

Commit 0e4dffc

Browse files
Fix a leak in wasm_loader_emit_br_info (#3900)
Reference Info: 377955855 wamr:wasm_mutator_fuzz_loader: Direct-leak in wasm_loader_emit_br_info https://issues.oss-fuzz.com/issues/377955855
1 parent 226bf22 commit 0e4dffc

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

core/iwasm/common/wasm_application.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
105105
bool ret, is_import_func = true, is_memory64 = false;
106106
#if WASM_ENABLE_MEMORY64 != 0
107107
WASMModuleInstance *wasm_module_inst = (WASMModuleInstance *)module_inst;
108-
is_memory64 = wasm_module_inst->memories[0]->is_memory64;
108+
if (wasm_module_inst->memory_count > 0)
109+
is_memory64 = wasm_module_inst->memories[0]->is_memory64;
109110
#endif
110111

111112
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);

core/iwasm/interpreter/wasm_loader.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9885,13 +9885,6 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
98859885
}
98869886
#endif /* WASM_ENABLE_FAST_INTERP */
98879887

9888-
#define RESERVE_BLOCK_RET() \
9889-
do { \
9890-
if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
9891-
error_buf_size)) \
9892-
goto fail; \
9893-
} while (0)
9894-
98959888
#define PUSH_TYPE(type) \
98969889
do { \
98979890
if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
@@ -11612,7 +11605,10 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1161211605
#if WASM_ENABLE_FAST_INTERP != 0
1161311606
/* if the result of if branch is in local or const area, add a
1161411607
* copy op */
11615-
RESERVE_BLOCK_RET();
11608+
if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
11609+
error_buf, error_buf_size)) {
11610+
goto fail;
11611+
}
1161611612

1161711613
emit_empty_label_addr_and_frame_ip(PATCH_END);
1161811614
apply_label_patch(loader_ctx, 1, PATCH_ELSE);
@@ -11672,7 +11668,15 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1167211668
#if WASM_ENABLE_FAST_INTERP != 0
1167311669
skip_label();
1167411670
/* copy the result to the block return address */
11675-
RESERVE_BLOCK_RET();
11671+
if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
11672+
error_buf, error_buf_size)) {
11673+
/* it could be tmp frame_csp allocated from opcode like
11674+
* OP_BR and not counted in loader_ctx->csp_num, it won't
11675+
* be freed in wasm_loader_ctx_destroy(loader_ctx) so need
11676+
* to free the loader_ctx->frame_csp if fails */
11677+
free_label_patch_list(loader_ctx->frame_csp);
11678+
goto fail;
11679+
}
1167611680

1167711681
apply_label_patch(loader_ctx, 0, PATCH_END);
1167811682
free_label_patch_list(loader_ctx->frame_csp);

core/iwasm/interpreter/wasm_mini_loader.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5592,13 +5592,6 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
55925592

55935593
#endif /* WASM_ENABLE_FAST_INTERP */
55945594

5595-
#define RESERVE_BLOCK_RET() \
5596-
do { \
5597-
if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
5598-
error_buf_size)) \
5599-
goto fail; \
5600-
} while (0)
5601-
56025595
#define PUSH_TYPE(type) \
56035596
do { \
56045597
if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
@@ -6366,7 +6359,10 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
63666359
#if WASM_ENABLE_FAST_INTERP != 0
63676360
/* if the result of if branch is in local or const area, add a
63686361
* copy op */
6369-
RESERVE_BLOCK_RET();
6362+
if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
6363+
error_buf, error_buf_size)) {
6364+
goto fail;
6365+
}
63706366

63716367
emit_empty_label_addr_and_frame_ip(PATCH_END);
63726368
apply_label_patch(loader_ctx, 1, PATCH_ELSE);
@@ -6426,7 +6422,11 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
64266422
#if WASM_ENABLE_FAST_INTERP != 0
64276423
skip_label();
64286424
/* copy the result to the block return address */
6429-
RESERVE_BLOCK_RET();
6425+
if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
6426+
error_buf, error_buf_size)) {
6427+
free_label_patch_list(loader_ctx->frame_csp);
6428+
goto fail;
6429+
}
64306430

64316431
apply_label_patch(loader_ctx, 0, PATCH_END);
64326432
free_label_patch_list(loader_ctx->frame_csp);

0 commit comments

Comments
 (0)