xmem: the title's VirtualProtect never reports or restores one of our guards as its protection (RT-TITLE-TEXTURE-WRITE-TRACKED)

Run 62 died on a write to a title page left read-only with no guard to answer for it ("stray read-only title
page ... no guard owns it"). TitleVirtualProtect passed the title's call straight through: over a page one of
our guards (a slot-133 shadow's, or a geometry-watch cell, which the title texture watch now arms over all
watched texture memory) had made read-only, the call returned that read-only protection in `old`, and its
Event::replaced made the cell forget the page. A title that saves `old` and sets it back later then made the
page read-only for good, with no owner, and its next write faulted.

The call now runs under a kernel-write pin over the range (kernel_writes::begin_write, as ReadFile does): our
guards come off first and no refresh puts one back until it returns, so `old` is the title's own protection.

Test: d3d12_xmem_placed's "title VirtualProtect over a guard" (an armed cell, the title's VirtualProtect to
read-write and back to what it reported, then a write); it fails without this (`old` is PAGE_READONLY).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jan Trencansky
2026-11-27 04:07:38 -05:00
parent cadf43276a
commit 6edfe235b2
2 changed files with 34 additions and 0 deletions

View File

@ -2515,7 +2515,13 @@ BOOL WINAPI TitleVirtualProtect(LPVOID address, SIZE_T size, DWORD protect, PDWO
const auto start = reinterpret_cast<uintptr_t>(address);
// Before the call: a fault on another thread in between must not take the title's protection for a lost guard.
note_title_protect(start, size, protect);
// Our guards over the range (the slot-133 shadows', the geometry and title texture watches' cells) come off first
// and stay off while the call runs, as for a kernel write (a pin: no refresh guards a pinned page): the protection
// the call returns in `old` is the title's own, never one of our read-only guards. A title that saved such an `old`
// and set it back later left the page read-only with no guard to answer for it, and its next write faulted.
const std::uint64_t pin = leonida::kernel_writes::begin_write(address, size, nullptr);
const BOOL result = VirtualProtect(address, size, protect, old);
leonida::kernel_writes::end_write(pin, false);
if (result) {
leonida::title_memory_map::changed(start, start + size);
leonida::geometry_writes::notify(start, start + size, leonida::geometry_writes::Event::replaced);

View File

@ -482,6 +482,33 @@ int paired_views(ID3D12Device* real) {
return failures;
}
// The title's own VirtualProtect (xmem's TitleVirtualProtect) never sees one of our read-only guards as the protection
// it had: a title that saves `old` and sets it back later would leave the page read-only with no guard to answer for it,
// and its next write faults (run 62's "stray read-only title page"). A cell of the geometry watch, armed as the title
// texture watch arms them, stands for the guards.
int title_protect_round_trip() {
const char* who = "title VirtualProtect over a guard";
using VirtualProtectFn = BOOL(WINAPI*)(LPVOID, SIZE_T, DWORD, PDWORD);
const auto protect = reinterpret_cast<VirtualProtectFn>(rt::shims::find("kernel32.dll", "VirtualProtect"));
EXPECT(protect);
auto* pages = static_cast<std::uint8_t*>(VirtualAlloc(nullptr, kPage64k, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
EXPECT(pages);
const auto va = reinterpret_cast<std::uintptr_t>(pages);
auto* cell = leonida::geometry_writes::acquire(va);
EXPECT(leonida::geometry_writes::arm(va, *cell));
EXPECT((protection_of(pages + 0x1000) & 0xff) == PAGE_READONLY); // guarded
DWORD old = 0, again = 0;
EXPECT(protect(pages + 0x1000, 0x1000, PAGE_READWRITE, &old));
EXPECT((old & 0xff) == PAGE_READWRITE); // what the title set, not our guard
EXPECT(protect(pages + 0x1000, 0x1000, old, &again)); // set back as the title saw it
EXPECT((protection_of(pages + 0x1000) & 0xff) == PAGE_READWRITE);
pages[0x1964] = 1; // and written: no fault left to the title
leonida::geometry_writes::release(va);
EXPECT(VirtualFree(pages, 0, MEM_RELEASE));
std::printf("%s: the title saw and restored its own protection\n", who);
return 0;
}
int exposure_map_ranges(void* device) {
auto* pages = static_cast<char*>(VirtualAlloc(nullptr, 2 * kPage64k, MEM_RESERVE, PAGE_READWRITE));
EXPECT(pages && VirtualAlloc(pages, kPage64k, MEM_COMMIT, PAGE_READWRITE));
@ -697,6 +724,7 @@ int main(int argc, char** argv) {
failures += queue_remap(real.p, device.p);
failures += queue_remap_default_route(real.p, device.p);
failures += paired_views(real.p);
failures += title_protect_round_trip();
}
}
if (vk.validation_errors()) {