Пример #1
0
 def try_js(args):
   shared.try_delete(filename + '.js')
   print '(compile)'
   shared.check_execute([shared.PYTHON, shared.EMCC, opts, fullname, '-o', filename + '.js'] + CSMITH_CFLAGS + args)
   assert os.path.exists(filename + '.js')
   print '(run)'
   js = shared.run_js(filename + '.js', engine=engine1, check_timeout=True, assert_returncode=None, cwd='/tmp/emscripten_temp')
   assert correct1 == js or correct2 == js, ''.join([a.rstrip()+'\n' for a in difflib.unified_diff(correct1.split('\n'), js.split('\n'), fromfile='expected', tofile='actual')])
Пример #2
0
 def try_js(args):
   shared.try_delete(filename + '.js')
   print '(compile)'
   shared.check_execute([shared.EMCC, opts, filename + '.c', '-o', filename + '.js'] + CSMITH_CFLAGS + args)
   assert os.path.exists(filename + '.js')
   print '(run)'
   js = shared.run_js(filename + '.js', stderr=PIPE, engine=engine1, check_timeout=True)
   assert correct1 == js or correct2 == js, ''.join([a.rstrip()+'\n' for a in difflib.unified_diff(correct1.split('\n'), js.split('\n'), fromfile='expected', tofile='actual')])
Пример #3
0
 def try_js(args):
   shared.try_delete(filename + '.js')
   print '(compile)'
   shared.execute([shared.EMCC, '-O2', '-s', 'ASM_JS=1', filename + '.c', '-o', filename + '.js'] + CSMITH_CFLAGS + args, stderr=PIPE)
   assert os.path.exists(filename + '.js')
   print '(run)'
   js = shared.run_js(filename + '.js', stderr=PIPE, engine=engine1, check_timeout=True)
   assert correct1 == js or correct2 == js, ''.join([a.rstrip()+'\n' for a in difflib.unified_diff(correct1.split('\n'), js.split('\n'), fromfile='expected', tofile='actual')])
Пример #4
0
 def try_js(args):
   shared.try_delete(filename + '.js')
   print '(compile)'
   shared.check_execute([shared.PYTHON, shared.EMCC, opts, fullname, '-o', filename + '.js', '-s', 'PRECISE_F32=1'] + CSMITH_CFLAGS + args)
   assert os.path.exists(filename + '.js')
   print '(run in %s)' % engine1
   js = shared.run_js(filename + '.js', engine=engine1, check_timeout=True, assert_returncode=None, cwd='/tmp/emscripten_temp')
   js = js.split('\n')[0] + '\n' # remove any extra printed stuff (node workarounds)
   assert correct1 == js or correct2 == js, ''.join([a.rstrip()+'\n' for a in difflib.unified_diff(correct1.split('\n'), js.split('\n'), fromfile='expected', tofile='actual')])
Пример #5
0
 def execute_js(engine):
     print '(run in %s)' % engine
     js = shared.run_js(filename + '.js',
                        engine=engine1,
                        check_timeout=True,
                        assert_returncode=None)
     js = js.split('\n')[
         0] + '\n'  # remove any extra printed stuff (node workarounds)
     return correct1 == js or correct2 == js
 def execute_js(engine):
   print '(run in %s)' % engine
   try:
     js = shared.run_js(filename + '.js', engine=engine, check_timeout=True, assert_returncode=None)
   except:
     print 'failed to run in primary'
     return False
   js = js.split('\n')[0] + '\n' # remove any extra printed stuff (node workarounds)
   return correct1 == js or correct2 == js
def try_js(args):
    shared.try_delete(filename + '.js')
    shared.execute([
        shared.EMCC, '-O2', '-s', 'ASM_JS=1', '-s', 'PRECISE_I64_MATH=1', '-s',
        'PRECISE_I32_MUL=1', filename + '.c', '-o', filename + '.js'
    ] + CSMITH_CFLAGS + args,
                   stderr=PIPE)
    assert os.path.exists(filename + '.js')
    js = shared.run_js(filename + '.js', stderr=PIPE, engine=engine1)
    assert correct == js, ''.join([
        a.rstrip() + '\n' for a in difflib.unified_diff(correct.split('\n'),
                                                        js.split('\n'),
                                                        fromfile='expected',
                                                        tofile='actual')
    ])
Пример #8
0
 def try_js(args):
     shared.try_delete(filename + ".js")
     print "(compile)"
     shared.execute(
         [shared.EMCC, "-O2", "-s", "ASM_JS=1", filename + ".c", "-o", filename + ".js"] + CSMITH_CFLAGS + args,
         stderr=PIPE,
     )
     assert os.path.exists(filename + ".js")
     print "(run)"
     js = shared.run_js(filename + ".js", stderr=PIPE, engine=engine1, check_timeout=True)
     assert correct1 == js or correct2 == js, "".join(
         [
             a.rstrip() + "\n"
             for a in difflib.unified_diff(
                 correct1.split("\n"), js.split("\n"), fromfile="expected", tofile="actual"
             )
         ]
     )
