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 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 try: os.makedirs('debug/ast') except FileExistsError: pass def fuzz(self): self.generator.generate_ast() self.gaz_source_gen = GazUnparser(self.generator.ast, True) try: self.gaz_source_gen.unparse() except NoneTagException as n: raise NoneTagException("", n.xml) 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: pass # exec(str(self.ground_truth)) # exec(self.ground_truth, globals(), locals()) # FIXME the exec doesn't actually execute for some reason... self.write_ast() # FIXME sometimes this is none 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, file=sys.stderr) with open("debug/ast/debug_{}.xml".format(randint), 'w') as f: f.write(pretty)