2023-11-19 16:13:08 -07:00
|
|
|
import sys
|
|
|
|
from io import StringIO
|
|
|
|
import xml
|
|
|
|
|
2023-11-18 10:59:00 -07:00
|
|
|
import yaml
|
|
|
|
import ast_parser
|
2023-11-18 20:13:15 -07:00
|
|
|
from ast_generator.ast_generator import *
|
|
|
|
from ast_parser.gaz_unparser import *
|
2023-11-19 16:13:08 -07:00
|
|
|
from ast_parser.python_unparser import PythonUnparser
|
|
|
|
import inspect
|
2023-11-18 20:13:15 -07:00
|
|
|
|
2023-11-18 10:59:00 -07:00
|
|
|
|
2023-11-17 16:57:53 -07:00
|
|
|
class GazpreaFuzzer:
|
2023-11-18 10:59:00 -07:00
|
|
|
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
|
|
|
|
|
2023-11-19 16:13:08 -07:00
|
|
|
self.gaz_source_gen = None
|
2023-11-18 20:13:15 -07:00
|
|
|
self.generator = AstGenerator(settings)
|
|
|
|
|
|
|
|
self.ast = None
|
|
|
|
self.source = None
|
2023-11-19 16:13:08 -07:00
|
|
|
self.ground_truth = None
|
2023-11-18 20:13:15 -07:00
|
|
|
self.out = None
|
|
|
|
|
|
|
|
def fuzz(self):
|
|
|
|
self.generator.generate_ast()
|
2023-11-19 16:13:08 -07:00
|
|
|
self.write_ast()
|
|
|
|
|
|
|
|
self.gaz_source_gen = GazUnparser(self.generator.ast, True)
|
|
|
|
self.gaz_source_gen.unparse()
|
2023-11-18 10:59:00 -07:00
|
|
|
|
2023-11-18 20:13:15 -07:00
|
|
|
self.ast = self.generator.ast
|
2023-11-19 16:13:08 -07:00
|
|
|
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)
|