Skip to content

Commit bc411d9

Browse files
committed
Custom mutator
1 parent 86c8f15 commit bc411d9

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

test/fuzzer/parser_fuzzer.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,28 @@ bool wabt_parse(const uint8_t* data, size_t data_size) noexcept
9696

9797
} // namespace
9898

99-
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t data_size) noexcept
99+
extern "C" {
100+
101+
size_t LLVMFuzzerMutate(uint8_t* data, size_t size, size_t max_size) noexcept;
102+
103+
size_t LLVMFuzzerCustomMutator(
104+
uint8_t* data, size_t size, size_t max_size, [[maybe_unused]] unsigned int seed) noexcept
105+
{
106+
static constexpr uint8_t wasm_prefix[]{0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00};
107+
static constexpr auto wasm_prefix_size = sizeof(wasm_prefix);
108+
109+
// For inputs shorter than wasm prefix just mutate it.
110+
if (size <= wasm_prefix_size)
111+
return LLVMFuzzerMutate(data, size, max_size);
112+
113+
// For other, leave prefix unchanged. It is likely to be valid and we don't want to waste time
114+
// on mutating the prefix.
115+
const auto new_size_without_prefix = LLVMFuzzerMutate(
116+
data + wasm_prefix_size, size - wasm_prefix_size, max_size - wasm_prefix_size);
117+
return new_size_without_prefix + wasm_prefix_size;
118+
}
119+
120+
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t data_size) noexcept
100121
{
101122
const auto expected = wabt_parse(data, data_size);
102123

@@ -148,3 +169,4 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t data_size) noe
148169

149170
return 0;
150171
}
172+
}

0 commit comments

Comments
 (0)