The stack walk checks memory before it reads it, so VICTORY_PROFILE no longer crashes the game (VICTORY_UNWIND_CHECKS=0 for the old walk) #343
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Belobug--VIctory-Belobug-AMD!343
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-343"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@thejacte on GitHub · 2026-11-26 21:17 UTC · original
thejacte:codex/rx9070xt-profile-unwind→rx9070xt· +450 −15 · 2 files · 1 commits · openOn an RX 9070 XT,
VICTORY_PROFILEcrashed the game after about 4 minutes. The access violation was inunwind_into: the profiler walks the stacks of suspended threads withRtlVirtualUnwind, and a bad RIP or RSP in a sample makes it read memory that isn't there. The crash report andVICTORY_STACKSuse the same walk.With
VICTORY_UNWIND_CHECKSon (the default), each step of the walk checks first:pop,add rsp,lea rsp), where Windows would simulate the epilogue from the code instead of the record;A record it can't bound (unknown opcodes, version-2 epilogue codes) ends the walk early rather than guessing. Memory is read through
ReadProcessMemory, which returns an error instead of faulting.Code without unwind data still gets the leaf rule (return address at RSP), but only at the first frame. That keeps the caller of a crash inside our own thunks and stubs, which are
VirtualAlloc'd and so not image memory.VICTORY_UNWIND_CHECKS=0gives the old walk. The switch is read once, before title code runs, so the exception handler never reads the environment.Measured (Windows 11, RX 9070 XT, driver 32.0.32029.2010, Ryzen 7 9800X3D, 64 GB),
rx9070xtatcd8dfc0, diagnostics build:VICTORY_PROFILEover a 9-minute windowOne thing to know: each frame now costs about ten
VirtualQuery/ReadProcessMemorycalls, so a sampled thread stays suspended longer than before (not measured yet). If that skews profiles, a follow-up can remember each image's ranges once per walk.Tests (
crates/victory-loader/src/crash.rs), all on synthetic unwind records and the test's own stack:unwinds_like_the_system_walkruns the checked walk over real frames.Checked:
cargo clippy --workspace --all-targets --locked -- -D warningsand the same with--all-features: no new warnings. Checked with #341's four lines applied, sincerx9070xtfails both without them.cargo test --workspace --locked --no-fail-fast: passes, exceptemit::size_tests::the_record_count_of_a_structured_buffer_read_by_index, the AMD compiler crash that #339 works around.cargo xtask policy: passes.Written with Codex; reviewed by Claude Code (a rust-reviewer pass against
upstream/rx9070xt, whose one finding, about the first frame in our own stubs, is fixed here).🤖 Generated with Claude Code
@JonyWater on GitHub · 2026-11-26 21:23 UTC · original
Review notes by Claude
Thanks for this. Bounding every read before
RtlVirtualUnwindtouches it is a clean fix, and I checked the UNWIND_INFO parsing (slot counts, scaling, the padded chained entry, overflow checks) without finding problems. Some things I'd like settled before merge:Before merge
acbeb30is waiting for maintainer approval, so clippy and tests haven't been checked by CI yet.Questions
Version-2 records with UWOP_EPILOG codes end the walk. Recent MSVC output, and I believe many Windows system DLLs, use these records. If so, threads paused in system code (waits, for example) would come back 1–3 frames deep. Do you know how often walks stop at this rule in the 52k-stack run?
Truncation is silent. A walk also stops early when:
All of these yield a short stack that looks like a real one, which can skew a profile. Could the walk count (or tag) stacks it stopped because of a check, so we can see how often that happens?
Suspension time. You mention about 10
VirtualQuery/ReadProcessMemorycalls per frame. Could you add a before/after number for how long a sampled thread stays suspended? If it's significant, the per-walk cache of image ranges you suggest seems worth doing here.Minor
stops_at_a_pc_outside_an_imagesets RIP to a stack address, so it really tests "not executable" rather than "executable but outside an image". The stub test covers the second case. Maybe rename it, or add a case for executable memory outside an image beyond the first frame.VirtualQuerycheck andRtlVirtualUnwind's read. That's rare, and probably fine to leave, but worth a comment.@JonyWater on GitHub · 2026-11-26 21:30 UTC · original
Follow-up (review notes by Claude): I checked my version-2 question on a Windows 11 machine, and it's mostly a non-issue. Records the checked walk would reject (v2 with UWOP_EPILOG): ntdll 70/4,136 (1.7%), KernelBase 0/3,529, kernel32 0/760, d3d12 0/241, ucrtbase 2/2,799. The ntdll ones are mostly the
*_sstring helpers,RtlInterlocked*SList,__chkstkandKiUserExceptionDispatcher. No wait functions, so threads paused in waits keep their full stacks. I haven't checked the title's own binaries. The other questions (counting truncated walks, suspension time) still stand.We're still reviewing the rest of the PR in more depth and will follow up with anything else we find.
@JonyWater on GitHub · 2026-11-26 21:38 UTC · original
Review notes by Claude: deeper pass
One regression in crash reports: a bad instruction pointer loses its caller. For a call through a null or garbage function pointer (or a jump into data that DEP blocks), the faulting RIP isn't executable. On
rx9070xt,unwind_intofound no function entry, applied the leaf rule, and read the caller's return address at[rsp], which is usually the most useful line in the report. With checks on,readable_region/executablefails at the first step and the walk breaks, so the report shows only the bad address.stops_at_a_pc_outside_an_imageasserts this one-frame result. Profiles aren't affected (a suspended thread's RIP is real code).Suggested fix: let the first frame take the leaf rule whatever RIP points at.
[rsp]is already read throughReadProcessMemory, and the next step checks that the recovered RIP is executable image code:with a test like
stops_at_a_pc_outside_an_imagebut RIP = 0, expectingframes.starts_with(&[0, pc]). The current test could move to a non-image PC at the second frame.Not from this PR, but related: the
clzeroand write-site stubs (console_insn.rs,write_sites.rs) areVirtualAlloc'd and entered byjmp, notcall. A sample inside one takes the leaf rule, but[rsp]there is the title function's local data, not a return address, so the second frame is junk. The old walk did the same. A real fix would unwind through the title function the stub stands in for. Possibly worth an issue.Checked and fine:
jmpto their handler, TLS thunks are leaves reached bycallfrom the title, and theclzero/write-site stubs arejmpin and out. Samples inside any of them are first frames, which the checked walk accepts.amdxc64.dll: 3 of 47,155 records would be rejected. So the chain and version-2 rules barely bite on real binaries, and the "chained record pushes or allocates" case from my first comment doesn't come up in them.VirtualQuery/ReadProcessMemory;RtlLookupFunctionEntry/RtlVirtualUnwindstill read directly, after the PR bounds the ranges (thoughRtlVirtualUnwind's epilogue scan can read more code than the 8 bytes checked).Correction to my earlier follow-up: "no wait functions" went by exported names only; 57 of the rejected ntdll functions are internal and unnamed.
The regression is confirmed by reading the old and new code side by side, not by a test run.
@thejacte on GitHub · 2026-11-26 22:54 UTC · original
Thanks, good catch. Since #343 is already merged into a-publier, the fix is in #378, the way you suggested: the first frame takes the leaf rule whatever RIP points at. It also covers one more edge case, a caller whose return address is the stack's last word. The
clzero/write-site stub note is left for its own issue.@adrianmjim on GitHub · 2026-11-27 06:25 UTC · original
Tested on RX 7900 XT (RDNA3), driver 32.0.32029.2010, Windows 11 26300, Rust 1.98.1. a-publier at
4eb6cfd(contains this PR asd6f95b2),cargo test --profile diag -p victory-loader --features diagnostics. CPU only. No game run: the title was not launched. This is a follow-up on the merged code, on the suspension-time question above. #378 does not change this path.Suspension time, checked walk vs
VICTORY_UNWIND_CHECKS=0(medians of 5 interleaved rounds; other load was on the machine, so compare ratios, not absolute times):stacks::frames_ofin the test binary, 10k calls, thread 12 calls deep: waiting thread 11.3 us -> 257.5 us (about 23x, 22 frames); spinning thread 12.6 us -> 332.9 us (about 26x, 18 frames). p99 20-29 us -> 415-570 us.BUSIESTin profile.rs), one round takes about 0.3 s, for any<ms>interval below that.Cause:
VirtualQueryon image code gets slower with the distance from the queried page to the end of its region, once those pages are mapped in.readable_region(rip), thenread_imageof the 8 bytes at the PC. The record and unwind-info reads add at least three more image queries per frame (not timed separately).VirtualQuery0.46-0.8 us, 8-byteReadProcessMemory0.44 us,RtlLookupFunctionEntry20 ns.What a cache would save (estimate from the per-call costs above, not measured):
VirtualQueryreturns: helps only partly. A returned region starts at the queried page, so it only covers higher addresses. It removes the second code query of a frame, since the PC bytes are in the region just returned, but a frame at a lower address still pays a full query.ReadProcessMemoryalone as the fault guard: no image queries are left. What remains per frame is about four small reads, one stack query andRtlVirtualUnwind, about 3 us. That is about 60 us for a 20-frame title stack, against about 10 ms now and about 10 us for the old walk.Title records and truncation (asked above):
stack_reads_endrefused 1 of 3,991 sampled title records, through the epilogue-instruction rule.park_timeout): 4 to 158 per 10,000, depending on the run.#378 at
01d3f8c:crash::andstacks::pass 27/27, withVICTORY_UNWIND_CHECKSunset and with it set to 0.Not tested:
VirtualProtect) may split the code section into smaller regions in the real process, which would make the queries cheaper than in this model.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.