Repository navigation
loader/windows: batch IAT VirtualProtect calls per 4 KiB page (D3D12-LOADER-BATCHED-IAT-PROTECT) - #2891
loader/windows: batch IAT VirtualProtect calls per 4 KiB page (D3D12-LOADER-BATCHED-IAT-PROTECT)#2891djrolfes wants to merge 1 commit into
Conversation
|
Independent test of mixed protections across the fallback boundary at ce130c7 + 0ff8a53. The two stock Windows tests passed under Wine 11.22. I added a check on private synthetic memory with adjacent pages initially read-only and read/write. Aligned slots on those pages preserve both protections. With a cross-page slot, the data writes successfully but VirtualQuery shows both pages read-only afterward. The cross-page VirtualProtect call saves the old protection of the first page, then applies that single value to both pages when restoring. This matches the Win32 API documentation. A local trial saving and restoring the pages separately passed the mixed case and both stock tests. This is a result for the defensive fallback on synthetic memory. I did not verify whether the image parser produces such a slot on a normal load. MEASURED. AI-assisted (Codex). |
KelboLive
left a comment
There was a problem hiding this comment.
Batching itself looks right: stable sort by RVA, grouping on addr & !4095, and per-page save/restore preserve mixed .rdata/.didat protections, duplicate RVAs still last-writer-wins, and the mock test pins the call count (4 calls for 21 slots over 2 pages vs 42 on main). Restore is load-bearing — replacing it with let _ = old fails both new tests with page 0x0 protection not restored. Merge is clean against current main, and it pairs cleanly with #2889 and #2890.
One real gap, and it matches merzleckeier's note: the unaligned-straddle fallback (windows.rs:496-503). VirtualProtect(addr, 8, …) rounds to cover both pages, but old is only page 0's protection and the restore at :500 writes it to both, so a mixed RO/RW boundary ends uniformly page 0's protection. I reproduced it on synthetic memory (page0=0x2 page1=0x2 for both the new and the old per-slot path). It is pre-existing, not a regression — main's loop did the same — and write_unaligned is a genuine improvement over write_volatile on a possibly unaligned pointer (main's loop trips the debug alignment abort). But the doc claim at :478-480 ("preserving each page's original protection") is too strong for this branch, and the added real test only exercises RO/RO.
Suggest: save/restore each page separately in the fallback (or reject unaligned IAT RVAs), extend the real test to page0=RO/page1=RW, and soften that doc sentence either way. Restore errors at :519 are still ignored, same as main, which is fine to leave.
Not verifiable here: the per-page call-count reduction on a real title image and the 300 s in-world gate. Also worth checking whether the PE parser can even yield an unaligned slot — leonida-pe/src/image.rs:337-343 bounds-checks rva+8 <= SizeOfImage but not 8-byte alignment.
Claimed in the coordination issue as:
D3D12-LOADER-BATCHED-IAT-PROTECT.What and why
Batch IAT
VirtualProtectcalls per 4 KiB page insideLoadedImage::write_slots(d3d12/crates/leonida-loader/src/windows.rs) instead of protecting and restoring every 8-byte import slot individually.main,LoadedImage::write_slotsissues twoVirtualProtectsyscalls per import slot (make writable → write 8 bytes → restore). Across hundreds/thousands of bound title imports (and delay-load tables in.didat), this causes two OS page-protection syscalls per import slot on every startup.write_slots_os/write_slots_withind3d12/crates/leonida-loader/src/windows.rs.(rva, address)pairs by RVA (preserving last-writer-wins semantics for duplicate RVAs), groups consecutive slots sharing the same 4 KiB page (addr & !4095), and issues oneVirtualProtect(page, 4096, PAGE_READWRITE, &mut old)+ restore per page — preserving distinct protections across adjacent pages/sections (e.g..rdatavs.didat).(addr & 4095) > 4088) with an 8-byte unaligned fallback so it never writes past the protected page.D3D12-LOADER-CONTENT-ADDRESSED-IMAGE, verified clean merge viagit merge-tree).d3d12/crates/leonida-loader/src/windows.rs) + red→green unit tests (both mock syscall-count test and realVirtualAlloc+PAGE_READONLY+VirtualQuerytest) + MEASURED 300 s in-world game gate.Tested
python3 -I scripts/check_no_game_data.py d3d12/crates/leonida-loader/src/windows.rs→ exit 0.leonida-loaderunit test binary (x86_64-pc-windows-msvc) and ran under Wine:origin/mainunbatched loop (runningwindows::tests::write_slots_batches_virtual_protect_per_pageagainstorigin/main's per-slot loop):ce130c717(rebased onto3e7b34a52):ce130c717, Series S + DLSS Quality1280×720 → 1920×1080, Option Amods/gate_drivermode = roamloaded):310.93 sin-world after world entry at69.46 s; also passed an earlier cold-cache 310.22 s run with0errors).3,273frames after +60 s (3,377total world presents),15.35median FPS (15.77end median FPS),111.32 msp95 frame time,3.188 slongest present gap (<= 10.0 s).6,690 MiBpeak VRAM (<= 7,400 MiB),0device losses (0x887A0005),0crashes,0panics,0faults before stop,0kernelNVRM: Xiderrors.gate_drivernote:mods/gate_driverloaded inmode = roamwith0GATE FAIL; its 10 s menu-press cadence missed the brief world-entry probe window so the 310.93 s run stayed on the in-world camera cleanly at15.35FPS.GPUs tested: NVIDIA GeForce RTX 3070 Ti 8 GB, driver 620.73.04, Arch Linux (Wine 11.22 + VKD3D-Proton 3.2.0).
AI-assisted: yes (authored by Gemini 4 Neon; reviewed in a separate independent review pass by Gemini 4 Neon).
Checklist
main(3e7b34a52) and merges cleanly (a conflicting pull request is labeledneeds rebase).check_no_game_data.pyclean on both source and PR description).docs/design.md, with how it was confirmed. (n/a — loader IAT page-protection batching)