diff --git a/ast_parser/general_unparser.py b/ast_parser/general_unparser.py index 4744db2..5c38ff0 100644 --- a/ast_parser/general_unparser.py +++ b/ast_parser/general_unparser.py @@ -1,5 +1,6 @@ +import xml +import xml.dom.minidom import xml.etree.ElementTree as ET -from abc import ABC, abstractmethod from constants import * @@ -107,7 +108,11 @@ class GeneralUnparser: self.unparse_brackets(node) elif node.tag == GAZ_BREAK_TAG: self.unparse_break(node) + elif node.tag is None: + raise NoneTagException("None tag encountered", self.xml) else: + with open("unknown_tag.xml", "w") as f: + f.write(self.pretty_xml()) raise Exception("Unknown tag: " + node.tag) def unparse_block(self, node): @@ -319,4 +324,7 @@ class GeneralUnparser: def format_single_arg(self, param, param1): raise NotImplementedError - + def pretty_xml(self): + dom = xml.dom.minidom.parseString(ET.tostring(self.xml).decode('utf-8')) + pretty: str = dom.toprettyxml() + return pretty diff --git a/constants.py b/constants.py index 4761d92..3522105 100644 --- a/constants.py +++ b/constants.py @@ -47,3 +47,11 @@ GAZ_STRING_KEY = "string" GAZ_CHAR_KEY = "char" GAZ_BRACKET_TAG = "brackets" GAZ_BREAK_TAG = "break" + + +class NoneTagException(Exception): + + def __init__(self, message, xml): + super().__init__(message) + self.xml = xml + diff --git a/fuzzer.py b/fuzzer.py index a5ddd98..e12f939 100644 --- a/fuzzer.py +++ b/fuzzer.py @@ -33,7 +33,10 @@ class GazpreaFuzzer: self.write_ast() # FIXME sometimes this is none self.gaz_source_gen = GazUnparser(self.generator.ast, True) - self.gaz_source_gen.unparse() + 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 @@ -55,6 +58,6 @@ class GazpreaFuzzer: pretty: str = dom.toprettyxml() randint = random.randint(0, 1000) - print(randint) + print(randint, file=sys.stderr) with open("debug/ast/debug_{}.xml".format(randint), 'w') as f: f.write(pretty) diff --git a/gazprea_fuzzer.py b/gazprea_fuzzer.py index 91c7154..386012d 100644 --- a/gazprea_fuzzer.py +++ b/gazprea_fuzzer.py @@ -1,5 +1,6 @@ import os import random +import warnings from contextlib import redirect_stdout import yaml @@ -10,6 +11,9 @@ import argparse import xml.dom.minidom import xml.etree.ElementTree as ET +from constants import NoneTagException + + class Fuzzer(): def __init__(self, config: str, batch: int, seed: str, file_name: str = "fuzz"): with open(config) as yaml_file: @@ -26,11 +30,21 @@ class Fuzzer(): os.mkdir("fuzzer") os.mkdir("fuzzer/input") os.mkdir("fuzzer/debug") + os.mkdir("fuzzer/debug/ast") os.mkdir("fuzzer/instream") os.mkdir("fuzzer/outputs") os.mkdir("fuzzer/ground_truth") for i in range(self.batch): - self.fuzzer.fuzz() + try: + self.fuzzer.fuzz() + except NoneTagException as n: + r = random.randint(0, 1000000) + warnings.warn("None Tag Exception encountered, writing stack trace and xml to debug/ast/{}.xml\n" + "Look for a top-level tag or send it to Ayrton and I'll see what I can see" + "".format(r)) + with open("fuzzer/debug/ast/{}.xml".format(r), 'w') as f: + f.write(xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8')).toprettyxml()) + continue dom = xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8')) pretty: str = dom.toprettyxml() @@ -41,7 +55,7 @@ class Fuzzer(): with redirect_stdout(y): # Workaround for fuzzer.py:49 try: exec(f.read(), globals(), locals()) - except OverflowError: + except (OverflowError, ZeroDivisionError): os.system("rm -f fuzzer/ground_truth/{}_{}.py".format(self.file_name, i)) continue with open("fuzzer/input/{}_{}.in".format(self.file_name, i), 'w') as f: diff --git a/unknown_tag.xml b/unknown_tag.xml new file mode 100644 index 0000000..345b7f0 --- /dev/null +++ b/unknown_tag.xml @@ -0,0 +1,603 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +