Compare commits
8 commits
ayrton/ref
...
main
Author | SHA1 | Date | |
---|---|---|---|
aCompetentBean | 6a7e3f98f5 | ||
ayrton | 880c8e0713 | ||
ayrton | 9f915bd80e | ||
aCompetentBean | 0d96551e82 | ||
Ayrton | 1c91f6ac86 | ||
ayrton | ddc3875f56 | ||
ayrton | 1aef59672f | ||
aCompetentBean | 15449c8899 |
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
|||
fuzzer/
|
||||
exec/
|
||||
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import string
|
||||
import warnings
|
||||
|
||||
from english_words import get_english_words_set
|
||||
|
||||
from ast_generator.tiny_py_unparse import TinyPyUnparser
|
||||
from ast_generator.utils import *
|
||||
from ast_generator.utils import filter_options, _choose_option
|
||||
from constants import *
|
||||
|
@ -84,8 +86,8 @@ class AstGenerator:
|
|||
|
||||
def _init_names(self):
|
||||
names = get_english_words_set(['web2'], alpha=True)
|
||||
possible_names = filter(lambda x: self.settings['properties']['id-length']['max'] <= len(x) <=
|
||||
self.settings['properties']['id-length']['max'] and not keyword.iskeyword(x),
|
||||
possible_names = filter(lambda x: (self.settings['properties']['id-length']['max'] <= len(x) <=
|
||||
self.settings['properties']['id-length']['max']) and not keyword.iskeyword(x),
|
||||
names)
|
||||
var_name_list = list(possible_names)
|
||||
var_name_len = len(var_name_list)
|
||||
|
@ -140,21 +142,23 @@ class AstGenerator:
|
|||
self.current_ast_element.append(element)
|
||||
self.current_ast_element = element
|
||||
|
||||
if block_type in [GAZ_PROCEDURE_TAG, GAZ_FUNCTION_TAG]:
|
||||
self.generate_statements()
|
||||
else:
|
||||
self.generate_statements(include='declaration')
|
||||
self.generate_statements(exclude='declaration')
|
||||
|
||||
# Generate the loop condition increment if we are in a loop
|
||||
if block_type == GAZ_LOOP_TAG:
|
||||
self.generate_loop_condition_check(loop_var)
|
||||
self.generate_loop_condition_increment(loop_var)
|
||||
|
||||
if block_type not in [GAZ_PROCEDURE_TAG, GAZ_FUNCTION_TAG]:
|
||||
self.generate_statements()
|
||||
else:
|
||||
self.generate_statements(include='declaration')
|
||||
self.generate_statements(exclude='declaration')
|
||||
|
||||
if return_stmt:
|
||||
self.generate_return(return_type=return_type, return_value=return_value)
|
||||
if self.settings['generation-options']['generate-dead-code']:
|
||||
self.generate_statements(exclude='declaration')
|
||||
|
||||
self.print_all_current_scope_vars()
|
||||
self.pop_scope()
|
||||
self.current_ast_element = parent
|
||||
|
||||
|
@ -433,13 +437,66 @@ class AstGenerator:
|
|||
self.make_scoped_element(GAZ_LOOP_TAG, [])
|
||||
self.current_control_flow_nesting_depth += 1
|
||||
|
||||
self.generate_expression(GAZ_BOOL_KEY) # the loop entry condition #TODO force true
|
||||
truthiness = self.get_truthiness()
|
||||
|
||||
while True:
|
||||
self.generate_expression(GAZ_BOOL_KEY) # the loop entry condition #TODO force true, if false, generate a simple block and return, no need to nest code we won't use
|
||||
|
||||
iterator = self.current_ast_element.iter()
|
||||
next(iterator)
|
||||
conditional = next(iterator)
|
||||
if not self.truth_check(conditional, truthiness):
|
||||
self.current_ast_element.remove(conditional)
|
||||
else:
|
||||
break
|
||||
|
||||
self.generate_block(block_type=GAZ_LOOP_TAG,
|
||||
loop_var=init_var) # append a variable increment and prepend a break statement if var is > max loop iterations
|
||||
|
||||
self.current_control_flow_nesting_depth -= 1
|
||||
self.exit_scoped_element(parent)
|
||||
|
||||
def get_truthiness(self):
|
||||
return random.random() < self.settings['misc-weights']['truthiness']
|
||||
|
||||
def truth_check(self, element: ET.Element, truthiness: bool):
|
||||
# evaluate the element
|
||||
uncompiled = self.unparse_conditional_statement(element)
|
||||
substr1 = uncompiled.find("**")
|
||||
substr2 = uncompiled.find("**", substr1 + 1)
|
||||
|
||||
if -1 not in [substr1, substr2]:
|
||||
return False
|
||||
|
||||
# print(uncompiled)
|
||||
try:
|
||||
res = eval(uncompiled)
|
||||
except (OverflowError, ZeroDivisionError):
|
||||
res = False
|
||||
|
||||
# check it against truthiness and return the comparison
|
||||
return res == truthiness
|
||||
|
||||
def unparse_conditional_statement(self, element: ET.Element):
|
||||
py_unparse = TinyPyUnparser(element, True)
|
||||
unparsed = ""
|
||||
if element.tag == GAZ_LIT_TAG:
|
||||
py_unparse.unparse_literal(element)
|
||||
unparsed = py_unparse.source
|
||||
elif element.tag == GAZ_OPERATOR_TAG:
|
||||
py_unparse.unparse_operator(element)
|
||||
unparsed = py_unparse.source
|
||||
elif element.tag == GAZ_VAR_TAG:
|
||||
xml = self.current_scope.resolve(element.get("name"))
|
||||
py_unparse.unparse_variable(xml)
|
||||
unparsed = py_unparse.source
|
||||
elif element.tag == GAZ_BRACKET_TAG:
|
||||
py_unparse.unparse_brackets(element)
|
||||
unparsed = py_unparse.source
|
||||
else:
|
||||
raise ValueError("Unknown element type for conditional argument: " + element.tag)
|
||||
return unparsed
|
||||
|
||||
def generate_zero_declaration(self):
|
||||
"""
|
||||
@brief generate a declaration int a = 0 for some a
|
||||
|
@ -642,7 +699,7 @@ class AstGenerator:
|
|||
self.comp_op_numline,
|
||||
self.comp_op_cutoffs,
|
||||
self.comp_op_options,
|
||||
comparison=True)
|
||||
comparison=True) #, evals=self.get_truth())
|
||||
|
||||
def push_scope(self, xml_element: ET.Element = None):
|
||||
scope = Scope(self.current_scope)
|
||||
|
@ -898,3 +955,25 @@ class AstGenerator:
|
|||
self.generate_binary(op, random.choice([GAZ_INT_KEY, GAZ_FLOAT_KEY]))
|
||||
else:
|
||||
self.generate_binary(op, random.choice(expr_type))
|
||||
|
||||
def print_all_current_scope_vars(self):
|
||||
for key, value in self.current_scope.symbols.items():
|
||||
if isinstance(value, Variable):
|
||||
self.print_variable(value)
|
||||
|
||||
def print_variable(self, var: Variable):
|
||||
"""
|
||||
@brief generate an outstream for a variable
|
||||
|
||||
@param var: the variable to print
|
||||
@return:
|
||||
"""
|
||||
parent = self.current_ast_element
|
||||
|
||||
args = [
|
||||
("type", GAZ_OUT_STREAM),
|
||||
]
|
||||
self.make_element(GAZ_STREAM_TAG, args)
|
||||
self.current_ast_element.append(var.xml)
|
||||
|
||||
self.current_ast_element = parent
|
||||
|
|
|
@ -9,7 +9,7 @@ generation-options:
|
|||
max-globals: 5 # maximum number of global variables
|
||||
properties:
|
||||
max-range-length: 5 # maximum length of ranges, vectors and tuples, (AxA matrices can exist)
|
||||
use-english-words: True # use english words instead of random names (this may limit the maximum number of names)
|
||||
use-english-words: True # use english words instead of random names (if we run out, we switch to random)
|
||||
id-length: # length of identifiers
|
||||
min: 1
|
||||
max: 5
|
||||
|
@ -68,12 +68,12 @@ statement-weights: # set to 0 for any statements y
|
|||
in-stream: 5
|
||||
|
||||
type-weights:
|
||||
value-types:
|
||||
integer: 50
|
||||
real: 50
|
||||
boolean: 50
|
||||
character: 50
|
||||
void: 10
|
||||
atomic-types:
|
||||
int: 50 # TODO change these to the gaz types
|
||||
float: 50
|
||||
bool: 50
|
||||
char: 50
|
||||
void: 0 # TODO add support for void
|
||||
composite-types:
|
||||
vector: 20
|
||||
tuple: 5
|
||||
|
@ -90,6 +90,7 @@ misc-weights:
|
|||
type-qualifier-weights:
|
||||
const: 10
|
||||
var: 60
|
||||
truthiness: 0.9 # Probability of conditionals being true
|
||||
|
||||
block-termination-probability: 0.2 # probability for a block to terminate
|
||||
|
||||
|
|
|
@ -245,12 +245,13 @@ class TestGeneration(unittest.TestCase):
|
|||
self.assertFalse(self.is_no_op(operator))
|
||||
|
||||
def test_create_global(self):
|
||||
self.ast_gen.ast = ET.Element("block")
|
||||
element = ET.Element("block")
|
||||
self.ast_gen.ast = element
|
||||
self.ast_gen.current_ast_element = self.ast_gen.ast
|
||||
self.ast_gen.generate_main()
|
||||
|
||||
global_block = self.ast_gen.current_ast_element
|
||||
global_scope = self.ast_gen.current_scope
|
||||
global_block = element
|
||||
global_scope = self.ast_gen.current_scope.get_top_scope()
|
||||
|
||||
self.assertIsNotNone(self.ast_gen.ast.find("procedure"))
|
||||
self.ast_gen.current_ast_element = self.ast_gen.ast.find("procedure")
|
||||
|
@ -309,6 +310,79 @@ class TestGeneration(unittest.TestCase):
|
|||
|
||||
return res
|
||||
|
||||
def test_print_all_current_scope_vars(self):
|
||||
element = ET.Element("block")
|
||||
self.ast_gen.ast = element
|
||||
self.ast_gen.current_ast_element = element
|
||||
self.ast_gen.current_scope = Scope(None)
|
||||
self.ast_gen.generate_declaration(mut='var')
|
||||
self.ast_gen.generate_declaration(mut='var')
|
||||
self.ast_gen.generate_declaration(mut='var')
|
||||
self.ast_gen.generate_declaration(mut='var')
|
||||
|
||||
self.ast_gen.print_all_current_scope_vars()
|
||||
|
||||
|
||||
# print(ET.tostring(self.ast_gen.ast))
|
||||
|
||||
streams = self.ast_gen.ast.findall("stream")
|
||||
|
||||
self.assertEqual(4, len(streams))
|
||||
|
||||
def test_conditional_test_1(self):
|
||||
element = ET.fromstring('<literal type="bool" value="False"/>')
|
||||
|
||||
self.assertIsNotNone(element)
|
||||
|
||||
res = self.ast_gen.truth_check(element, True)
|
||||
self.assertFalse(res)
|
||||
|
||||
def test_conditional_test_2(self):
|
||||
element = ET.fromstring('<literal type="bool" value="True"/>')
|
||||
self.assertIsNotNone(element)
|
||||
|
||||
res = self.ast_gen.truth_check(element, True)
|
||||
self.assertTrue(res)
|
||||
|
||||
def test_conditional_test_3(self):
|
||||
with open("xml/conditional.xml", 'r') as f:
|
||||
element = ET.fromstring(f.read())
|
||||
|
||||
self.assertIsNotNone(element)
|
||||
|
||||
res = self.ast_gen.truth_check(element, True)
|
||||
self.assertTrue(res)
|
||||
|
||||
def test_conditional_test_4(self):
|
||||
with open("xml/conditional_2.xml", 'r') as f:
|
||||
element = ET.fromstring(f.read())
|
||||
|
||||
self.assertIsNotNone(element)
|
||||
|
||||
res = self.ast_gen.truth_check(element, True)
|
||||
self.assertFalse(res)
|
||||
|
||||
def test_conditional_test_5(self):
|
||||
with open("xml/conditional_3.xml", 'r') as f:
|
||||
element = ET.fromstring(f.read())
|
||||
|
||||
self.assertIsNotNone(element)
|
||||
|
||||
res = self.ast_gen.truth_check(element, True)
|
||||
self.assertTrue(res)
|
||||
|
||||
# def test_conditional_test_variable(self):
|
||||
# element = ET.fromstring('<variable name="harold" type="int" mut="var"/>')
|
||||
#
|
||||
# var = Variable("harold", "int", "var")
|
||||
# var.decl_xml = ET.fromstring('<literal type="bool" value="True"/>')
|
||||
# self.ast_gen.current_scope.append("harold", element)
|
||||
# self.assertIsNotNone(element)
|
||||
#
|
||||
# res = self.ast_gen.truth_check(element, True)
|
||||
# self.assertTrue(res)
|
||||
|
||||
|
||||
def write_ast(self):
|
||||
dom = xml.dom.minidom.parseString(ET.tostring(self.ast_gen.ast).decode('utf-8'))
|
||||
pretty: str = dom.toprettyxml()
|
||||
|
|
8
ast_generator/test/xml/conditional.xml
Normal file
8
ast_generator/test/xml/conditional.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<operator op="==" type="int">
|
||||
<lhs>
|
||||
<literal mut="var" type="int" value="42"/>
|
||||
</lhs>
|
||||
<rhs>
|
||||
<literal type="int" value="42" />
|
||||
</rhs>
|
||||
</operator>
|
8
ast_generator/test/xml/conditional_2.xml
Normal file
8
ast_generator/test/xml/conditional_2.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<operator op="!=" type="int">
|
||||
<lhs>
|
||||
<literal mut="var" type="int" value="42"/>
|
||||
</lhs>
|
||||
<rhs>
|
||||
<literal type="int" value="42" />
|
||||
</rhs>
|
||||
</operator>
|
15
ast_generator/test/xml/conditional_3.xml
Normal file
15
ast_generator/test/xml/conditional_3.xml
Normal file
|
@ -0,0 +1,15 @@
|
|||
<operator op="!=" type="int">
|
||||
<lhs>
|
||||
<literal mut="var" type="int" value="False"/>
|
||||
</lhs>
|
||||
<rhs>
|
||||
<operator op=">" type="int">
|
||||
<lhs>
|
||||
<literal mut="var" type="int" value="43"/>
|
||||
</lhs>
|
||||
<rhs>
|
||||
<literal type="int" value="42" />
|
||||
</rhs>
|
||||
</operator>
|
||||
</rhs>
|
||||
</operator>
|
101
ast_generator/tiny_py_unparse.py
Normal file
101
ast_generator/tiny_py_unparse.py
Normal file
|
@ -0,0 +1,101 @@
|
|||
from ast_parser.general_unparser import GeneralUnparser
|
||||
|
||||
|
||||
import warnings
|
||||
from xml.etree import ElementTree as ET
|
||||
|
||||
from ast_parser.gaz_unparser import GazUnparser
|
||||
from ast_parser.general_unparser import GeneralUnparser
|
||||
from constants import *
|
||||
|
||||
|
||||
def to_python_type(ty):
|
||||
if ty == GAZ_INT_KEY:
|
||||
return "int"
|
||||
elif ty == GAZ_BOOL_KEY:
|
||||
return "bool"
|
||||
elif ty == GAZ_FLOAT_KEY:
|
||||
return "float"
|
||||
elif ty == GAZ_CHAR_KEY:
|
||||
return "str"
|
||||
elif ty == GAZ_STRING_KEY:
|
||||
return "str"
|
||||
else:
|
||||
raise Exception("Unknown type: " + ty)
|
||||
|
||||
|
||||
def to_python_op(param, ty):
|
||||
if param == "negation" or param == "subtraction":
|
||||
return "-"
|
||||
elif param == "addition" or param == "noop":
|
||||
return "+"
|
||||
elif param == "multiplication":
|
||||
return "*"
|
||||
elif param == "division" and ty != GAZ_INT_KEY:
|
||||
return "/"
|
||||
elif param == "division" and ty == GAZ_INT_KEY:
|
||||
return "//"
|
||||
elif param == "modulo":
|
||||
return "%"
|
||||
elif param == "power":
|
||||
return "**"
|
||||
elif param == "equality":
|
||||
return "=="
|
||||
elif param == "inequality":
|
||||
return "!="
|
||||
elif param == "less-than":
|
||||
return "<"
|
||||
elif param == "less-than-or-equal":
|
||||
return "<="
|
||||
elif param == "greater-than":
|
||||
return ">"
|
||||
elif param == "greater-than-or-equal":
|
||||
return ">="
|
||||
elif param == "xor":
|
||||
return "!="
|
||||
else:
|
||||
warnings.warn("Warning, unknown operator: " + param)
|
||||
return param
|
||||
|
||||
|
||||
class TinyPyUnparser(GeneralUnparser):
|
||||
def __init__(self, ast: ET.Element, debug=False):
|
||||
super().__init__(ast, debug,
|
||||
endline='\n',
|
||||
outstream_begin_delimiter="gprint(",
|
||||
outstream_end_delimiter=", end='')",
|
||||
function_return_type_indicator_predicate="->",
|
||||
loop_start_delimiter="while ",
|
||||
loop_end_delimiter=":",
|
||||
conditional_case_delimiter="elif ",
|
||||
conditional_start_delimiter="if ",
|
||||
conditional_else_delimiter="else:",
|
||||
conditional_end_delimiter=":",
|
||||
block_start_delimiter="",
|
||||
block_end_delimiter="", # TODO can this contain the pass?
|
||||
strip_conditionals=True)
|
||||
|
||||
self.source = ''
|
||||
|
||||
def format_variable(self, mut, ty, name, declaration: bool = False):
|
||||
if declaration:
|
||||
return "{}: {}".format(name, ty)
|
||||
else:
|
||||
return "{}".format(name)
|
||||
|
||||
def translate_value(self, val):
|
||||
return str(val)
|
||||
|
||||
def translate_op(self, param, ty=None):
|
||||
return to_python_op(param, ty)
|
||||
|
||||
def translate_type(self, ty):
|
||||
return to_python_type(ty)
|
||||
|
||||
def unparse_block(self, node):
|
||||
raise TypeError("Cannot unparse blocks for this type of evaluation")
|
||||
|
||||
def setup(self):
|
||||
pass
|
||||
|
||||
|
|
@ -5,12 +5,13 @@ from constants import GAZ_VAR_TAG, GAZ_ARG_TAG
|
|||
|
||||
|
||||
class Variable:
|
||||
def __init__(self, name: str, type: str, qualifier: str, value: any = None):
|
||||
def __init__(self, name: str, type: str, qualifier: str, value: any = None): # decl_xml: ET.Element = None,
|
||||
self.name = name
|
||||
self.type = type
|
||||
self.value = value
|
||||
self.qualifier = qualifier
|
||||
self.xml = self._build_xml()
|
||||
# self.decl_xml = decl_xml
|
||||
|
||||
def _build_xml(self):
|
||||
args = [
|
||||
|
|
|
@ -98,3 +98,6 @@ class GazUnparser(GeneralUnparser):
|
|||
|
||||
def format_single_arg(self, ty, name):
|
||||
return "{} {}".format(ty, name)
|
||||
|
||||
def setup(self):
|
||||
pass
|
||||
|
|
|
@ -69,6 +69,9 @@ class GeneralUnparser:
|
|||
:return: a string of valid gazprea code
|
||||
"""
|
||||
self.source = ""
|
||||
|
||||
self.setup()
|
||||
|
||||
for node in self.xml:
|
||||
self.unparse_node(node)
|
||||
|
||||
|
@ -324,6 +327,9 @@ class GeneralUnparser:
|
|||
def format_single_arg(self, param, param1):
|
||||
raise NotImplementedError
|
||||
|
||||
def setup(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def pretty_xml(self):
|
||||
dom = xml.dom.minidom.parseString(ET.tostring(self.xml).decode('utf-8'))
|
||||
pretty: str = dom.toprettyxml()
|
||||
|
|
|
@ -59,7 +59,7 @@ class PythonUnparser(GeneralUnparser):
|
|||
def __init__(self, ast: ET.Element, debug=False):
|
||||
super().__init__(ast, debug,
|
||||
endline='\n',
|
||||
outstream_begin_delimiter="print(",
|
||||
outstream_begin_delimiter="gprint(",
|
||||
outstream_end_delimiter=", end='')",
|
||||
function_return_type_indicator_predicate="->",
|
||||
loop_start_delimiter="while ",
|
||||
|
@ -113,3 +113,13 @@ class PythonUnparser(GeneralUnparser):
|
|||
def unparse(self):
|
||||
super().unparse()
|
||||
self.source += "\nif __name__ == '__main__':\n main()"
|
||||
|
||||
def setup(self):
|
||||
self.source += ("def gprint(expr, end=''):\n"
|
||||
" if type(expr) is bool:\n"
|
||||
" if expr:\n"
|
||||
" print('T', end=end)\n"
|
||||
" else:\n"
|
||||
" print('F', end=end)\n"
|
||||
" else:\n"
|
||||
" print(expr, end=end)\n\n")
|
||||
|
|
|
@ -4,7 +4,7 @@ generation-options:
|
|||
max-nesting-depth: 5 # maximum nesting depth for statements
|
||||
max-conditionals-loops: 5 # maximum number of loops/conditionals per routine
|
||||
max-number-of-routines: 5 # maximum number of routines (main will always be generated)
|
||||
generate-dead-code: True # generate dead code
|
||||
generate-dead-code: False # generate dead code
|
||||
max-loop-iterations: 100 # maximum number of iterations in a loop
|
||||
max-globals: 5 # maximum number of global variables
|
||||
properties:
|
||||
|
@ -90,6 +90,7 @@ misc-weights:
|
|||
type-qualifier-weights:
|
||||
const: 10
|
||||
var: 60
|
||||
truthiness: 0.99 # Probability of conditionals being true
|
||||
|
||||
block-termination-probability: 0.2 # probability for a block to terminate
|
||||
|
||||
|
|
|
@ -61,3 +61,11 @@ class NoneTagException(Exception):
|
|||
super().__init__(message)
|
||||
self.xml = xml
|
||||
|
||||
class GazTypeError(Exception):
|
||||
|
||||
def __init__(self, message, op, ty1, ty2):
|
||||
super().__init__(message)
|
||||
self.op = op
|
||||
self.ty1 = ty1
|
||||
self.ty2 = ty2
|
||||
|
||||
|
|
10
fuzzer.py
10
fuzzer.py
|
@ -28,9 +28,13 @@ class GazpreaFuzzer:
|
|||
self.ground_truth = None
|
||||
self.out = None
|
||||
|
||||
try:
|
||||
os.makedirs('debug/ast')
|
||||
except FileExistsError:
|
||||
pass
|
||||
|
||||
def fuzz(self):
|
||||
self.generator.generate_ast()
|
||||
self.write_ast() # FIXME sometimes this is none
|
||||
|
||||
self.gaz_source_gen = GazUnparser(self.generator.ast, True)
|
||||
try:
|
||||
|
@ -48,8 +52,10 @@ class GazpreaFuzzer:
|
|||
# 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...
|
||||
# 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()
|
||||
|
||||
|
|
|
@ -14,6 +14,18 @@ import xml.etree.ElementTree as ET
|
|||
|
||||
from constants import NoneTagException
|
||||
|
||||
import signal as sig
|
||||
|
||||
|
||||
def gprint(expr, end=''):
|
||||
if type(expr) is bool:
|
||||
if expr:
|
||||
print('T', end=end)
|
||||
else:
|
||||
print('F', end=end)
|
||||
else:
|
||||
print(expr, end=end)
|
||||
|
||||
|
||||
class Fuzzer():
|
||||
def __init__(self, config: str, batch: int, seed: str, file_name: str = "fuzz"):
|
||||
|
@ -27,16 +39,35 @@ class Fuzzer():
|
|||
self.fuzzer = fz.GazpreaFuzzer(config)
|
||||
|
||||
def fuzz(self):
|
||||
os.system("rm -rf fuzzer")
|
||||
os.mkdir("fuzzer")
|
||||
os.mkdir("fuzzer/input")
|
||||
os.mkdir("fuzzer/debug")
|
||||
os.mkdir("fuzzer/debug/ast_err")
|
||||
os.mkdir("fuzzer/instream")
|
||||
os.mkdir("fuzzer/outputs")
|
||||
os.mkdir("fuzzer/ground_truth")
|
||||
os.system("cp tester_config.json fuzzer/tester_config.json")
|
||||
for i in range(self.batch):
|
||||
sig.signal(sig.SIGALRM, handler)
|
||||
sig.alarm(30)
|
||||
|
||||
base_dir = os.getcwd()
|
||||
fuzzer_root = base_dir + '/fuzzer'
|
||||
|
||||
fuzzer_input = fuzzer_root + '/input/fuzzer'
|
||||
fuzzer_debug = fuzzer_root + '/debug'
|
||||
fuzzer_asterr = fuzzer_debug + '/ast_err'
|
||||
fuzzer_instream = fuzzer_root + '/instream'
|
||||
fuzzer_outputs = fuzzer_root + '/outputs/fuzzer'
|
||||
fuzzer_ground_truth = fuzzer_root + '/ground_truth'
|
||||
|
||||
os.system(f"rm -rf {fuzzer_root}")
|
||||
os.makedirs(fuzzer_root)
|
||||
os.makedirs(fuzzer_input)
|
||||
os.makedirs(fuzzer_debug)
|
||||
os.makedirs(fuzzer_asterr)
|
||||
os.makedirs(fuzzer_instream)
|
||||
os.makedirs(fuzzer_outputs)
|
||||
os.makedirs(fuzzer_ground_truth)
|
||||
try:
|
||||
os.system(f"cp tester_config.json {fuzzer_root}/tester_config.json")
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
i = 0
|
||||
min_i = 0
|
||||
while i < self.batch:
|
||||
try:
|
||||
self.fuzzer.fuzz()
|
||||
except NoneTagException as n:
|
||||
|
@ -44,41 +75,92 @@ class Fuzzer():
|
|||
warnings.warn("None Tag Exception encountered, writing stack trace and xml to debug/ast/{}.xml\n"
|
||||
"Look for a top-level <argument> tag or send it to Ayrton and I'll see what I can see"
|
||||
"".format(r))
|
||||
with open("fuzzer/debug/ast_err/{}.xml".format(r), 'w') as f:
|
||||
f.write(xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8')).toprettyxml())
|
||||
with open(f"{fuzzer_asterr}/{r}.xml", 'w') as f:
|
||||
try:
|
||||
f.write(xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8')).toprettyxml())
|
||||
except AttributeError:
|
||||
print("Failed to write to the file", file=sys.stderr)
|
||||
if i - 1 >= min_i:
|
||||
i -= 1
|
||||
else:
|
||||
i = min_i
|
||||
continue
|
||||
dom = xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8'))
|
||||
pretty: str = dom.toprettyxml()
|
||||
|
||||
with open("fuzzer/ground_truth/{}_{}.py".format(self.file_name, i), 'w') as f:
|
||||
with open("{}/{}_{}.py".format(fuzzer_ground_truth, self.file_name, i), 'w') as f:
|
||||
f.write(self.fuzzer.ground_truth)
|
||||
with open("fuzzer/ground_truth/{}_{}.py".format(self.file_name, i), 'r') as f:
|
||||
with open("fuzzer/outputs/{}_{}.out".format(self.file_name, i), 'w') as y:
|
||||
with redirect_stdout(y): # Workaround for fuzzer.py:49
|
||||
with open("{}/{}_{}.py".format(fuzzer_ground_truth, self.file_name, i), 'r') as f:
|
||||
with open("{}/{}_{}.out".format(fuzzer_outputs, self.file_name, i), 'w') as y:
|
||||
with redirect_stdout(y): # Workaround for fuzzer.py:49
|
||||
try:
|
||||
exec(f.read(), globals(), locals())
|
||||
except (OverflowError, ZeroDivisionError, ValueError):
|
||||
os.system("rm -f fuzzer/ground_truth/{}_{}.py".format(self.file_name, i))
|
||||
read = f.read()
|
||||
exec(read, globals())
|
||||
except (OverflowError, ZeroDivisionError, ValueError, TypeError, SyntaxError):
|
||||
os.system("rm -f {}/{}_{}.py".format(fuzzer_ground_truth, self.file_name, i))
|
||||
os.system("rm -f {}/{}_{}.out".format(fuzzer_outputs, self.file_name, i))
|
||||
warnings.warn("Runtime error encountered, retrying")
|
||||
if i - 1 >= min_i:
|
||||
i -= 1
|
||||
else:
|
||||
i = min_i
|
||||
continue
|
||||
except KeyboardInterrupt:
|
||||
r = random.randint(0, 1000000)
|
||||
warnings.warn("Execution halted, result written to debug/ast/{}.xml\n"
|
||||
"".format(r))
|
||||
with open("fuzzer/debug/ast_err/{}.xml".format(r), 'w') as f:
|
||||
f.write(xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8')).toprettyxml())
|
||||
with open("{}/{}.xml".format(fuzzer_asterr, r), 'w') as f:
|
||||
f.write(xml.dom.minidom.parseString(
|
||||
ET.tostring(self.fuzzer.ast).decode('utf-8')).toprettyxml())
|
||||
sys.exit(1)
|
||||
with open("fuzzer/input/{}_{}.in".format(self.file_name, i), 'w') as f:
|
||||
except TimeoutError:
|
||||
r = random.randint(0, 1000000)
|
||||
warnings.warn("Execution timed out, result written to debug/ast/{}.xml\n"
|
||||
"".format(r))
|
||||
with open("{}/{}.xml".format(fuzzer_asterr, r), 'w') as f:
|
||||
f.write(xml.dom.minidom.parseString(
|
||||
ET.tostring(self.fuzzer.ast).decode('utf-8')).toprettyxml())
|
||||
if i - 1 >= min_i:
|
||||
i -= 1
|
||||
else:
|
||||
i = min_i
|
||||
continue
|
||||
|
||||
with open("{}/{}_{}.out".format(fuzzer_outputs, self.file_name, i), 'r') as y:
|
||||
out = y.read()
|
||||
if out == "":
|
||||
print("Empty output, skipping", file=sys.stderr)
|
||||
os.system("rm -f {}/{}_{}.py".format(fuzzer_ground_truth, self.file_name, i))
|
||||
os.system("rm -f {}/{}_{}.out".format(fuzzer_outputs, self.file_name, i))
|
||||
if i - 1 >= min_i:
|
||||
i -= 1
|
||||
else:
|
||||
i = min_i
|
||||
continue
|
||||
|
||||
|
||||
with open("{}/{}_{}.in".format(fuzzer_input, self.file_name, i), 'w') as f:
|
||||
f.write(self.fuzzer.source)
|
||||
with open("fuzzer/debug/{}_{}.xml".format(self.file_name, i), 'w') as f:
|
||||
with open("{}/{}_{}.xml".format(fuzzer_debug, self.file_name, i), 'w') as f:
|
||||
f.write(pretty)
|
||||
# y.write(self.fuzzer.out)
|
||||
# y.write(self.fuzzer.out)
|
||||
# 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:
|
||||
# f.write(self.fuzzer.out)
|
||||
print("test {}/{} generated".format(i, self.batch))
|
||||
i += 1
|
||||
min_i = i
|
||||
|
||||
|
||||
def handler(signum, frame):
|
||||
print("Execution Timeout")
|
||||
raise TimeoutError
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Procedurally generate a test case for Gazprea'
|
||||
)
|
||||
|
@ -94,5 +176,3 @@ if __name__ == '__main__':
|
|||
args = parser.parse_args()
|
||||
fuzzer = Fuzzer(config=args.config_file, batch=args.batch_size, seed=args.seed)
|
||||
fuzzer.fuzz()
|
||||
|
||||
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"inDir": "<inDir>",
|
||||
"outDir": "<outDir>",
|
||||
"inStrDir": "<inStrDir>",
|
||||
"inDir": "input",
|
||||
"outDir": "outputs",
|
||||
"inStrDir": "instream",
|
||||
"testedExecutablePaths": {
|
||||
"<team id>": "<path_to_gazc_exe>"
|
||||
"fuzzer": "../exec/gazc"
|
||||
},
|
||||
"runtimes": {
|
||||
"<team id>": "<path_to_libgazrt.so>"
|
||||
"fuzzer": "../exec/libgazrt.so"
|
||||
},
|
||||
"toolchains": {
|
||||
"gazprea": [
|
||||
|
|
Loading…
Reference in a new issue