def update_binaryen_js_tests():
  if not (MOZJS or NODEJS):
    print 'no vm to run binaryen.js tests'
    return

  if not os.path.exists(BINARYEN_JS):
    print 'no binaryen.js build to test'
    return

  print '\n[ checking binaryen.js testcases... ]\n'
  node_has_wasm = NODEJS and node_has_webassembly(NODEJS)
  for s in sorted(os.listdir(os.path.join('test', 'binaryen.js'))):
    if not s.endswith('.js'):
      continue
    print s
    f = open('a.js', 'w')
    f.write(open(BINARYEN_JS).read())
    if NODEJS:
      f.write(node_test_glue())
    test_path = os.path.join('test', 'binaryen.js', s)
    test_src = open(test_path).read()
    f.write(test_src)
    f.close()
    if MOZJS or node_has_wasm or 'WebAssembly.' not in test_src:
      cmd = [MOZJS or NODEJS, 'a.js']
      if 'fatal' not in s:
        out = run_command(cmd, stderr=subprocess.STDOUT)
      else:
        # expect an error - the specific error code will depend on the vm
        out = run_command(cmd, stderr=subprocess.STDOUT, expected_status=None)
      with open(os.path.join('test', 'binaryen.js', s + '.txt'), 'w') as o:
        o.write(out)
    else:
      print 'Skipping ' + test_path + ' because WebAssembly might not be supported'
def update_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]
      run_command(cmd)
      actual = open('a.wast').read()
      out = t + '.out'
      with open(out, 'w') as o:
        o.write(actual)
Exemple #3
0
 def run_spec_test(wast):
   cmd = WASM_SHELL + [wast]
   # we must skip the stack machine portions of spec tests or apply other extra args
   extra = {
   }
   cmd = cmd + (extra.get(os.path.basename(wast)) or [])
   return run_command(cmd, stderr=subprocess.PIPE)
def update_asm_js_tests():
  print '[ processing and updating testcases... ]\n'
  for asm in sorted(os.listdir('test')):
    if asm.endswith('.asm.js'):
      for precise in [0, 1, 2]:
        for opts in [1, 0]:
          cmd = ASM2WASM + [os.path.join('test', asm)]
          if 'threads' in asm:
            cmd += ['--enable-threads']
          wasm = asm.replace('.asm.js', '.fromasm')
          if not precise:
            cmd += ['--trap-mode=allow', '--ignore-implicit-traps']
            wasm += '.imprecise'
          elif precise == 2:
            cmd += ['--trap-mode=clamp']
            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 '4GB' in asm:
            cmd += ['--mem-max=4294967296']
          if 'i64' in asm or 'wasm-only' in asm or 'noffi' in asm:
            cmd += ['--wasm-only']
          print ' '.join(cmd)
          actual = run_command(cmd)
          with open(os.path.join('test', wasm), 'w') as o:
            o.write(actual)
          if 'debugInfo' in asm:
            cmd += ['--source-map', os.path.join('test', wasm + '.map'), '-o', 'a.wasm']
            run_command(cmd)
def update_wasm_dis_tests():
  print '\n[ checking wasm-dis on provided binaries... ]\n'
  for t in os.listdir('test'):
    if t.endswith('.wasm') and not t.startswith('spec'):
      print '..', t
      t = os.path.join('test', t)
      cmd = WASM_DIS + [t]
      if os.path.isfile(t + '.map'):
        cmd += ['--source-map', t + '.map']
      actual = run_command(cmd)

      open(t + '.fromBinary', 'w').write(actual)
