Skip to content

Commit 256187c

Browse files
authored
Use v8 to test wasm binaries are valid in test suite binary checks (#2206)
1 parent f82e708 commit 256187c

File tree

7 files changed

+74
-362
lines changed

7 files changed

+74
-362
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
# get jsvu in order to get more js engines
5656
- npm install jsvu -g
5757
- export PATH="${HOME}/.jsvu:${PATH}"
58-
- jsvu --os=linux64 --engines=spidermonkey
58+
- jsvu --os=linux64 --engines=spidermonkey,v8
5959
script:
6060
- set -o errexit
6161
- flake8

check.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
binary_format_check, delete_from_orbit, fail, fail_with_error,
2828
fail_if_not_identical, fail_if_not_contained, has_vanilla_emcc,
2929
has_vanilla_llvm, minify_check, options, tests, requested, warnings,
30-
has_shell_timeout, fail_if_not_identical_to_file, with_pass_debug
30+
has_shell_timeout, fail_if_not_identical_to_file, with_pass_debug,
31+
validate_binary
3132
)
3233

3334
# For shared.num_failures. Cannot import directly because modifications made in
@@ -189,6 +190,8 @@ def check():
189190

190191
with_pass_debug(check)
191192

193+
validate_binary(t)
194+
192195

193196
def run_crash_tests():
194197
print "\n[ checking we don't crash on tricky inputs... ]\n"
@@ -380,7 +383,7 @@ def fix_actual(x):
380383
o.write(module + '\n' + '\n'.join(asserts))
381384
run_spec_test('split.wast') # before binary stuff - just check it's still ok split out
382385
run_opt_test('split.wast') # also that our optimizer doesn't break on it
383-
result_wast = binary_format_check('split.wast', verify_final_result=False)
386+
result_wast = binary_format_check('split.wast', verify_final_result=False, original_wast=wast)
384387
# add the asserts, and verify that the test still passes
385388
open(result_wast, 'a').write('\n' + '\n'.join(asserts))
386389
actual += run_spec_test(result_wast)

scripts/fuzz_opt.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import shutil
2222
import time
2323

24-
from test.shared import options, NODEJS
24+
from test.shared import options, NODEJS, V8_OPTS
2525

2626

2727
# parameters
@@ -32,18 +32,6 @@
3232

3333
FUZZ_OPTS = []
3434

35-
V8_OPTS = [
36-
'--experimental-wasm-eh',
37-
'--experimental-wasm-mv',
38-
'--experimental-wasm-sat-f2i-conversions',
39-
'--experimental-wasm-se',
40-
'--experimental-wasm-threads',
41-
'--experimental-wasm-simd',
42-
'--experimental-wasm-anyref',
43-
'--experimental-wasm-bulk-memory',
44-
'--experimental-wasm-return-call'
45-
]
46-
4735
INPUT_SIZE_LIMIT = 150 * 1024
4836

4937
LOG_LIMIT = 125

scripts/test/shared.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def is_exe(fpath):
157157
which('g++') or which('clang++'))
158158
NODEJS = os.getenv('NODE', which('nodejs') or which('node'))
159159
MOZJS = which('mozjs') or which('spidermonkey')
160+
V8 = which('v8') or which('d8')
160161
EMCC = which('emcc')
161162

162163
BINARYEN_INSTALL_DIR = os.path.dirname(options.binaryen_bin)
@@ -190,7 +191,13 @@ def wrap_with_valgrind(cmd):
190191
ASM2WASM = wrap_with_valgrind(ASM2WASM)
191192
WASM_SHELL = wrap_with_valgrind(WASM_SHELL)
192193

193-
os.environ['BINARYEN'] = os.getcwd()
194+
195+
def in_binaryen(*args):
196+
__rootpath__ = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
197+
return os.path.join(__rootpath__, *args)
198+
199+
200+
os.environ['BINARYEN'] = in_binaryen()
194201

195202

196203
def get_platform():
@@ -204,6 +211,19 @@ def has_shell_timeout():
204211
return get_platform() != 'windows' and os.system('timeout 1s pwd') == 0
205212

206213

214+
# Default options to pass to v8. These enable all features.
215+
V8_OPTS = [
216+
'--experimental-wasm-eh',
217+
'--experimental-wasm-mv',
218+
'--experimental-wasm-sat-f2i-conversions',
219+
'--experimental-wasm-se',
220+
'--experimental-wasm-threads',
221+
'--experimental-wasm-simd',
222+
'--experimental-wasm-anyref',
223+
'--experimental-wasm-bulk-memory',
224+
'--experimental-wasm-return-call'
225+
]
226+
207227
has_vanilla_llvm = False
208228

209229
# external tools
@@ -377,8 +397,18 @@ def fail_if_not_identical_to_file(actual, expected_file):
377397

378398
# check utilities
379399

400+
401+
def validate_binary(wasm):
402+
if V8:
403+
cmd = [V8] + V8_OPTS + [in_binaryen('scripts', 'validation_shell.js'), '--', wasm]
404+
print ' ', ' '.join(cmd)
405+
subprocess.check_call(cmd, stdout=subprocess.PIPE)
406+
else:
407+
print '(skipping v8 binary validation)'
408+
409+
380410
def binary_format_check(wast, verify_final_result=True, wasm_as_args=['-g'],
381-
binary_suffix='.fromBinary'):
411+
binary_suffix='.fromBinary', original_wast=None):
382412
# checks we can convert the wast to binary and back
383413

384414
print ' (binary format check)'
@@ -389,6 +419,13 @@ def binary_format_check(wast, verify_final_result=True, wasm_as_args=['-g'],
389419
subprocess.check_call(cmd, stdout=subprocess.PIPE)
390420
assert os.path.exists('a.wasm')
391421

422+
# make sure it is a valid wasm, using a real wasm VM
423+
if os.path.basename(original_wast or wast) not in [
424+
'atomics.wast', # https://bugs.chromium.org/p/v8/issues/detail?id=9425
425+
'simd.wast', # https://bugs.chromium.org/p/v8/issues/detail?id=8460
426+
]:
427+
validate_binary('a.wasm')
428+
392429
cmd = WASM_DIS + ['a.wasm', '-o', 'ab.wast']
393430
print ' ', ' '.join(cmd)
394431
if os.path.exists('ab.wast'):

scripts/validation_shell.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Test a file is valid, by just loading it.
2+
3+
// Shell integration.
4+
if (typeof console === 'undefined') {
5+
console = { log: print };
6+
}
7+
var binary;
8+
if (typeof process === 'object' && typeof require === 'function' /* node.js detection */) {
9+
var args = process.argv.slice(2);
10+
binary = require('fs').readFileSync(args[0]);
11+
if (!binary.buffer) binary = new Uint8Array(binary);
12+
} else {
13+
var args;
14+
if (typeof scriptArgs != 'undefined') {
15+
args = scriptArgs;
16+
} else if (typeof arguments != 'undefined') {
17+
args = arguments;
18+
}
19+
if (typeof readbuffer === 'function') {
20+
binary = new Uint8Array(readbuffer(args[0]));
21+
} else {
22+
binary = read(args[0], 'binary');
23+
}
24+
}
25+
26+
// Test the wasm for validity by compiling it.
27+
new WebAssembly.Module(binary);
28+

test/badvartype.wasm

-698 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)