"""A simulated-turf list that a program reads the game's way never grows past the game's size (11-29 GPU hang). python -I -B tests/test_sim_turf_hold.py [--cxx clang++] Cuts `SimTurfPrivate` and `sim_turf_hold` out of gpu/vk/vk_draw.cpp and builds them against the real gpu/vk/sim_turf_capacity.h, then plays the 11-29 crash at them. His Kokiri Forest edit asked for 132033 of the blade list's 143360 records; the list grew to 165056, and the pool producer (3de6d86ec7e6720e, left as the game has it by the IR pass) read record 143359 - i for i past 143359, far outside the buffer: the dispatch never ended, device lost. Checks: without a hold the list grows (as before: the control); held, it stays at the game's size under the same demand; a list already grown when the hold comes is made the game's size again at the next install. """ from __future__ import annotations import argparse import os import subprocess import sys import tempfile from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) # (python -I leaves the check's own folder off the path) from srccut import block # noqa: E402 RUN_ALL_ARGS = ["--cxx", "{cxx}"] ROOT = Path(__file__).resolve().parents[1] HARNESS = r""" #include #include #include #include "sim_turf_capacity.h" using VkBuffer = void*; using VmaAllocation = void*; #define VK_NULL_HANDLE nullptr namespace Shader::Optimization { inline constexpr uint32_t kSimTurfBladeCap = 143360; inline constexpr uint32_t kSimTurfPoolCap = 138240; } namespace mkwvk { @CUT@; @HOLD@ } #define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); std::exit(1); } } while (0) int main() { using mkwvk::SimTurfPrivate; for (const bool pool : {false, true}) { const uint32_t stock = pool ? 138240u : 143360u, wanted = pool ? 127000u : 132033u; { // the control: nobody reads it the game's way, so it grows as before SimTurfPrivate g(pool, 1u << 20); CHECK(g.policy.observe(wanted, 100)); g.policy.installed(); CHECK(g.policy.capacity() > stock); } { // held from the start (the unpatched reader runs every frame long before the demand): it never grows SimTurfPrivate g(pool, 1u << 20); mkwvk::sim_turf_hold(g, pool); CHECK(!g.policy.observe(wanted, 100)); CHECK(!g.policy.observe(1u << 19, 101)); CHECK(g.policy.capacity() == stock && g.policy.target() == stock && g.policy.maximum() == stock); } { // grown first, held after: the next install is the game's size again SimTurfPrivate g(pool, 1u << 20); CHECK(g.policy.observe(wanted, 100)); g.policy.installed(); CHECK(g.policy.capacity() > stock); mkwvk::sim_turf_hold(g, pool); CHECK(g.policy.target() == stock); // (target != capacity: the producer's next binding resizes) g.policy.installed(); CHECK(g.policy.capacity() == stock); CHECK(!g.policy.observe(wanted, 200)); g.policy.limit(1u << 20); // (a reader's larger bound never lifts the hold) CHECK(!g.policy.observe(wanted, 201) && g.policy.capacity() == stock); } } std::puts("PASS: a simulated-turf list read the game's way stays at the game's size"); } """ def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--cxx", default="clang++") args = parser.parse_args() draw = (ROOT / "gpu" / "vk" / "vk_draw.cpp").read_text(encoding="utf-8") cut = block(draw, "struct SimTurfPrivate {") hold = block(draw, "void sim_turf_hold(SimTurfPrivate& g, bool pool) {") with tempfile.TemporaryDirectory(prefix="sim-turf-hold-") as folder: src = Path(folder) / "sim_turf_hold.cpp" src.write_text(HARNESS.replace("@CUT@", cut).replace("@HOLD@", hold), encoding="utf-8") exe = Path(folder) / "sim_turf_hold.exe" subprocess.run([args.cxx, "-std=c++20", "-I" + str(ROOT / "gpu" / "vk"), str(src), "-o", str(exe)], check=True) env = os.environ.copy() env["PATH"] = str(Path(args.cxx).resolve().parent) + os.pathsep + env.get("PATH", "") subprocess.run([str(exe)], check=True, env=env) if __name__ == "__main__": main()