Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/tools/fuzzing.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ class TranslateToFuzzReader {
// The name of an empty tag.
Name trivialTag;

// Whether we were given initial functions.
bool haveInitialFunctions;

// RAII helper for managing the state used to create a single function.
struct FunctionCreationContext {
TranslateToFuzzReader& parent;
Expand Down
16 changes: 15 additions & 1 deletion src/tools/fuzzing/fuzzing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ TranslateToFuzzReader::TranslateToFuzzReader(Module& wasm,
random(std::move(input), wasm.features),
publicTypeValidator(wasm.features) {

haveInitialFunctions = !wasm.functions.empty();

// - funcref cannot be logged because referenced functions can be inlined or
// removed during optimization
// - there's no point in logging anyref because it is opaque
Expand Down Expand Up @@ -1391,6 +1393,14 @@ void TranslateToFuzzReader::processFunctions() {
const int RESOLUTION = 10;
auto chance = upTo(RESOLUTION + 1);

// We do not want to always add new functions, if there are initial ones:
// adding many additional functions will cause a lot of global properties to
// change, e.g., if the initial content was a carefully crafted testcase
// showing some situation of reads and writes between struct fields, adding
// many new functions will likely add reads and writes to all the fields,
// preventing global operations like field removal or immutabilification.
auto allowNew = !haveInitialFunctions || !oneIn(10);

// Keep working while we have random data.
while (!random.finished()) {
if (!moddable.empty() && upTo(RESOLUTION) < chance) {
Expand All @@ -1403,7 +1413,7 @@ void TranslateToFuzzReader::processFunctions() {
// place, and truncating.
moddable[index] = moddable.back();
moddable.pop_back();
} else {
} else if (allowNew) {
// Add a new function
auto* func = addFunction();
addInvocations(func);
Expand All @@ -1413,6 +1423,10 @@ void TranslateToFuzzReader::processFunctions() {
if (allowOOB) {
moddable.push_back(func);
}
} else {
// If we found nothing to do, consume some data so that we make progress
// towards the loop ending.
get();
}
}

Expand Down
Loading