mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 16:08:37 +00:00
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 <noreply@anthropic.com>
117 lines
4.9 KiB
C++
117 lines
4.9 KiB
C++
// 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 <windows.h>
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <vector>
|
|
|
|
#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<uint64_t>(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<uint8_t*>(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<Loop>(code);
|
|
const auto shape_b = reinterpret_cast<Loop>(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;
|
|
}
|