Skip to content

Commit 1550eac

Browse files
committed
fix: reject non-wasm files quickly
1 parent b915910 commit 1550eac

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

tests/fuzz/wasm-mutator-fuzz/aot-compiler/aot_compiler_fuzz.cc

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,31 @@ handle_aot_recent_error(const char *tag)
2626
extern "C" int
2727
LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
2828
{
29-
wasm_module_t module = NULL;
29+
wasm_module_t wasm_module = NULL;
3030
char error_buf[128] = { 0 };
3131
AOTCompOption option = { 0 };
3232
aot_comp_data_t comp_data = NULL;
3333
aot_comp_context_t comp_ctx = NULL;
34+
uint8 *aot_file_buf = NULL;
35+
uint32 aot_file_size = 0;
36+
wasm_module_t aot_module = NULL;
37+
wasm_module_inst_t aot_inst = NULL;
3438

3539
/* libfuzzer don't allow to modify the given Data, so make a copy here */
3640
std::vector<uint8_t> myData(Data, Data + Size);
3741

3842
if (Size >= 4
3943
&& get_package_type(myData.data(), Size) != Wasm_Module_Bytecode) {
40-
printf("Invalid wasm file: magic header not detected\n");
44+
handle_aot_recent_error("[INVALID WASM FILE]");
4145
return 0;
4246
}
4347

4448
wasm_runtime_init();
4549

46-
module = wasm_runtime_load((uint8_t *)myData.data(), Size, error_buf, 120);
47-
if (!module) {
48-
std::cout << "[LOADING] " << error_buf << std::endl;
50+
wasm_module =
51+
wasm_runtime_load((uint8_t *)myData.data(), Size, error_buf, 120);
52+
if (!wasm_module) {
53+
handle_aot_recent_error("[LOADING MODULE]");
4954
goto DESTROY_RUNTIME;
5055
}
5156

@@ -61,7 +66,7 @@ LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
6166
option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
6267

6368
comp_data =
64-
aot_create_comp_data(module, option.target_arch, option.enable_gc);
69+
aot_create_comp_data(wasm_module, option.target_arch, option.enable_gc);
6570
if (!comp_data) {
6671
handle_aot_recent_error("[CREATING comp_data]");
6772
goto UNLOAD_MODULE;
@@ -78,12 +83,39 @@ LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
7883
goto DESTROY_COMP_CTX;
7984
}
8085

86+
aot_file_buf = aot_emit_aot_file_buf(comp_ctx, comp_data, &aot_file_size);
87+
if (!aot_file_buf || aot_file_size == 0) {
88+
handle_aot_recent_error("[EMITTING AOT FILE]");
89+
goto DESTROY_COMP_CTX;
90+
}
91+
92+
aot_module = wasm_runtime_load(aot_file_buf, aot_file_size, error_buf, 120);
93+
if (!aot_module) {
94+
handle_aot_recent_error("[LOADING AOT MODULE]");
95+
goto RELEASE_AOT_FILE;
96+
}
97+
98+
aot_inst = wasm_runtime_instantiate(
99+
aot_module, 8 * 1024 * 1024, 16 * 1024 * 1024, error_buf, 120);
100+
if (!aot_inst) {
101+
handle_aot_recent_error("[INSTANTIATING AOT MODULE]");
102+
goto UNLOAD_AOT_MODULE;
103+
}
104+
105+
DEINSTANTIATE_AOT_MODULE:
106+
wasm_runtime_deinstantiate(aot_inst);
107+
UNLOAD_AOT_MODULE:
108+
wasm_runtime_unload(aot_module);
109+
RELEASE_AOT_FILE:
110+
if (aot_file_buf) {
111+
wasm_runtime_free(aot_file_buf);
112+
}
81113
DESTROY_COMP_CTX:
82114
aot_destroy_comp_context(comp_ctx);
83115
DESTROY_COMP_DATA:
84116
aot_destroy_comp_data(comp_data);
85117
UNLOAD_MODULE:
86-
wasm_runtime_unload(module);
118+
wasm_runtime_unload(wasm_module);
87119
DESTROY_RUNTIME:
88120
wasm_runtime_destroy();
89121

tests/fuzz/wasm-mutator-fuzz/wasm-mutator/wasm_mutator_fuzz.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
164164
* here */
165165
std::vector<uint8_t> myData(Data, Data + Size);
166166

167+
/* only wasm bytecode */
168+
if (Size >= 4
169+
&& get_package_type(myData.data(), Size) != Wasm_Module_Bytecode) {
170+
printf("Invalid wasm file: magic header not detected\n");
171+
return 0;
172+
}
173+
167174
/* init runtime environment */
168175
wasm_runtime_init();
169176

0 commit comments

Comments
 (0)