def update_binaryen_js_tests():
    if not (shared.MOZJS or shared.NODEJS):
        print('no vm to run binaryen.js tests')
        return

    if not os.path.exists(shared.BINARYEN_JS):
        print('no binaryen.js build to test')
        return

    print('\n[ checking binaryen.js testcases... ]\n')
    node_has_wasm = shared.NODEJS and support.node_has_webassembly(
        shared.NODEJS)
    for s in shared.get_tests(shared.get_test_dir('binaryen.js'), ['.js']):
        basename = os.path.basename(s)
        print(basename)
        f = open('a.js', 'w')
        f.write(open(shared.BINARYEN_JS).read())
        if shared.NODEJS:
            f.write(support.node_test_glue())
        test_src = open(s).read()
        f.write(test_src)
        f.close()
        if shared.MOZJS or node_has_wasm or 'WebAssembly.' not in test_src:
            cmd = [shared.MOZJS or shared.NODEJS, 'a.js']
            if 'fatal' not in basename:
                out = support.run_command(cmd, stderr=subprocess.STDOUT)
            else:
                # expect an error - the specific error code will depend on the vm
                out = support.run_command(cmd,
                                          stderr=subprocess.STDOUT,
                                          expected_status=None)
            with open(s + '.txt', 'w') as o:
                o.write(out)
        else:
            print('Skipping ' + basename +
                  ' because WebAssembly might not be supported')
Exemple #7
0
def run_ctor_eval_tests():
    print '\n[ checking wasm-ctor-eval... ]\n'

    test_dir = os.path.join(options.binaryen_test, 'ctor-eval')
    for t in os.listdir(test_dir):
        if t.endswith(('.wast', '.wasm')):
            print '..', t
            t = os.path.join(test_dir, 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)
Exemple #8
0
def run_wasm_metadce_tests():
  print '\n[ checking wasm-metadce ]\n'

  test_dir = os.path.join(options.binaryen_test, 'metadce')
  for t in os.listdir(test_dir):
    if t.endswith(('.wast', '.wasm')):
      print '..', t
      t = os.path.join(test_dir, 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:
        fail_if_not_identical_to_file(seen.read(), expected)
      fail_if_not_identical_to_file(stdout, expected + '.stdout')
def update_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)
      actual = open('a.wast').read()
      out = t + '.dced'
      with open(out, 'w') as o:
        o.write(actual)
      with open(out + '.stdout', 'w') as o:
        o.write(stdout)
Exemple #10
0
def run_wasm_metadce_tests():
    print('\n[ checking wasm-metadce ]\n')

    for t in shared.get_tests(shared.get_test_dir('metadce'),
                              ['.wast', '.wasm']):
        print('..', os.path.basename(t))
        graph = t + '.graph.txt'
        cmd = shared.WASM_METADCE + [
            t, '--graph-file=' + graph, '-o', 'a.wat', '-S', '-all'
        ]
        stdout = support.run_command(cmd)
        expected = t + '.dced'
        with open('a.wat') as seen:
            shared.fail_if_not_identical_to_file(seen.read(), expected)
        shared.fail_if_not_identical_to_file(stdout, expected + '.stdout')
def update_metadce_tests():
    print('\n[ checking wasm-metadce... ]\n')
    for t in os.listdir(os.path.join(options.binaryen_test, 'metadce')):
        if t.endswith(('.wast', '.wasm')):
            print('..', t)
            t = os.path.join(options.binaryen_test, 'metadce', t)
            graph = t + '.graph.txt'
            cmd = WASM_METADCE + [t, '--graph-file=' + graph, '-o', 'a.wast', '-S']
            stdout = run_command(cmd)
            actual = open('a.wast').read()
            out = t + '.dced'
            with open(out, 'w') as o:
                o.write(actual)
            with open(out + '.stdout', 'w') as o:
                o.write(stdout)
Exemple #12
0
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 update_spec_tests():
    print('\n[ updating wasm-shell spec testcases... ]\n')

    for t in shared.options.spec_tests:
        print('..', os.path.basename(t))

        cmd = shared.WASM_SHELL + [t]
        expected = os.path.join(shared.get_test_dir('spec'), 'expected-output', os.path.basename(t) + '.log')
        if os.path.isfile(expected):
            stdout = support.run_command(cmd, stderr=subprocess.PIPE)
            # filter out binaryen interpreter logging that the spec suite
            # doesn't expect
            filtered = [line for line in stdout.splitlines() if not line.startswith('[trap')]
            stdout = '\n'.join(filtered) + '\n'
            with open(expected, 'w') as o:
                o.write(stdout)
