"""The left-out ranges' check answered from the gaps it found before (gpu/vk/capgap.h, RX_VK_CAP_EXCL_GAP; vk_memory.cpp cap_excl_note -> cap_excl_hit_gap; no game, no GPU). (Triforce, 11-26, the GPU audit's C09.) With RX_VK_CAP_ONLY_GPU=1 every buffer binding, replay read and texture upload asked a binary search over about 3,000 left-out ranges whether it reaches into them, for a count that is nearly always not raised. A thread now keeps the last 8 gaps its searches found; a question wholly inside one is "no" without a search. Compiles a driver against the header and vk_memory.cpp's own ranges_hit (cut from the source): - random sorted lists (the empty list, ranges from 0, ones with empty ranges, a last range up to ~0) and random questions (inside gaps, across an edge, a gap's first and last byte, inside ranges, empty, wrapped): the rule's answer is ranges_hit's at every question, with list changes (and the memo's clear) in between; - 1,000 ascending questions inside one gap search once; questions going round 8 gaps search 8 times; round 9 gaps still the same answers; - questions that reach into a range are searched every time and keep nothing; - the switch's values. Compiles vk_memory.cpp's own cap_excl_hit_gap with cap_excl_hit and ranges_hit (cut from the source; the list, its lock and version, the lab's phase and on() stubbed) and runs it with RX_VK_CAP_EXCL_GAP unset, 0 and check, over list changes and lab phases (none, another knob's, nocapgap's): every answer is ranges_hit's over the list of the moment; the lab question is asked once a phase move, not at every question, and never with =0; nocapgap's phases and =0 go by the search (the memo's counts do not move), the other phases by the memo (counted every 256 questions); check counts no difference. Reads the source: cap_excl_note asks cap_excl_hit_gap; that reads RX_VK_CAP_EXCL_GAP once and the lab's "nocapgap", clears the memo where it takes the list again, uses the search's answer in check mode and counts the differences; the write-back's clip and mode 2's page count still ask cap_excl_hit, and cap_excl_hit is still the plain search; the 10 s line carries the counts; the switches table has the row. python -I -B tests/test_capgap.py [--cxx clang++] """ import argparse import os import subprocess import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUN_ALL_ARGS = ["--cxx", "{cxx}"] DRIVER = r''' #include "capgap.h" #include #include using namespace mkwvk::capgap; // (vk_memory.cpp's ranges_hit, cut from the source) @RANGES_HIT@ static int checks, fails; static void want(const char* what, bool ok) { checks++; if (!ok) fails++, printf("FAIL %s\n", what); } static std::vector random_list(std::mt19937_64& rng, int kind) { std::vector v; const int n = kind == 0 ? 0 : (int)(rng() % 40) + 1; uint64_t at = kind == 1 ? 0 : rng() % 5000; for (int k = 0; k < n; k++) { const uint64_t len = kind == 2 && rng() % 4 == 0 ? 0 : rng() % 3000 + 1; // (kind 2: some empty ranges) v.push_back({at, at + len}); at += len + rng() % 4000 + 1; // (never touching: as ranges_clip leaves them) } if (kind == 3 && !v.empty()) v.back().second = ~uint64_t(0); return v; } int main() { want("the switch", mode(nullptr) == kOn && mode("") == kOn && mode("1") == kOn && mode("0") == kOff && mode("check") == kCheck && mode("00") == kOn); want("use", use(kOn, false) && use(kCheck, false) && !use(kOff, false) && !use(kOn, true) && !use(kCheck, true)); std::mt19937_64 rng(20261126); long asked = 0, diffs = 0, from_memo = 0, hits = 0; for (int round = 0; round < 400; round++) { Memo memo; std::vector v; for (int change = 0; change < 6; change++) { v = random_list(rng, (round + change) % 4); memo.clear(); // (the caller's part: with its copy of the list) const uint64_t top = v.empty() ? 10000 : v.back().first + 6000; for (int q = 0; q < 300; q++) { uint64_t lo, hi; const int how = (int)(rng() % 10); if (how < 4 || v.empty()) { lo = rng() % top, hi = lo + rng() % 600 + 1; } else if (how < 8) { // (at a range's edges: the gap's first and last bytes, one across, one inside) const Range& r = v[rng() % v.size()]; const uint64_t edge = rng() % 2 ? r.first : r.second; lo = edge - std::min(edge, rng() % 3), hi = lo + rng() % 4; } else if (how == 8) { lo = rng() % top, hi = lo; // (empty) } else { lo = ~uint64_t(0) - rng() % 8, hi = lo + rng() % 16; // (at the top; wraps) } bool searched = false; const bool got = hit(memo, v, lo, hi, &searched), old = ranges_hit(v, lo, hi); asked++, diffs += got != old, from_memo += !searched, hits += old; if (got != old && diffs < 5) printf(" differs: [%llx, %llx) memo %d search %d\n", (unsigned long long)lo, (unsigned long long)hi, got, old); } } } printf("random: %ld questions, %ld from the memo, %ld reaching into the ranges, %ld differing\n", asked, from_memo, hits, diffs); want("the rule's answer is ranges_hit's at every random question", diffs == 0); want("the random questions use the memo and reach into the ranges", from_memo > asked / 10 && hits > asked / 20); // (10 ranges of 0x1000 every 0x10000: gap g is [g * 0x10000 + 0x1000, (g + 1) * 0x10000)) std::vector v; for (uint64_t k = 0; k < 10; k++) v.push_back({k * 0x10000, k * 0x10000 + 0x1000}); { Memo memo; int searches = 0, wrong = 0; for (uint64_t k = 0; k < 1000; k++) { bool searched = false; const uint64_t lo = 0x31000 + k * 0x30; wrong += hit(memo, v, lo, lo + 0x40, &searched) != ranges_hit(v, lo, lo + 0x40); searches += searched; } printf("1000 ascending questions in one gap: %d searches\n", searches); want("1000 ascending questions inside one gap search once", searches == 1 && wrong == 0); } for (int gaps = 8; gaps <= 9; gaps++) { Memo memo; int searches = 0, wrong = 0; for (int k = 0; k < 800; k++) { bool searched = false; const uint64_t lo = (uint64_t)(k % gaps) * 0x10000 + 0x2000 + (uint64_t)k; wrong += hit(memo, v, lo, lo + 0x100, &searched) != ranges_hit(v, lo, lo + 0x100); searches += searched; } printf("800 questions round %d gaps: %d searches\n", gaps, searches); if (gaps == 8) want("questions going round 8 gaps search 8 times", searches == 8 && wrong == 0); else want("round 9 gaps the answers are still the search's", wrong == 0 && searches > 8); } { Memo memo; int searches = 0, said = 0; bool searched = false; hit(memo, v, 0x31000, 0x31040, &searched); // (one gap kept) for (int k = 0; k < 100; k++) { said += hit(memo, v, 0x3ff00, 0x40010, &searched), searches += searched; // (across the gap's end) said += hit(memo, v, 0x40800, 0x40900, &searched), searches += searched; // (inside a range) } want("questions reaching into a range are searched every time", searches == 200 && said == 200); int kept = 0; for (int k = 0; k < kGaps; k++) kept += memo.lo[k] <= memo.hi[k]; want("and keep nothing: the one gap stays alone", kept == 1); want("the gap's whole extent answers, to its last byte", !hit(memo, v, 0x31000, 0x40000, &searched) && !searched); memo.clear(); want("a cleared memo searches", !hit(memo, v, 0x31000, 0x31040, &searched) && searched); } { Memo memo; bool searched = false; const std::vector none; want("the empty list: no, searched once", !hit(memo, none, 5, 9, &searched) && searched); want("then everything from the memo", !hit(memo, none, 0, ~uint64_t(0), &searched) && !searched); } printf("%d checks, %d failed\n", checks, fails); return fails ? 1 : 0; } ''' GAP_DRIVER = r''' #include "capgap.h" #include #include #include #include #include #include #include using namespace mkwvk; static void logf(const char*, ...) {} namespace lab_ab { // (lab_ab.h's phase, and an on() that counts its calls: nocapgap is knob 2) std::atomic active{0}; static long asked = 0; static bool on(const char* name) { asked++; return active.load(std::memory_order_relaxed) == 2 && !std::strcmp(name, "nocapgap"); } } static std::mutex cap_excl_mu; static std::vector> cap_excl; static std::atomic cap_excl_ver{1}; static std::atomic st_gap_memo{0}, st_gap_search{0}, st_gap_diff{0}; // (vk_memory.cpp's ranges_hit, cap_excl_hit and cap_excl_hit_gap, cut from the source) @CUT@ int main() { const char* e = std::getenv("RX_VK_CAP_EXCL_GAP"); const bool off = e && !std::strcmp(e, "0"); std::mt19937_64 rng(20261127); long questions = 0, wrong = 0, by_memo = 0, moves = 0; int phase = -1; for (int round = 0; round < 90; round++) { { // (a new list, published as capture_union_locked publishes it) std::vector> v; uint64_t at = rng() % 5000; for (int k = 0, n = (int)(rng() % 40); k < n; k++) { const uint64_t len = rng() % 3000 + 1; v.push_back({at, at + len}); at += len + rng() % 4000 + 1; } std::lock_guard lk(cap_excl_mu); cap_excl.swap(v); cap_excl_ver.fetch_add(1, std::memory_order_release); } const uint64_t top = cap_excl.empty() ? 10000 : cap_excl.back().second + 6000; for (int q = 0; q < 1000; q++) { const int now = (round + (q >= 600)) % 3; // (0: none; 1: another knob's phase; 2: nocapgap's) if (now != phase) lab_ab::active.store(now, std::memory_order_relaxed), phase = now, moves++; uint64_t lo = rng() % top, hi = lo + rng() % 600 + 1; if (q % 50 == 7) hi = lo; // (empty) const bool got = cap_excl_hit_gap(lo, hi); wrong += got != ranges_hit(cap_excl, lo, hi); questions++; by_memo += !off && phase != 2; } } const long counted = (long)(st_gap_memo.load() + st_gap_search.load()); std::printf("%s: %ld questions, %ld wrong, the lab asked %ld times over %ld phase moves, %ld counted of %ld by the memo, " "%lld differing\n", e ? e : "unset", questions, wrong, lab_ab::asked, moves, counted, by_memo, (long long)st_gap_diff.load()); int fails = 0; if (wrong) fails++, std::printf("FAIL an answer is not ranges_hit's over the list of the moment\n"); if (lab_ab::asked != (off ? 0 : moves)) fails++, std::printf("FAIL the lab knob is asked %ld times, not once a phase move (%ld; never with =0)\n", lab_ab::asked, off ? 0 : moves); if (counted != by_memo / 256 * 256) fails++, std::printf("FAIL the memo counted %ld questions, not the %ld outside nocapgap's phases (by 256)\n", counted, by_memo); if (st_gap_diff.load()) fails++, std::printf("FAIL check counts differences\n"); return fails; } ''' def cut(src, start, end="\n}\n"): i = src.index(start) return src[i:src.index(end, i) + len(end)] def check_source(): src = (ROOT / "gpu/vk/vk_memory.cpp").read_text(encoding="utf-8") if '#include "capgap.h"' not in src: raise SystemExit("FAIL: vk_memory.cpp does not include capgap.h") note = cut(src, "void cap_excl_note(uint64_t gpu, uint64_t n, const char* who) {") if "if (!g || !cap_excl_hit_gap(g, g + n)) return;" not in note: raise SystemExit("FAIL: cap_excl_note does not ask cap_excl_hit_gap") gap = cut(src, "bool cap_excl_hit_gap(uint64_t lo, uint64_t hi) {") for needle in ('getenv("RX_VK_CAP_EXCL_GAP")', "capgap::mode(e)", 'lab_ab::on("nocapgap")', "return cap_excl_hit(lo, hi);", "memo.clear();", "capgap::hit(memo, mine, lo, hi, &searched)", "ranges_hit(mine, lo, hi)", "st_gap_diff.fetch_add(1, std::memory_order_relaxed)"): if needle not in gap: raise SystemExit("FAIL: cap_excl_hit_gap no longer has: " + needle) if "static const capgap::Mode env" not in gap: raise SystemExit("FAIL: RX_VK_CAP_EXCL_GAP is not read once") # (the memo is cleared in the branch that takes the list again) retake = gap[gap.index("if (ver != now) {"):] retake = retake[:retake.index("}")] if "mine = cap_excl;" not in retake or "memo.clear();" not in retake: raise SystemExit("FAIL: the memo is not cleared where the thread takes the left-out ranges again") # (check mode: the search's answer is the one used) if "return old;" not in gap: raise SystemExit("FAIL: check mode does not use the search's answer") # (cap_excl_hit is still the plain search, and the clip and mode 2 still ask it) plain = cut(src, "bool cap_excl_hit(uint64_t lo, uint64_t hi) {") if "return ranges_hit(mine, lo, hi);" not in plain or "capgap" in plain: raise SystemExit("FAIL: cap_excl_hit is no longer the plain search") for needle in ("if (excl_clip && cap_excl_hit(p->lo + off, p->lo + wb_end)) {", "if (cap_excl_hit(g, g + 1)) st_excl_pages.fetch_add(1, std::memory_order_relaxed);"): if needle not in src: raise SystemExit("FAIL: vk_memory.cpp no longer has: " + needle) if src.count("cap_excl_hit_gap(") != 3: # (its declaration, its definition, cap_excl_note) raise SystemExit("FAIL: cap_excl_hit_gap has a caller besides cap_excl_note") present = cut(src, "void memory_cap_present() {") for needle in ("left-out checks in all from the gap memo %lld, searched %lld, differing %lld (RX_VK_CAP_EXCL_GAP)", "st_gap_memo.load(", "st_gap_search.load(", "st_gap_diff.load("): if needle not in present: raise SystemExit("FAIL: the capture's 10 s line no longer has: " + needle) docs = (ROOT / "docs/development.md").read_text(encoding="utf-8") row = [l for l in docs.splitlines() if l.startswith("| `RX_VK_CAP_EXCL_GAP=0`")] if len(row) != 1 or "nocapgap" not in row[0] or "capgap.h" not in row[0] or "=check" not in row[0]: raise SystemExit("FAIL: docs/development.md has no RX_VK_CAP_EXCL_GAP row with the knob, the header and check") ranges_hit = cut(src, "bool ranges_hit(const std::vector>& v, uint64_t lo, uint64_t hi) {") return ranges_hit, "\n".join((ranges_hit, plain, gap)) def main(): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("--cxx", default="clang++") args = parser.parse_args() if not (ROOT / "gpu/vk/capgap.h").exists(): raise SystemExit("FAIL: gpu/vk/capgap.h is missing: the left-out check searches at every question") ranges_hit, gap_cut = check_source() with tempfile.TemporaryDirectory(prefix="capgap-") as tmp: cpp, exe = Path(tmp) / "capgap_test.cpp", Path(tmp) / "capgap.exe" cpp.write_text(DRIVER.replace("@RANGES_HIT@", ranges_hit), encoding="utf-8") subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(ROOT / "gpu/vk"), str(cpp), "-o", str(exe)], check=True) r = subprocess.run([str(exe)], capture_output=True, text=True) print(r.stdout.rstrip()) if r.returncode: raise SystemExit("FAIL: the gap memo's checks failed") cpp, exe = Path(tmp) / "capgap_note.cpp", Path(tmp) / "capgap_note.exe" cpp.write_text(GAP_DRIVER.replace("@CUT@", gap_cut), encoding="utf-8") subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(ROOT / "gpu/vk"), str(cpp), "-o", str(exe)], check=True) env = {k: v for k, v in os.environ.items() if not k.startswith("RX_VK_")} for value in (None, "0", "check"): run_env = dict(env, **({"RX_VK_CAP_EXCL_GAP": value} if value is not None else {})) r = subprocess.run([str(exe)], capture_output=True, text=True, env=run_env) print(r.stdout.rstrip()) if r.returncode: raise SystemExit("FAIL: vk_memory.cpp's cap_excl_hit_gap with RX_VK_CAP_EXCL_GAP=%s" % (value or "unset")) print("PASS: the left-out check is answered from kept gaps with the search's answers; RX_VK_CAP_EXCL_GAP=0 searches as before") if __name__ == "__main__": main()