Skip to content

Commit d67cc26

Browse files
authored
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.
1 parent c04ef6b commit d67cc26

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

core/iwasm/common/wasm_runtime_common.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,12 +1417,39 @@ wasm_runtime_load_ex(uint8 *buf, uint32 size, const LoadArgs *args,
14171417
char *error_buf, uint32 error_buf_size)
14181418
{
14191419
WASMModuleCommon *module_common = NULL;
1420+
uint32 package_type;
1421+
bool magic_header_detected = false;
14201422

14211423
if (!args) {
1424+
set_error_buf(error_buf, error_buf_size,
1425+
"WASM module load failed: null load arguments");
1426+
return NULL;
1427+
}
1428+
1429+
if (size < 4) {
1430+
set_error_buf(error_buf, error_buf_size,
1431+
"WASM module load failed: unexpected end");
1432+
return NULL;
1433+
}
1434+
1435+
package_type = get_package_type(buf, size);
1436+
if (package_type == Wasm_Module_Bytecode) {
1437+
#if WASM_ENABLE_INTERP != 0
1438+
magic_header_detected = true;
1439+
#endif
1440+
}
1441+
else if (package_type == Wasm_Module_AoT) {
1442+
#if WASM_ENABLE_AOT != 0
1443+
magic_header_detected = true;
1444+
#endif
1445+
}
1446+
if (!magic_header_detected) {
1447+
set_error_buf(error_buf, error_buf_size,
1448+
"WASM module load failed: magic header not detected");
14221449
return NULL;
14231450
}
14241451

1425-
if (get_package_type(buf, size) == Wasm_Module_Bytecode) {
1452+
if (package_type == Wasm_Module_Bytecode) {
14261453
#if WASM_ENABLE_INTERP != 0
14271454
module_common =
14281455
(WASMModuleCommon *)wasm_load(buf, size,
@@ -1435,7 +1462,7 @@ wasm_runtime_load_ex(uint8 *buf, uint32 size, const LoadArgs *args,
14351462
args->wasm_binary_freeable;
14361463
#endif
14371464
}
1438-
else if (get_package_type(buf, size) == Wasm_Module_AoT) {
1465+
else if (package_type == Wasm_Module_AoT) {
14391466
#if WASM_ENABLE_AOT != 0
14401467
module_common = (WASMModuleCommon *)aot_load_from_aot_file(
14411468
buf, size, args, error_buf, error_buf_size);
@@ -1444,15 +1471,7 @@ wasm_runtime_load_ex(uint8 *buf, uint32 size, const LoadArgs *args,
14441471
args->wasm_binary_freeable;
14451472
#endif
14461473
}
1447-
else {
1448-
if (size < 4)
1449-
set_error_buf(error_buf, error_buf_size,
1450-
"WASM module load failed: unexpected end");
1451-
else
1452-
set_error_buf(error_buf, error_buf_size,
1453-
"WASM module load failed: magic header not detected");
1454-
return NULL;
1455-
}
1474+
14561475
if (!module_common) {
14571476
LOG_DEBUG("WASM module load failed");
14581477
return NULL;

0 commit comments

Comments
 (0)