Skip to content

Commit

Permalink
feat(fuzzer,runner): simplify code, enable parallelism and implement …
Browse files Browse the repository at this point in the history
…fuzzing of spirv-opt
  • Loading branch information
rayanht committed May 3, 2022
1 parent beface7 commit ec00929
Show file tree
Hide file tree
Showing 13 changed files with 534 additions and 396 deletions.
60 changes: 37 additions & 23 deletions amber_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import platform
import subprocess
import tempfile
from functools import reduce
from itertools import repeat
from operator import iconcat
Expand All @@ -11,8 +12,12 @@
from google.cloud import bigquery
from google.cloud import storage

from run import *
from src.monitor import Event
from src.monitor import Monitor
from src.shader_brokerage import download_shader_from_gcs
from src.shader_utils import create_amber_file
from src.shader_utils import SPIRVShader
from src.utils import get_spirvsmith_version

AMBER_PATH = "bin/amber"
Expand Down Expand Up @@ -50,12 +55,6 @@ def get_pending_shaders_query(
"""


def fetch_amber_file_from_GCS(shader_id: str) -> None:
print(f"Fetching shader {shader_id} from GCS...")
blob = BUCKET.blob(f"{shader_id}/out.amber")
blob.download_to_filename("tmp.amber")


def insert_BQ_entry(
original_entry,
platform_os: str,
Expand Down Expand Up @@ -92,7 +91,7 @@ def insert_BQ_entry(
BQ_CLIENT.query(delete_query).result()


def run_amber(n_buffers: int, shader_id: str) -> str:
def run_amber(amber_filename: str, n_buffers: int, shader_id: str) -> str:
process: subprocess.CompletedProcess = subprocess.run(
[
AMBER_PATH,
Expand All @@ -107,12 +106,11 @@ def run_amber(n_buffers: int, shader_id: str) -> str:
zip(repeat("-B"), [f"pipeline:0:{i}" for i in range(n_buffers)]),
[],
),
"tmp.amber",
amber_filename,
],
capture_output=True,
)
if process.stderr:
print(process.stderr.decode("utf-8"))
MONITOR.error(
event=Event.AMBER_FAILURE,
extra={
Expand All @@ -123,6 +121,17 @@ def run_amber(n_buffers: int, shader_id: str) -> str:
)
return None

if process.returncode == 139:
MONITOR.error(
event=Event.AMBER_SEGFAULT,
extra={
"stderr": process.stderr.decode("utf-8"),
"cli_args": str(process.args),
"shader_id": shader_id,
},
)
return "SEGFAULT"

MONITOR.info(event=Event.AMBER_SUCCESS, extra={"shader_id": shader_id})

with open("out.txt", "r") as f:
Expand Down Expand Up @@ -176,18 +185,23 @@ def run_amber(n_buffers: int, shader_id: str) -> str:
f"Found {n_pending_shaders} pending shaders for {platform_os}/{hardware_vendor}/{backend}"
)
for row in query_job:
fetch_amber_file_from_GCS(row.shader_id)
buffer_dump: str = run_amber(
n_buffers=row.n_buffers, shader_id=row.shader_id
)
if buffer_dump:
insert_BQ_entry(
row,
platform_os,
hardware_type,
hardware_vendor,
hardware_driver_version,
backend,
row.shader_id,
buffer_dump,
shader: SPIRVShader = download_shader_from_gcs(row.shader_id)
with tempfile.NamedTemporaryFile(suffix=".amber") as amber_file:
create_amber_file(shader, amber_file.name)
buffer_dump: str = run_amber(
amber_filename=amber_file.name,
n_buffers=row.n_buffers,
shader_id=row.shader_id,
)
if buffer_dump:
insert_BQ_entry(
row,
platform_os,
hardware_type,
hardware_vendor,
hardware_driver_version,
backend,
row.shader_id,
buffer_dump,
)
os.remove("out.txt")
4 changes: 4 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class BinariesConfig:
ASSEMBLER_PATH: str = "bin/spirv-as"
VALIDATOR_PATH: str = "bin/spirv-val"
OPTIMISER_PATH: str = "bin/spirv-opt"
CROSS_PATH: str = "bin/spirv-cross"
AMBER_PATH: str = "bin/amber"


Expand Down Expand Up @@ -51,6 +52,9 @@ class FuzzingStrategyConfig:
# P(generating a statement at step t + 1 | a statement was generated at step t)
p_statement: float = 0.995

# Number of optimiser fuzzing iterations
optimiser_fuzzing_iterations: int = 20


@dataclass
class MiscConfig:
Expand Down
2 changes: 1 addition & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
excluded_identifiers = [
"id",
"symbol_table",
# "context",
"context",
"get_required_capabilities",
"iteritems",
"keys",
Expand Down
134 changes: 0 additions & 134 deletions src/amber_generator.py

This file was deleted.

4 changes: 2 additions & 2 deletions src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ class CompositeConstant(Constant):
class OpConstantTrue(ScalarConstant):
type: Type = None

def fuzz(self, context: "Context") -> list[OpCode]:
def fuzz(self, _: "Context") -> list[OpCode]:
self.type = OpTypeBool()
return [self.type, self]


class OpConstantFalse(ScalarConstant):
type: Type = None

def fuzz(self, context: "Context") -> list[OpCode]:
def fuzz(self, _: "Context") -> list[OpCode]:
self.type = OpTypeBool()
return [self.type, self]

Expand Down
12 changes: 3 additions & 9 deletions src/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class Context:
annotations: list[Annotation]
execution_model: ExecutionModel
config: "SPIRVSmithConfig"
monitor: Monitor
extension_sets: dict[str, "OpCode"]

def __init__(
Expand All @@ -58,7 +57,6 @@ def __init__(
annotations: list[Annotation],
execution_model: ExecutionModel,
config: "SPIRVSmithConfig",
monitor: Monitor,
) -> None:
self.id = uuid4()
self.symbol_table = []
Expand All @@ -69,7 +67,6 @@ def __init__(
self.tvc = dict()
self.extension_sets = dict()
self.config = config
self.monitor = monitor

def __eq__(self, other):
if type(other) is type(self):
Expand All @@ -87,9 +84,8 @@ def create_global_context(
cls,
execution_model: ExecutionModel,
config: "SPIRVSmithConfig",
monitor: Monitor,
) -> "Context":
context = cls(None, None, [], execution_model, config, monitor)
context = cls(None, None, [], execution_model, config)
void_type = OpTypeVoid()
main_type = OpTypeFunction()
main_type.return_type = void_type
Expand All @@ -108,7 +104,6 @@ def make_child_context(self, function: OpTypeFunction = None):
self.annotations,
self.execution_model,
self.config,
self.monitor,
)
else:
context = Context(
Expand All @@ -117,7 +112,6 @@ def make_child_context(self, function: OpTypeFunction = None):
self.annotations,
self.execution_model,
self.config,
self.monitor,
)
context.tvc = self.tvc
context.extension_sets = self.extension_sets
Expand Down Expand Up @@ -147,7 +141,7 @@ def get_random_variable(
try:
return random.SystemRandom().choice(variables)
except IndexError:
self.monitor.warning(
Monitor().warning(
event=Event.NO_OPERAND_FOUND,
extra={
"opcode": inspect.stack()[1][0].f_locals["self"].__class__.__name__,
Expand Down Expand Up @@ -313,7 +307,7 @@ def get_random_operand(
try:
return random.SystemRandom().choice(list(statements) + list(constants))
except IndexError:
self.monitor.warning(
Monitor().warning(
event=Event.NO_OPERAND_FOUND,
extra={
"opcode": inspect.stack()[1][0].f_locals["self"].__class__.__name__,
Expand Down
Loading

0 comments on commit ec00929

Please sign in to comment.