2023-11-18 20:13:15 -07:00
|
|
|
import os
|
|
|
|
import random
|
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-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-19 16:13:08 -07:00
|
|
|
os.system("rm -rf fuzzer")
|
|
|
|
os.mkdir("fuzzer")
|
|
|
|
os.mkdir("fuzzer/input")
|
|
|
|
os.mkdir("fuzzer/debug")
|
|
|
|
os.mkdir("fuzzer/instream")
|
|
|
|
os.mkdir("fuzzer/outputs")
|
|
|
|
os.mkdir("fuzzer/ground_truth")
|
2023-11-18 20:13:15 -07:00
|
|
|
for i in range(self.batch):
|
|
|
|
self.fuzzer.fuzz()
|
|
|
|
dom = xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8'))
|
|
|
|
pretty: str = dom.toprettyxml()
|
2023-11-19 16:52:14 -07:00
|
|
|
with open("fuzzer/input/{}_{}.in".format(self.file_name, i), 'w') as f:
|
2023-11-18 20:13:15 -07:00
|
|
|
f.write(self.fuzzer.source)
|
2023-11-19 16:13:08 -07:00
|
|
|
with open("fuzzer/debug/{}_{}.out".format(self.file_name, i), 'w') as f:
|
2023-11-18 20:13:15 -07:00
|
|
|
f.write(pretty)
|
2023-11-19 16:13:08 -07:00
|
|
|
with open("fuzzer/ground_truth/{}_{}.py".format(self.file_name, i), 'w') as f:
|
|
|
|
f.write(self.fuzzer.ground_truth)
|
2023-11-21 08:07:34 -07:00
|
|
|
with open("fuzzer/ground_truth/{}_{}.py".format(self.file_name, i), 'r') as f:
|
|
|
|
with open("fuzzer/outputs/{}_{}.out".format(self.file_name, i), 'w') as y:
|
|
|
|
with redirect_stdout(y): # Workaround for fuzzer.py:49
|
|
|
|
exec(f.read(), globals(), locals())
|
|
|
|
# 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-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()
|
2023-11-17 16:57:53 -07:00
|
|
|
|
|
|
|
|