Пример #9
0
def try_js(args):
  shared.check_execute([shared.EMCC] + EMCC_ARGS + CSMITH_CFLAGS + args +
    [filename, '-o', js_filename])
  js = shared.run_js(js_filename, stderr=PIPE, engine=ENGINE)
  assert correct == js
Пример #10
0
    print >> sys.stderr, 'cannot use WASM=1 when full asm.js validation was disabled (make sure to run in at least -O1, and look for warnings about other options that might force asm.js off)'
  else:
    print >> sys.stderr, 'cannot use WASM=1, no asm.js module to wasm-ify'
  sys.exit(1)
start = module.index('function')
end = module.rindex(')')
asm = module[start:end]
open(tempfile2, 'w').write(asm)

print 'run polyfill packer on', tempfile2
dir = os.getcwd()
try:
  os.chdir(os.path.dirname(os.path.abspath(tempfile2)))
  # try in spidermonkey first, because it's faster
  try:
    out = emscripten.run_js(pack_asmjs, args=[os.path.basename(tempfile2), 'output.binary'], stdout=PIPE, engine=emscripten.SPIDERMONKEY_ENGINE)
  except:
    out = emscripten.run_js(pack_asmjs, args=[os.path.basename(tempfile2), 'output.binary'], stdout=PIPE)
finally:
  os.chdir(dir)
out = json.loads(out)
open(wasmfile, 'wb').write(''.join(map(chr, out)))

