Skip to content

Commit f0632ed

Browse files
authored
Clone the input binary during wasm_module_validate (#2483)
1 parent a2f76cf commit f0632ed

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

core/iwasm/common/wasm_c_api.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,8 +2290,10 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
22902290
bool
22912291
wasm_module_validate(wasm_store_t *store, const wasm_byte_vec_t *binary)
22922292
{
2293+
wasm_byte_vec_t local_binary = { 0 };
22932294
struct WASMModuleCommon *module_rt;
22942295
char error_buf[128] = { 0 };
2296+
bool ret;
22952297

22962298
bh_assert(singleton_engine);
22972299

@@ -2300,15 +2302,26 @@ wasm_module_validate(wasm_store_t *store, const wasm_byte_vec_t *binary)
23002302
return false;
23012303
}
23022304

2303-
if ((module_rt = wasm_runtime_load((uint8 *)binary->data,
2304-
(uint32)binary->size, error_buf, 128))) {
2305+
/* make a copy of binary */
2306+
wasm_byte_vec_new_uninitialized(&local_binary, binary->size);
2307+
if (binary->size && !local_binary.data)
2308+
return false;
2309+
2310+
wasm_byte_vec_copy(&local_binary, binary);
2311+
2312+
module_rt = wasm_runtime_load((uint8 *)local_binary.data,
2313+
(uint32)local_binary.size, error_buf, 128);
2314+
wasm_byte_vec_delete(&local_binary);
2315+
if (module_rt) {
23052316
wasm_runtime_unload(module_rt);
2306-
return true;
2317+
ret = true;
23072318
}
23082319
else {
2320+
ret = false;
23092321
LOG_VERBOSE(error_buf);
2310-
return false;
23112322
}
2323+
2324+
return ret;
23122325
}
23132326

23142327
static void

samples/wasm-c-api/src/hello.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ int main(int argc, const char* argv[]) {
6565
}
6666
fclose(file);
6767

68+
if (!wasm_module_validate(store, &binary)) {
69+
printf("> Error validate module!\n");
70+
wasm_byte_vec_delete(&binary);
71+
return 1;
72+
}
73+
6874
// Compile.
6975
printf("Compiling module...\n");
7076
own wasm_module_t* module = wasm_module_new(store, &binary);

0 commit comments

Comments
 (0)