2023-11-18 20:13:15 -07:00
|
|
|
import os
|
|
|
|
import random
|
2023-11-24 06:55:12 -07:00
|
|
|
import sys
|
2023-11-22 20:33:34 -07:00
|
|
|
import warnings
|
2023-11-21 08:07:34 -07:00
|
|
|
from contextlib import redirect_stdout
|
2023-11-17 16:57:53 -07:00
|
|
|
|
2023-11-18 20:13:15 -07:00
|
|
|
import yaml
|
2023-11-17 16:57:53 -07:00
|
|
|
|
2023-11-18 20:13:15 -07:00
|
|
|
import fuzzer as fz
|
|
|
|
import argparse
|
2023-11-17 16:57:53 -07:00
|
|
|
|
2023-11-18 20:13:15 -07:00
|
|
|
import xml.dom.minidom
|
|
|
|
import xml.etree.ElementTree as ET
|
2023-11-17 16:57:53 -07:00
|
|
|
|
2023-11-22 20:33:34 -07:00
|
|
|
from constants import NoneTagException
|
|
|
|
|
|
|
|
|
2023-11-24 09:31:18 -07:00
|
|
|
def gprint(expr, end=''):
|
|
|
|
if type(expr) is bool:
|
|
|
|
if expr:
|
|
|
|
print('T', end=end)
|
|
|
|
else:
|
|
|
|
print('F', end=end)
|
|
|
|
else:
|
|
|
|
print(expr, end=end)
|
|
|
|
|
|
|
|
|
2023-11-18 20:13:15 -07:00
|
|
|
class Fuzzer():
|
2023-11-19 16:13:08 -07:00
|
|
|
def __init__(self, config: str, batch: int, seed: str, file_name: str = "fuzz"):
|
2023-11-18 20:13:15 -07:00
|
|
|
with open(config) as yaml_file:
|
|
|
|
settings: dict = yaml.safe_load(yaml_file)
|
|
|
|
self.settings = settings
|
|
|
|
self.batch = batch
|
|
|
|
random.seed(seed)
|
2023-11-19 16:13:08 -07:00
|
|
|
self.file_name = file_name
|
2023-11-18 20:13:15 -07:00
|
|
|
|
|
|
|
self.fuzzer = fz.GazpreaFuzzer(config)
|
|
|
|
|
|
|
|
def fuzz(self):
|
2023-11-24 09:31:18 -07:00
|
|
|
base_dir = os.getcwd()
|
|
|
|
fuzzer_root = base_dir + '/fuzzer'
|
|
|
|
|
|
|
|
fuzzer_input = fuzzer_root + '/input/fuzzer'
|
|
|
|
fuzzer_debug = fuzzer_root + '/debug'
|
|
|
|
fuzzer_asterr = fuzzer_debug + '/ast_err'
|
|
|
|
fuzzer_instream = fuzzer_root + '/instream'
|
|
|
|
fuzzer_outputs = fuzzer_root + '/outputs/fuzzer'
|
|
|
|
fuzzer_ground_truth = fuzzer_root + '/ground_truth'
|
|
|
|
|
|
|
|
os.system(f"rm -rf {fuzzer_root}")
|
|
|
|
os.makedirs(fuzzer_root)
|
|
|
|
os.makedirs(fuzzer_input)
|
|
|
|
os.makedirs(fuzzer_debug)
|
|
|
|
os.makedirs(fuzzer_asterr)
|
|
|
|
os.makedirs(fuzzer_instream)
|
|
|
|
os.makedirs(fuzzer_outputs)
|
|
|
|
os.makedirs(fuzzer_ground_truth)
|
|
|
|
try:
|
|
|
|
os.system(f"cp tester_config.json {fuzzer_root}/tester_config.json")
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
|
|
|
|
|
|
|
i = 0
|
2023-11-24 15:45:38 -07:00
|
|
|
min_i = 0
|
2023-11-24 09:31:18 -07:00
|
|
|
while i < self.batch:
|
2023-11-22 20:33:34 -07:00
|
|
|
try:
|
|
|
|
self.fuzzer.fuzz()
|
|
|
|
except NoneTagException as n:
|
|
|
|
r = random.randint(0, 1000000)
|
|
|
|
warnings.warn("None Tag Exception encountered, writing stack trace and xml to debug/ast/{}.xml\n"
|
|
|
|
"Look for a top-level <argument> tag or send it to Ayrton and I'll see what I can see"
|
|
|
|
"".format(r))
|
2023-11-24 09:31:18 -07:00
|
|
|
with open(f"{fuzzer_asterr}/{r}.xml", 'w') as f:
|
2023-11-22 20:33:34 -07:00
|
|
|
f.write(xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8')).toprettyxml())
|
2023-11-24 15:45:38 -07:00
|
|
|
if i - 1 >= min_i:
|
|
|
|
i -= 1
|
|
|
|
else:
|
|
|
|
i = min_i
|
2023-11-22 20:33:34 -07:00
|
|
|
continue
|
2023-11-18 20:13:15 -07:00
|
|
|
dom = xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8'))
|
|
|
|
pretty: str = dom.toprettyxml()
|
2023-11-22 13:50:56 -07:00
|
|
|
|
2023-11-24 09:31:18 -07:00
|
|
|
with open("{}/{}_{}.py".format(fuzzer_ground_truth, self.file_name, i), 'w') as f:
|
2023-11-19 16:13:08 -07:00
|
|
|
f.write(self.fuzzer.ground_truth)
|
2023-11-24 09:31:18 -07:00
|
|
|
with open("{}/{}_{}.py".format(fuzzer_ground_truth, self.file_name, i), 'r') as f:
|
|
|
|
with open("{}/{}_{}.out".format(fuzzer_outputs, self.file_name, i), 'w') as y:
|
|
|
|
with redirect_stdout(y): # Workaround for fuzzer.py:49
|
2023-11-22 13:50:56 -07:00
|
|
|
try:
|
2023-11-24 09:31:18 -07:00
|
|
|
read = f.read()
|
|
|
|
exec(read, globals())
|
|
|
|
except (OverflowError, ZeroDivisionError, ValueError, TypeError, SyntaxError):
|
|
|
|
os.system("rm -f {}/{}_{}.py".format(fuzzer_ground_truth, self.file_name, i))
|
|
|
|
os.system("rm -f {}/{}_{}.py".format(fuzzer_outputs, self.file_name, i))
|
|
|
|
warnings.warn("Runtime error encountered, retrying")
|
2023-11-24 15:45:38 -07:00
|
|
|
if i - 1 >= min_i:
|
|
|
|
i -= 1
|
|
|
|
else:
|
|
|
|
i = min_i
|
2023-11-22 13:50:56 -07:00
|
|
|
continue
|
2023-11-24 06:55:12 -07:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
r = random.randint(0, 1000000)
|
|
|
|
warnings.warn("Execution halted, result written to debug/ast/{}.xml\n"
|
|
|
|
"".format(r))
|
2023-11-24 09:31:18 -07:00
|
|
|
with open("{}/{}.xml".format(fuzzer_asterr, r), 'w') as f:
|
|
|
|
f.write(xml.dom.minidom.parseString(
|
|
|
|
ET.tostring(self.fuzzer.ast).decode('utf-8')).toprettyxml())
|
2023-11-24 06:55:12 -07:00
|
|
|
sys.exit(1)
|
2023-11-24 09:31:18 -07:00
|
|
|
with open("{}/{}_{}.in".format(fuzzer_input, self.file_name, i), 'w') as f:
|
2023-11-22 13:50:56 -07:00
|
|
|
f.write(self.fuzzer.source)
|
2023-11-24 09:31:18 -07:00
|
|
|
with open("{}/{}_{}.xml".format(fuzzer_debug, self.file_name, i), 'w') as f:
|
2023-11-22 13:50:56 -07:00
|
|
|
f.write(pretty)
|
2023-11-24 09:31:18 -07:00
|
|
|
# y.write(self.fuzzer.out)
|
2023-11-18 20:13:15 -07:00
|
|
|
# with open("fuzzer/instream/{}.in".format(i), 'w') as f:
|
|
|
|
# f.write(self.fuzzer.source)
|
|
|
|
# with open("fuzzer/outputs/{}.out".format(i), 'w') as f:
|
|
|
|
# f.write(self.fuzzer.out)
|
2023-11-24 09:31:18 -07:00
|
|
|
i += 1
|
2023-11-24 15:45:38 -07:00
|
|
|
min_i = i
|
2023-11-17 16:57:53 -07:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-11-18 20:13:15 -07:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Procedurally generate a test case for Gazprea'
|
|
|
|
)
|
2023-11-20 20:28:55 -07:00
|
|
|
parser.add_argument('-b', '--batch_size', type=int, required=False, default=1,
|
2023-11-19 11:05:52 -07:00
|
|
|
help="generate BATCH cases (fuzzer/source/nameX.in, /instream/..., /outputs/...)")
|
2023-11-18 20:13:15 -07:00
|
|
|
parser.add_argument('--seed', type=int, required=False, action="store",
|
|
|
|
help="rng seed")
|
|
|
|
parser.add_argument('config_file', type=str, action="store",
|
|
|
|
help="path to your configuration file")
|
2023-11-19 16:13:08 -07:00
|
|
|
parser.add_argument('file_name', type=str, action="store",
|
|
|
|
help="name for the generated files")
|
2023-11-18 20:13:15 -07:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
fuzzer = Fuzzer(config=args.config_file, batch=args.batch_size, seed=args.seed)
|
|
|
|
fuzzer.fuzz()
|