forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·86 lines (76 loc) · 2.74 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
import argparse
import logging
import os
import re
import subprocess
from concurrent.futures import ThreadPoolExecutor, as_completed
def job(command):
"""Takes a list of str, and runs it as a subprocess."""
command_line = " ".join(command)
logging.debug(f"Running '{command_line}'\n")
res = subprocess.run(command, check=True, stderr=subprocess.PIPE,
universal_newlines=True).stderr
logging.debug(f"Command '{command_line} output:\n'{res}'")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Run the fuzz targets a given amount of times, or generate"
" new seeds.",
)
parser.add_argument(
"seed_dir",
help="The parent directory for the seed corpora of each target.",
)
parser.add_argument("-g", "--generate", action="store_true")
parser.add_argument(
"-j",
"--par",
type=int,
default=1,
help="How many targets to run in parallel.",
)
parser.add_argument(
"-n",
"--runs",
default=100000,
help="How many times to run each target (if generating).",
)
parser.add_argument(
"-m",
"--merge_dir",
default=None,
help="The parent directory to merge each target corpus from.",
)
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG)
target_dir = os.path.abspath(os.path.dirname(__file__))
targets = [os.path.join(target_dir, f) for f in os.listdir(target_dir)
if re.compile(r"^fuzz-[\w-]*$").findall(f)]
with ThreadPoolExecutor(max_workers=args.par) as pool:
jobs = []
runs = args.runs if args.generate else 1
for target in targets:
seed_dir = os.path.join(args.seed_dir, os.path.basename(target))
os.makedirs(seed_dir, exist_ok=True)
command = [
target,
f"-runs={runs}",
seed_dir,
]
if args.merge_dir is not None:
input_target = os.path.join(args.merge_dir,
os.path.basename(target))
if not os.path.exists(input_target):
continue
command = [
target,
"-merge=1",
"-shuffle=0",
"-prefer_small=1",
"-use_value_profile=1", # Also used by OSS-Fuzz: https://github.com/google/oss-fuzz/issues/1406#issuecomment-387790487
seed_dir,
input_target,
]
jobs.append(pool.submit(job, command))
for completed in as_completed(jobs):
completed.result()