def run_wasm_merge_tests(): print '\n[ checking wasm-merge... ]\n' test_dir = os.path.join(options.binaryen_test, 'merge') for t in os.listdir(test_dir): if t.endswith(('.wast', '.wasm')): print '..', t t = os.path.join(test_dir, t) u = t + '.toMerge' for finalize in [0, 1]: for opt in [0, 1]: cmd = WASM_MERGE + [ t, u, '-o', 'a.wast', '-S', '--verbose' ] if finalize: cmd += [ '--finalize-memory-base=1024', '--finalize-table-base=8' ] if opt: cmd += ['-O'] stdout = run_command(cmd) actual = open('a.wast').read() out = t + '.combined' if finalize: out += '.finalized' if opt: out += '.opt' with open(out) as f: fail_if_not_identical(f.read(), actual) with open(out + '.stdout') as f: fail_if_not_identical(f.read(), stdout)
def run_ctor_eval_tests(): print '\n[ checking wasm-ctor-eval... ]\n' for t in os.listdir(os.path.join('test', 'ctor-eval')): if t.endswith(('.wast', '.wasm')): print '..', t t = os.path.join('test', 'ctor-eval', t) ctors = open(t + '.ctors').read().strip() cmd = WASM_CTOR_EVAL + [t, '-o', 'a.wast', '-S', '--ctors', ctors] stdout = run_command(cmd) actual = open('a.wast').read() out = t + '.out' with open(out) as f: fail_if_not_identical(f.read(), actual)
def run_wasm_reduce_tests(): print '\n[ checking wasm-reduce ]\n' for t in os.listdir(os.path.join('test', 'reduce')): if t.endswith('.wast'): print '..', t t = os.path.join('test', 'reduce', t) # convert to wasm run_command(WASM_AS + [t, '-o', 'a.wasm']) print run_command(WASM_REDUCE + ['a.wasm', '--command=%s b.wasm --fuzz-exec' % WASM_OPT[0], '-t', 'b.wasm', '-w', 'c.wasm']) expected = t + '.txt' run_command(WASM_DIS + ['c.wasm', '-o', 'a.wast']) with open('a.wast') as seen: with open(expected) as correct: fail_if_not_identical(seen.read(), correct.read())
def run_wasm_metadce_tests(): print '\n[ checking wasm-metadce ]\n' for t in os.listdir(os.path.join('test', 'metadce')): if t.endswith(('.wast', '.wasm')): print '..', t t = os.path.join('test', 'metadce', t) graph = t + '.graph.txt' cmd = WASM_METADCE + [t, '--graph-file=' + graph, '-o', 'a.wast', '-S'] stdout = run_command(cmd) expected = t + '.dced' with open('a.wast') as seen: with open(expected) as correct: fail_if_not_identical(seen.read(), correct.read()) with open(expected + '.stdout') as correct: fail_if_not_identical(stdout, correct.read())
def run_wasm_opt_tests(): print '\n[ checking wasm-opt -o notation... ]\n' for extra_args in [[], ['--no-validation']]: wast = os.path.join(options.binaryen_test, 'hello_world.wast') delete_from_orbit('a.wast') cmd = WASM_OPT + [wast, '-o', 'a.wast', '-S'] + extra_args run_command(cmd) fail_if_not_identical_to_file(open('a.wast').read(), wast) print '\n[ checking wasm-opt binary reading/writing... ]\n' shutil.copyfile(os.path.join(options.binaryen_test, 'hello_world.wast'), 'a.wast') delete_from_orbit('a.wasm') delete_from_orbit('b.wast') run_command(WASM_OPT + ['a.wast', '-o', 'a.wasm']) assert open('a.wasm', 'rb').read()[0] == '\0', 'we emit binary by default' run_command(WASM_OPT + ['a.wasm', '-o', 'b.wast', '-S']) assert open('b.wast', 'rb').read()[0] != '\0', 'we emit text with -S' print '\n[ checking wasm-opt passes... ]\n' for t in sorted(os.listdir(os.path.join(options.binaryen_test, 'passes'))): if t.endswith(('.wast', '.wasm')): print '..', t binary = '.wasm' in t base = os.path.basename(t).replace('.wast', '').replace('.wasm', '') passname = base if passname.isdigit(): passname = open( os.path.join(options.binaryen_test, 'passes', passname + '.passes')).read().strip() opts = [('--' + p if not p.startswith('O') else '-' + p) for p in passname.split('_')] t = os.path.join(options.binaryen_test, 'passes', t) actual = '' for module, asserts in split_wast(t): assert len(asserts) == 0 with open('split.wast', 'w') as o: o.write(module) cmd = WASM_OPT + opts + ['split.wast', '--print'] curr = run_command(cmd) actual += curr # also check debug mode output is valid debugged = run_command(cmd + ['--debug'], stderr=subprocess.PIPE) fail_if_not_contained(actual, debugged) # also check pass-debug mode def check(): pass_debug = run_command(cmd) fail_if_not_identical(curr, pass_debug) with_pass_debug(check) expected_file = os.path.join( options.binaryen_test, 'passes', base + ('.bin' if binary else '') + '.txt') fail_if_not_identical_to_file(actual, expected_file) if 'emit-js-wrapper' in t: with open('a.js') as actual: fail_if_not_identical_to_file(actual.read(), t + '.js') if 'emit-spec-wrapper' in t: with open('a.wat') as actual: fail_if_not_identical_to_file(actual.read(), t + '.wat') print '\n[ checking wasm-opt parsing & printing... ]\n' for t in sorted(os.listdir(os.path.join(options.binaryen_test, 'print'))): if t.endswith('.wast'): print '..', t wasm = os.path.basename(t).replace('.wast', '') cmd = WASM_OPT + [ os.path.join(options.binaryen_test, 'print', t), '--print' ] print ' ', ' '.join(cmd) actual, err = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() expected_file = os.path.join(options.binaryen_test, 'print', wasm + '.txt') fail_if_not_identical_to_file(actual, expected_file) cmd = WASM_OPT + [ os.path.join(options.binaryen_test, 'print', t), '--print-minified' ] print ' ', ' '.join(cmd) actual, err = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() fail_if_not_identical( actual.strip(), open( os.path.join(options.binaryen_test, 'print', wasm + '.minified.txt')).read().strip()) print '\n[ checking wasm-opt testcases... ]\n' for t in tests: if t.endswith('.wast') and not t.startswith('spec'): print '..', t t = os.path.join(options.binaryen_test, t) f = t + '.from-wast' cmd = WASM_OPT + [t, '--print'] actual = run_command(cmd) actual = actual.replace('printing before:\n', '') fail_if_not_identical_to_file(actual, f) binary_format_check(t, wasm_as_args=['-g']) # test with debuginfo binary_format_check(t, wasm_as_args=[], binary_suffix='.fromBinary.noDebugInfo' ) # test without debuginfo minify_check(t)
def check(): pass_debug = run_command(cmd) fail_if_not_identical(curr, pass_debug)
def run_wasm_opt_tests(): print('\n[ checking wasm-opt -o notation... ]\n') for extra_args in [[], ['--no-validation']]: wast = os.path.join(shared.options.binaryen_test, 'hello_world.wat') shared.delete_from_orbit('a.wat') out = 'a.wat' cmd = shared.WASM_OPT + [wast, '-o', out, '-S'] + extra_args support.run_command(cmd) shared.fail_if_not_identical_to_file(open(out).read(), wast) print('\n[ checking wasm-opt binary reading/writing... ]\n') shutil.copyfile( os.path.join(shared.options.binaryen_test, 'hello_world.wat'), 'a.wat') shared.delete_from_orbit('a.wasm') shared.delete_from_orbit('b.wast') support.run_command(shared.WASM_OPT + ['a.wat', '-o', 'a.wasm']) assert open('a.wasm', 'rb').read()[0] == 0, 'we emit binary by default' support.run_command(shared.WASM_OPT + ['a.wasm', '-o', 'b.wast', '-S']) assert open('b.wast', 'rb').read()[0] != 0, 'we emit text with -S' print('\n[ checking wasm-opt passes... ]\n') for t in shared.get_tests(shared.get_test_dir('passes'), ['.wast', '.wasm']): print('..', os.path.basename(t)) binary = '.wasm' in t base = os.path.basename(t).replace('.wast', '').replace('.wasm', '') passname = base passes_file = os.path.join(shared.get_test_dir('passes'), passname + '.passes') if os.path.exists(passes_file): passname = open(passes_file).read().strip() opts = [('--' + p if not p.startswith('O') and p != 'g' else '-' + p) for p in passname.split('_')] actual = '' for module, asserts in support.split_wast(t): assert len(asserts) == 0 support.write_wast('split.wast', module) cmd = shared.WASM_OPT + opts + ['split.wast', '--print'] curr = support.run_command(cmd) actual += curr # also check debug mode output is valid debugged = support.run_command(cmd + ['--debug'], stderr=subprocess.PIPE) shared.fail_if_not_contained(actual, debugged) # also check pass-debug mode def check(): pass_debug = support.run_command(cmd) shared.fail_if_not_identical(curr, pass_debug) shared.with_pass_debug(check) expected_file = os.path.join( shared.get_test_dir('passes'), base + ('.bin' if binary else '') + '.txt') shared.fail_if_not_identical_to_file(actual, expected_file) if 'emit-js-wrapper' in t: with open('a.js') as actual: shared.fail_if_not_identical_to_file(actual.read(), t + '.js') if 'emit-spec-wrapper' in t: with open('a.wat') as actual: shared.fail_if_not_identical_to_file(actual.read(), t + '.wat') print('\n[ checking wasm-opt parsing & printing... ]\n') for t in shared.get_tests(shared.get_test_dir('print'), ['.wast']): print('..', os.path.basename(t)) wasm = os.path.basename(t).replace('.wast', '') cmd = shared.WASM_OPT + [t, '--print', '-all'] print(' ', ' '.join(cmd)) actual, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate() expected_file = os.path.join(shared.get_test_dir('print'), wasm + '.txt') shared.fail_if_not_identical_to_file(actual, expected_file) cmd = shared.WASM_OPT + [ os.path.join(shared.get_test_dir('print'), t), '--print-minified', '-all' ] print(' ', ' '.join(cmd)) actual, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate() shared.fail_if_not_identical( actual.strip(), open( os.path.join(shared.get_test_dir('print'), wasm + '.minified.txt')).read().strip()) print('\n[ checking wasm-opt testcases... ]\n') for t in shared.get_tests(shared.options.binaryen_test, ['.wast']): print('..', os.path.basename(t)) f = t + '.from-wast' cmd = shared.WASM_OPT + [t, '--print', '-all'] actual = support.run_command(cmd) actual = actual.replace('printing before:\n', '') shared.fail_if_not_identical_to_file(actual, f) # FIXME Remove this condition after nullref is implemented in V8 if 'reference-types.wast' not in t: shared.binary_format_check(t, wasm_as_args=['-g' ]) # test with debuginfo shared.binary_format_check(t, wasm_as_args=[], binary_suffix='.fromBinary.noDebugInfo' ) # test without debuginfo shared.minify_check(t) print('\n[ checking wasm-opt debugInfo read-write... ]\n') for t in shared.get_tests(shared.options.binaryen_test, ['.fromasm']): if 'debugInfo' not in t: continue print('..', os.path.basename(t)) f = t + '.read-written' support.run_command(shared.WASM_AS + [t, '--source-map=a.map', '-o', 'a.wasm', '-g']) support.run_command(shared.WASM_OPT + [ 'a.wasm', '--input-source-map=a.map', '-o', 'b.wasm', '--output-source-map=b.map', '-g' ]) actual = support.run_command(shared.WASM_DIS + ['b.wasm', '--source-map=b.map']) shared.fail_if_not_identical_to_file(actual, f)
def check(): pass_debug = support.run_command(cmd) shared.fail_if_not_identical(curr, pass_debug)
[os.path.join(options.binaryen_bin, e), '--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() assert len(out) == 0, 'Expected no stdout, got:\n%s' % out assert e.replace( '.exe', '') in err, 'Expected help to contain program name, got:\n%s' % err assert len(err.split('\n')) > 8, 'Expected some help, got:\n%s' % err print '\n[ checking wasm-opt -o notation... ]\n' wast = os.path.join(options.binaryen_test, 'hello_world.wast') delete_from_orbit('a.wast') cmd = WASM_OPT + [wast, '-o', 'a.wast', '-S'] run_command(cmd) fail_if_not_identical(open('a.wast').read(), open(wast).read()) print '\n[ checking wasm-opt binary reading/writing... ]\n' shutil.copyfile(os.path.join(options.binaryen_test, 'hello_world.wast'), 'a.wast') delete_from_orbit('a.wasm') delete_from_orbit('b.wast') run_command(WASM_OPT + ['a.wast', '-o', 'a.wasm']) assert open('a.wasm', 'rb').read()[0] == '\0', 'we emit binary by default' run_command(WASM_OPT + ['a.wasm', '-o', 'b.wast', '-S']) assert open('b.wast', 'rb').read()[0] != '\0', 'we emit text with -S' print '\n[ checking wasm-opt passes... ]\n' for t in sorted(os.listdir(os.path.join(options.binaryen_test, 'passes'))):
def run_asm2wasm_tests(): print '[ checking asm2wasm testcases... ]\n' for asm in tests: if asm.endswith('.asm.js'): for precise in [0, 1, 2]: for opts in [1, 0]: cmd = ASM2WASM + [os.path.join(options.binaryen_test, asm)] wasm = asm.replace('.asm.js', '.fromasm') if not precise: cmd += [ '--emit-potential-traps', '--ignore-implicit-traps' ] wasm += '.imprecise' elif precise == 2: cmd += ['--emit-clamped-potential-traps'] wasm += '.clamp' if not opts: wasm += '.no-opts' if precise: cmd += ['-O0'] # test that -O0 does nothing else: cmd += ['-O'] if 'debugInfo' in asm: cmd += ['-g'] if 'noffi' in asm: cmd += ['--no-legalize-javascript-ffi'] if precise and opts: # test mem init importing open('a.mem', 'wb').write(asm) cmd += ['--mem-init=a.mem'] if asm[0] == 'e': cmd += ['--mem-base=1024'] if 'i64' in asm or 'wasm-only' in asm or 'noffi' in asm: cmd += ['--wasm-only'] wasm = os.path.join(options.binaryen_test, wasm) print '..', asm, wasm def do_asm2wasm_test(): actual = run_command(cmd) # verify output if not os.path.exists(wasm): fail_with_error( 'output .wast file %s does not exist' % wasm) expected = open(wasm, 'rb').read() if actual != expected: fail(actual, expected) binary_format_check(wasm, verify_final_result=False) # test both normally and with pass debug (so each inter-pass state is validated) old_pass_debug = os.environ.get('BINARYEN_PASS_DEBUG') try: os.environ['BINARYEN_PASS_DEBUG'] = '1' do_asm2wasm_test() del os.environ['BINARYEN_PASS_DEBUG'] do_asm2wasm_test() finally: if old_pass_debug is not None: os.environ['BINARYEN_PASS_DEBUG'] = old_pass_debug else: if 'BINARYEN_PASS_DEBUG' in os.environ: del os.environ['BINARYEN_PASS_DEBUG'] # verify in wasm if options.interpreter: # remove imports, spec interpreter doesn't know what to do with them subprocess.check_call(WASM_OPT + ['--remove-imports', wasm], stdout=open('ztemp.wast', 'w'), stderr=subprocess.PIPE) proc = subprocess.Popen( [options.interpreter, 'ztemp.wast'], stderr=subprocess.PIPE) out, err = proc.communicate() if proc.returncode != 0: try: # to parse the error reported = err.split(':')[1] start, end = reported.split('-') start_line, start_col = map( int, start.split('.')) lines = open('ztemp.wast').read().split('\n') print print '=' * 80 print lines[start_line - 1] print(' ' * (start_col - 1)) + '^' print(' ' * (start_col - 2)) + '/_\\' print '=' * 80 print err except Exception, e: fail_with_error('wasm interpreter error: ' + err) # failed to pretty-print fail_with_error('wasm interpreter error') # verify debug info if 'debugInfo' in asm: jsmap = 'a.wasm.map' cmd += [ '--source-map', jsmap, '--source-map-url', 'http://example.org/' + jsmap, '-o', 'a.wasm' ] run_command(cmd) if not os.path.isfile(jsmap): fail_with_error('Debug info map not created: %s' % jsmap) with open(wasm + '.map', 'rb') as expected: with open(jsmap, 'rb') as actual: fail_if_not_identical(actual.read(), expected.read()) with open('a.wasm', 'rb') as binary: url_section_name = bytearray( [16]) + bytearray('sourceMappingURL') payload = 'http://example.org/' + jsmap assert len(payload) < 256, 'name too long' url_section_contents = bytearray( [len(payload)]) + bytearray(payload) print url_section_name binary_contents = bytearray(binary.read()) if url_section_name not in binary_contents: fail_with_error( 'source map url section not found in binary' ) if url_section_contents not in binary_contents[ binary_contents.index(url_section_name):]: fail_with_error( 'source map url not found in url section')
def test_wasm2js_output(): for opt in (0, 1): for t in tests + spec_tests + wasm2js_tests: basename = os.path.basename(t) if basename in wasm2js_blacklist: continue asm = basename.replace('.wast', '.2asm.js') expected_file = os.path.join(shared.get_test_dir('wasm2js'), asm) if opt: expected_file += '.opt' if not os.path.exists(expected_file): continue print('..', os.path.basename(t)) all_out = [] for module, asserts in support.split_wast(t): support.write_wast('split.wast', module, asserts) cmd = shared.WASM2JS + ['split.wast', '-all'] if opt: cmd += ['-O'] if 'emscripten' in t: cmd += ['--emscripten'] out = support.run_command(cmd) all_out.append(out) if not shared.NODEJS and not shared.MOZJS: print('No JS interpreters. Skipping spec tests.') continue open('a.2asm.mjs', 'w').write(out) cmd += ['--allow-asserts'] out = support.run_command(cmd) # also verify it passes pass-debug verifications shared.with_pass_debug(lambda: support.run_command(cmd)) open('a.2asm.asserts.mjs', 'w').write(out) # verify asm.js is valid js, note that we're using --experimental-modules # to enable ESM syntax and we're also passing a custom loader to handle the # `spectest` and `env` modules in our tests. if shared.NODEJS: loader = os.path.join(shared.options.binaryen_root, 'scripts', 'test', 'node-esm-loader.mjs') node = [ shared.NODEJS, '--experimental-modules', '--loader', loader ] cmd = node[:] cmd.append('a.2asm.mjs') out = support.run_command(cmd) shared.fail_if_not_identical(out, '') cmd = node[:] cmd.append('a.2asm.asserts.mjs') out = support.run_command(cmd, expected_err='', err_ignore='ExperimentalWarning') shared.fail_if_not_identical(out, '') shared.fail_if_not_identical_to_file(''.join(all_out), expected_file)
for e in executables: print '.. %s --help' % e out, err = subprocess.Popen([os.path.join(options.binaryen_bin, e), '--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() assert len(out) == 0, 'Expected no stdout, got:\n%s' % out assert e.replace('.exe', '') in err, 'Expected help to contain program name, got:\n%s' % err assert len(err.split('\n')) > 8, 'Expected some help, got:\n%s' % err print '\n[ checking wasm-opt -o notation... ]\n' wast = os.path.join(options.binaryen_test, 'hello_world.wast') delete_from_orbit('a.wast') cmd = WASM_OPT + [wast, '-o', 'a.wast'] run_command(cmd) fail_if_not_identical(open('a.wast').read(), open(wast).read()) print '\n[ checking wasm-opt passes... ]\n' for t in sorted(os.listdir(os.path.join(options.binaryen_test, 'passes'))): if t.endswith('.wast'): print '..', t passname = os.path.basename(t).replace('.wast', '') opts = ['-' + passname] if passname.startswith('O') else ['--' + p for p in passname.split('_')] t = os.path.join(options.binaryen_test, 'passes', t) actual = '' for module, asserts in split_wast(t): assert len(asserts) == 0 with open('split.wast', 'w') as o: o.write(module) cmd = WASM_OPT + opts + ['split.wast', '--print'] actual += run_command(cmd)