def test_asm2wasm_binary(): print('\n[ checking asm2wasm binary reading/writing... ]\n') asmjs = os.path.join(shared.options.binaryen_test, 'hello_world.asm.js') shared.delete_from_orbit('a.wasm') shared.delete_from_orbit('b.wast') support.run_command(shared.ASM2WASM + [asmjs, '-o', 'a.wasm']) assert open('a.wasm', 'rb').read()[0] == 0, 'we emit binary by default' support.run_command(shared.ASM2WASM + [asmjs, '-o', 'b.wast', '-S']) assert open('b.wast', 'rb').read()[0] != 0, 'we emit text with -S'
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 run_emscripten_tests(): print '\n[ checking wasm.js methods... ]\n' for method_init in [ 'interpret-asm2wasm', 'interpret-s-expr', 'asmjs', 'interpret-binary', 'asmjs,interpret-binary', 'interpret-binary,asmjs' ]: # check success and failure for simple modes, only success for combined/fallback ones for success in [1, 0] if ',' not in method_init else [1]: method = method_init command = [ EMCC, '-o', 'a.wasm.js', '-s', 'BINARYEN=1', os.path.join(options.binaryen_test, 'hello_world.c') ] command += ['-s', 'BINARYEN_METHOD="' + method + '"'] print method, ' : ', ' '.join(command), ' => ', success subprocess.check_call(command) see_polyfill = 'var WasmJS = ' in open('a.wasm.js').read() if method and 'interpret' not in method: assert not see_polyfill, 'verify polyfill was not added - we specified a method, and it does not need it' else: assert see_polyfill, 'we need the polyfill' def break_cashew(): asm = open('a.wasm.asm.js').read() asm = asm.replace( '"almost asm"', '"use asm"; var not_in_asm = [].length + (true || { x: 5 }.x);' ) asm = asm.replace( "'almost asm'", '"use asm"; var not_in_asm = [].length + (true || { x: 5 }.x);' ) with open('a.wasm.asm.js', 'w') as o: o.write(asm) if method.startswith('interpret-asm2wasm'): delete_from_orbit( 'a.wasm.wast') # we should not need the .wast if not success: break_cashew() # we need cashew elif method.startswith('interpret-s-expr'): delete_from_orbit( 'a.wasm.asm.js') # we should not need the .asm.js if not success: delete_from_orbit('a.wasm.wast') elif method.startswith('asmjs'): delete_from_orbit( 'a.wasm.wast') # we should not need the .wast break_cashew() # we don't use cashew, so ok to break it if not success: delete_from_orbit('a.wasm.js') elif method.startswith('interpret-binary'): delete_from_orbit( 'a.wasm.wast') # we should not need the .wast delete_from_orbit( 'a.wasm.asm.js') # we should not need the .asm.js if not success: delete_from_orbit('a.wasm.wasm') else: 1 / 0 if NODEJS: proc = subprocess.Popen([NODEJS, 'a.wasm.js'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate() if success: assert proc.returncode == 0, err assert 'hello, world!' in out, out else: assert proc.returncode != 0, err assert 'hello, world!' not in out, out
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)
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', '-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'
def run_emscripten_tests(): if MOZJS and 0: print '\n[ checking native wasm support ]\n' command = [ EMCC, '-o', 'a.wasm.js', '-s', 'BINARYEN=1', os.path.join(options.binaryen_test, 'hello_world.c'), '-s', 'BINARYEN_METHOD="native-wasm"', '-s', 'BINARYEN_SCRIPTS="spidermonkify.py"' ] print ' '.join(command) subprocess.check_call(command) cmd = [MOZJS, 'a.wasm.js'] out = run_command(cmd) assert 'hello, world!' in out, out proc = subprocess.Popen([NODEJS, 'a.wasm.js'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate() assert proc.returncode != 0, 'should fail on no wasm support' assert 'no native wasm support detected' in err, err print '\n[ checking wasm.js methods... ]\n' for method_init in [ 'interpret-asm2wasm', 'interpret-s-expr', 'asmjs', 'interpret-binary', 'asmjs,interpret-binary', 'interpret-binary,asmjs' ]: # check success and failure for simple modes, only success for combined/fallback ones for success in [1, 0] if ',' not in method_init else [1]: method = method_init command = [ EMCC, '-o', 'a.wasm.js', '-s', 'BINARYEN=1', os.path.join(options.binaryen_test, 'hello_world.c') ] command += ['-s', 'BINARYEN_METHOD="' + method + '"'] print method, ' : ', ' '.join(command), ' => ', success subprocess.check_call(command) see_polyfill = 'var WasmJS = ' in open('a.wasm.js').read() if method and 'interpret' not in method: assert not see_polyfill, 'verify polyfill was not added - we specified a method, and it does not need it' else: assert see_polyfill, 'we need the polyfill' def break_cashew(): asm = open('a.wasm.asm.js').read() asm = asm.replace( '"almost asm"', '"use asm"; var not_in_asm = [].length + (true || { x: 5 }.x);' ) asm = asm.replace( "'almost asm'", '"use asm"; var not_in_asm = [].length + (true || { x: 5 }.x);' ) with open('a.wasm.asm.js', 'w') as o: o.write(asm) if method.startswith('interpret-asm2wasm'): delete_from_orbit( 'a.wasm.wast') # we should not need the .wast if not success: break_cashew() # we need cashew elif method.startswith('interpret-s-expr'): delete_from_orbit( 'a.wasm.asm.js') # we should not need the .asm.js if not success: delete_from_orbit('a.wasm.wast') elif method.startswith('asmjs'): delete_from_orbit( 'a.wasm.wast') # we should not need the .wast break_cashew() # we don't use cashew, so ok to break it if not success: delete_from_orbit('a.wasm.js') elif method.startswith('interpret-binary'): delete_from_orbit( 'a.wasm.wast') # we should not need the .wast delete_from_orbit( 'a.wasm.asm.js') # we should not need the .asm.js if not success: delete_from_orbit('a.wasm.wasm') else: 1 / 0 if NODEJS: proc = subprocess.Popen([NODEJS, 'a.wasm.js'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate() if success: assert proc.returncode == 0, err assert 'hello, world!' in out, out else: assert proc.returncode != 0, err assert 'hello, world!' not in out, out print '\n[ checking wasm.js testcases... ]\n' for c in tests: if c.endswith(('.c', '.cpp')): print '..', c base = c.replace('.cpp', '').replace('.c', '') post = base + '.post.js' try: post = open(os.path.join(options.binaryen_test, post)).read() except: post = None expected = open(os.path.join(options.binaryen_test, base + '.txt')).read() emcc = os.path.join(options.binaryen_test, base + '.emcc') extra = [] if os.path.exists(emcc): extra = json.loads(open(emcc).read()) if os.path.exists('a.normal.js'): os.unlink('a.normal.js') for opts in [[], ['-O1'], ['-O2'], ['-O3'], ['-Oz']]: for method in [ 'interpret-asm2wasm', 'interpret-s-expr', 'interpret-binary' ]: command = [ EMCC, '-o', 'a.wasm.js', '-s', 'BINARYEN=1', os.path.join(options.binaryen_test, c) ] + opts + extra command += ['-s', 'BINARYEN_METHOD="' + method + '"'] command += ['-s', 'BINARYEN_TRAP_MODE="js"'] print '....' + ' '.join(command) subprocess.check_call(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if post: open('a.wasm.js', 'a').write(post) else: print ' (no post)' for which in ['wasm']: print '......', which try: args = json.loads( open( os.path.join(options.binaryen_test, base + '.args')).read()) except: args = [] print ' (no args)' def execute(): if NODEJS: cmd = [NODEJS, 'a.' + which + '.js'] + args out = run_command(cmd) if out.strip() != expected.strip(): fail(out, expected) execute() if method in ['interpret-s-expr']: # binary and back shutil.copyfile('a.wasm.wast', 'a.wasm.original.wast') recreated = binary_format_check( 'a.wasm.wast', verify_final_result=False) shutil.copyfile(recreated, 'a.wasm.wast') execute()
[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') print '\n[ checking asm2wasm binary reading/writing... ]\n' asmjs = os.path.join(options.binaryen_test, 'hello_world.asm.js') delete_from_orbit('a.wasm') delete_from_orbit('b.wast') run_command(ASM2WASM + [asmjs, '-o', 'a.wasm']) assert open('a.wasm', 'rb').read()[0] == '\0', 'we emit binary by default' run_command(ASM2WASM + [asmjs, '-o', 'b.wast', '-S']) assert open('b.wast', 'rb').read()[0] != '\0', 'we emit text with -S' def run_wasm_dis_tests(): print '\n[ checking wasm-dis on provided binaries... ]\n' for t in tests: if t.endswith('.wasm') and not t.startswith('spec'): print '..', t t = os.path.join(options.binaryen_test, t) cmd = WASM_DIS + [t]
executables = sorted(filter(lambda x: not any(x.endswith(s) for s in not_executable_suffix) and os.path.isfile(x), os.listdir(options.binaryen_bin))) 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
executables = sorted(filter(lambda x: not any(x.endswith(s) for s in not_executable_suffix) and os.path.isfile(x), os.listdir(options.binaryen_bin))) 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', '-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'