def update_metadce_tests():
    print('\n[ checking wasm-metadce... ]\n')
    for t in shared.get_tests(shared.get_test_dir('metadce'),
                              ['.wast', '.wasm']):
        print('..', os.path.basename(t))
        graph = t + '.graph.txt'
        cmd = shared.WASM_METADCE + [
            t, '--graph-file=' + graph, '-o', 'a.wast', '-S', '-all'
        ]
        stdout = support.run_command(cmd)
        actual = open('a.wast').read()
        out = t + '.dced'
        with open(out, 'w') as o:
            o.write(actual)
        with open(out + '.stdout', 'w') as o:
            o.write(stdout)
Exemple #15
0
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]
            if os.path.isfile(t + '.map'): cmd += ['--source-map', t + '.map']

            actual = run_command(cmd)

            with open(t + '.fromBinary') as f:
                expected = f.read()
                if actual != expected:
                    fail(actual, expected)
Exemple #16
0
def run_vanilla_tests():
    if not (has_vanilla_emcc and has_vanilla_llvm and 0):
        print('\n[ skipping emcc WASM_BACKEND testcases...]\n')
        return

    print('\n[ checking emcc WASM_BACKEND testcases...]\n')

    try:
        if has_vanilla_llvm:
            os.environ['LLVM'] = BIN_DIR  # use the vanilla LLVM
        else:
            # if we did not set vanilla llvm, then we must set this env var to make emcc use the wasm backend.
            # (if we are using vanilla llvm, things should just work)
            print('(not using vanilla llvm, so setting env var to tell emcc to use wasm backend)')
            os.environ['EMCC_WASM_BACKEND'] = '1'
        VANILLA_EMCC = os.path.join(options.binaryen_test, 'emscripten', 'emcc')
        # run emcc to make sure it sets itself up properly, if it was never run before
        command = [VANILLA_EMCC, '-v']
        print('____' + ' '.join(command))
        subprocess.check_call(command)

        for c in sorted(os.listdir(os.path.join(options.binaryen_test, 'wasm_backend'))):
            if not c.endswith('cpp'):
                continue
            print('..', c)
            base = c.replace('.cpp', '').replace('.c', '')
            expected = open(os.path.join(options.binaryen_test, 'wasm_backend', base + '.txt')).read()
            for opts in [[], ['-O1'], ['-O2']]:
                # only my code is a hack we used early in wasm backend dev, which somehow worked, but only with -O1
                only = [] if opts != ['-O1'] or '_only' not in base else ['-s', 'ONLY_MY_CODE=1']
                command = [VANILLA_EMCC, '-o', 'a.wasm.js', os.path.join(options.binaryen_test, 'wasm_backend', c)] + opts + only
                print('....' + ' '.join(command))
                if os.path.exists('a.wasm.js'):
                    os.unlink('a.wasm.js')
                subprocess.check_call(command)
                if NODEJS:
                    print('  (check in node)')
                    cmd = [NODEJS, 'a.wasm.js']
                    out = run_command(cmd)
                    if out.strip() != expected.strip():
                        fail(out, expected)
    finally:
        if has_vanilla_llvm:
            del os.environ['LLVM']
        else:
            del os.environ['EMCC_WASM_BACKEND']
Exemple #17
0
def update_lld_tests():
    print '\n[ checking wasm-emscripten-finalize testcases... ]\n'

    extension_arg_map = {
        '.out': [],
        '.jscall.out': ['--emscripten-reserved-function-pointers=3'],
    }
    for wast_path in files_with_pattern('test', 'lld', '*.wast'):
        print '..', wast_path
        for ext, ext_args in extension_arg_map.items():
            out_path = wast_path + ext
            if ext != '.out' and not os.path.exists(out_path):
                continue
            cmd = (WASM_EMSCRIPTEN_FINALIZE +
                   [wast_path, '-S', '--global-base=568'] + ext_args)
            actual = run_command(cmd)
            with open(out_path, 'w') as o:
                o.write(actual)
Exemple #18
0
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]
      if os.path.isfile(t + '.map'): cmd += ['--source-map', t + '.map']

      actual = run_command(cmd)
      fail_if_not_identical_to_file(actual, t + '.fromBinary')

      # also verify there are no validation errors
      def check():
        cmd = WASM_OPT + [t]
        actual = run_command(cmd)
      with_pass_debug(check)
