From 5e5d6aeba9dcd3fd50a5251f2f8aaf772c886dff Mon Sep 17 00:00:00 2001 From: ayrton Date: Sun, 19 Nov 2023 16:13:08 -0700 Subject: [PATCH] Fixed generation bug where no op would be generated Took 54 minutes --- ast_generator/ast_generator.py | 10 ++++- ast_generator/test/test_ast_generator.py | 48 ++++++++++++++++++++++++ fuzzer.py | 35 +++++++++++++++-- fuzzer/input/name9.in | 29 -------------- gazprea_fuzzer.py | 27 ++++++++----- 5 files changed, 104 insertions(+), 45 deletions(-) delete mode 100644 fuzzer/input/name9.in diff --git a/ast_generator/ast_generator.py b/ast_generator/ast_generator.py index f1491e8..21eae7f 100644 --- a/ast_generator/ast_generator.py +++ b/ast_generator/ast_generator.py @@ -249,10 +249,14 @@ class AstGenerator: return op = "" - a = random.randint(0, number_line) + a = random.randint(0, number_line - 1) i = 0 for i in range(len(cutoffs) - 1): - if cutoffs[i] < a < cutoffs[i + 1]: + if i == 0: + if a < cutoffs[i]: + op = options[i] + break + if cutoffs[i] <= a < cutoffs[i + 1]: op = options[i] break @@ -283,6 +287,8 @@ class AstGenerator: def generate_binary(self, op, op_type): parent = self.current_ast_element + if op == "": + raise ValueError("op is empty!") args = [ ("op", op), ("type", op_type), diff --git a/ast_generator/test/test_ast_generator.py b/ast_generator/test/test_ast_generator.py index 9fece7e..ca85630 100644 --- a/ast_generator/test/test_ast_generator.py +++ b/ast_generator/test/test_ast_generator.py @@ -223,6 +223,54 @@ class TestGeneration(unittest.TestCase): main = True self.assertTrue(main) + def test_no_op_operation(self): + for l in range(1000): + # print("iteration: " + str(l)) + self.ast_gen.ast = ET.Element("block") + self.ast_gen.current_ast_element = self.ast_gen.ast + self.ast_gen.generate_int_real_expr() + # self.write_ast() + + if self.ast_gen.ast.find("operator") is None: + continue + operator = self.ast_gen.ast.find("operator") + self.assertFalse(self.is_no_op(operator)) + + + def is_no_op(self, operator): + """ + recursively check if operator is a no-op + @param operator: + @return: + """ + res = False + if operator.get("op") == '': + return True + else: + lhs = operator.find("lhs") + rhs = operator.find("rhs") + if lhs.find("operator") is not None: + res = self.is_no_op(lhs.find("operator")) + elif lhs.find("unary") is not None: + res = self.is_no_op(lhs.find("unary")) + elif rhs.find("operator") is not None: + res = self.is_no_op(rhs.find("operator")) + elif rhs.find("unary") is not None: + res = self.is_no_op(lhs.find("unary")) + + return res + + def write_ast(self): + dom = xml.dom.minidom.parseString(ET.tostring(self.ast_gen.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) + + + if __name__ == '__main__': with open("config.yaml", 'r') as stream: diff --git a/fuzzer.py b/fuzzer.py index 2997480..b2dcbd8 100644 --- a/fuzzer.py +++ b/fuzzer.py @@ -1,7 +1,13 @@ +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: @@ -12,17 +18,38 @@ class GazpreaFuzzer: settings: dict = yaml.safe_load(yaml_file) self.settings = settings - self.parser = None + 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.parser = GazUnparser(self.generator.ast, True) - self.parser.unparse() + 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.parser.source + 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) diff --git a/fuzzer/input/name9.in b/fuzzer/input/name9.in deleted file mode 100644 index 26a363c..0000000 --- a/fuzzer/input/name9.in +++ /dev/null @@ -1,29 +0,0 @@ -procedure main() returns integer { - var integer lQnOqNtVPW = 698.7344103570729 * -606.7195181938484 + 1182583819 / 338469442 -132074245 237434130 * -708692944 / 530298228 / 228341377 * -1043540342 / 338.5382125746569 - -1560602812 2034076062 -677.2384366071922 * -1396288472 / -294.03910951537273 / 280.6003642162291 * 686277004 + -733.108807335117 - 1914739986 + -758706778 815.5612439852075 - 730843697 / -2035135312 761.468341780042 - 829972738; - 529.6931700408081 * -53.71572853074235 - 731650703 263042418 * 1435010036 * -737.690803185421 - -395.5236453408153 / 1196211792 * -863.1836123458379 * -749.2669025070672 / 233.51151490705547 * -51.0686729564427 + -621.939364952389 / -361.3032921336703 - 1199853509 + 907542641 - 305.66149149183957 - -969.8710268189419 + 740.6073234896774 * 1719139590 - -243.75739859831253 - 1996675237 -> std_output; - var integer HWk = 1069696775; - var integer MIk = 1514360961; - var integer HE = -649.8393258588937 * -420.15021805874414 * 1761285701 -1667293001 / 29.5677167859983 * -844.2501263700528 / 1501314800 -1162542243 - -1217599986 + 1615995882 -1764940899 + -1917325853 + 835.4795594258562 * 590.6099641194992 - 472.7184791317236 * 1344256840 * 62.679409268019754 - 484.02060330123754 * 777.5221533723766 330.52965727523974 -380.69699980042685 * -760070929 * -949.6644980763808 - -447.8157902650353 * 464.1790468792867; - MIk = 953967969 + -268.8694010427613 / 791.5413559693457 + -7.679861742818616 / 1437831656 + 403.979481098871 * -456304863 - 2091190143 * 737295943 -1039468631 / -681.5757290664048 + -971.7430559091156 * -445.84393937984385 * -222.0375737233644 1070575402 -999.5935819561656 - -173.41289931519555 -1190366598 / 613.2734677906551 -531818545; - -397.7646630448985 - 1809763600 -982.6387961874157 + 256902008 * 425.58253894287964 / -718.3862648199979 -429.5886775058759 * 158.12171431595698 * -599.7405849078852 -733.8460310316564 * -742.5867261833964 * 2086150131 - 703.2177962216272 -> std_output; - lQnOqNtVPW = -680.8890396739438; - HWk = -254718851 / 264.0381140059085 * 996.2297492346818 - -77.96516420152841; - 2100864484 - 2007894240 * -212.01378334488163 * 1379091426 -412243492 / -317390514 1229512655 / 1726566479 / 1747747042 764692152 + 624951054 + -1324352806 + 399.1702565288206 + -893.8711860029409 + -711.4400942267645 + 179.0145034263055 - -723.3632700757751 -2079239194 * -729.6073740027866 + -629.3818640065572 * 142789298 / -489.50899343461106 - 561691654 + -1414809862 * 222.37308869516414 / 857.1114294659471 549.7759000995088 + -666.6748642112184 * 385.38510100490385 -> std_output; - return 0; - HWk = 2059289556 + -403.9892396676721 / 1653655432 - -588.6575838596884 * -467.74405479942334 -789.6678603205703 - -2079730883 * -206.976327703003 - -99.23168409674201 -368.21174081123957 -2125902669 * 721.014283295284 * 684.9390791239739 * -336.9272218346822 / -549.8949978968651 / 31.821007498567496 / -386544376 36.406643230034206 -979.8695533308437 - -405.3890042820476 + -241343604 * 947.4009884595018 * -741574263; -} - -procedure DzeIKcCNqh(integer rejZppcSMv, integer LkOieFctp, integer r, integer fGPZCKaJ, integer wLK, integer AeUntf, integer MWsHdvBSR, integer OuXoynGm, integer VPVOgzU, integer SMxhab) returns integer { - NAsBez = 807.1145861289976; - -1046538300 -> std_output; - return -211048078; -} - -function UkU(integer ImmgbVHS, integer LfvBpxnRq) returns integer { - NAsBez = 961878732; - return -1293956197 / -773.5583114264432 / 759992758 - 324.6814914711299 / -773.3733374415364 * -823.4158348051863 * -1178415147 / 749.7213693296735 - -131.4240459218521 - -607197268 * -71900919 768.0290042747429 * 1529150661 * -657.5023412274927 / -201.66669058694868 / 692.9818240317327 / -1241350558 / -652.1792880359916 * 673.9253031170642 -61.25909942165197 * -2037670958 -1400547457 - 588219524 / -539.7564210381759 * 1856853047 / -1654156060 + -648756285; - NAsBez = 186.7935151585625 + 439669401 / 545.2783919286676 - -53306996 -9214657 - 184.5664265585533 / 291945719 * -443885835 + -566896102 - -346.630385606735 / 990.8304675995412 1582261953 - -1818951954 - -80664147 - 466.8821342017786 / 459.0521603737384 249.74968247519246; - var integer xtoZPgYmq = 810.2648762093065 / 482.261574544076 * 1927906771 / 630.1087104660135 -860.8294002903949 / 1932738597 + 1967595112 + -1136874252 * 580228054 -344.98371454114204 * -528.4467764471448 * 1842148455 + -166.07081934179928 / 90.4861524809719 / -177909614 / 2014149666; - var integer MmXWOxIXst = -135150791 / -842.279867288959 -873.3805968639836 + 137.73347301182048 + -668.6167365403119 - -531.4895607809302 - -486203036 / 1163466657 / -1996539944 * 908219867 * 684.3378150732071 - -351.83526537527894 / 569.8408814272439 * 1560698923; -} - diff --git a/gazprea_fuzzer.py b/gazprea_fuzzer.py index b5292ce..4160e58 100644 --- a/gazprea_fuzzer.py +++ b/gazprea_fuzzer.py @@ -10,31 +10,36 @@ import xml.dom.minidom import xml.etree.ElementTree as ET class Fuzzer(): - def __init__(self, config: str, batch: int, seed: str): + def __init__(self, config: str, batch: int, seed: str, file_name: str = "fuzz"): with open(config) as yaml_file: settings: dict = yaml.safe_load(yaml_file) self.settings = settings self.batch = batch random.seed(seed) + self.file_name = file_name self.fuzzer = fz.GazpreaFuzzer(config) - self.fuzzer.fuzz() def fuzz(self): + os.system("rm -rf fuzzer") + os.mkdir("fuzzer") + os.mkdir("fuzzer/input") + os.mkdir("fuzzer/debug") + os.mkdir("fuzzer/instream") + os.mkdir("fuzzer/outputs") + os.mkdir("fuzzer/ground_truth") for i in range(self.batch): self.fuzzer.fuzz() dom = xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8')) pretty: str = dom.toprettyxml() - os.system("rm -rf fuzzer") - os.mkdir("fuzzer") - os.mkdir("fuzzer/input") - os.mkdir("fuzzer/debug") - os.mkdir("fuzzer/instream") - os.mkdir("fuzzer/outputs") - with open("fuzzer/source/name{}.in".format(i), 'w') as f: + with open("fuzzer/source/{}_{}.in".format(self.file_name, i), 'w') as f: f.write(self.fuzzer.source) - with open("fuzzer/debug/name{}.out".format(i), 'w') as f: + with open("fuzzer/debug/{}_{}.out".format(self.file_name, i), 'w') as f: f.write(pretty) + with open("fuzzer/outputs/{}_{}.out".format(self.file_name, i), 'w') as f: + f.write(self.fuzzer.out) + with open("fuzzer/ground_truth/{}_{}.py".format(self.file_name, i), 'w') as f: + f.write(self.fuzzer.ground_truth) # with open("fuzzer/instream/{}.in".format(i), 'w') as f: # f.write(self.fuzzer.source) # with open("fuzzer/outputs/{}.out".format(i), 'w') as f: @@ -51,6 +56,8 @@ if __name__ == '__main__': help="rng seed") parser.add_argument('config_file', type=str, action="store", help="path to your configuration file") + parser.add_argument('file_name', type=str, action="store", + help="name for the generated files") args = parser.parse_args() fuzzer = Fuzzer(config=args.config_file, batch=args.batch_size, seed=args.seed)