Skip to content

Commit 9d6999b

Browse files
authored
Fuzzer: Add a flag to avoid emitting invokes (#9212)
By default we emit invoker functions for things, basically new exports that call functions that might not be called otherwise. This gets more things to execute. However, in fuzzers like Fuzzilli, we don't want to add this extra size to things, so add a flag to control it.
1 parent 36a0c5e commit 9d6999b

6 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎src/tools/fuzzing.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class TranslateToFuzzReader {
133133
preserveImportsAndExports = preserveImportsAndExports_;
134134
}
135135
void setAgainstJS(bool againstJS_) { againstJS = againstJS_; }
136+
void setNoInvokes(bool noInvokes_) { noInvokes = noInvokes_; }
136137
void setImportedModule(std::string importedModuleName);
137138

138139
void build();
@@ -165,6 +166,9 @@ class TranslateToFuzzReader {
165166
// which might cause issues when linked against wasm or used otherwise.
166167
bool againstJS = false;
167168

169+
// Whether to avoid emitting invoker functions.
170+
bool noInvokes = false;
171+
168172
// An optional module to import from.
169173
std::optional<Module> importedModule;
170174

‎src/tools/fuzzing/fuzzing.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,6 +2758,10 @@ void TranslateToFuzzReader::dropToLog(Function* func) {
27582758
}
27592759

27602760
void TranslateToFuzzReader::addInvocations(Function* func) {
2761+
if (noInvokes) {
2762+
return;
2763+
}
2764+
27612765
Name name = func->name.toString() + std::string("_invoker");
27622766
if (wasm.getFunctionOrNull(name) || wasm.getExportOrNull(name)) {
27632767
return;

‎src/tools/wasm-opt.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ int main(int argc, const char* argv[]) {
8787
bool fuzzOOB = true;
8888
bool fuzzPreserveImportsAndExports = false;
8989
bool fuzzAgainstJS = false;
90+
bool fuzzNoInvokes = false;
9091
std::string fuzzImport;
9192
std::string emitSpecWrapper;
9293
std::string emitWasm2CWrapper;
@@ -219,6 +220,13 @@ For more on how to optimize effectively, see
219220
WasmOptOption,
220221
Options::Arguments::Zero,
221222
[&](Options* o, const std::string& arguments) { fuzzAgainstJS = true; })
223+
.add(
224+
"--fuzz-no-invokes",
225+
"",
226+
"do not emit invoker functions in the fuzzer",
227+
WasmOptOption,
228+
Options::Arguments::Zero,
229+
[&](Options* o, const std::string& arguments) { fuzzNoInvokes = true; })
222230
.add(
223231
"--fuzz-import",
224232
"",
@@ -358,6 +366,7 @@ For more on how to optimize effectively, see
358366
reader.setAllowOOB(fuzzOOB);
359367
reader.setPreserveImportsAndExports(fuzzPreserveImportsAndExports);
360368
reader.setAgainstJS(fuzzAgainstJS);
369+
reader.setNoInvokes(fuzzNoInvokes);
361370
if (!fuzzImport.empty()) {
362371
reader.setImportedModule(fuzzImport);
363372
}

‎test/lit/fuzz-no-invokes.wast‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
;; Test the flag to avoid emitting invoker functions in fuzzer generation.
2+
3+
;; Normally we emit invokers.
4+
;; RUN: wasm-opt %s.dat -all -ttf -S -o - | filecheck %s --check-prefix=NORMAL
5+
;; NORMAL: invoker
6+
7+
;; But not with the flag.
8+
;; RUN: wasm-opt %s.dat -all -ttf --fuzz-no-invokes -S -o - | filecheck %s --check-prefix=FLAG
9+
;; FLAG-NOT: invoker
10+

‎test/lit/fuzz-no-invokes.wast.dat‎

4 KB
Binary file not shown.

‎test/lit/help/wasm-opt.test‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
;; CHECK-NEXT: that assume it is used only from
7777
;; CHECK-NEXT: JS
7878
;; CHECK-EMPTY:
79+
;; CHECK-NEXT: --fuzz-no-invokes do not emit invoker functions in
80+
;; CHECK-NEXT: the fuzzer
81+
;; CHECK-EMPTY:
7982
;; CHECK-NEXT: --fuzz-import a module to use as an import in
8083
;; CHECK-NEXT: -ttf mode
8184
;; CHECK-EMPTY:

0 commit comments

Comments
 (0)