"""Unit tests for tools/shader_library.py (the pipeline library in the zlib part of shaders.rpf). python -I tests/test_shader_library.py Zero game data: every library and archive is built here from synthetic bytes. """ import contextlib import hashlib import io import json import os import struct import sys import tempfile import unittest from unittest import mock import zlib ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, "tools")) import shader_disasm # noqa: E402 import shader_library as sl # noqa: E402 import shader_meta # noqa: E402 ENDPGM = struct.pack("> 16) & 0xFFFF, 0): out += struct.pack(".dxbc for its input signature: Unsupported for that shader, not a FileNotFoundError that stops the run.""" import rdna2spirv with tempfile.TemporaryDirectory() as tmp: src = os.path.join(tmp, "lib.bin") with open(src, "wb") as f: f.write(sample()) out = os.path.join(tmp, "out") self.run_main(src, "--out", out, "--stages", "ps") os.makedirs(os.path.join(out, "disasm")) s_path = os.path.join(out, "disasm", "lib_ps_00000.s") with open(s_path, "w", encoding="utf-8", newline="\n") as f: f.write("; lib_ps_00000 code 0x24..0x30 (fspx), gfx1013, 0 invalid words\n\ts_endpgm\n") rdna2spirv._STAGES = None # stage_of caches the first index.json it reads try: with self.assertRaisesRegex(rdna2spirv.Unsupported, "no shader container"): rdna2spirv.translate(s_path) finally: rdna2spirv._STAGES = None def test_carve_reports_corrupt_and_unreadable_containers(self): with tempfile.TemporaryDirectory() as tmp: carve = os.path.join(tmp, "carve") os.makedirs(carve) with open(os.path.join(carve, "PS_A.dxbc"), "wb") as f: f.write(dxbc(D_PS)) with open(os.path.join(carve, "VS_C.dxbc"), "wb") as f: f.write(dxbc(D_VS)[:20]) # truncated header with open(os.path.join(carve, "MS_D.dxbc"), "wb") as f: f.write(dxbc(D_MS)[:-4]) # truncated part payload os.makedirs(os.path.join(carve, "CS_B.dxbc")) # exists on disk, unreadable OSError index = [ {"name": "PS_A", "file": "PS_A.dxbc"}, {"name": "gone", "file": "gone.dxbc"}, # missing: skipped silently {"name": "VS_C", "file": "VS_C.dxbc"}, {"name": "MS_D", "file": "MS_D.dxbc"}, {"name": "CS_B", "file": "CS_B.dxbc"}, ] with open(os.path.join(carve, "index.json"), "w", encoding="utf-8", newline="\n") as f: json.dump(index, f) src = os.path.join(tmp, "lib.bin") with open(src, "wb") as f: f.write(sample()) out = os.path.join(tmp, "out") stdout, stderr = io.StringIO(), io.StringIO() with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr): rc = sl.main([src, "--out", out, "--carve", carve]) self.assertEqual(rc, 0) self.assertIn("1 of 4 named by a carved shader's HASH digest, 3 not in the carve, " "3 unreadable/corrupt containers", stdout.getvalue()) err_lines = stderr.getvalue().splitlines() self.assertEqual(len(err_lines), 3) self.assertEqual(err_lines[0], "carve: VS_C.dxbc: truncated DXBC header") self.assertEqual(err_lines[1], "carve: MS_D.dxbc: truncated DXBC part payload") self.assertTrue(err_lines[2].startswith("carve: CS_B.dxbc: ") and len(err_lines[2]) > len("carve: CS_B.dxbc: ")) self.assertNotIn("gone.dxbc", stderr.getvalue()) self.assertTrue(os.path.isfile(os.path.join(out, "lib_ps_00000.dxbc"))) def test_carve_missing_only_stays_silent(self): with tempfile.TemporaryDirectory() as tmp: carve = os.path.join(tmp, "carve") os.makedirs(carve) with open(os.path.join(carve, "PS_A.dxbc"), "wb") as f: f.write(dxbc(D_PS)) index = [ {"name": "PS_A", "file": "PS_A.dxbc"}, {"name": "gone", "file": "gone.dxbc"}, ] with open(os.path.join(carve, "index.json"), "w", encoding="utf-8", newline="\n") as f: json.dump(index, f) src = os.path.join(tmp, "lib.bin") with open(src, "wb") as f: f.write(sample()) out = os.path.join(tmp, "out") stdout, stderr = io.StringIO(), io.StringIO() with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr): rc = sl.main([src, "--out", out, "--carve", carve]) self.assertEqual(rc, 0) self.assertEqual(stderr.getvalue(), "") self.assertIn("1 of 4 named by a carved shader's HASH digest, 3 not in the carve\n", stdout.getvalue()) self.assertNotIn("unreadable/corrupt", stdout.getvalue()) def test_carve_single_corrupt_container_and_direct_api(self): with tempfile.TemporaryDirectory() as tmp: carve = os.path.join(tmp, "carve") os.makedirs(carve) with open(os.path.join(carve, "PS_A.dxbc"), "wb") as f: f.write(dxbc(D_PS)) with open(os.path.join(carve, "ZERO.dxbc"), "wb") as f: f.write(bytes(64)) # valid length/count=0, wrong magic index = [ {"name": "PS_A", "file": "PS_A.dxbc"}, {"name": "ZERO", "file": "ZERO.dxbc"}, ] with open(os.path.join(carve, "index.json"), "w", encoding="utf-8", newline="\n") as f: json.dump(index, f) direct_err = io.StringIO() with contextlib.redirect_stderr(direct_err): digests = sl.carve_digests(carve) self.assertEqual(digests, {D_PS: [("PS_A", os.path.join(carve, "PS_A.dxbc"))]}) self.assertEqual(direct_err.getvalue().splitlines(), ["carve: ZERO.dxbc: not a DXBC container"]) src = os.path.join(tmp, "lib.bin") with open(src, "wb") as f: f.write(sample()) out = os.path.join(tmp, "out") stdout, stderr = io.StringIO(), io.StringIO() with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr): rc = sl.main([src, "--out", out, "--carve", carve]) self.assertEqual(rc, 0) self.assertIn("1 of 4 named by a carved shader's HASH digest, 3 not in the carve, " "1 unreadable/corrupt container\n", stdout.getvalue()) self.assertEqual(stderr.getvalue().splitlines(), ["carve: ZERO.dxbc: not a DXBC container"]) def test_disasm_auto_falls_back_per_program_without_regressing_clean_programs(self): ps_prog = prog(0x11112222) vs_body = bytes.fromhex("02 1a 28 f4 00 00 00 fa") + ENDPGM vs_code = prologue(0x33334444) + vs_body vs_prog = vs_code + CODE_END * ((-len(vs_code)) % 256 // 4) blobs = [layout_blob(2, D_PS), layout_blob(2, D_VS)] graphics = [(NONE, NONE, {0: (0, 0), 1: (1, 1)})] lib_bytes = build(graphics, [], blobs, [ps_prog, vs_prog]) def fake_run_mc(code, mcpu, wave32=True): if code == V_MOV * 2 + ENDPGM: return "\t; mocked llvm-mc ps\n\ts_endpgm\n", 0, "" return "\ts_endpgm\n", 2, ":1:1: warning: invalid instruction encoding\n" with tempfile.TemporaryDirectory() as tmp: src = os.path.join(tmp, "lib.bin") with open(src, "wb") as f: f.write(lib_bytes) out = os.path.join(tmp, "out") with mock.patch.object(shader_disasm, "probe") as probe_mock, \ mock.patch.object(shader_disasm, "run_mc", side_effect=fake_run_mc): rc, _ = self.run_main(src, "--out", out, "--disasm", "--jobs", "1") self.assertEqual(rc, 0) probe_mock.assert_called_once_with("gfx1013") with open(os.path.join(out, "disasm", "lib_ps_00000.s"), encoding="utf-8") as f: ps_text = f.read() self.assertIn("; mocked llvm-mc ps", ps_text) self.assertIn("0 invalid words", ps_text) self.assertNotIn("; --- llvm-mc messages ---", ps_text) with open(os.path.join(out, "disasm", "lib_vs_00001.s"), encoding="utf-8") as f: vs_text = f.read() self.assertIn("0 invalid words", vs_text) self.assertIn("[s104,s105,vcc_lo,vcc_hi]", vs_text) self.assertNotIn("; --- llvm-mc messages ---", vs_text) with open(os.path.join(out, "index.json"), encoding="utf-8") as f: by = {e["file"]: e for e in json.load(f)} self.assertEqual(by["lib_ps_00000.dxbc"]["invalid_words"], 0) self.assertEqual(by["lib_vs_00001.dxbc"]["invalid_words"], 0) if __name__ == "__main__": unittest.main()