diff --git a/make_game.py b/make_game.py index 0f5c499cbc..5423312eca 100644 --- a/make_game.py +++ b/make_game.py @@ -1203,6 +1203,7 @@ def shader_outputs_reusable(work: str, emulate: bool, keep_emulated: bool, return False had = read_shader_setting(os.path.join(spirv, "wave64_emulate.txt"), default="off") return (had == requested_shader_mode(had, emulate, keep_emulated) + and read_shader_setting(os.path.join(dxbc, "carve_sources.sha256")) == carve_sources_digest() and read_shader_setting(os.path.join(spirv, "translator_flags.txt")) == translator_flags_setting(fallbacks) and read_shader_setting(os.path.join(spirv, "translation_sources.sha256")) == translation_sources_digest() and os.path.isfile(os.path.join(dxbc, "disasm.done")) @@ -1241,15 +1242,21 @@ def shaders(game: str, work: str, jobs: int, emulate: bool = False, keep_emulate # Each stage counts as finished only when the file it writes last is there, never because its folder exists # or holds many files: a run stopped part-way leaves both. Re-running a stage drops the later stages' proof. carved = os.path.join(dxbc, "index.json") # shader_carve.py, renamed into place at the end + carve_sources_file = os.path.join(dxbc, "carve_sources.sha256") unpacked = os.path.join(dxbc, "fssm_index.json") # fssm_unpack.py, the same disassembly_started = os.path.join(dxbc, "disasm.started") disassembled = os.path.join(dxbc, "disasm.done") # records LLVM and the listings produced translated = os.path.join(spirv, "index.json") # rdna2spirv.py --merge, renamed into place shard_plan = os.path.join(spirv, SHARD_PLAN) # lets translate() keep finished shards; see there signature = llvm_mc_signature() - if not os.path.isfile(carved): + carve_sources = carve_sources_digest() + if not os.path.isfile(carved) or read_shader_setting(carve_sources_file) != carve_sources: log("shaders: carving shaders.rpf") - drop(translated, shard_plan, disassembled, disassembly_started, unpacked) + drop(carved, translated, shard_plan, disassembled, disassembly_started, unpacked) + os.makedirs(dxbc, exist_ok=True) + # The carve writes its index last, so the stamp alone cannot mark an interrupted run complete. + with open(carve_sources_file, "w", encoding="utf-8", newline="\n") as f: + f.write(carve_sources + "\n") py("shader_carve.py", src, dxbc) if unpacked_shader_stems(dxbc) is None: log("shaders: unpacking native code") @@ -1862,6 +1869,11 @@ def translation_sources_digest() -> str: return sources_digest(TOOLS, TRANSLATION_SOURCES) +def carve_sources_digest() -> str: + """Digest of the tools that produce the DXBC carve and its index.""" + return sources_digest(TOOLS, ("shader_carve.py", "shader_carve_accel.py")) + + def drop(*paths: str) -> None: """Remove the files that mark later stages finished, so they run again on this stage's new output.""" for p in paths: diff --git a/tests/test_shader_carve.py b/tests/test_shader_carve.py index 24a9e7393c..bcef255d93 100644 --- a/tests/test_shader_carve.py +++ b/tests/test_shader_carve.py @@ -183,6 +183,28 @@ def test_shader_model_version_unpacking(self): class EntryMarkerRegexTest(unittest.TestCase): + def test_carved_names_exclude_printable_length_prefix(self): + dxbc = make_dxbc([(b"DXIL", make_dxil_payload(0x65, 5))]) + names = ("A" * 64, "~" + "n" * 125) + entries = b"".join( + b"\x00" + bytes([len(name) + 1]) + make_rpf_entry(name, 2, dxbc) + for name in names + ) + with tempfile.TemporaryDirectory() as tmp: + src = os.path.join(tmp, "synthetic.bin") + out = os.path.join(tmp, "carved") + with open(src, "wb") as f: + f.write(entries) + with contextlib.redirect_stdout(io.StringIO()): + self.assertEqual(shader_carve.main([src, out]), 0) + with open(os.path.join(out, "index.json"), encoding="utf-8") as f: + index = json.load(f) + self.assertEqual([entry["name"] for entry in index], list(names)) + self.assertEqual([entry["file"] for entry in index], [name + ".dxbc" for name in names]) + for entry in index: + with open(os.path.join(out, entry["file"]), "rb") as f: + self.assertEqual(f.read(), dxbc) + def test_valid_entry_pattern_matching(self): entry = make_rpf_entry("valid_name", 2, b"DXBC" + bytes(range(40)), entry_id=100) match = shader_carve.ENTRY.search(entry) diff --git a/tests/test_shader_stage_resume.py b/tests/test_shader_stage_resume.py index e3645dbc5b..86fb6b7749 100644 --- a/tests/test_shader_stage_resume.py +++ b/tests/test_shader_stage_resume.py @@ -100,6 +100,11 @@ def touch(self, *parts): elif p.endswith(".s"): f.write("synthetic listing\n") + def record_current_carve(self): + self.touch(self.dxbc, "index.json") + with open(os.path.join(self.dxbc, "carve_sources.sha256"), "w", encoding="utf-8", newline="\n") as f: + f.write(make_game.carve_sources_digest() + "\n") + def fake_py(self, tool, *args): self.ran.append(tool) if tool == "shader_carve.py": @@ -129,6 +134,21 @@ def test_fresh_folder_runs_every_step_then_nothing(self): self.assertEqual(self.run_stage(), ["shader_carve.py", "fssm_unpack.py", "shader_disasm.py", "translate"]) self.assertEqual(self.run_stage(), []) + def test_legacy_or_stale_carve_rebuilds_all_shader_steps(self): + self.run_stage() + stamp = os.path.join(self.dxbc, "carve_sources.sha256") + for old_stamp in (None, "stale"): + with self.subTest(old_stamp=old_stamp): + if old_stamp is None: + os.remove(stamp) + else: + with open(stamp, "w", encoding="utf-8", newline="\n") as f: + f.write(old_stamp + "\n") + self.assertFalse(make_game.shader_outputs_reusable(self.work, False, False)) + self.assertEqual(self.run_stage(), ["shader_carve.py", "fssm_unpack.py", "shader_disasm.py", "translate"]) + self.assertTrue(make_game.shader_outputs_reusable(self.work, False, False)) + self.assertEqual(self.run_stage(), []) + def test_llvm_version_change_at_the_same_path_refreshes_only_the_dependent_steps(self): wrapper = os.path.join(self.work, "fake llvm-mc.py") with open(wrapper, "w", encoding="utf-8") as f: @@ -174,19 +194,19 @@ def test_version_probe_fails_with_a_clear_error_for_a_failed_command(self): self.assertIn("Check LLVM_MC: distribution not found", stderr.getvalue()) def test_unpack_stopped_after_creating_fssm_folder_runs_again(self): - self.touch(self.dxbc, "index.json") + self.record_current_carve() os.makedirs(os.path.join(self.dxbc, "fssm")) # what a stopped fssm_unpack.py leaves self.assertEqual(self.run_stage(), ["fssm_unpack.py", "shader_disasm.py", "translate"]) def test_disasm_stopped_after_many_files_runs_again(self): - self.touch(self.dxbc, "index.json") + self.record_current_carve() self.touch(self.dxbc, "fssm_index.json") for i in range(1200): # past the old 1000-file threshold self.touch(self.dxbc, "disasm", f"s{i}.s") self.assertEqual(self.run_stage(), ["shader_disasm.py", "translate"]) def test_disassembly_resume_keeps_completed_listings_for_the_same_llvm(self): - self.touch(self.dxbc, "index.json") + self.record_current_carve() self.touch(self.dxbc, "fssm_index.json") listing = os.path.join(self.dxbc, "disasm", "s0.s") @@ -218,7 +238,7 @@ def check_reuse(tool, *args): self.assertFalse(os.path.exists(os.path.join(self.dxbc, "disasm.started"))) def test_finished_disassembly_repairs_missing_recorded_listing_before_translation(self): - self.touch(self.dxbc, "index.json") + self.record_current_carve() self.touch(self.dxbc, "fssm_index.json") with open(os.path.join(self.dxbc, "index.json"), "w", encoding="utf-8", newline="\n") as f: json.dump([{"file": name + ".dxbc"} for name in ("s0", "s1", "unsupported")], f) @@ -288,7 +308,7 @@ def test_translation_stopped_after_many_files_runs_again(self): self.assertEqual(self.run_stage(), ["translate"]) def test_failed_disasm_leaves_no_marker(self): - self.touch(self.dxbc, "index.json") + self.record_current_carve() self.touch(self.dxbc, "fssm_index.json") def failing_py(tool, *args): @@ -322,7 +342,7 @@ def test_old_disassembly_with_a_noncurrent_shader_is_refreshed(self): self.assertEqual(self.run_stage(), []) def test_legacy_folder_without_llvm_version_refreshes_disassembly_once(self): - self.touch(self.dxbc, "index.json") + self.record_current_carve() self.touch(self.dxbc, "fssm_index.json") self.touch(self.dxbc, "disasm", "s0.s") self.touch(self.spirv, "index.json") # translated by a build from before disasm.done @@ -333,7 +353,7 @@ def test_legacy_folder_without_llvm_version_refreshes_disassembly_once(self): def test_legacy_completion_marker_with_a_missing_listing_disassembles_everything_again(self): # A disasm.done from before the listings were recorded holds only the llvm-mc signature: with a listing # missing it can't tell which ones are current, so the whole disassembly runs again, not a resume. - self.touch(self.dxbc, "index.json") + self.record_current_carve() self.touch(self.dxbc, "fssm_index.json") self.touch(self.spirv, "index.json") with open(os.path.join(self.dxbc, "disasm.done"), "w", encoding="utf-8", newline="\n") as f: @@ -353,7 +373,7 @@ def disassemble(tool, *args): self.assertEqual(self.run_stage(), []) def test_old_disassembly_layout_is_rebuilt_in_the_current_location(self): - self.touch(self.dxbc, "index.json") + self.record_current_carve() self.touch(self.dxbc, "fssm_index.json") self.touch(self.work, "shaders", "disasm", "old.s") # disasm next to the carve folder, the old layout self.touch(self.spirv, "index.json") @@ -470,13 +490,16 @@ def test_an_unreadable_sources_file_translates_again(self): self.assertEqual(self.run_stage(), ["translate"]) self.assertEqual(self.run_stage(), []) - def test_editing_any_translation_source_translates_again_once(self): + def test_editing_shader_sources_rebuilds_the_dependent_steps_once(self): tools = self.copy_tools() self.run_stage() for name in make_game.TRANSLATION_SOURCES: with open(os.path.join(tools, name), "ab") as f: f.write(b"\n") - self.assertEqual(self.run_stage(), ["translate"], name) + expected = ["translate"] + if name in ("shader_carve.py", "shader_carve_accel.py"): + expected = ["shader_carve.py", "fssm_unpack.py", "shader_disasm.py", "translate"] + self.assertEqual(self.run_stage(), expected, name) self.assertEqual(self.run_stage(), [], name) def test_editing_another_tool_or_changing_jobs_keeps_the_translation(self): diff --git a/tools/shader_carve.py b/tools/shader_carve.py index 0a3f501398..917eedc02f 100644 --- a/tools/shader_carve.py +++ b/tools/shader_carve.py @@ -2,7 +2,8 @@ Each shader in shaders.rpf is stored as: - 00 + 00 + and inflates to a DXBC container (`size` bytes) holding DXIL plus an Xbox PRIV part. @@ -26,6 +27,7 @@ from replace_retry import replace as replace_retry # noqa: E402 from shader_carve_accel import entry_search # noqa: E402 +# Scan for candidate headers with the same pattern as the optional native scanner. ENTRY = re.compile(rb"([\x21-\x7e]{3,200})\x00(.)(....)(....)\3(....)", re.DOTALL) STAGES = {0: "ps", 1: "vs", 2: "gs", 3: "hs", 4: "ds", 5: "cs", 6: "lib", 13: "ms", 14: "as"} RESERVED = {"CON", "PRN", "AUX", "NUL", *(f"COM{i}" for i in range(1, 10)), *(f"LPT{i}" for i in range(1, 10))} @@ -103,7 +105,14 @@ def main(argv=None) -> int: # A rejected header is rescanned past its name (a real entry can start inside it); a carved # shader resumes after its stored bytes, so deflate output never becomes a name or a match. pos = mt.end(1) + 1 - name = mt.group(1).decode("latin-1") + name_bytes = mt.group(1) + name_start = mt.start(1) + # A printable length byte can be inside the match. A length byte just outside + # it takes precedence, even if the real first character also looks like a length. + if (name_bytes[0] == len(name_bytes) + and (name_start == 0 or m[name_start - 1] != len(name_bytes) + 1)): + name_bytes = name_bytes[1:] + name = name_bytes.decode("latin-1") stored = struct.unpack("