Replace elidable asserts in shared check_gpu_graphics_load_binary.py harness with require()

Replace all 31 assert statements in check_gpu_graphics_load_binary.py
(including side-effecting calls in memory_descriptor(), construct(), and main())
with _gpu_graphics_oracle_check.require() so python3 -O runs every required
setup call and invariant check across all five graphics binary oracles.
Add check_gpu_graphics_load_binary.py to the AST no-assert offline test.
This commit is contained in:
morebrackets
2026-12-02 16:22:30 -08:00
parent 1e95a63382
commit e8dfdfc652
2 changed files with 36 additions and 32 deletions

View File

@ -25,11 +25,14 @@ SCRIPTS = (
class GpuGraphicsBinaryOraclesTests(unittest.TestCase):
def test_no_elidable_asserts_and_check_flag_wired(self) -> None:
for rel in SCRIPTS:
for rel in ("tools/check_gpu_graphics_load_binary.py", *SCRIPTS):
src = (ROOT / rel).read_text()
tree = ast.parse(src, filename=rel)
asserts = [node.lineno for node in ast.walk(tree) if isinstance(node, ast.Assert)]
self.assertEqual(asserts, [], f"{rel} still contains assert at lines {asserts}")
for rel in SCRIPTS:
src = (ROOT / rel).read_text()
tree = ast.parse(src, filename=rel)
self.assertIn("--check", src, f"{rel} missing --check flag")
self.assertIn("write_or_check_text", src, f"{rel} missing write_or_check_text")
self.assertIn("write_or_check_json", src, f"{rel} missing write_or_check_json")

View File

@ -12,6 +12,7 @@ from pathlib import Path
import struct
import sys
import warnings
from _gpu_graphics_oracle_check import require
warnings.filterwarnings("ignore", category=UserWarning)
from capstone import CS_ARCH_X86, CS_MODE_64, Cs # noqa: E402
@ -60,7 +61,7 @@ CLASSES = (("Graphics", GR), ("KernelGraphics", KGR), ("GraphicsManager", GRMGR)
("MemoryManager", MEMMGR), ("KernelFifo", KFIFO), ("OBJFIFO", OFIFO))
raw = ORIGINAL.read_bytes()
assert hashlib.sha256(raw).hexdigest() == DIGEST
require(hashlib.sha256(raw).hexdigest() == DIGEST)
# Binders and property initializers per class, as this harness was validated with them
# (reports/gv100-engine-hal-bindings.json of 2026-11-19 before its regeneration). They are
# pinned here because that report is regenerated with other contents: it now also lists
@ -125,7 +126,7 @@ for name, blob in relocations.items():
elif kind == 10:
struct.pack_into("<I", data, offset, target + addend)
else:
assert kind == 11, kind
require(kind == 11, kind)
struct.pack_into("<i", data, offset, target + addend)
@ -181,7 +182,7 @@ def text(address):
def allocate(size, fill=0):
address = state["heap"]
state["heap"] += (size + 0x1f) & ~0xf
assert state["heap"] < HEAP + HEAP_SIZE
require(state["heap"] < HEAP + HEAP_SIZE)
cpu.mem_write(address, bytes([fill]) * size)
return address
@ -241,7 +242,7 @@ def call(target, *arguments, stack=()):
registers = {name: hex(cpu.reg_read(register)) for name, register in THUNKS.items()}
raise RuntimeError(f"{error} at {pc:#x} {function_at(pc)}\ncalls {calls[-16:]}\n"
f"events {state['events'][-12:]}\n{registers}\n{listing(pc)}") from error
assert cpu.reg_read(UC_X86_REG_RIP) == STOP, target
require(cpu.reg_read(UC_X86_REG_RIP) == STOP, target)
return cpu.reg_read(UC_X86_REG_RAX)
@ -278,24 +279,24 @@ def read_value(offset):
def read_register(a):
assert (a[0], a[1] & 0xffffffff, a[2] & 0xffffffff) == (GPU + 0x4120, 0, 0)
require((a[0], a[1] & 0xffffffff, a[2] & 0xffffffff) == (GPU + 0x4120, 0, 0))
return read_value(a[3] & 0xffffffff)
def write_register(a):
assert (a[0], a[1] & 0xffffffff, a[2] & 0xffffffff) == (GPU + 0x4120, 0, 0)
require((a[0], a[1] & 0xffffffff, a[2] & 0xffffffff) == (GPU + 0x4120, 0, 0))
emit(f"w:{a[3] & 0xffffffff:x}:{a[4] & 0xffffffff:x}")
return 0
def mapped_read(a):
"""_nv049612rm(gpu, mapping, offset): the 32-bit read of a register object."""
assert (a[0], a[1]) == (GPU, MAPPING)
require((a[0], a[1]) == (GPU, MAPPING))
return read_value(a[2] & 0xffffffff)
def mapped_write(a):
assert (a[0], a[1]) == (GPU, MAPPING)
require((a[0], a[1]) == (GPU, MAPPING))
emit(f"w:{a[2] & 0xffffffff:x}:{a[3] & 0xffffffff:x}")
return 0
@ -391,7 +392,7 @@ install(FUSE_118, fuse_query)
install(FUSE_100, fuse_read)
# MemorySystem+fa8 (_nv045299rm), the ZBC table service: (gpu, memory system, argument).
def zbc_service(a):
assert b(GR + 0x1593) == 1
require(b(GR + 0x1593) == 1)
emit(f"zbc:{a[2] & 0xffffffff:x}")
return 0
@ -467,7 +468,7 @@ V100_BUFFERS = (0x7fdf80000, 0x7fdf90000, 0x10000) # KernelGmmu+410, +408 and t
def memory_descriptor(address, size, space=2):
"""A descriptor made by the original _nv043694rm and described with _nv043698rm."""
slot = allocate(8)
assert call("_nv043694rm", slot, GPU, size, 0x1000, 1, space, stack=(2, 0)) & 0xffffffff == 0
require(call("_nv043694rm", slot, GPU, size, 0x1000, 1, space, stack=(2, 0)) & 0xffffffff == 0)
call("_nv043698rm", q(slot), space, address, size)
return q(slot)
@ -504,9 +505,9 @@ def construct(case):
wq(GPU + offset, target)
# The engine table from the device-info registers of the physical V100.
state["phase"] = "table"
assert call(q(KFIFO + 0x1a8), GPU, KFIFO) & 0xffffffff == 0
require(call(q(KFIFO + 0x1a8), GPU, KFIFO) & 0xffffffff == 0)
state["phase"] = "init"
assert function_at(q(MEMSYS + 0xfa8)) == "_nv045299rm"
require(function_at(q(MEMSYS + 0xfa8)) == "_nv045299rm")
wq(MEMSYS + 0xfa8, ZBC_FA8)
wq(GPU + 0x1ea0, MC)
wq(FUSE + 0x118, FUSE_118)
@ -523,8 +524,8 @@ def construct(case):
wq(LPWR + 0x11d8, CHILD)
wq(LPWR + 0x11c0, PGCTRL)
# The constructor of the base object at Graphics+100 (the +1a0 pointer): +38 = -1.
assert q(GR + 0x1a0) == GR + 0x100
assert call("_nv037808rm", GR + 0x100) & 0xffffffff == 0
require(q(GR + 0x1a0) == GR + 0x100)
require(call("_nv037808rm", GR + 0x100) & 0xffffffff == 0)
stages = {}
for name, arguments in (("_nv033946rm", (GPU, GRMGR, 0)),):
stages[name] = call(name, *arguments) & 0xffffffff
@ -558,7 +559,7 @@ def fixture(name, handler):
def engine_busy(a):
"""_nv027871rm(gpu, fifo, engine, 0, &busy, 0): the FIFO engine status query."""
assert a[2] & 0xffffffff == 0xd334df00
require(a[2] & 0xffffffff == 0xd334df00)
status, busy = pop("busy_status", 0), pop("busy", 0)
if status == 0:
# The original stores the answer only when its engine lookups succeed.
@ -578,10 +579,10 @@ def overrides(a):
def timeout_check(a):
assert a[0] == GPU + 0x1e08
require(a[0] == GPU + 0x1e08)
status = pop("check", 0)
state["checks"] += 1
assert state["checks"] < 4000, "the case never ends its wait"
require(state["checks"] < 4000, "the case never ends its wait")
emit(f"tc:{a[2] & 0xffffffff:x}:{status:x}")
return status
@ -621,7 +622,7 @@ def prepare(case):
case["reads"].items() if offset in V100_GEOMETRY})
geometry["reads"][0x200] = list(V100_GEOMETRY[0x200])
stages = construct(geometry)
assert not any(stages.values()), stages
require(not any(stages.values()), stages)
CACHE[key] = (bytes(cpu.mem_read(OBJ, OBJ_SIZE)),
bytes(cpu.mem_read(HEAP, state["heap"] - HEAP)), state["heap"],
{name: bytes(cpu.mem_read(BASES[name], len(sections[name])))
@ -711,7 +712,7 @@ def run(kind, **changes):
status = call(entry, GPU, GR, timeout, 1) & 0xffffffff
else:
status = call(entry, GPU, GR, case["flags"]) & 0xffffffff
assert not state["continuations"]
require(not state["continuations"])
events = list(state["events"])
info = q(GRMGR + 0x2b0)
outputs = ([f"{b(info + offset):x}" for offset in FLAGS]
@ -775,7 +776,7 @@ def geometry(words, fuse):
def main():
assert pop("check", 0) == 0
require(pop("check", 0) == 0)
# The traced V100: every body original except time, diagnostics, the VBIOS table, the
# LPWR query and the MemorySystem ZBC service.
reads, tokens = transcript_case()
@ -818,15 +819,15 @@ def main():
and name not in ("_nv027871rm", "_nv033505rm",
"_nv027641rm", "_nv029884rm")})
paths = paths[-len(physical):]
assert [token for token, path in paths] == physical
require([token for token, path in paths] == physical)
registers = [event for event in physical if event[0] in "rw" and event[1] == ":"]
service = physical.index("zbc:0") if "zbc:0" in physical else None
assert status == 0 and service is not None
require(status == 0 and service is not None)
before = sum(1 for event in physical[:service] if event[0] in "rw" and event[1] == ":")
zbc = ZBC_LINES[1] - ZBC_LINES[0] + 1
assert before == ZBC_LINES[0] - FIRST_LINE, before
assert registers == tokens[:before] + tokens[before + zbc:], "transcript"
assert not any(event[0] in "eAS" and event[1] == ":" for event in physical)
require(before == ZBC_LINES[0] - FIRST_LINE, before)
require(registers == tokens[:before] + tokens[before + zbc:], "transcript")
require(not any(event[0] in "eAS" and event[1] == ":" for event in physical))
# Attribution of every transcript line to the original call path that made it.
steps, line, previous, start = [], FIRST_LINE, None, FIRST_LINE
for token, path in paths:
@ -850,7 +851,7 @@ def main():
previous, start = key, line
line += width
steps.append([start, line - 1, " > ".join(previous)])
assert line - 1 == LAST_LINE
require(line - 1 == LAST_LINE)
merged = []
for first, last, path in steps:
# Collapse the per-TPC repetition of the GPC loop into one entry per run of paths.
@ -882,20 +883,20 @@ def main():
# The same traced inputs with the first class-enable byte (Graphics+160c) cleared by hand.
# No original GV100 path does that (see class_enable_stores); this shows what the byte gates.
case, status, compute = tracked_run(dict(v100, gr={0x160c: 0}), [])
assert status == 0 and "zbc:0" not in compute
require(status == 0 and "zbc:0" not in compute)
compute_registers = [event for event in compute if event[0] in "rw" and event[1] == ":"]
def written(events):
return {int(event.split(":")[1], 16) for event in events if event.startswith("w:")}
dropped = sorted(written(registers) - written(compute_registers))
assert not written(compute_registers) - written(registers)
require(not written(compute_registers) - written(registers))
position, skipped = 0, 0
for event in registers:
if position < len(compute_registers) and compute_registers[position] == event:
position += 1
else:
skipped += 1
assert position == len(compute_registers), "the reduced sequence is not a subsequence"
require(position == len(compute_registers), "the reduced sequence is not a subsequence")
compute_only = {
"how": "Graphics+160c written 0 by the fixture before _nv033665rm; no original GV100 "
"path stores 0 there",
@ -990,7 +991,7 @@ def main():
out = ROOT / "target/gpu-stage"
out.mkdir(parents=True, exist_ok=True)
table = struct.unpack("<96I", cpu.mem_read(by_name["_nv053876rm"], 384))
assert all(table[index - 1] == -(-(1 << 23) // index) for index in range(1, 97))
require(all(table[index - 1] == -(-(1 << 23) // index) for index in range(1, 97)))
rows.insert(0, "T;" + " ".join(f"{value:x}" for value in table))
oracle = "\n".join(rows) + "\n"
(out / "original-graphics-load.txt").write_text(oracle)
@ -1068,7 +1069,7 @@ if __name__ == "__main__":
for binder, (second, third) in binder_order().items():
# A two-argument binder shows 1 for the argument it does not take.
rm_first = third > second and second > 1
assert rm_first == (binder in RM_FIRST), binder
require(rm_first == (binder in RM_FIRST), binder)
print(binder, second, third, "(rm, chip)" if rm_first else "(chip, rm) or chip only")
elif "--disassemble" in sys.argv:
for name in sys.argv[sys.argv.index("--disassemble") + 1:]: