gazprea-fuzzer-python/fuzzer.py
2023-11-19 16:13:08 -07:00

56 lines
1.6 KiB
Python

import sys
from io import StringIO
import xml
import yaml
import ast_parser
from ast_generator.ast_generator import *
from ast_parser.gaz_unparser import *
from ast_parser.python_unparser import PythonUnparser
import inspect
class GazpreaFuzzer:
def __init__(self, config: str):
# parse a yaml config file with path in config
# and set the appropriate parameters
with open(config) as yaml_file:
settings: dict = yaml.safe_load(yaml_file)
self.settings = settings
self.gaz_source_gen = None
self.generator = AstGenerator(settings)
self.ast = None
self.source = None
self.ground_truth = None
self.out = None
def fuzz(self):
self.generator.generate_ast()
self.write_ast()
self.gaz_source_gen = GazUnparser(self.generator.ast, True)
self.gaz_source_gen.unparse()
self.ast = self.generator.ast
self.source = self.gaz_source_gen.source
self.python_source_gen = PythonUnparser(self.generator.ast, True)
self.python_source_gen.unparse()
self.ground_truth = self.python_source_gen.source
output = StringIO()
sys.stdout = output
exec(self.ground_truth)
self.out = output.getvalue()
def write_ast(self):
dom = xml.dom.minidom.parseString(ET.tostring(self.generator.ast).decode('utf-8'))
pretty: str = dom.toprettyxml()
randint = random.randint(0, 1000)
print(randint)
with open("debug/ast/debug_{}.xml".format(randint), 'w') as f:
f.write(pretty)