Skip to content

Commit

Permalink
fix(fuzzer): replace use of pseudo-random generator by system random …
Browse files Browse the repository at this point in the history
…generator
  • Loading branch information
rayanht committed Apr 17, 2022
1 parent 180d2ec commit fa95d3c
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/amber_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class AmberStructMember:
class AmberStructDeclaration:
name: str
members: list[AmberStructMember]

def to_amberscript(self):
return f"STRUCT {self.name}\n{chr(10).join([f'{member.type.value} {member.name}' for member in self.members])}\nEND"

Expand Down
2 changes: 1 addition & 1 deletion src/amber_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def run_amber():
"spv1.3",
"-v",
"1.2",
f"tmp.amber",
"tmp.amber",
],
capture_output=True,
)
Expand Down
2 changes: 1 addition & 1 deletion src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class OpConstantComposite(CompositeConstant):
constituents: Tuple[OpCode] = None

def fuzz(self, context: "Context") -> List[OpCode]:
composite_type = random.choice(
composite_type = random.SystemRandom().choice(
[OpTypeArray, OpTypeVector] # , OpTypeMatrix]
)().fuzz(context)
self.type: OpTypeArray | OpTypeVector = composite_type[-1]
Expand Down
10 changes: 7 additions & 3 deletions src/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,15 @@ def gen_global_variables(self):
for i in range(random.SystemRandom().randint(1, 3)):
variable = self.create_on_demand_variable(StorageClass.StorageBuffer)
if len(self.tvc) != n:
self.add_annotation(OpDecorate(None, variable.type.type, Decoration.Block))
self.add_annotation(
OpDecorate(None, variable.type.type, Decoration.Block)
)
self.add_annotation(
OpDecorate(None, variable, Decoration.DescriptorSet, (0,))
)
self.add_annotation(OpDecorate(None, variable, Decoration.Binding, (i,)))
self.add_annotation(
OpDecorate(None, variable, Decoration.Binding, (i,))
)
offset = 0
for j, t in enumerate(variable.type.type.types):
self.add_annotation(
Expand Down Expand Up @@ -355,7 +359,7 @@ def create_on_demand_variable(
if type:
pointer_type.type = type
else:
pointer_type.type = random.choice(
pointer_type.type = random.SystemRandom().choice(
list(
filter(
lambda tvc: isinstance(tvc, OpTypeStruct),
Expand Down
10 changes: 5 additions & 5 deletions src/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(
function_type: OpTypeFunction = None,
) -> None:
self.return_type: Type = return_type
self.function_control_mask: FunctionControlMask = random.choice(
self.function_control_mask: FunctionControlMask = random.SystemRandom().choice(
list(FunctionControlMask)
)
self.function_type: OpTypeFunction = function_type
Expand Down Expand Up @@ -96,7 +96,7 @@ def fuzz(self, context: "Context") -> List[OpCode]:
true_label = if_block[0]
false_label = else_block[0]
try:
condition = random.choice(
condition = random.SystemRandom().choice(
context.get_statements(
lambda s: not isinstance(s, Untyped)
and isinstance(s.type, OpTypeBool)
Expand All @@ -123,7 +123,7 @@ def fuzz(self, context: "Context") -> List[OpCode]:
# loop_label = OpLabel().fuzz(context)[0]
# block = fuzz_block(context, loop_label)
# try:
# condition = random.choice(context.get_statements(
# condition = random.SystemRandom().choice(context.get_statements(
# lambda s: not isinstance(s, Untyped) and isinstance(s.type, OpTypeBool)
# ))
# except IndexError:
Expand All @@ -143,12 +143,12 @@ def fuzz(self, context: "Context") -> List[OpCode]:
# def fuzz(self, context: "Context") -> Tuple[OpCode]:
# if context.get_depth() > context.limits.max_depth:
# return []
# self.selection_control = random.choice(list(SelectionControlMask))
# self.selection_control = random.SystemRandom().choice(list(SelectionControlMask))
# self.default_label = OpLabel().fuzz(context)[0]
# self.case_labels = []
# for _ in range(random.randint(1, 5)):
# self.case_labels.append(OpLabel().fuzz(context)[0])
# self.value = random.choice(context.get_statements(
# self.value = random.SystemRandom().choice(context.get_statements(
# lambda s: not isinstance(s, Untyped) and isinstance(s.type, OpTypeBool)
# )
# ).fuzz(context)[0]
Expand Down
4 changes: 2 additions & 2 deletions src/fuzzing_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ def validate(self, shader: SPIRVShader, opt: bool = False):
return process.returncode == 0

def gen_shader(self) -> SPIRVShader:
# execution_model = random.choice(list(ExecutionModel))
# execution_model = random.choice(
# execution_model = random.SystemRandom().choice(list(ExecutionModel))
# execution_model = random.SystemRandom().choice(
# [ExecutionModel.GLCompute, ExecutionModel.Kernel]
# )
execution_model = ExecutionModel.GLCompute
Expand Down
14 changes: 7 additions & 7 deletions src/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def fuzz(self, context: "Context") -> list[OpCode]:
self.context = context
dynamic = False
try:
self.type = random.choice(
self.type = random.SystemRandom().choice(
list(
filter(
lambda t: isinstance(t, OpTypePointer)
Expand Down Expand Up @@ -63,7 +63,7 @@ def fuzz(self, context: "Context") -> list[OpCode]:
else StorageClass.Input
)
if storage_class == target_storage_class:
self.type.type = random.choice(
self.type.type = random.SystemRandom().choice(
list(
filter(
lambda tvc: isinstance(tvc, (OpTypeStruct))
Expand All @@ -73,7 +73,7 @@ def fuzz(self, context: "Context") -> list[OpCode]:
)
)
else:
self.type.type = random.choice(
self.type.type = random.SystemRandom().choice(
list(
filter(
lambda tvc: isinstance(tvc, (OpTypeStruct)),
Expand All @@ -93,7 +93,7 @@ class OpLoad(MemoryOperator):
# memory_operands: Optional[???]

def fuzz(self, context: "Context") -> List[OpCode]:
variable: OpVariable = random.choice(
variable: OpVariable = random.SystemRandom().choice(
list(
filter(
lambda x: x.storage_class != StorageClass.Output,
Expand All @@ -114,7 +114,7 @@ class OpStore(MemoryOperator, OpCode, Untyped, VoidOp):
def fuzz(self, context: "Context") -> List[OpCode]:
dynamic = False
try:
object: Statement = random.choice(
object: Statement = random.SystemRandom().choice(
context.get_statements(
lambda sym: not isinstance(sym, (OpVariable, Untyped))
)
Expand All @@ -132,7 +132,7 @@ def fuzz(self, context: "Context") -> List[OpCode]:
):
filtered_variables.append(variable)
try:
variable = random.choice(filtered_variables)
variable = random.SystemRandom().choice(filtered_variables)
except IndexError:
variable = context.create_on_demand_variable(
StorageClass.Function, object.type
Expand All @@ -151,7 +151,7 @@ def fuzz(self, context: "Context") -> List[OpCode]:
# indexes: List[OpCode] = []

# def fuzz(self, context: "Context") -> List[OpCode]:
# self.base: OpVariable = random.choice(
# self.base: OpVariable = random.SystemRandom().choice(
# context.get_local_variables() + context.get_global_variables()
# )
# self.type: Type = self.base.type.type
Expand Down
24 changes: 12 additions & 12 deletions src/types/concrete_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class OpTypeVector(UniformContainerType, ArithmeticType):

def fuzz(self, context: "Context") -> List[OpCode]:
self.type = ScalarType().fuzz(context)[0]
self.size = random.choice([2, 3, 4]) # 8, 16])
self.size = random.SystemRandom().choice([2, 3, 4]) # 8, 16])
return [self.type, self]

def __len__(self):
Expand All @@ -85,7 +85,7 @@ def fuzz(self, context: "Context") -> List[OpCode]:
matrix_type = OpTypeVector().fuzz(context)[-1]
matrix_type.type = OpTypeFloat().fuzz(context)[0]
self.type = matrix_type
self.size = random.choice([2, 3, 4]) # 8, 16])
self.size = random.SystemRandom().choice([2, 3, 4]) # 8, 16])
return [matrix_type.type, matrix_type, self]

def __len__(self):
Expand Down Expand Up @@ -117,12 +117,12 @@ def get_base_type(self):
# # will randomly pick from the reparametrized
# # probability distribution
# raise ReparametrizationError
# self.dim = random.choice(list(Dim))
# self.depth = random.choice([0, 1, 2])
# self.arrayed = random.choice([0, 1])
# self.MS = random.choice([0, 1])
# self.sampled = random.choice([0, 1, 2])
# self.image_format = random.choice(list(ImageFormat))
# self.dim = random.SystemRandom().choice(list(Dim))
# self.depth = random.SystemRandom().choice([0, 1, 2])
# self.arrayed = random.SystemRandom().choice([0, 1])
# self.MS = random.SystemRandom().choice([0, 1])
# self.sampled = random.SystemRandom().choice([0, 1, 2])
# self.image_format = random.SystemRandom().choice(list(ImageFormat))
# self.type = ScalarType().fuzz(context)[0]
# return [self.type, self]

Expand Down Expand Up @@ -163,7 +163,7 @@ def fuzz(self, context: "Context") -> List[OpCode]:
side_effect_types = []
for _ in range(random.randint(2, 5)):
# TODO relax parameter type constraint
parameter_type = random.choice([NumericalType])().fuzz(context)
parameter_type = random.SystemRandom().choice([NumericalType])().fuzz(context)
if parameter_type == []:
continue
self.types.append(parameter_type[-1])
Expand All @@ -181,7 +181,7 @@ class OpTypePointer(UniformContainerType):

def fuzz(self, context: "Context") -> List[OpCode]:
self.storage_class = StorageClass.StorageBuffer
fuzzed_type = random.choice([ScalarType, ContainerType])().fuzz(context)
fuzzed_type = random.SystemRandom().choice([ScalarType, ContainerType])().fuzz(context)
self.type = fuzzed_type[-1]
return [*fuzzed_type, self]

Expand All @@ -194,13 +194,13 @@ def fuzz(self, context: "Context") -> List[OpCode]:
if len(context.get_function_types()) >= context.config.limits.n_functions:
MiscType.set_zero_probability(self.__class__)
raise ReparametrizationError
# return_type = random.choice([ScalarType, ContainerType])().fuzz(context)
# return_type = random.SystemRandom().choice([ScalarType, ContainerType])().fuzz(context)
return_type = [OpTypeVoid()]
self.return_type = return_type[-1]
self.parameter_types = []
all_types = return_type
for _ in range(random.randint(4, 7)):
parameter_type = random.choice([ScalarType, ContainerType])().fuzz(context)
parameter_type = random.SystemRandom().choice([ScalarType, ContainerType])().fuzz(context)
self.parameter_types.append(parameter_type[-1])
all_types += parameter_type
self.parameter_types = tuple(self.parameter_types)
Expand Down
2 changes: 1 addition & 1 deletion utils/inspect_f.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
args = parser.parse_args()

shader_path = f"out/{random.choice(os.listdir('out/'))}/shader.spv"
shader_path = f"out/{random.SystemRandom().choice(os.listdir('out/'))}/shader.spv"
if args.target == "msl":
process: subprocess.CompletedProcess = subprocess.run(
[
Expand Down

0 comments on commit fa95d3c

Please sign in to comment.