print 'create patched out js'
js = open(jsfile).read()
patched = js.replace(asm, 'unwasmed') # we assume the module is right there
assert patched != js
patched = '''
function runEmscriptenModule(Module, unwasmed_) {
  var unwasmed = unwasmed_;
  arguments = undefined; // emscripten shell code looks at arguments, which it uses as commandline args
def try_js(args):
    shared.run_process([shared.EMCC] + EMCC_ARGS + CSMITH_CFLAGS + args +
                       [filename, '-o', js_filename])
    js = shared.run_js(js_filename, stderr=PIPE, engine=ENGINE)
    assert correct == js
Пример #12
0
def try_js(args):
  shared.run_process([shared.PYTHON, shared.EMCC] + EMCC_ARGS + CSMITH_CFLAGS + args +
    [filename, '-o', js_filename])
  js = shared.run_js(js_filename, stderr=PIPE, engine=ENGINE)
  assert correct == js
Пример #13
0
    if not try_js():
        fail()
        continue
    if not execute_js(engine1):
        fail()
        continue
    if engine2 and not execute_js(engine2):
        fail()
        continue

    # This is ok. Try validation in secondary JS engine
    if opts != '-O0' and engine2:
        try:
            js2 = shared.run_js(filename + '.js',
                                stderr=PIPE,
                                engine=engine2 + ['-w'],
                                full_output=True,
                                check_timeout=True,
                                assert_returncode=None)
        except:
            print 'failed to run in secondary', js2
            break

        # asm.js testing
        if 'warning: Successfully compiled asm.js code' not in js2:
            print "ODIN VALIDATION BUG"
            notes['embug'] += 1
            fail()
            continue
        print '[asm.js validation ok in %s]' % str(engine2)
Пример #14
0
def try_js(args):
  shared.check_execute([shared.PYTHON, shared.EMCC] + EMCC_ARGS + CSMITH_CFLAGS + args +
    [filename, '-o', js_filename])
  js = shared.run_js(js_filename, stderr=PIPE, engine=ENGINE)
  assert correct == js
Пример #15
0
    fails += 1
    shutil.copyfile('fuzzcode.c', 'newfail%d.c' % fails)
    continue
  #if not ok:
  #  try: # finally, try with safe heap. if that is triggered, this is nonportable code almost certainly
  #    try_js(['-s', 'SAFE_HEAP=1'])
  #  except Exception, e:
  #    print e
  #    js = shared.run_js(filename + '.js', stderr=PIPE, full_output=True)
  #  print js
  #  if 'SAFE_HEAP' in js:
  #    notes['safeheap'] += 1
  #  else:
  #    break

  # This is ok. Try in secondary JS engine too
  if engine2 and normal:
    try:
      js2 = shared.run_js(filename + '.js', stderr=PIPE, engine=engine2, full_output=True, check_timeout=True)
    except:
      print 'failed to run in secondary', js2
      break

    # asm.js testing
    assert 'warning: Successfully compiled asm.js code' in js2, 'must validate'
    js2 = js2.replace('\nwarning: Successfully compiled asm.js code\n', '')

    assert js2 == correct1 or js2 == correct2, ''.join([a.rstrip()+'\n' for a in difflib.unified_diff(correct1.split('\n'), js2.split('\n'), fromfile='expected', tofile='actual')]) + 'ODIN FAIL'
    print 'odin ok'

Пример #16
0
def try_js(args):
  shared.try_delete(filename + '.js')
  shared.execute([shared.EMCC, '-O2', '-s', 'ASM_JS=1', '-s', 'PRECISE_I64_MATH=1', '-s', 'PRECISE_I32_MUL=1', filename + '.c', '-o', filename + '.js'] + CSMITH_CFLAGS + args, stderr=PIPE)
  assert os.path.exists(filename + '.js')
  js = shared.run_js(filename + '.js', stderr=PIPE, engine=engine1)
  assert correct == js, ''.join([a.rstrip()+'\n' for a in difflib.unified_diff(correct.split('\n'), js.split('\n'), fromfile='expected', tofile='actual')])
def inspect_code(headers, cpp_opts, structs, defines):
  show('Generating C code...')
  
  code = ['#include <stdio.h>', '#include <stddef.h>']
  # Include all the needed headers.
  for path in headers:
    code.append('#include "' + path + '"')
  
  code.append('int main() {')
  c_descent('structs', code)
  for name, struct in structs.items():
    gen_inspect_code([name], struct, code)
  
  c_ascent(code)
  c_descent('defines', code)
  for name, type_ in defines.items():
    # Add the necessary python type, if missing.
    if '%' not in type_:
      if type_[-1] in ('d', 'i', 'u'):
        # integer
        type_ = 'i%' + type_
      elif type_[-1] in ('f', 'F', 'e', 'E', 'g', 'G'):
        # float
        type_ = 'f%' + type_
      elif type_[-1] in ('x', 'X', 'a', 'A', 'c', 's'):
        # hexadecimal or string
        type_ = 's%' + type_
    
    c_set(name, type_, name, code)
  
  code.append('return 0;')
  code.append('}')
  
  # Write the source code to a temporary file.
  src_file = tempfile.mkstemp('.c')
  js_file = tempfile.mkstemp('.js')
  
  os.write(src_file[0], '\n'.join(code))
  
  # Close all unneeded FDs.
  os.close(src_file[0])
  os.close(js_file[0])

  # Remove dangerous env modifications
  safe_env = os.environ.copy()
  for opt in ['EMCC_FORCE_STDLIBS', 'EMCC_ONLY_FORCED_STDLIBS']:
    if opt in safe_env:
      del safe_env[opt]

  # Use binaryen, if necessary
  binaryen = os.environ.get('EMCC_WASM_BACKEND_BINARYEN')
  if binaryen:
    cpp_opts += ['-s', 'BINARYEN=1']

  info = []

  try:
    try:
      # Compile the program.
      show('Compiling generated code...')
      subprocess.check_call([shared.PYTHON, shared.EMCC] + cpp_opts + ['-o', js_file[1], src_file[1], '-s', 'BOOTSTRAPPING_STRUCT_INFO=1', '-s', 'WARN_ON_UNDEFINED_SYMBOLS=0', '-Oz', '--js-opts', '0', '--memory-init-file', '0'], env=safe_env) # -Oz optimizes enough to avoid warnings on code size/num locals
    except:
      sys.stderr.write('FAIL: Compilation failed!\n')
      sys.exit(1)

    # Run the compiled program.
    show('Calling generated program...')
    try:
      info = shared.run_js(js_file[1]).splitlines()
    except subprocess.CalledProcessError:
      sys.stderr.write('FAIL: Running the generated program failed!\n')
      sys.exit(1)

  finally:
    # Remove all temporary files.
    os.unlink(src_file[1])
    
    if os.path.exists(js_file[1]):
      os.unlink(js_file[1])
  
  # Parse the output of the program into a dict.
  return parse_c_output(info)
Пример #18
0
    notes['embug'] += 1
    fails += 1
    shutil.copyfile(fullname, 'newfail_%d_%d%s' % (os.getpid(), fails, suffix))

  if not try_js():
    fail()
    continue
  if not execute_js(engine1):
    fail()
    continue
  if engine2 and not execute_js(engine2):
    fail()
    continue

  # This is ok. Try validation in secondary JS engine
  if opts != '-O0' and engine2:
    try:
      js2 = shared.run_js(filename + '.js', stderr=PIPE, engine=engine2 + ['-w'], full_output=True, check_timeout=True, assert_returncode=None)
    except:
      print 'failed to run in secondary', js2
      break

    # asm.js testing
    if 'warning: Successfully compiled asm.js code' not in js2:
      print "ODIN VALIDATION BUG"
      notes['embug'] += 1
      fail()
      continue
    print '[asm.js validation ok in %s]' % str(engine2)

Пример #19
0
def inspect_code(headers, cpp_opts, structs, defines):
    show('Generating C code...')

    code = ['#include <stdio.h>', '#include <stddef.h>']
    # Include all the needed headers.
    for path in headers:
        code.append('#include "' + path + '"')

    code.append('int main() {')
    c_descent('structs', code)
    for name, struct in structs.items():
        gen_inspect_code([name], struct, code)

    c_ascent(code)
    c_descent('defines', code)
    for name, type_ in defines.items():
        # Add the necessary python type, if missing.
        if '%' not in type_:
            if type_[-1] in ('d', 'i', 'u'):
                # integer
                type_ = 'i%' + type_
            elif type_[-1] in ('f', 'F', 'e', 'E', 'g', 'G'):
                # float
                type_ = 'f%' + type_
            elif type_[-1] in ('x', 'X', 'a', 'A', 'c', 's'):
                # hexadecimal or string
                type_ = 's%' + type_

        c_set(name, type_, name, code)

    code.append('return 0;')
    code.append('}')

    # Write the source code to a temporary file.
    src_file = tempfile.mkstemp('.c')
    js_file = tempfile.mkstemp('.js')

    os.write(src_file[0], '\n'.join(code))

    # Close all unneeded FDs.
    os.close(src_file[0])
    os.close(js_file[0])

    # Remove dangerous env modifications
    safe_env = os.environ.copy()
    for opt in ['EMCC_FORCE_STDLIBS', 'EMCC_ONLY_FORCED_STDLIBS']:
        if opt in safe_env:
            del safe_env[opt]

    info = []
    try:
        # Compile the program.
        show('Compiling generated code...')
        subprocess.check_call(
            [shared.PYTHON, shared.EMCC] + cpp_opts + [
                '-o', js_file[1], src_file[1], '-s',
                'BOOTSTRAPPING_STRUCT_INFO=1', '-s',
                'WARN_ON_UNDEFINED_SYMBOLS=0', '-Oz', '--js-opts', '0',
                '--memory-init-file', '0'
            ],
            env=safe_env
        )  # -Oz optimizes enough to avoid warnings on code size/num locals
        # Run the compiled program.
        show('Calling generated program...')
        info = shared.run_js(js_file[1]).splitlines()
    except subprocess.CalledProcessError:
        if os.path.isfile(js_file[1]):
            sys.stderr.write('FAIL: Running the generated program failed!\n')
        else:
            sys.stderr.write('FAIL: Compilation failed!\n')

        sys.exit(1)
    finally:
        # Remove all temporary files.
        os.unlink(src_file[1])

        if os.path.exists(js_file[1]):
            os.unlink(js_file[1])

    # Parse the output of the program into a dict.
    return parse_c_output(info)
Пример #20
0
    #    try_js(['-s', 'SAFE_HEAP=1'])
    #  except Exception, e:
    #    print e
    #    js = shared.run_js(filename + '.js', stderr=PIPE, full_output=True)
    #  print js
    #  if 'SAFE_HEAP' in js:
    #    notes['safeheap'] += 1
    #  else:
    #    break

    # This is ok. Try in secondary JS engine too
    if engine2 and normal:
        try:
            js2 = shared.run_js(filename + '.js',
                                stderr=PIPE,
                                engine=engine2,
                                full_output=True,
                                check_timeout=True)
        except:
            print 'failed to run in secondary', js2
            break

        # asm.js testing
        if 'warning: Successfully compiled asm.js code' not in js2:
            print "ODIN VALIDATION BUG"
            notes['embug'] += 1
            fails += 1
            shutil.copyfile(filename + '.c', 'newfail%d.c' % fails)
            continue

        js2 = js2.replace('\nwarning: Successfully compiled asm.js code\n', '')
Пример #21
0
        print >> sys.stderr, 'cannot use WASM=1, no asm.js module to wasm-ify'
    sys.exit(1)
start = module.index('function')
end = module.rindex(')')
asm = module[start:end]
open(tempfile2, 'w').write(asm)

print 'run polyfill packer on', tempfile2
dir = os.getcwd()
try:
    os.chdir(os.path.dirname(os.path.abspath(tempfile2)))
    # try in spidermonkey first, because it's faster
    try:
        out = emscripten.run_js(
            pack_asmjs,
            args=[os.path.basename(tempfile2), 'output.binary'],
            stdout=PIPE,
            engine=emscripten.SPIDERMONKEY_ENGINE)
    except:
        out = emscripten.run_js(
            pack_asmjs,
            args=[os.path.basename(tempfile2), 'output.binary'],
            stdout=PIPE)
finally:
    os.chdir(dir)
out = json.loads(out)
open(wasmfile, 'wb').write(''.join(map(chr, out)))

print 'create patched out js'
js = open(jsfile).read()
patched = js.replace(asm, 'unwasmed')  # we assume the module is right there