gazprea-fuzzer-python/fuzzer.py

61 lines
1.9 KiB
Python
Raw Normal View History

import io
import sys
from contextlib import redirect_stdout
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
2023-11-17 16:57:53 -07:00
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() # FIXME sometimes this is none
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
# input = "if __name__ == '__main__':\n while True:\n pass\n" # debug
with redirect_stdout(io.StringIO()) as buf:
# exec(str(self.ground_truth))
exec(self.ground_truth, globals(), locals()) # FIXME the exec doesn't actually execute for some reason...
self.out = buf.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)