2019-04-04 21:04:49 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import re
|
|
|
|
import glob
|
2019-04-29 09:14:16 +00:00
|
|
|
import threading
|
|
|
|
import time
|
2019-04-04 21:04:49 +00:00
|
|
|
|
|
|
|
DESCRIPTION = """Regressor is a tool to run regression tests in a CI env."""
|
|
|
|
|
2021-01-21 15:17:01 +00:00
|
|
|
class PrintDotsThread:
|
2019-04-29 09:14:16 +00:00
|
|
|
"""Prints a dot every "interval" (default is 300) seconds"""
|
|
|
|
|
|
|
|
def __init__(self, interval=300):
|
|
|
|
self.interval = interval
|
|
|
|
|
|
|
|
thread = threading.Thread(target=self.run, args=())
|
|
|
|
thread.daemon = True
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
""" Runs until the main Python thread exits. """
|
|
|
|
## Print a newline at the very beginning.
|
|
|
|
print("")
|
|
|
|
while True:
|
|
|
|
# Print dot
|
|
|
|
print(".")
|
|
|
|
time.sleep(self.interval)
|
2019-04-04 21:04:49 +00:00
|
|
|
|
2021-01-21 15:17:01 +00:00
|
|
|
class regressor:
|
2020-02-18 07:18:21 +00:00
|
|
|
_re_sanitizer_log = re.compile(r"""ERROR: (libFuzzer|UndefinedBehaviorSanitizer)""")
|
2019-04-04 21:04:49 +00:00
|
|
|
|
|
|
|
def __init__(self, description, args):
|
|
|
|
self._description = description
|
|
|
|
self._args = self.parseCmdLine(description, args)
|
|
|
|
self._repo_root = os.path.dirname(sys.path[0])
|
|
|
|
self._fuzzer_path = os.path.join(self._repo_root,
|
|
|
|
"build/test/tools/ossfuzz")
|
|
|
|
self._logpath = os.path.join(self._repo_root, "test_results")
|
|
|
|
|
2021-12-21 14:23:52 +00:00
|
|
|
@classmethod
|
|
|
|
def parseCmdLine(cls, description, args):
|
2019-04-04 21:04:49 +00:00
|
|
|
argParser = ArgumentParser(description)
|
|
|
|
argParser.add_argument('-o', '--out-dir', required=True, type=str,
|
2020-02-18 07:18:21 +00:00
|
|
|
help="""Directory where test results will be written""")
|
2019-04-04 21:04:49 +00:00
|
|
|
return argParser.parse_args(args)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def run_cmd(command, logfile=None, env=None):
|
2019-04-29 10:38:11 +00:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
command (str): command to run
|
|
|
|
logfile (str): log file name
|
|
|
|
env (dict): dictionary holding key-value pairs for bash environment
|
|
|
|
variables
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int: The exit status of the command. Exit status codes are:
|
|
|
|
0 -> Success
|
|
|
|
1-255 -> Failure
|
|
|
|
"""
|
2019-04-04 21:04:49 +00:00
|
|
|
if not logfile:
|
|
|
|
logfile = os.devnull
|
|
|
|
|
|
|
|
if not env:
|
|
|
|
env = os.environ.copy()
|
|
|
|
|
2021-08-23 16:22:51 +00:00
|
|
|
with open(logfile, 'w', encoding='utf8') as logfh:
|
2021-04-26 04:33:14 +00:00
|
|
|
with subprocess.Popen(command, shell=True, executable='/bin/bash',
|
|
|
|
env=env, stdout=logfh,
|
|
|
|
stderr=subprocess.STDOUT) as proc:
|
|
|
|
ret = proc.wait()
|
|
|
|
logfh.close()
|
|
|
|
return ret
|
2019-04-04 21:04:49 +00:00
|
|
|
|
|
|
|
def process_log(self, logfile):
|
2019-04-29 10:38:11 +00:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
logfile (str): log file name
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: Test status.
|
|
|
|
True -> Success
|
|
|
|
False -> Failure
|
|
|
|
"""
|
|
|
|
|
2019-04-26 09:47:46 +00:00
|
|
|
## Log may contain non ASCII characters, so we simply stringify them
|
|
|
|
## since they don't matter for regular expression matching
|
2021-08-23 16:22:51 +00:00
|
|
|
with open(logfile, 'rb', encoding=None) as f:
|
2021-06-30 08:21:41 +00:00
|
|
|
rawtext = str(f.read())
|
2020-02-18 07:18:21 +00:00
|
|
|
return not re.search(self._re_sanitizer_log, rawtext)
|
2019-04-04 21:04:49 +00:00
|
|
|
|
|
|
|
def run(self):
|
2019-04-29 10:38:11 +00:00
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
bool: Test status.
|
|
|
|
True -> All tests succeeded
|
|
|
|
False -> At least one test failed
|
|
|
|
"""
|
|
|
|
|
|
|
|
testStatus = []
|
2021-09-16 17:22:23 +00:00
|
|
|
for fuzzer in glob.iglob(f"{self._fuzzer_path}/*_ossfuzz"):
|
2019-04-04 21:04:49 +00:00
|
|
|
basename = os.path.basename(fuzzer)
|
2021-09-16 17:22:23 +00:00
|
|
|
logfile = os.path.join(self._logpath, f"{basename}.log")
|
|
|
|
corpus_dir = f"/tmp/solidity-fuzzing-corpus/{basename}_seed_corpus"
|
|
|
|
cmd = f"find {corpus_dir} -type f | xargs -n1 sh -c '{fuzzer} $0 || exit 255'"
|
2020-02-18 07:18:21 +00:00
|
|
|
self.run_cmd(cmd, logfile=logfile)
|
|
|
|
ret = self.process_log(logfile)
|
|
|
|
if not ret:
|
|
|
|
print(
|
2021-09-16 17:22:23 +00:00
|
|
|
f"\t[-] libFuzzer reported failure for {basename}. "
|
|
|
|
"Failure logged to test_results")
|
2020-02-18 07:18:21 +00:00
|
|
|
testStatus.append(False)
|
2019-04-04 21:04:49 +00:00
|
|
|
else:
|
2021-09-16 17:22:23 +00:00
|
|
|
print(f"\t[+] {basename} passed regression tests.")
|
2020-02-18 07:18:21 +00:00
|
|
|
testStatus.append(True)
|
|
|
|
return all(testStatus)
|
2019-04-04 21:04:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-04-29 09:14:16 +00:00
|
|
|
dotprinter = PrintDotsThread()
|
2019-04-04 21:04:49 +00:00
|
|
|
tool = regressor(DESCRIPTION, sys.argv[1:])
|
2020-02-18 07:18:21 +00:00
|
|
|
sys.exit(not tool.run())
|