From d6eaf0f603984593c76e30a59b1b29ee750486f4 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 27 Sep 2021 09:53:40 -0700 Subject: [PATCH] Remove shared.timeout_run (#15133) This function is not longer needed since python's builtin `subprocess.run` takes a `timeout` argument. Did my best to keep csmith_driver.py running, and it now at least does run successfully (prior to this change it had bitrotted). --- tests/fuzz/creduce_tester.py | 7 ++++--- tests/fuzz/csmith_driver.py | 27 +++++++++++---------------- tools/ctor_evaller.py | 11 +++-------- tools/shared.py | 20 -------------------- 4 files changed, 18 insertions(+), 47 deletions(-) diff --git a/tests/fuzz/creduce_tester.py b/tests/fuzz/creduce_tester.py index ebb68626677f6..2663871abf6a6 100755 --- a/tests/fuzz/creduce_tester.py +++ b/tests/fuzz/creduce_tester.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Copyright 2013 The Emscripten Authors. All rights reserved. # Emscripten is available under two separate licenses, the MIT license and the # University of Illinois/NCSA Open Source License. Both these licenses can be @@ -9,7 +9,8 @@ import os import sys -from subprocess import Popen, PIPE +import subprocess +from subprocess import PIPE sys.path += [os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'tools')] import shared @@ -28,7 +29,7 @@ print('2) Compile natively') shared.run_process([shared.CLANG_CC, '-O2', filename, '-o', obj_filename] + CSMITH_CFLAGS) print('3) Run natively') - correct = shared.timeout_run(Popen([obj_filename], stdout=PIPE, stderr=PIPE), 3) + correct = subprocess.run([obj_filename], stdout=PIPE, stderr=PIPE, timeout=3).stdout except Exception as e: print('Failed or infinite looping in native, skipping', e) sys.exit(1) # boring diff --git a/tests/fuzz/csmith_driver.py b/tests/fuzz/csmith_driver.py index 82dded02645ac..cb6213e736b11 100755 --- a/tests/fuzz/csmith_driver.py +++ b/tests/fuzz/csmith_driver.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Copyright 2013 The Emscripten Authors. All rights reserved. # Emscripten is available under two separate licenses, the MIT license and the # University of Illinois/NCSA Open Source License. Both these licenses can be @@ -13,15 +13,15 @@ import sys import shutil import random +import subprocess +from subprocess import check_call, PIPE, STDOUT, CalledProcessError from distutils.spawn import find_executable -from subprocess import check_call, Popen, PIPE, CalledProcessError script_dir = os.path.dirname(os.path.abspath(__file__)) sys.path.append(os.path.join(os.path.dirname(os.path.dirname(script_dir)))) from tools import shared from tools import config -from tools import utils # can add flags like --no-threads --ion-offthread-compile=off engine = eval('config.' + sys.argv[1]) if len(sys.argv) > 1 else config.JS_ENGINES[0] @@ -84,22 +84,17 @@ notes['invalid'] += 1 continue - shared.run_process([COMP, '-m32', opts, '-emit-llvm', '-c', fullname, '-o', filename + '.bc'] + CSMITH_CFLAGS + shared.get_cflags() + ['-w']) - shared.run_process([utils.path_from_root('tools/nativize_llvm.py'), filename + '.bc'], stderr=PIPE) - shutil.move(filename + '.bc.run', filename + '2') + shared.run_process([COMP, '-m32', opts, '-emit-llvm', '-c', fullname, '-o', filename + '.bc'] + CSMITH_CFLAGS + ['-w']) shared.run_process([COMP, fullname, '-o', filename + '3'] + CSMITH_CFLAGS + ['-w']) print('3) Run natively') try: - correct1 = shared.timeout_run(Popen([filename + '1'], stdout=PIPE, stderr=PIPE), 3) - if 'Segmentation fault' in correct1 or len(correct1) < 10: + correct1 = subprocess.run([filename + '1'], stdout=PIPE, stderr=STDOUT, timeout=3) + if b'Segmentation fault' in correct1.stdout or len(correct1.stdout) < 10: raise Exception('segfault') - correct2 = shared.timeout_run(Popen([filename + '2'], stdout=PIPE, stderr=PIPE), 3) - if 'Segmentation fault' in correct2 or len(correct2) < 10: + correct3 = subprocess.run([filename + '3'], stdout=PIPE, stderr=STDOUT, timeout=3) + if b'Segmentation fault' in correct3.stdout or len(correct3.stdout) < 10: raise Exception('segfault') - correct3 = shared.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: + if correct1.stdout != correct3.stdout: raise Exception('clang opts change result') except Exception as e: print('Failed or infinite looping in native, skipping', e) @@ -165,12 +160,12 @@ def try_js(args=[]): def execute_js(engine): print('(run in %s)' % engine) try: - js = shared.timeout_run(Popen(shared.NODE_JS + [filename + '.js'], stdout=PIPE, stderr=PIPE), 15 * 60) + js = subprocess.run(shared.NODE_JS + [filename + '.js'], stdout=PIPE, stderr=PIPE, timeout=15 * 60) except Exception: 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 + return correct1.stdout == js def fail(): global fails diff --git a/tools/ctor_evaller.py b/tools/ctor_evaller.py index 5a74c468ce22d..dfef685ae34f2 100755 --- a/tools/ctor_evaller.py +++ b/tools/ctor_evaller.py @@ -17,7 +17,7 @@ sys.path.insert(1, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from tools import shared, utils +from tools import utils js_file = sys.argv[1] @@ -68,16 +68,11 @@ def eval_ctors(js, wasm_file, num): if debug_info: cmd += ['-g'] logger.debug('wasm ctor cmd: ' + str(cmd)) - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) try: - err = shared.timeout_run(proc, timeout=10, full_output=True, check=False) - except Exception as e: - if 'Timed out' not in str(e): - raise + err = subprocess.run(cmd, stderr=subprocess.PIPE, timeout=10).stdout + except subprocess.TimeoutExpired: logger.debug('ctors timed out\n') return 0, js - if proc.returncode != 0: - shared.exit_with_error('unexpected error while trying to eval ctors:\n' + err) num_successful = err.count('success on') logger.debug(err) if len(ctors) == num_successful: diff --git a/tools/shared.py b/tools/shared.py index 4f6e4758aa999..c90b2c224bdc1 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -15,7 +15,6 @@ import re import shutil import subprocess -import time import signal import sys import tempfile @@ -234,25 +233,6 @@ def run_js_tool(filename, jsargs=[], *args, **kw): return check_call(command, *args, **kw).stdout -# Only used by tests and by ctor_evaller.py. Once fastcomp is removed -# this can most likely be moved into the tests/jsrun.py. -def timeout_run(proc, timeout=None, full_output=False, check=True): - start = time.time() - if timeout is not None: - while time.time() - start < timeout and proc.poll() is None: - time.sleep(0.1) - if proc.poll() is None: - proc.kill() # XXX bug: killing emscripten.py does not kill it's child process! - raise Exception("Timed out") - stdout, stderr = proc.communicate() - out = ['' if o is None else o for o in (stdout, stderr)] - if check and proc.returncode != 0: - raise subprocess.CalledProcessError(proc.returncode, '', stdout, stderr) - if TRACK_PROCESS_SPAWNS: - logging.info(f'Process {proc.pid} finished after {time.time() - start} seconds. Exit code: {proc.returncode}') - return '\n'.join(out) if full_output else out[0] - - def get_npm_cmd(name): if WINDOWS: cmd = [path_from_root('node_modules/.bin', name + '.cmd')]