From d67cc26d56345d3494e5fd2ebab5fa5bcadaa5d7 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Tue, 20 Aug 2024 16:03:04 +0800 Subject: [PATCH] Fix load error not reported when magic header is invalid (#3734) When AOT isn't enabled and the input is a wasm file, wasm_runtime_load doesn't report error. Same when interpreter isn't enabled and the input is AOT file. This PR makes wasm_runtime_load report error "magic header not detected" for such situations. --- core/iwasm/common/wasm_runtime_common.c | 41 ++++++++++++++++++------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 37a89f8fb..f54ac4ab9 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -1417,12 +1417,39 @@ wasm_runtime_load_ex(uint8 *buf, uint32 size, const LoadArgs *args, char *error_buf, uint32 error_buf_size) { WASMModuleCommon *module_common = NULL; + uint32 package_type; + bool magic_header_detected = false; if (!args) { + set_error_buf(error_buf, error_buf_size, + "WASM module load failed: null load arguments"); return NULL; } - if (get_package_type(buf, size) == Wasm_Module_Bytecode) { + if (size < 4) { + set_error_buf(error_buf, error_buf_size, + "WASM module load failed: unexpected end"); + return NULL; + } + + package_type = get_package_type(buf, size); + if (package_type == Wasm_Module_Bytecode) { +#if WASM_ENABLE_INTERP != 0 + magic_header_detected = true; +#endif + } + else if (package_type == Wasm_Module_AoT) { +#if WASM_ENABLE_AOT != 0 + magic_header_detected = true; +#endif + } + if (!magic_header_detected) { + set_error_buf(error_buf, error_buf_size, + "WASM module load failed: magic header not detected"); + return NULL; + } + + if (package_type == Wasm_Module_Bytecode) { #if WASM_ENABLE_INTERP != 0 module_common = (WASMModuleCommon *)wasm_load(buf, size, @@ -1435,7 +1462,7 @@ wasm_runtime_load_ex(uint8 *buf, uint32 size, const LoadArgs *args, args->wasm_binary_freeable; #endif } - else if (get_package_type(buf, size) == Wasm_Module_AoT) { + else if (package_type == Wasm_Module_AoT) { #if WASM_ENABLE_AOT != 0 module_common = (WASMModuleCommon *)aot_load_from_aot_file( buf, size, args, error_buf, error_buf_size); @@ -1444,15 +1471,7 @@ wasm_runtime_load_ex(uint8 *buf, uint32 size, const LoadArgs *args, args->wasm_binary_freeable; #endif } - else { - if (size < 4) - set_error_buf(error_buf, error_buf_size, - "WASM module load failed: unexpected end"); - else - set_error_buf(error_buf, error_buf_size, - "WASM module load failed: magic header not detected"); - return NULL; - } + if (!module_common) { LOG_DEBUG("WASM module load failed"); return NULL;