Exemple #19
0
def run_wasm_dis_tests():
    print('\n[ checking wasm-dis on provided binaries... ]\n')

    for t in shared.get_tests(shared.options.binaryen_test, ['.wasm']):
        print('..', os.path.basename(t))
        cmd = shared.WASM_DIS + [t]
        if os.path.isfile(t + '.map'):
            cmd += ['--source-map', t + '.map']

        actual = support.run_command(cmd)
        shared.fail_if_not_identical_to_file(actual, t + '.fromBinary')

        # also verify there are no validation errors
        def check():
            cmd = shared.WASM_OPT + [t, '-all']
            support.run_command(cmd)

        shared.with_pass_debug(check)
def update_reduce_tests():
  if not has_shell_timeout():
    return
  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', expected])
Exemple #21
0
def update_reduce_tests():
  if not has_shell_timeout():
    return
  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', expected])
Exemple #22
0
def run_binaryen_js_tests():
    print '\n[ checking binaryen.js testcases... ]\n'

    for s in sorted(
            os.listdir(os.path.join(options.binaryen_test, 'binaryen.js'))):
        if not s.endswith('.js'): continue
        print s
        f = open('a.js', 'w')
        f.write(open(os.path.join(options.binaryen_bin, 'binaryen.js')).read())
        f.write(
            open(os.path.join(options.binaryen_test, 'binaryen.js', s)).read())
        f.close()
        cmd = [MOZJS, 'a.js']
        out = run_command(cmd, stderr=subprocess.STDOUT)
        expected = open(
            os.path.join(options.binaryen_test, 'binaryen.js',
                         s + '.txt')).read()
        if expected not in out:
            fail(out, expected)
Exemple #23
0
def run_wasm_reduce_tests():
  print '\n[ checking wasm-reduce ]\n'

  test_dir = os.path.join(options.binaryen_test, 'reduce')
  for t in os.listdir(test_dir):
    if t.endswith('.wast'):
      print '..', t
      t = os.path.join(test_dir, 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:
        fail_if_not_identical_to_file(seen.read(), expected)
Exemple #24
0
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())
Exemple #25
0
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'
          fail_if_not_identical_to_file(actual, out)
          fail_if_not_identical_to_file(stdout, out + '.stdout')
Exemple #26
0
 def run_spec_test(wast):
     cmd = WASM_SHELL + [wast]
     # we must skip the stack machine portions of spec tests or apply other extra args
     extra = {}
     cmd = cmd + (extra.get(os.path.basename(wast)) or [])
     return run_command(cmd, stderr=subprocess.PIPE)
Exemple #27
0
 def check():
     cmd = WASM_OPT + [t]
     actual = run_command(cmd)
Exemple #28
0
 def check():
     pass_debug = run_command(cmd)
     fail_if_not_identical(curr, pass_debug)
Exemple #29
0
                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']
                print ' '.join(cmd)
                actual = run_command(cmd)
                with open(os.path.join('test', wasm), 'w') as o:
                    o.write(actual)
                if 'debugInfo' in asm:
                    cmd += [
                        '--source-map',
                        os.path.join('test', wasm + '.map'), '-o', 'a.wasm'
                    ]
                    run_command(cmd)

