Skip to content

Commit d81020a

Browse files
committed
[fuzzing] Enable instantiation (bytecodealliance#3958)
- Increase input seed size for wasm-tools to generate larger WebAssembly modules - Add instantiation in wasm mutator fuzz tests
1 parent 4d76964 commit d81020a

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

tests/fuzz/wasm-mutator-fuzz/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ $ wasm-tools help
1919
mkdir build && cd build
2020
# Without custom mutator (libfuzzer modify the buffer randomly)
2121
cmake ..
22+
# TODO: TBC. `wasm-tools mutate` is not supported yet
2223
# With custom mutator (wasm-tools mutate)
2324
cmake .. -DCUSTOM_MUTATOR=1
2425
make -j$(nproc)

tests/fuzz/wasm-mutator-fuzz/smith_wasm.sh

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,36 @@ function try_generate_wasm()
3333

3434
local try_i=0
3535
until [[ -f $GENERATED_WASM_NAME ]]; do
36-
head -c 100 /dev/urandom | wasm-tools smith $SMITH_OPTIONS -o $GENERATED_WASM_NAME >/dev/null 2>&1
36+
# Larger input seeds tend to generate larger WebAssembly modules. (256KB)
37+
head -c 262144 /dev/urandom | wasm-tools smith $SMITH_OPTIONS -o $GENERATED_WASM_NAME >/dev/null 2>&1
3738
try_i=$((try_i+1))
3839
done
3940

4041
printf -- "-- output ${GENERATED_WASM_NAME} in %d retries\n" $try_i
4142
}
4243

43-
# try_generate_wasm "--min-memories=1 --min-tables=1" "test_min.wasm"
44+
WASM_SHAPE=" --allow-invalid-funcs true \
45+
--generate-custom-sections true \
46+
--min-funcs 5 \
47+
--max-instructions 1024 \
48+
--min-globals 10"
49+
50+
WASM_MVP_FEATURES=" --bulk-memory-enabled true \
51+
--multi-value-enabled true \
52+
--reference-types-enabled true \
53+
--simd-enabled true \
54+
--tail-call-enabled true"
4455

4556
for i in $(seq 1 $EXPECTED_NUM)
4657
do
47-
# by default
48-
try_generate_wasm "" test_$i.wasm
49-
50-
# with different features
5158
# mvp
52-
try_generate_wasm "--min-memories=1 --min-tables=1" test_min_$i.wasm
53-
try_generate_wasm "--min-memories=1 --min-tables=1 --bulk-memory-enabled true" test_bulk_$i.wasm
54-
try_generate_wasm "--min-memories=1 --min-tables=1 --reference-types-enabled true" test_ref_$i.wasm
55-
try_generate_wasm "--min-memories=1 --min-tables=1 --multi-value-enabled true" test_multi_$i.wasm
56-
try_generate_wasm "--min-memories=1 --min-tables=1 --simd-enabled true" test_simd_$i.wasm
57-
try_generate_wasm "--min-memories=1 --min-tables=1 --tail-call-enabled true " test_tail_$i.wasm
59+
try_generate_wasm "${WASM_SHAPE} ${WASM_MVP_FEATURES}" test_mvp_$i.wasm
5860

59-
# enable me when compiling iwasm with those features
60-
#try_generate_wasm "--min-memories=1 --min-tables=1 --threads-enabled true" test_thread_$i.wasm
61-
#try_generate_wasm "--min-memories=1 --min-tables=1 --memory64-enabled true" test_memory64_$i.wasm
62-
#try_generate_wasm "--min-memories=1 --min-tables=1 --exceptions-enabled true" test_exception_$i.wasm
63-
#try_generate_wasm "--min-memories=1 --min-tables=1 --gc-enabled true" test_gc_$i.wasm
64-
# with custom-section
65-
try_generate_wasm "--min-memories=1 --min-tables=1 --generate-custom-sections true" test_custom_$i.wasm
61+
# other proposals
62+
try_generate_wasm "${WASM_SHAPE} --exceptions-enabled true" test_exception_$i.wasm
63+
try_generate_wasm "${WASM_SHAPE} --gc-enabled true" test_gc_$i.wasm
64+
try_generate_wasm "${WASM_SHAPE} --memory64-enabled true" test_memory64_$i.wasm
65+
try_generate_wasm "${WASM_SHAPE} --threads-enabled true" test_threads_$i.wasm
6666
done
6767

6868
printf "Done\n"

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,41 @@
1313

1414
using namespace std;
1515

16-
extern "C" WASMModuleCommon *
17-
wasm_runtime_load(uint8 *buf, uint32 size, char *error_buf,
18-
uint32 error_buf_size);
19-
20-
extern "C" WASMModuleInstanceCommon *
21-
wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
22-
uint32 heap_size, char *error_buf,
23-
uint32 error_buf_size);
24-
2516
extern "C" int
2617
LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
2718
{
2819
/* libfuzzer don't allow us to modify the given Data, so we copy the data
2920
* here */
3021
std::vector<uint8_t> myData(Data, Data + Size);
22+
3123
/* init runtime environment */
3224
wasm_runtime_init();
25+
26+
char error_buf[128] = { 0 };
3327
wasm_module_t module =
34-
wasm_runtime_load((uint8_t *)myData.data(), Size, nullptr, 0);
35-
if (module) {
28+
wasm_runtime_load((uint8_t *)myData.data(), Size, error_buf, 120);
29+
if (!module) {
30+
std::cout << "[LOADING] " << error_buf << std::endl;
31+
wasm_runtime_destroy();
32+
/* return SUCCESS because the failure has been handled */
33+
return 0;
34+
}
35+
36+
wasm_module_inst_t inst = wasm_runtime_instantiate(
37+
module, 8 * 1024 * 1024, 16 * 1024 * 1024, error_buf, 120);
38+
if (!inst) {
39+
std::cout << "[INSTANTIATE] " << error_buf << std::endl;
3640
wasm_runtime_unload(module);
41+
wasm_runtime_destroy();
42+
/* return SUCCESS because the failure has been handled */
43+
return 0;
3744
}
38-
/* destroy runtime environment */
39-
wasm_runtime_destroy();
4045

46+
std::cout << "PASS" << std::endl;
47+
48+
wasm_runtime_deinstantiate(inst);
49+
wasm_runtime_unload(module);
50+
wasm_runtime_destroy();
4151
return 0; /* Values other than 0 and -1 are reserved for future use. */
4252
}
4353

0 commit comments

Comments
 (0)