From 273b3bdf9d6e736442ad584ad464df6e86ee928b Mon Sep 17 00:00:00 2001 From: Moriz82 <72836413+Moriz82@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:42:12 -0600 Subject: [integration] Linux/Proton and native Windows: boot through the story load (RT-PROTON-STORY-LOAD) One squashed commit of a fork that was developed on a Linux/Proton machine and tested on two native Windows machines. On all of them the retail 1.0.2.1 package now launches, shows its launch movie, first-run pages and landing prompt, runs the story load to its end with the loading slideshow drawing its artwork, and enters the world phase. A first world frame is not verified yet. docs/linux_proton.md has the setup, a healthy boot's timeline, the machines and where it stops. Runtime: frames in the title's own window; DirectStorage pinned reads, page-list scatter/gather, BCPack and unshuffle with a locally generated predictor file; pooled, device-local and deferred image backing for streamed and placed textures; enhanced barriers into LAYOUT_UNDEFINED; a bindless table that degrades instead of refusing a submission; a descriptor hash finalizer (off until a library carries it); directly indexed heap offsets for compute dispatches and VS/PS/GS draws; title buffer windows outside a fragmented arena; opt-in device-local copies of streamed buffer views; write-fault retries under the tracker lock, shared trackers and write-watched title memory for native Windows; device-loss, compute-input and memory probes. Translator: loop bounds at structured exits, the eight FidelityFX programs that use a packed-dot form the disassembler skips, buffer bounds modes per view kind, a compaction option, the hash finalizer option. Tools and scripts: Linux boot scripts (Wine/Proton, no network), a Windows boot script, the paired compute translation script. No game data. AI-assisted (Claude Code and Codex agents), reviewed and booted by people on the machines named in docs/linux_proton.md. Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 --- runtime/test/clzero_patch_test.cpp | 116 +++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 runtime/test/clzero_patch_test.cpp (limited to 'runtime/test/clzero_patch_test.cpp') diff --git a/runtime/test/clzero_patch_test.cpp b/runtime/test/clzero_patch_test.cpp new file mode 100644 index 000000000..dc34de8f8 --- /dev/null +++ b/runtime/test/clzero_patch_test.cpp @@ -0,0 +1,116 @@ +// clzero_patch.h: the two CLZERO loop shapes the title uses are found, rewritten to jump to stubs, and the stubs +// zero exactly the lines the loops would zero and leave the registers the loops leave. The loops are hand-assembled +// here (no game code), and only the patched copies run, so the test runs on any x86-64 CPU. +#include + +#include +#include +#include +#include + +#include "clzero_patch.h" + +namespace { + +int failures = 0; +int checks = 0; + +void check(bool ok, const char* what) { + ++checks; + if (!ok) ++failures; + std::printf("%s %s\n", ok ? "PASS" : "FAIL", what); +} + +// uint64_t f(void* begin, void* end): shape A over [begin, end), returns rax at the loop's exit. +const uint8_t kShapeA[] = { + 0x48, 0x89, 0xC8, // mov rax, rcx + 0x49, 0x89, 0xD2, // mov r10, rdx + 0x0F, 0x01, 0xFC, // L: clzero + 0x48, 0x83, 0xC0, 0x40, // add rax, 40h + 0x4C, 0x39, 0xD0, // cmp rax, r10 + 0x72, 0xF4, // jb L + 0xC3, // ret +}; + +// uint64_t f(void* begin, void* end): shape B over [begin, end), returns the counter at the loop's exit. +const uint8_t kShapeB[] = { + 0x49, 0x89, 0xCA, // mov r10, rcx + 0x49, 0x89, 0xD3, // mov r11, rdx + 0x4C, 0x89, 0xD0, // L: mov rax, r10 + 0x49, 0x83, 0xC2, 0x40, // add r10, 40h + 0x0F, 0x01, 0xFC, // clzero + 0x4D, 0x39, 0xDA, // cmp r10, r11 + 0x72, 0xF1, // jb L + 0x4C, 0x89, 0xD0, // mov rax, r10 + 0xC3, // ret +}; + +// Shape A with cmp rax, rcx: rcx is the register the stub saves, so this loop is left alone. +const uint8_t kShapeAWithRcx[] = { + 0x0F, 0x01, 0xFC, // clzero + 0x48, 0x83, 0xC0, 0x40, // add rax, 40h + 0x48, 0x39, 0xC8, // cmp rax, rcx + 0x72, 0xF4, // jb + 0xC3, +}; + +using Loop = uint64_t (*)(void*, void*); + +// Fills a buffer, runs `loop` over [buf + first, buf + last) and checks which bytes changed. +bool zeroes_exactly(Loop loop, size_t first, size_t last, uint64_t& returned) { + alignas(64) static uint8_t buf[4096]; + std::memset(buf, 0xAB, sizeof buf); + returned = loop(buf + first, buf + last); + for (size_t i = 0; i < sizeof buf; ++i) { + const bool inside = i >= (first & ~size_t(63)) && i < last; + if (buf[i] != (inside ? 0 : 0xAB)) return false; + } + return returned == reinterpret_cast(buf + last); +} + +} // namespace + +int main() { + namespace cz = rt::clzero_patch; + { + const auto a = cz::find(kShapeA, sizeof kShapeA); + check(a.size() == 1 && a[0].head == 6 && a[0].length == 12 && a[0].clzero == 0, "shape A is found at its CLZERO"); + const auto b = cz::find(kShapeB, sizeof kShapeB); + check(b.size() == 1 && b[0].head == 6 && b[0].length == 15 && b[0].clzero == 7, + "shape B is found at its mov rax, CUR"); + check(cz::find(kShapeAWithRcx, sizeof kShapeAWithRcx).empty(), "a loop that compares against rcx is left alone"); + const uint8_t lone[] = {0x0F, 0x01, 0xFC, 0xC3}; + check(cz::find(lone, sizeof lone).empty(), "a CLZERO outside both loop shapes is left alone"); + } + + auto* code = static_cast(VirtualAlloc(nullptr, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE)); + check(code != nullptr, "code page"); + if (!code) return 1; + std::memcpy(code, kShapeA, sizeof kShapeA); + std::memcpy(code + 0x100, kShapeB, sizeof kShapeB); + const auto r = cz::patch({{code, 0x1000}}, uint64_t(code), uint64_t(code) + 0x1000); + check(r.sites == 2 && r.stubs != nullptr, "both loops are patched"); + check(code[6] == 0xE9 && code[0x106] == 0xE9, "each loop starts with a jmp rel32 to its stub"); + const int64_t distance = int64_t(r.stubs) - int64_t(code); + check(distance > -(int64_t(1) << 31) && distance < (int64_t(1) << 31), "the stubs are within 2 GiB of the code"); + FlushInstructionCache(GetCurrentProcess(), code, 0x1000); + + const auto shape_a = reinterpret_cast(code); + const auto shape_b = reinterpret_cast(code + 0x100); + uint64_t ret = 0; + check(zeroes_exactly(shape_a, 64, 64 + 1024, ret), "shape A zeroes its 16 lines and returns rax = end"); + check(zeroes_exactly(shape_a, 128, 192, ret), "shape A runs once for a one-line range"); + check(zeroes_exactly(shape_b, 256, 256 + 0x380, ret), "shape B zeroes its 14 lines and leaves CUR = end"); + { + // An unaligned start: CLZERO zeroes the whole line rAX points into, and the loop steps by 64 from rAX. + alignas(64) static uint8_t buf[512]; + std::memset(buf, 0xAB, sizeof buf); + shape_a(buf + 64 + 8, buf + 64 + 8 + 128); + bool ok = true; + for (size_t i = 0; i < sizeof buf; ++i) ok &= buf[i] == ((i >= 64 && i < 64 + 128) ? 0 : 0xAB); + check(ok, "an unaligned start zeroes the lines rAX points into"); + } + + std::printf("clzero_patch_test: %s (%d checks)\n", failures ? "FAILED" : "OK", checks); + return failures ? 1 : 0; +} -- cgit v1.2.3 From a013410f8ec354cd134adbca8266ed707eb3c409 Mon Sep 17 00:00:00 2001 From: IlyassRS Date: Wed, 25 Nov 2026 19:42:17 +0100 Subject: runtime: guard rel32 range, stub_page arithmetic, and VirtualProtect failure in clzero_patch --- runtime/src/clzero_patch.h | 96 +++++++++++++++++++++++++++++--------- runtime/test/clzero_patch_test.cpp | 15 ++++++ 2 files changed, 88 insertions(+), 23 deletions(-) (limited to 'runtime/test/clzero_patch_test.cpp') diff --git a/runtime/src/clzero_patch.h b/runtime/src/clzero_patch.h index 7ab90e303..ec694f2f4 100644 --- a/runtime/src/clzero_patch.h +++ b/runtime/src/clzero_patch.h @@ -51,6 +51,7 @@ inline unsigned reg_reg(uint8_t rex, uint8_t modrm) { return ((modrm >> 3) & 7) // Finds the loops in `code[0, size)`. A CLZERO that is in neither shape is left to the #UD path. inline std::vector find(const uint8_t* code, size_t size) { std::vector sites; + if (!code || size < 12) return sites; for (size_t k = 0; k + 3 <= size; ++k) { if (!clzero_at(code + k)) continue; // Shape A: the CLZERO is the loop's first instruction. @@ -91,8 +92,25 @@ inline std::vector find(const uint8_t* code, size_t size) { return sites; } +// Overflow-free check that a rel32 branch from `from_next` to `to` fits in signed 32 bits. +inline bool rel32_offset(uint64_t from_next, uint64_t to, int32_t& out) { + constexpr uint64_t kMaxPos = static_cast(INT32_MAX); + constexpr uint64_t kMaxNeg = static_cast(INT32_MAX) + 1; + if (to >= from_next) { + const uint64_t d = to - from_next; + if (d > kMaxPos) return false; + out = static_cast(d); + return true; + } + const uint64_t d = from_next - to; + if (d > kMaxNeg) return false; + out = d == kMaxNeg ? INT32_MIN : -static_cast(d); + return true; +} + // The stub for one loop: its instructions before the CLZERO, the zeroing block, its instructions after the CLZERO up // to the jb, a jb back to the stub's start, then a jmp to `resume` (the instruction after the original jb). +// Returns an empty vector if the final jmp rel32 cannot reach `resume`. inline std::vector stub(const uint8_t* loop, const Site& s, uint64_t stub_at, uint64_t resume) { std::vector out(loop, loop + s.clzero); // shape B: mov rax, CUR; add CUR, 40h const uint8_t zero_line[] = { @@ -116,8 +134,8 @@ inline std::vector stub(const uint8_t* loop, const Site& s, uint64_t st out.push_back(0x72); out.push_back(uint8_t(int8_t(back))); out.push_back(0xE9); - const int64_t rel = int64_t(resume) - int64_t(stub_at + out.size() + 4); - const int32_t rel32 = int32_t(rel); + int32_t rel32 = 0; + if (out.size() + 4 > UINT64_MAX - stub_at || !rel32_offset(stub_at + out.size() + 4, resume, rel32)) return {}; const auto* b = reinterpret_cast(&rel32); out.insert(out.end(), b, b + 4); return out; @@ -125,17 +143,30 @@ inline std::vector stub(const uint8_t* loop, const Site& s, uint64_t st // Reserves a page within 2 GiB above or below [lo, hi) for the stubs (the image does not move after this). inline uint8_t* stub_page(uint64_t lo, uint64_t hi, size_t size) { - SYSTEM_INFO si; + if (!size || hi < lo) return nullptr; + SYSTEM_INFO si{}; GetSystemInfo(&si); - const uint64_t gran = si.dwAllocationGranularity; + const uint64_t gran = si.dwAllocationGranularity ? si.dwAllocationGranularity : 0x10000; const uint64_t limit = uint64_t(1) << 31; - for (uint64_t at = (hi + gran - 1) & ~(gran - 1); at + size < lo + limit - 0x1000; at += gran) { - if (void* p = VirtualAlloc(reinterpret_cast(at), size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)) - return static_cast(p); + const uint64_t max_span = limit - 0x1000; + if (hi - lo < max_span && hi <= UINT64_MAX - (gran - 1)) { + const uint64_t start = (hi + gran - 1) & ~(gran - 1); + const uint64_t ceiling = lo <= UINT64_MAX - max_span ? lo + max_span : UINT64_MAX; + for (uint64_t at = start; size <= ceiling && at <= ceiling - size;) { + if (void* p = VirtualAlloc(reinterpret_cast(at), size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)) + return static_cast(p); + if (at > UINT64_MAX - gran) break; + at += gran; + } } - for (uint64_t at = (lo - size) & ~(gran - 1); at > gran && hi - at < limit - 0x1000; at -= gran) { - if (void* p = VirtualAlloc(reinterpret_cast(at), size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)) - return static_cast(p); + if (lo >= size) { + uint64_t at = (lo - size) & ~(gran - 1); + while (at >= gran && hi - at < max_span) { + if (void* p = VirtualAlloc(reinterpret_cast(at), size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)) + return static_cast(p); + if (at < gran) break; + at -= gran; + } } return nullptr; } @@ -165,31 +196,50 @@ inline Result patch(const std::vector>& ranges, uint if (!stubs) return r; size_t used = 0; std::vector> bodies; + std::vector jump_rels; + bodies.reserve(all.size()); + jump_rels.reserve(all.size()); for (const auto& [code, s] : all) { uint8_t* const loop = code + s.head; - const uint64_t at = uint64_t(stubs + used); - bodies.push_back(stub(loop, s, at, uint64_t(loop + s.length))); - used += (bodies.back().size() + 15) & ~size_t(15); - if (used > page) { + const uint64_t at = reinterpret_cast(stubs + used); + int32_t jmp_rel = 0; + if (!rel32_offset(reinterpret_cast(loop + 5), at, jmp_rel)) { VirtualFree(stubs, 0, MEM_RELEASE); return r; } + auto body = stub(loop, s, at, reinterpret_cast(loop + s.length)); + if (body.empty()) { + VirtualFree(stubs, 0, MEM_RELEASE); + return r; + } + const size_t padded = (body.size() + 15) & ~size_t(15); + if (padded > page - used) { + VirtualFree(stubs, 0, MEM_RELEASE); + return r; + } + bodies.push_back(std::move(body)); + jump_rels.push_back(jmp_rel); + used += padded; + } + size_t offset = 0; + for (const auto& body : bodies) { + std::memcpy(stubs + offset, body.data(), body.size()); + offset += (body.size() + 15) & ~size_t(15); + } + DWORD old = 0; + if (!VirtualProtect(stubs, page, PAGE_EXECUTE_READ, &old)) { + VirtualFree(stubs, 0, MEM_RELEASE); + return r; } - used = 0; + FlushInstructionCache(GetCurrentProcess(), stubs, page); + // Only overwrite the image loops once the stub page is populated and executable. for (size_t i = 0; i < all.size(); ++i) { const auto& [code, s] = all[i]; uint8_t* const loop = code + s.head; - uint8_t* const at = stubs + used; - std::memcpy(at, bodies[i].data(), bodies[i].size()); - used += (bodies[i].size() + 15) & ~size_t(15); - const int32_t rel = int32_t(int64_t(at) - int64_t(loop + 5)); loop[0] = 0xE9; - std::memcpy(loop + 1, &rel, 4); + std::memcpy(loop + 1, &jump_rels[i], 4); std::memset(loop + 5, 0xCC, s.length - 5); } - DWORD old = 0; - VirtualProtect(stubs, page, PAGE_EXECUTE_READ, &old); - FlushInstructionCache(GetCurrentProcess(), stubs, page); r.sites = all.size(); r.stubs = stubs; r.stub_bytes = used; diff --git a/runtime/test/clzero_patch_test.cpp b/runtime/test/clzero_patch_test.cpp index dc34de8f8..ab5fbcf4c 100644 --- a/runtime/test/clzero_patch_test.cpp +++ b/runtime/test/clzero_patch_test.cpp @@ -81,6 +81,21 @@ int main() { check(cz::find(kShapeAWithRcx, sizeof kShapeAWithRcx).empty(), "a loop that compares against rcx is left alone"); const uint8_t lone[] = {0x0F, 0x01, 0xFC, 0xC3}; check(cz::find(lone, sizeof lone).empty(), "a CLZERO outside both loop shapes is left alone"); + check(cz::find(nullptr, 64).empty(), "null code buffer returns no sites"); + check(cz::stub_page(0x1000, 0x2000, 0) == nullptr, "stub_page rejects size 0"); + check(cz::stub_page(0x2000, 0x1000, 0x10000) == nullptr, "stub_page rejects inverted [lo, hi)"); + int32_t rel = 0; + check(cz::rel32_offset(0x100000000ull, 0x100000000ull + INT32_MAX, rel) && rel == INT32_MAX, + "rel32_offset accepts INT32_MAX"); + check(!cz::rel32_offset(0x100000000ull, 0x100000000ull + uint64_t(INT32_MAX) + 1, rel), + "rel32_offset rejects INT32_MAX + 1"); + check(cz::rel32_offset(0x180000000ull, 0x100000000ull, rel) && rel == INT32_MIN, + "rel32_offset accepts INT32_MIN"); + check(!cz::rel32_offset(0x180000001ull, 0x100000000ull, rel), + "rel32_offset rejects INT32_MIN - 1"); + const cz::Site site_a{6, 12, 0}; + check(cz::stub(kShapeA + 6, site_a, 0x100000000ull, 0x500000000ull).empty(), + "stub refuses resume address beyond +/-2 GiB"); } auto* code = static_cast(VirtualAlloc(nullptr, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE)); -- cgit v1.2.3