extension_arg_map = {
    '.wast': [],
    '.clamp.wast': ['--trap-mode=clamp'],
    '.js.wast': ['--trap-mode=js'],
    '.jscall.wast': ['--emscripten-reserved-function-pointers=3'],
}
def update_wasm_opt_tests():
    print('\n[ checking wasm-opt -o notation... ]\n')
    wast = os.path.join(shared.options.binaryen_test, 'hello_world.wast')
    cmd = shared.WASM_OPT + [wast, '-o', 'a.wast', '-S']
    support.run_command(cmd)
    open(wast, 'w').write(open('a.wast').read())

    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 = t.replace('.wast', '')
        cmd = shared.WASM_OPT + [t, '--print', '-all']
        print('    ', ' '.join(cmd))
        actual = subprocess.check_output(cmd)
        print(cmd, actual)
        with open(wasm + '.txt', 'wb') as o:
            o.write(actual)
        cmd = shared.WASM_OPT + [t, '--print-minified', '-all']
        print('    ', ' '.join(cmd))
        actual = subprocess.check_output(cmd)
        with open(wasm + '.minified.txt', 'wb') as o:
            o.write(actual)

    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 = t.endswith('.wasm')
        base = os.path.basename(t).replace('.wast', '').replace('.wasm', '')
        passname = base
        if passname.isdigit():
            passname = open(
                os.path.join(shared.options.binaryen_test, 'passes',
                             passname + '.passes')).read().strip()
        opts = [('--' + p if not p.startswith('O') 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']
            actual += support.run_command(cmd)
        with open(
                os.path.join(shared.options.binaryen_test, 'passes',
                             base + ('.bin' if binary else '') + '.txt'),
                'w') as o:
            o.write(actual)
        if 'emit-js-wrapper' in t:
            with open('a.js') as i:
                with open(t + '.js', 'w') as o:
                    o.write(i.read())
        if 'emit-spec-wrapper' in t:
            with open('a.wat') as i:
                with open(t + '.wat', 'w') as o:
                    o.write(i.read())

    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', '')
        open(f, 'w').write(actual)

    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'])
        open(f, 'w').write(actual)
Exemple #31
0
def run_validator_tests():
    print('\n[ running validation tests... ]\n')
    # Ensure the tests validate by default
    cmd = shared.WASM_AS + [
        os.path.join(shared.get_test_dir('validator'), 'invalid_export.wast'),
        '-o', 'a.wasm'
    ]
    support.run_command(cmd)
    cmd = shared.WASM_AS + [
        os.path.join(shared.get_test_dir('validator'), 'invalid_import.wast'),
        '-o', 'a.wasm'
    ]
    support.run_command(cmd)
    cmd = shared.WASM_AS + [
        '--validate=web',
        os.path.join(shared.get_test_dir('validator'), 'invalid_export.wast'),
        '-o', 'a.wasm'
    ]
    support.run_command(cmd, expected_status=1)
    cmd = shared.WASM_AS + [
        '--validate=web',
        os.path.join(shared.get_test_dir('validator'), 'invalid_import.wast'),
        '-o', 'a.wasm'
    ]
    support.run_command(cmd, expected_status=1)
    cmd = shared.WASM_AS + [
        '--validate=none',
        os.path.join(shared.get_test_dir('validator'), 'invalid_return.wast'),
        '-o', 'a.wasm'
    ]
    support.run_command(cmd)
    cmd = shared.WASM_AS + [
        os.path.join(shared.get_test_dir('validator'), 'invalid_number.wast'),
        '-o', 'a.wasm'
    ]
    support.run_command(cmd, expected_status=1)
Exemple #32
0
 def check():
     cmd = shared.WASM_OPT + [t, '-all']
     support.run_command(cmd)
Exemple #33
0
 def run_opt_test(wast):
     # check optimization validation
     cmd = WASM_OPT + [wast, '-O']
     run_command(cmd)
Exemple #34
0
def update_wasm_opt_tests():
  print '\n[ checking wasm-opt -o notation... ]\n'
  wast = os.path.join('test', 'hello_world.wast')
  cmd = WASM_OPT + [wast, '-o', 'a.wast', '-S']
  run_command(cmd)
  open(wast, 'w').write(open('a.wast').read())

  print '\n[ checking wasm-opt parsing & printing... ]\n'
  for t in sorted(os.listdir(os.path.join('test', 'print'))):
    if t.endswith('.wast'):
      print '..', t
      wasm = os.path.basename(t).replace('.wast', '')
      cmd = WASM_OPT + [os.path.join('test', 'print', t), '--print', '-all']
      print '    ', ' '.join(cmd)
      actual = subprocess.check_output(cmd)
      print cmd, actual
      with open(os.path.join('test', 'print', wasm + '.txt'), 'w') as o:
        o.write(actual)
      cmd = WASM_OPT + [os.path.join('test', 'print', t), '--print-minified', '-all']
      print '    ', ' '.join(cmd)
      actual = subprocess.check_output(cmd)
      with open(os.path.join('test', 'print', wasm + '.minified.txt'), 'w') as o:
        o.write(actual)

  print '\n[ checking wasm-opt passes... ]\n'
  for t in sorted(os.listdir(os.path.join('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('test', 'passes', passname + '.passes')).read().strip()
      opts = [('--' + p if not p.startswith('O') else '-' + p) for p in passname.split('_')]
      t = os.path.join('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)
      with open(os.path.join('test', 'passes', base + ('.bin' if binary else '') + '.txt'), 'w') as o:
        o.write(actual)
      if 'emit-js-wrapper' in t:
        with open('a.js') as i:
          with open(t + '.js', 'w') as o:
            o.write(i.read())
      if 'emit-spec-wrapper' in t:
        with open('a.wat') as i:
          with open(t + '.wat', 'w') as o:
            o.write(i.read())

  print '\n[ checking wasm-opt testcases... ]\n'
  for t in os.listdir('test'):
    if t.endswith('.wast') and not t.startswith('spec'):
      print '..', t
      t = os.path.join('test', t)
      f = t + '.from-wast'
      cmd = WASM_OPT + [t, '--print', '-all']
      actual = run_command(cmd)
      actual = actual.replace('printing before:\n', '')
      open(f, 'w').write(actual)

  print '\n[ checking wasm-opt debugInfo read-write... ]\n'
  for t in os.listdir('test'):
    if t.endswith('.fromasm') and 'debugInfo' in t:
      print '..', t
      t = os.path.join('test', t)
      f = t + '.read-written'
      run_command(WASM_AS + [t, '--source-map=a.map', '-o', 'a.wasm', '-g'])
      run_command(WASM_OPT + ['a.wasm', '--input-source-map=a.map', '-o', 'b.wasm', '--output-source-map=b.map', '-g'])
      actual = run_command(WASM_DIS + ['b.wasm', '--source-map=b.map'])
      open(f, 'w').write(actual)
Exemple #35
0
                            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
      with open('split.wast', 'w') as o: o.write(module)
      cmd = WASM_OPT + opts + ['split.wast', '--print']
          wasm += '.no-opts'
          if precise:
            cmd += ['-O0'] # test that -O0 does nothing
        else:
          cmd += ['-O']
        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:
          cmd += ['--wasm-only']
        print '..', asm, wasm
        print '    ', ' '.join(cmd)
        actual = run_command(cmd)
        with open(os.path.join('test', wasm), 'w') as o: o.write(actual)

for dot_s_dir in ['dot_s', 'llvm_autogenerated']:
  for s in sorted(os.listdir(os.path.join('test', dot_s_dir))):
    if not s.endswith('.s'): continue
    print '..', s
    wasm = s.replace('.s', '.wast')
    full = os.path.join('test', dot_s_dir, s)
    stack_alloc = ['--allocate-stack=1024'] if dot_s_dir == 'llvm_autogenerated' else []
    cmd = [os.path.join('bin', 's2wasm'), full, '--emscripten-glue'] + stack_alloc
    if s.startswith('start_'):
      cmd.append('--start')
    actual = run_command(cmd, stderr=subprocess.PIPE, expected_err='')

    expected_file = os.path.join('test', dot_s_dir, wasm)
Exemple #37
0
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)
Exemple #38
0
 def run_spec_test(wast):
     cmd = shared.WASM_SHELL + [wast]
     return support.run_command(cmd, stderr=subprocess.PIPE)
Exemple #39
0
 def run_opt_test(wast):
     # check optimization validation
     cmd = shared.WASM_OPT + [wast, '-O', '-all']
     support.run_command(cmd)
Exemple #40
0
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)
Exemple #41
0
                            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'

for t in sorted(os.listdir(os.path.join(options.binaryen_test, 'passes'))):
Exemple #42
0
 def execute():
   if NODEJS:
     cmd = [NODEJS, 'a.' + which + '.js'] + args
     out = run_command(cmd)
     if out.strip() != expected.strip():
       fail(out, expected)