Skip to content

Commit d5e7f18

Browse files
authored
[Fuzzing] Add an env var to avoid modifying the given wasm file in fuzz_opt.py (#7703)
Normally we want to fix it up (remove NaNs etc., as we do normally), but the changes in each fuzzing iteration can make things hard to debug. Add an env var to simply trust the given wasm file and use it without changes.
1 parent 9c5a5ad commit d5e7f18

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

scripts/fuzz_opt.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,15 +1874,21 @@ def test_one(random_input, given_wasm):
18741874
print()
18751875

18761876
if given_wasm:
1877-
# if given a wasm file we want to use it as is, but we also want to
1878-
# apply properties like not having any NaNs, which the original fuzz
1879-
# wasm had applied. that is, we need to preserve properties like not
1880-
# having nans through reduction.
1881-
try:
1882-
run([in_bin('wasm-opt'), given_wasm, '-o', abspath('a.wasm')] + GEN_ARGS + FEATURE_OPTS)
1883-
except Exception as e:
1884-
print("Internal error in fuzzer! Could not run given wasm")
1885-
raise e
1877+
# We are given a wasm file to operate on. By default we modify it in the
1878+
# usual ways, like running DeNAN on it, which is important in many cases
1879+
# (imagine the reducer generates a NAN, then we need to restore the
1880+
# property of not having any). However, in some cases we do need to
1881+
# trust the wasm is correct, or it is simpler to debug things without
1882+
# constant changes in each reduction cycle, so we have an env var to
1883+
# control that, BINARYEN_TRUST_GIVEN_WASM.
1884+
if os.environ.get('BINARYEN_TRUST_GIVEN_WASM'):
1885+
shutil.copyfile(given_wasm, abspath('a.wasm'))
1886+
else:
1887+
try:
1888+
run([in_bin('wasm-opt'), given_wasm, '-o', abspath('a.wasm')] + GEN_ARGS + FEATURE_OPTS)
1889+
except Exception as e:
1890+
print("Internal error in fuzzer! Could not run given wasm")
1891+
raise e
18861892
else:
18871893
# emit the target features section so that reduction can work later,
18881894
# without needing to specify the features
@@ -2351,7 +2357,9 @@ def get_random_opts():
23512357
(If it does not, then one possible issue is that the fuzzer fails to write a
23522358
valid binary. If so, you can print the output of the fuzzer's first command
23532359
(using -ttf / --translate-to-fuzz) in text form and run the reduction from that,
2354-
passing --text to the reducer.)
2360+
passing --text to the reducer. Another possible fix is to avoid re-processing
2361+
the wasm for fuzzing in each iteration, by adding
2362+
BINARYEN_TRUST_GIVEN_WASM=1 in the env.)
23552363
23562364
You can also read "%(reduce_sh)s" which has been filled out for you and includes
23572365
docs and suggestions.

0 commit comments

Comments
 (0)