Skip to content

Commit b6eacd7

Browse files
authored
Fuzzer: Fix wasm2js trap handling (#7206)
wasm2js fuzzing must stop looking at code after a trap (since we trap differently than wasm semantics, e.g. no trap on load/store out of bounds). We must look at the fixed-up names of exports to properly find the place to stop. To do so, just move the trap-handling code to after we run fix_output_for_js().
1 parent 444682c commit b6eacd7

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

scripts/fuzz_opt.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,24 +1048,6 @@ def handle_pair(self, input, before_wasm, after_wasm, opts):
10481048
# with NaNs we can't compare the output, as a reinterpret through
10491049
# memory might end up different in JS than wasm
10501050
return
1051-
# we also cannot compare if the wasm hits a trap, as wasm2js does not
1052-
# trap on many things wasm would, and in those cases it can do weird
1053-
# undefined things. in such a case, at least compare up until before
1054-
# the trap, which lets us compare at least some results in some cases.
1055-
# (this is why wasm2js is not in CompareVMs, which does full
1056-
# comparisons - we need to limit the comparison in a special way here)
1057-
interpreter = run_bynterp(before_wasm_temp, ['--fuzz-exec-before'])
1058-
if TRAP_PREFIX in interpreter:
1059-
trap_index = interpreter.index(TRAP_PREFIX)
1060-
# we can't test this function, which the trap is in the middle of.
1061-
# erase everything from this function's output and onward, so we
1062-
# only compare the previous trap-free code
1063-
call_start = interpreter.rindex(FUZZ_EXEC_CALL_PREFIX, 0, trap_index)
1064-
call_end = interpreter.index('\n', call_start)
1065-
call_line = interpreter[call_start:call_end]
1066-
before = before[:before.index(call_line)]
1067-
after = after[:after.index(call_line)]
1068-
interpreter = interpreter[:interpreter.index(call_line)]
10691051

10701052
def fix_output_for_js(x):
10711053
# start with the normal output fixes that all VMs need
@@ -1117,6 +1099,28 @@ def fix_number(x):
11171099

11181100
before = fix_output_for_js(before)
11191101
after = fix_output_for_js(after)
1102+
1103+
# we must not compare if the wasm hits a trap, as wasm2js does not
1104+
# trap on many things wasm would, and in those cases it can do weird
1105+
# undefined things. in such a case, at least compare up until before
1106+
# the trap, which lets us compare at least some results in some cases.
1107+
# (this is why wasm2js is not in CompareVMs, which does full
1108+
# comparisons - we need to limit the comparison in a special way here)
1109+
interpreter = run_bynterp(before_wasm_temp, ['--fuzz-exec-before'])
1110+
if TRAP_PREFIX in interpreter:
1111+
trap_index = interpreter.index(TRAP_PREFIX)
1112+
# we can't test this function, which the trap is in the middle of.
1113+
# erase everything from this function's output and onward, so we
1114+
# only compare the previous trap-free code
1115+
call_start = interpreter.rindex(FUZZ_EXEC_CALL_PREFIX, 0, trap_index)
1116+
call_end = interpreter.index('\n', call_start)
1117+
call_line = interpreter[call_start:call_end]
1118+
# fix up the call line so it matches the JS
1119+
fixed_call_line = fix_output_for_js(call_line)
1120+
before = before[:before.index(fixed_call_line)]
1121+
after = after[:after.index(fixed_call_line)]
1122+
interpreter = interpreter[:interpreter.index(call_line)]
1123+
11201124
if compare_before_to_after:
11211125
compare_between_vms(before, after, 'Wasm2JS (before/after)')
11221126
if compare_to_interpreter:

0 commit comments

Comments
 (0)