Skip to content

Commit

Permalink
Remove shared.timeout_run (emscripten-core#15133)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
sbc100 authored Sep 27, 2021
1 parent 7577eab commit d6eaf0f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 47 deletions.
7 changes: 4 additions & 3 deletions tests/fuzz/creduce_tester.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
27 changes: 11 additions & 16 deletions tests/fuzz/csmith_driver.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions tools/ctor_evaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand Down
20 changes: 0 additions & 20 deletions tools/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import re
import shutil
import subprocess
import time
import signal
import sys
import tempfile
Expand Down Expand Up @@ -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')]
Expand Down

0 comments on commit d6eaf0f

Please sign in to comment.