def try_js(args=[]): shared.try_delete(filename + '.js') js_args = [shared.PYTHON, shared.EMCC, opts] + llvm_opts + [fullname, '-o', filename + '.js'] + CSMITH_CFLAGS + args + ['-w'] if random.random() < 0.5: js_args += ['-s', 'ALLOW_MEMORY_GROWTH=1'] if random.random() < 0.5 and 'ALLOW_MEMORY_GROWTH=1' not in js_args: js_args += ['-s', 'MAIN_MODULE=1'] if random.random() < 0.25: js_args += ['-s', 'INLINING_LIMIT=1'] # inline nothing, for more call interaction if random.random() < 0.333: js_args += ['-s', 'EMTERPRETIFY=1'] if random.random() < 0.5: if random.random() < 0.5: js_args += ['-s', 'EMTERPRETIFY_BLACKLIST=["_main"]'] # blacklist main and all inlined into it, but interpret the rest, tests mixing else: js_args += ['-s', 'EMTERPRETIFY_WHITELIST=["_main"]'] # the opposite direction if random.random() < 0.5: js_args += ['-s', 'EMTERPRETIFY_ASYNC=1'] if random.random() < 0.5: js_args += ["--memory-init-file", "0", "-s", "MEM_INIT_METHOD=2"] if random.random() < 0.5: js_args += ['-s', 'ASSERTIONS=1'] print '(compile)', ' '.join(js_args) open(fullname, 'a').write('\n// ' + ' '.join(js_args) + '\n\n') try: shared.check_execute(js_args) assert os.path.exists(filename + '.js') return js_args except: return False
def try_js(args=[]): shared.try_delete(filename + '.js') js_args = [shared.PYTHON, shared.EMCC, opts] + llvm_opts + [ fullname, '-o', filename + '.js' ] + CSMITH_CFLAGS + args if random.random() < 0.5: js_args += ['-s', 'ALLOW_MEMORY_GROWTH=1'] if random.random() < 0.25: js_args += ['-s', 'INLINING_LIMIT=1' ] # inline nothing, for more call interaction if random.random() < 0.333: js_args += ['-s', 'EMTERPRETIFY=1'] if random.random() < 0.5: if random.random() < 0.5: js_args += [ '-s', 'EMTERPRETIFY_BLACKLIST=["_main"]' ] # blacklist main and all inlined into it, but interpret the rest, tests mixing else: js_args += ['-s', 'EMTERPRETIFY_WHITELIST=["_main"]' ] # the opposite direction if random.random() < 0.5: js_args += ['-s', 'EMTERPRETIFY_ASYNC=1'] print '(compile)', ' '.join(js_args) open(fullname, 'a').write('\n// ' + ' '.join(js_args) + '\n\n') try: shared.check_execute(js_args) assert os.path.exists(filename + '.js') return True except: return False
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')])
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')])
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')])
def try_js(args=[]): shared.try_delete(filename + '.js') js_args = [shared.PYTHON, shared.EMCC, opts] + llvm_opts + [ fullname, '-o', filename + '.js' ] + CSMITH_CFLAGS + args + ['-w'] if random.random() < 0.5: js_args += ['-s', 'ALLOW_MEMORY_GROWTH=1'] if random.random() < 0.5 and 'ALLOW_MEMORY_GROWTH=1' not in js_args: js_args += ['-s', 'MAIN_MODULE=1'] if random.random() < 0.25: js_args += ['-s', 'INLINING_LIMIT=1' ] # inline nothing, for more call interaction if random.random() < 0.333: js_args += ['-s', 'EMTERPRETIFY=1'] if random.random() < 0.5: if random.random() < 0.5: js_args += [ '-s', 'EMTERPRETIFY_BLACKLIST=["_main"]' ] # blacklist main and all inlined into it, but interpret the rest, tests mixing else: js_args += ['-s', 'EMTERPRETIFY_WHITELIST=["_main"]' ] # the opposite direction if random.random() < 0.5: js_args += ['-s', 'EMTERPRETIFY_ASYNC=1'] if random.random() < 0.5: js_args += ["--memory-init-file", "0", "-s", "MEM_INIT_METHOD=2"] if random.random() < 0.5: js_args += ['-s', 'ASSERTIONS=1'] #js_args += ['-s', 'BINARYEN=1'] #if random.random() < 0.333: # js_args += ['-s', 'BINARYEN_METHOD="interpret-s-expr"'] #elif random.random() < 0.5: # js_args += ['-s', 'BINARYEN_METHOD="interpret-binary"'] #else: # js_args += ['-s', 'BINARYEN_METHOD="interpret-asm2wasm"'] print '(compile)', ' '.join(js_args) open(fullname, 'a').write('\n// ' + ' '.join(js_args) + '\n\n') try: shared.check_execute(js_args) assert os.path.exists(filename + '.js') return js_args except: return False
def try_js(args=[]): shared.try_delete(filename + '.js') js_args = [shared.PYTHON, shared.EMCC, fullname, '-o', filename + '.js'] + [opts] + llvm_opts + CSMITH_CFLAGS + args + ['-w'] if 0: # binaryen testing, off by default for now js_args += ['-s', 'BINARYEN=1'] r = random.random() if r < 0.45: js_args += ['-s', 'BINARYEN_METHOD="interpret-s-expr"'] elif r < 0.90: js_args += ['-s', 'BINARYEN_METHOD="interpret-binary"'] else: if random.random() < 0.5: js_args += ['-s', 'BINARYEN_METHOD="interpret-binary,asmjs"'] else: js_args += ['-s', 'BINARYEN_METHOD="interpret-s-expr,asmjs"'] if random.random() < 0.5: if random.random() < 0.5: js_args += ['--js-opts', '0'] else: js_args += ['--js-opts', '1'] if random.random() < 0.5: # pick random passes BINARYEN_PASSES = [ "duplicate-function-elimination", "dce", "remove-unused-brs", "remove-unused-names", "optimize-instructions", "precompute", "simplify-locals", "vacuum", "coalesce-locals", "reorder-locals", "merge-blocks", "remove-unused-functions", ] passes = [] while 1: passes.append(random.choice(BINARYEN_PASSES)) if random.random() < 0.1: break js_args += ['-s', 'BINARYEN_PASSES="' + ','.join(passes) + '"'] if random.random() < 0.5: js_args += ['-s', 'ALLOW_MEMORY_GROWTH=1'] if random.random() < 0.5 and 'ALLOW_MEMORY_GROWTH=1' not in js_args and 'BINARYEN=1' not in js_args: js_args += ['-s', 'MAIN_MODULE=1'] if random.random() < 0.25: js_args += ['-s', 'INLINING_LIMIT=1'] # inline nothing, for more call interaction if random.random() < 0.333: js_args += ['-s', 'EMTERPRETIFY=1'] if random.random() < 0.5: if random.random() < 0.5: js_args += ['-s', 'EMTERPRETIFY_BLACKLIST=["_main"]'] # blacklist main and all inlined into it, but interpret the rest, tests mixing else: js_args += ['-s', 'EMTERPRETIFY_WHITELIST=["_main"]'] # the opposite direction if random.random() < 0.5: js_args += ['-s', 'EMTERPRETIFY_ASYNC=1'] if random.random() < 0.5: js_args += ["--memory-init-file", "0", "-s", "MEM_INIT_METHOD=2"] if random.random() < 0.5: js_args += ['-s', 'ASSERTIONS=1'] print '(compile)', ' '.join(js_args) short_args = [shared.PYTHON, shared.EMCC, fail_output_name] + js_args[5:] escaped_short_args = map(lambda x : ("'" + x + "'") if '"' in x else x, short_args) open(fullname, 'a').write('\n// ' + ' '.join(escaped_short_args) + '\n\n') try: shared.check_execute(js_args) assert os.path.exists(filename + '.js') return js_args except: return False
COMP = shared.CLANG print COMP, extra_args fullname = filename + suffix check_call( [CSMITH, '--no-volatiles', '--no-packed-struct'] + extra_args, #['--max-block-depth', '2', '--max-block-size', '2', '--max-expr-complexity', '2', '--max-funcs', '2'], stdout=open(fullname, 'w')) print '1) Generate source... %.2f K' % (len(open(fullname).read()) / 1024.) tried += 1 print '2) Compile natively' shared.try_delete(filename) try: shared.check_execute( [COMP, '-m32', opts, fullname, '-o', filename + '1'] + CSMITH_CFLAGS + ['-w']) # + shared.EMSDK_OPTS except Exception, e: print 'Failed to compile natively using clang' notes['invalid'] += 1 continue shared.check_execute([ COMP, '-m32', opts, '-emit-llvm', '-c', fullname, '-o', filename + '.bc' ] + CSMITH_CFLAGS + shared.EMSDK_OPTS) shared.check_execute( [shared.path_from_root('tools', 'nativize_llvm.py'), filename + '.bc']) shutil.move(filename + '.bc.run', filename + '2') shared.check_execute([COMP, fullname, '-o', filename + '3'] + CSMITH_CFLAGS)
opts = '-O' + str(random.randint(0, 2)) print 'opt level:', opts print 'Tried %d, notes: %s' % (tried, notes) print '1) Generate C' check_call([CSMITH, '--no-volatiles', '--no-packed-struct'],# '--no-math64' #['--max-block-depth', '2', '--max-block-size', '2', '--max-expr-complexity', '2', '--max-funcs', '2'], stdout=open(filename + '.c', 'w')) #shutil.copyfile(filename + '.c', 'testcase%d.c' % tried) print '1) Generate C... %.2f K of C source' % (len(open(filename + '.c').read())/1024.) tried += 1 print '2) Compile natively' shared.try_delete(filename) shared.check_execute([shared.CLANG_CC, opts, filename + '.c', '-o', filename + '1'] + CSMITH_CFLAGS) # + shared.EMSDK_OPTS shared.check_execute([shared.CLANG_CC, opts, '-emit-llvm', '-c', '-Xclang', '-triple=i386-pc-linux-gnu', filename + '.c', '-o', filename + '.bc'] + CSMITH_CFLAGS + shared.EMSDK_OPTS) shared.check_execute([shared.path_from_root('tools', 'nativize_llvm.py'), filename + '.bc']) shutil.move(filename + '.bc.run', filename + '2') shared.check_execute([shared.CLANG_CC, filename + '.c', '-o', filename + '3'] + CSMITH_CFLAGS) print '3) Run natively' try: correct1 = shared.jsrun.timeout_run(Popen([filename + '1'], stdout=PIPE, stderr=PIPE), 3) if 'Segmentation fault' in correct1 or len(correct1) < 10: raise Exception('segfault') correct2 = shared.jsrun.timeout_run(Popen([filename + '2'], stdout=PIPE, stderr=PIPE), 3) if 'Segmentation fault' in correct2 or len(correct2) < 10: raise Exception('segfault') correct3 = shared.jsrun.timeout_run(Popen([filename + '3'], stdout=PIPE, stderr=PIPE), 3) if 'Segmentation fault' in correct3 or len(correct3) < 10: raise Exception('segfault') if correct1 != correct3: raise Exception('clang opts change result') except Exception, e: print 'Failed or infinite looping in native, skipping', e
print 'Tried %d, notes: %s' % (tried, notes) print '1) Generate C' check_call( [CSMITH, '--no-volatiles', '--no-math64', '--no-packed-struct'], # + #['--max-block-depth', '2', '--max-block-size', '2', '--max-expr-complexity', '2', '--max-funcs', '2'], stdout=open(filename + '.c', 'w')) #shutil.copyfile(filename + '.c', 'testcase%d.c' % tried) print '1) Generate C... %.2f K of C source' % ( len(open(filename + '.c').read()) / 1024.) tried += 1 print '2) Compile natively' shared.try_delete(filename) shared.check_execute( [shared.CLANG_CC, '-O2', filename + '.c', '-o', filename + '1'] + CSMITH_CFLAGS) # + shared.EMSDK_OPTS shared.check_execute([ shared.CLANG_CC, '-O2', '-emit-llvm', '-c', '-Xclang', '-triple=i386-pc-linux-gnu', filename + '.c', '-o', filename + '.bc' ] + CSMITH_CFLAGS + shared.EMSDK_OPTS) shared.check_execute( [shared.path_from_root('tools', 'nativize_llvm.py'), filename + '.bc']) shutil.move(filename + '.bc.run', filename + '2') shared.check_execute( [shared.CLANG_CC, filename + '.c', '-o', filename + '3'] + CSMITH_CFLAGS) print '3) Run natively' try: correct1 = shared.jsrun.timeout_run( Popen([filename + '1'], stdout=PIPE, stderr=PIPE), 3)
# creduce will only pass the filename of the C file as the first arg, so other # configuration options will have to be hardcoded. CSMITH_CFLAGS = ['-I', os.path.join(os.environ['CSMITH_PATH'], 'runtime')] ENGINE = shared.JS_ENGINES[0] EMCC_ARGS = ['-O2', '-s', 'ASM_JS=1', '-s', 'PRECISE_I64_MATH=1', '-s', 'PRECISE_I32_MUL=1'] filename = sys.argv[1] obj_filename = os.path.splitext(filename)[0] js_filename = obj_filename + '.js' print 'testing file', filename try: print '2) Compile natively' shared.check_execute([shared.CLANG_CC, '-O2', filename, '-o', obj_filename] + CSMITH_CFLAGS) print '3) Run natively' correct = jsrun.timeout_run(Popen([obj_filename], stdout=PIPE, stderr=PIPE), 3) except Exception, e: print 'Failed or infinite looping in native, skipping', e sys.exit(1) # boring print '4) Compile JS-ly and compare' 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 # Try normally, then try unaligned because csmith does generate nonportable code that requires x86 alignment
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
# creduce will only pass the filename of the C file as the first arg, so other # configuration options will have to be hardcoded. CSMITH_CFLAGS = ['-I', os.path.join(os.environ['CSMITH_PATH'], 'runtime')] ENGINE = shared.JS_ENGINES[0] EMCC_ARGS = ['-O2', '-s', 'ASM_JS=1', '-s', 'PRECISE_I64_MATH=1', '-s', 'PRECISE_I32_MUL=1'] filename = sys.argv[1] obj_filename = os.path.splitext(filename)[0] js_filename = obj_filename + '.js' print 'testing file', filename try: print '2) Compile natively' shared.check_execute([shared.CLANG_CC, '-O2', filename, '-o', obj_filename] + CSMITH_CFLAGS) print '3) Run natively' correct = jsrun.timeout_run(Popen([obj_filename], stdout=PIPE, stderr=PIPE), 3) except Exception, e: print 'Failed or infinite looping in native, skipping', e sys.exit(1) # boring print '4) Compile JS-ly and compare' 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 # Try normally, then try unaligned because csmith does generate nonportable code that requires x86 alignment
suffix += 'pp' COMP = shared.CLANG print COMP, extra_args fullname = filename + suffix check_call( [CSMITH, '--no-volatiles', '--no-packed-struct'] + extra_args, #['--max-block-depth', '2', '--max-block-size', '2', '--max-expr-complexity', '2', '--max-funcs', '2'], stdout=open(fullname, 'w')) print '1) Generate source... %.2f K' % (len(open(fullname).read()) / 1024.) tried += 1 print '2) Compile natively' shared.try_delete(filename) try: shared.check_execute([COMP, opts, fullname, '-o', filename + '1'] + CSMITH_CFLAGS + ['-w']) # + shared.EMSDK_OPTS except Exception, e: print 'Failed to compile natively using clang' notes['invalid'] += 1 continue shared.check_execute([ COMP, opts, '-emit-llvm', '-c', '-Xclang', '-triple=i386-pc-linux-gnu', fullname, '-o', filename + '.bc' ] + CSMITH_CFLAGS + shared.EMSDK_OPTS) shared.check_execute( [shared.path_from_root('tools', 'nativize_llvm.py'), filename + '.bc']) shutil.move(filename + '.bc.run', filename + '2') shared.check_execute([COMP, fullname, '-o', filename + '3'] + CSMITH_CFLAGS) print '3) Run natively'
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
extra_args += ['--lang-cpp'] suffix += 'pp' COMP = shared.CLANG print COMP, extra_args fullname = filename + suffix check_call([CSMITH, '--no-volatiles', '--no-packed-struct'] + extra_args, #['--max-block-depth', '2', '--max-block-size', '2', '--max-expr-complexity', '2', '--max-funcs', '2'], stdout=open(fullname, 'w')) print '1) Generate source... %.2f K' % (len(open(fullname).read())/1024.) tried += 1 print '2) Compile natively' shared.try_delete(filename) try: shared.check_execute([COMP, '-m32', opts, fullname, '-o', filename + '1'] + CSMITH_CFLAGS + ['-w']) # + shared.EMSDK_OPTS except Exception, e: print 'Failed to compile natively using clang' notes['invalid'] += 1 continue shared.check_execute([COMP, '-m32', opts, '-emit-llvm', '-c', fullname, '-o', filename + '.bc'] + CSMITH_CFLAGS + shared.EMSDK_OPTS) shared.check_execute([shared.path_from_root('tools', 'nativize_llvm.py'), filename + '.bc']) shutil.move(filename + '.bc.run', filename + '2') shared.check_execute([COMP, fullname, '-o', filename + '3'] + CSMITH_CFLAGS) print '3) Run natively' try: correct1 = shared.jsrun.timeout_run(Popen([filename + '1'], stdout=PIPE, stderr=PIPE), 3) if 'Segmentation fault' in correct1 or len(correct1) < 10: raise Exception('segfault') correct2 = shared.jsrun.timeout_run(Popen([filename + '2'], stdout=PIPE, stderr=PIPE), 3) if 'Segmentation fault' in correct2 or len(correct2) < 10: raise Exception('segfault')
def try_js(args=[]): shared.try_delete(filename + '.js') js_args = [ shared.PYTHON, shared.EMCC, fullname, '-o', filename + '.js' ] + [opts] + llvm_opts + CSMITH_CFLAGS + args + ['-w'] if 0: # binaryen testing, off by default for now js_args += ['-s', 'BINARYEN=1'] r = random.random() if r < 0.45: js_args += ['-s', 'BINARYEN_METHOD="interpret-s-expr"'] elif r < 0.90: js_args += ['-s', 'BINARYEN_METHOD="interpret-binary"'] else: if random.random() < 0.5: js_args += [ '-s', 'BINARYEN_METHOD="interpret-binary,asmjs"' ] else: js_args += [ '-s', 'BINARYEN_METHOD="interpret-s-expr,asmjs"' ] if random.random() < 0.5: if random.random() < 0.5: js_args += ['--js-opts', '0'] else: js_args += ['--js-opts', '1'] if random.random() < 0.5: # pick random passes BINARYEN_PASSES = [ "duplicate-function-elimination", "dce", "remove-unused-brs", "remove-unused-names", "optimize-instructions", "precompute", "simplify-locals", "vacuum", "coalesce-locals", "reorder-locals", "merge-blocks", "remove-unused-functions", ] passes = [] while 1: passes.append(random.choice(BINARYEN_PASSES)) if random.random() < 0.1: break js_args += ['-s', 'BINARYEN_PASSES="' + ','.join(passes) + '"'] if random.random() < 0.5: js_args += ['-s', 'ALLOW_MEMORY_GROWTH=1'] if random.random( ) < 0.5 and 'ALLOW_MEMORY_GROWTH=1' not in js_args and 'BINARYEN=1' not in js_args: js_args += ['-s', 'MAIN_MODULE=1'] if random.random() < 0.25: js_args += ['-s', 'INLINING_LIMIT=1' ] # inline nothing, for more call interaction if random.random() < 0.333: js_args += ['-s', 'EMTERPRETIFY=1'] if random.random() < 0.5: if random.random() < 0.5: js_args += [ '-s', 'EMTERPRETIFY_BLACKLIST=["_main"]' ] # blacklist main and all inlined into it, but interpret the rest, tests mixing else: js_args += ['-s', 'EMTERPRETIFY_WHITELIST=["_main"]' ] # the opposite direction if random.random() < 0.5: js_args += ['-s', 'EMTERPRETIFY_ASYNC=1'] if random.random() < 0.5: js_args += ["--memory-init-file", "0", "-s", "MEM_INIT_METHOD=2"] if random.random() < 0.5: js_args += ['-s', 'ASSERTIONS=1'] print '(compile)', ' '.join(js_args) short_args = [shared.PYTHON, shared.EMCC, fail_output_name ] + js_args[5:] escaped_short_args = map(lambda x: ("'" + x + "'") if '"' in x else x, short_args) open(fullname, 'a').write('\n// ' + ' '.join(escaped_short_args) + '\n\n') try: shared.check_execute(js_args) assert os.path.exists(filename + '.js') return js_args except: return False