WIP: fix(host): the stack walks run clang's AVX return suffix themselves and every step under a fault guard #1951
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1951
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:fix/sampler-epilogue-unwind"
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?
Summary
Fixes #1584. The all-thread sampler (
HOS_SAMPLE_ALL, whichHOS_PROFILE=1starts), the one-thread sampler(
HOS_SAMPLE), the slow-frame recorder, the crash report and the crash record walk host stacks one frame at a time.Since #1343 the walk checks the stack pointer and the frame register before it calls
RtlVirtualUnwind, and the gamecould still close with
0xc0000005insideRtlVirtualUnwind, in the sampler's thread.The cause: clang's AVX code (the lifted game, the host and the renderer at
-march=native) ends a function with itspops, then a
vzeroupper, then theret. Windows' unwinder knows an epilogue only as anadd/leatorsp, pops anda
retor jump, so a thread stopped on those pops or on thevzeroupperis unwound as if it were in the function'sbody: the whole prologue is undone again on a stack pointer the pops have already moved, and the unwinder reads up to
the frame's size too high. Inside the stack that gives the sample wrong callers; when the frame is larger than what the
thread's stack has above it, the read leaves the stack and faults.
This PR (
host/mkw_glue.c,host/hos_crash_record.c):unwind_avx_return: the walk runs that one suffix itself, as the processor will:pop r64(0 to 16, neverrsp),vzeroupper(c5 f8 77), a plainret(c3), every byte inside the function's ownRUNTIME_FUNCTIONrange, theregisters written through
CONTEXT's named members, the pops and the return address checked against the walkedthread's stack first. Everything else (the
add/leabefore the pops, where the body's unwind is still right;ret imm16; jumps; an epilogue without thevzeroupper, which Windows recognises itself) goes toRtlVirtualUnwind. Arecord that is not version 1 or 2 ends the walk; the frame register's frame must be on the stack, as before.
walk_guarded/walk_fault: every step runs under a vectored handler registered first at startup; a fault while astep runs (a no-access page inside the stack's range, unwind data that cannot be read) puts the thread back where the
step began and the walk ends there, the sample cut short.
walk_init:NtQueryInformationThreadand the handler are set up on the main thread inhos_commit_init, and againbefore the samplers' threads start in
hos_prof_start, so no sampler resolves anything on first use.host_stackinhost/hos_crash_record.c) steps through the same guardedunwind_step(
hos_crash_record_unwinder) instead of callingRtlVirtualUnwinditself.To see the bug without the game:
python -I -B tests/test_sampler_stack_walk.pyatf3ed9d4b(the new test on this PR'sfirst fix) or at this branch's first commit
be287021(the earlier test alone onmain), on Windows.Read first:
unwind_avx_return,step_frame,walk_guardedandunwind_stepinhost/mkw_glue.c;tests/test_sampler_stack_walk.py.Testing
Tested: Windows 11, i7-9700, RTX 4060 (NVIDIA 610.88), llvm-mingw clang 23.1.5; game-free checks only. This change
has not run in the game on this PC yet (Still to test). Each check ran in a hidden console of its own, through the PC's
job queue, on this branch rebased onto
main@f1f2dc5b, head186660c6:python -I -B tests/test_sampler_stack_walk.py --cc <llvm-mingw clang>atf3ed9d4b(the new test on this PR's firstfix): fails, 8 of 11 cases:
avx_native(ajmp r11in the body taken for an epilogue),ret_imm16(RSP 16bytes low),
lea_sib,version3,wrap, andbad_chain,no_access_suffix,no_access_bodywith exit0xc0000005. At the head (
186660c6): passes, all 11 cases, "PASS (runtime: every case ran)", the stepped AVX frame "53steps of avx_frame unwound to its caller (7 of them pops, 1 the vzeroupper)". The same at the head without
--cc(thedefault compiler,
LONGSHOT_MINGW's clang).host/mkw_glue.candhost/hos_crash_record.ccompiled-fsyntax-onlywith the host build's flags (llvm-mingw,-fms-extensions,-march=native): no error (at the fix's tree before the rebase ontof1f2dc5b, which touchesneither file).
python -I -B tests/run_all.py --strict --changed origin/mainat the head: 7 passed, 5 failed, 0 skipped. The 5failing checks (
test_crash_record.py,test_crash_dialog.py,test_profiling_mode.py,test_renderer_new_handler.py,test_slow_frames.py) fail onmain@f1f2dc5btoo, run by name in the same queuejob;
test_crash_record.pystops at a renderer source check before it builds the record, so the record's new step iscovered here only by the syntax check and the walk test's source check.
Still to test:
HOS_PROFILE=1in Castle Town's market, 120 s standing andturning, this PR and
mainalternated (A B B A A B), expecting noCRASH:line from the walk, a readable profile(
python -I scripts/perf.py reporton the run's profile folder), and the time each sampled thread stays suspended,the sweeps a second, the stack depths and the frame-time tails about as on
main, withHOS_PROFILE=1and with theslow-frame recorder (by hand, queued)
python3 -I -B tests/test_sampler_stack_walk.py, expecting the source checks to pass and "source only" (thewalk is Windows' only); the host build (by hand)
HOS_PROFILE=1, this PR againstmain, expecting no crash on this PR's side (@Beanstalk play Castle Town's marketfor two minutes with HOS_PROFILE=1 on Windows PCs, this PR against main, and report any crash)
Measurements
Verdict: in the authored fixture the walk unwinds every stop of clang's AVX return suffix, and the stops this PR's
first fix misread or faulted on (ret 16, lea through r12, jmp r11, no-access pages, a bad chain, a version 3 record) now
unwind right or end the walk. (The earlier fixture's 8 wrong steps of 53 on
main@fc4a6769stay at 0 of 53.) Notmeasured in play yet (Still to test).
Setup: Windows 11, i7-9700, llvm-mingw clang 23.1.5;
python -I -B tests/test_sampler_stack_walk.py, once a sidethrough the PC's job queue. Baseline: this PR's first fix (
a183b11e, rebased) with the new test (f3ed9d4b).f1f2dc5b+ this PR's first fix, with the new test (f3ed9d4b)186660c6tests/test_sampler_stack_walk.py: authored cases and the stepped AVX frameRisks
The change is in the stack walks only: the crash report's backtrace and the crash record's,
HOS_SAMPLE,HOS_SAMPLE_ALL(HOS_PROFILE) and the slow-frame recorder. A walk that a check or the guard ends early gives a shorterbacktrace or sample, as a refused step did before; a mistake in
unwind_avx_returnwould show as wrong callers in aprofile or a crash report, never in play without those switches (a crash walks only after the crash). The guard's
handler is first in the process's vectored chain; on any exception it reads one thread-local and passes the exception
on unless a step is running on that thread. A fault inside a sampler's step is dispatched on the sampler's thread while it holds the sampled
thread suspended. If the sampled thread was suspended while holding the lock that guards Windows' vectored-handler
list (held exclusively while a handler is added or removed), that dispatch waits for a lock only the suspended thread
can release: the sampler never finishes the step and never resumes the thread, a hang of both, not a delay. It is rare:
it needs a fault inside a step (a frame that passes the checks and reads an unreadable page) at the moment the sampled
thread is adding or removing a handler. In this tree handlers are added at start-up before the samplers start
(
hos_commit_init:commit_on_touch,report_fatalandwalk_fault, the last again inhos_prof_start), lateronly by debugging switches, once each (
HOS_WATCH's thread,RX_NVN_WATCH_TEX,RX_NVN_WATCH_TEXELS_SRC,RX_VK_WATCH_CPU,RX_VK_VERIFY_DIM), and none is removed; modules loaded from outside the tree (GPU drivers,overlays) are not bounded by this. The crash record without
hos_crash_record_unwinderset(its test fixture) walks frame pointers. A revert is enough; players notice nothing.
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Recommendation: Request changes. The Windows fixture shows that the patch fixes the reported AVX unwind error. The new decoder can still fault while reading instructions or metadata, and it can mistake an in-function jump for a return and corrupt the backtrace.
Windows profiles and the legacy crash log can recover callers that were lost when Clang inserted
vzeroupperbefore a return. The patch also adds a second interpretation of Windows unwind rules on every stack step, while the persisted crash record still uses its own unchecked walk.Does it help: Yes for the reported fixture. On one Windows 11 i7-9700 run per side with llvm-mingw Clang 23.1.5, the test-only baseline at ed7462ec unwound 8 of 53 instruction stops incorrectly and the patch at 6a84dcc4 unwound all 53 correctly. The tested commits precede this rebase. The game fix and the cost of walking stacks while threads are suspended remain unproven in play.
Other systems: The Windows single-thread sampler, all-thread sampler, slow-frame recorder and legacy crash log share this helper. Shorter or wrong stacks also change the output of profile readers that consume those samples. Suspended game and renderer threads can see extra latency on any Windows GPU driver. The persisted crash record and renderer hang dump have separate Windows unwinders. Linux and Steam Deck use separate paths. The diff touches neither gameplay data, saves, audio, input, renderer APIs nor sample formats.
Switches: No game switches added, changed or removed. The existing profiling and slow-frame defaults stay off. The Windows test adds
-mavxunconditionally for its runtime fixture, which needs a runtime feature check or a baseline-safe fixture.Reviewed:
7231d69ef1against2e1f6c32d3: Read host/mkw_glue.c and tests/test_sampler_stack_walk.py in full, both commits and their diff, AGENTS.md, CONTRIBUTING.md, the Forgejo template and docs/dev/standards.md. No repository review checklist was present. Traced the sampler startup and callers, slow-frame consumer, fiber switch, separate crash writer and test runner in the relevant sections. Skimmed CI and related test contracts. Read the reported runs and the supplied comments and reviews, which were empty. The two historical run commits were unavailable locally. No recorded run artifacts, translations or generated files were changed.; nothing run (a code review).Checked and fine:
Assisted-by:trailers. The patch has one topic and changes no game-derived files, generated files, vendor code, shaders or renderer API definitions._WIN32and introduces no production path handling, allocations, shared caches or per-thread allocations. The test uses a temporary directory and adds no downloads or writes to the checkout.fc4a6769to2e1f6c32does not touch either changed file.git diff --checkwas clean.Blocking (to change before merge)
leadecoder treats the ModRM r/m field as the entire memory operand. With r12 as the frame register,lea rsp,[r12+0x58]has a SIB byte before the displacement. The code takes the SIB as the displacement and reads0x58as apop rax, so a valid-looking suffix restores registers and RIP from the wrong addresses. Decode the complete operand or decline this form and use a guarded fallback.CONTEXTmembers through&ctx->Raxas though the members were elements of a C array. Apop r15writes through a pointer past the Rax object, which is undefined even with the expected Windows layout. Use named members or an explicit register-to-member mapping for this write and the frame-register reads.UWOP_EPILOG, which this code discards while it validates body reads. It also accepts unknown ops and dereferences chained metadata before verifying that the record is readable. An invalid chain can fault in the preflight itself. Use a fault-contained Windows step outside the narrow AVX case, or stop on records the preflight cannot validate and handle exactly. A guarded native step would also avoid interpreting each ordinary frame twice.on_stackcheck still accepts an RSP ofUINT64_MAX - 7becausep + 8wraps. The new range check also establishes only numeric containment. A save slot in a no-access page inside the recorded stack interval passes it and is then dereferenced. Use the subtraction-based check for the first and final RSP too, and contain faults from actual stack reads.HOS_SAMPLEandHOS_PROFILEare enabled. They can enterthread_stacktogether and race on the plainlookedandqueryglobals. Resolve the function before starting the sampler threads and publish it with thread creation, so neither sampler needs to initialize it with another thread suspended.record_exceptioncallshos_crash_record_write, whosehost_stackin host/hos_crash_record.c directly callsRtlVirtualUnwind. A crash with the same bad saved frame can fault there before the new checks run. Route the persisted record through a fault-contained step as well, or give it a safe partial backtrace when the frame cannot be read.vzeroupperbefore the pops can pass without testing the bug. The shape check prints a note and still reports success, so reverting the AVX-specific logic can go unnoticed on that compiler. Use an authored Windows fixture with the exact epilogue and assert that the walk reaches it. Keep the compiler-generated frame as additional coverage if useful.-mavxafter checking only the compiler target. A Windows x64 PC or VM with AVX masked out can fail with an illegal instruction, even though Longshot supports lower CPU targets. Probe CPU and OS support in baseline code before launching an AVX executable, or exercise the decoder on an authored instruction sequence without executing AVX.clang, andRUN_ALL_ARGSalso supplies--cc clang. On Windows withCCpointing to llvm-mingw and another Clang first on PATH, the documented command and the suite can use the wrong target and silently skip the runtime case. HonorCCwhen--ccis absent and report source-only and runtime passes separately.Waiting on the author (a run, a measurement or a decision; no edit does it)
Testing: Read-only review. No builds, tests or project scripts were run. The prior Windows changed-check run reports 6 passes and 4 failures also seen on the older baseline. No in-game run, current-head result or Linux run was supplied.
Overlaps and merge order: None found in the supplied review history or local checkout. #1343 is the merged predecessor.
Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.
@ -308,2 +308,2 @@unable to fault rather than guarded: a step whose stack pointer, or frame register, is outside [lo, hi), the thread'scommitted stack, ends the walk there, and the sample is only cut short */unable to fault rather than guarded: a step that would read outside [lo, hi), the thread's committed stack, ends the walkthere, and the sample is only cut short. What RtlVirtualUnwind will read is worked out from the frame's unwind codesThe persisted crash writer runs first and has its own unchecked
RtlVirtualUnwindinhost/hos_crash_record.c. Give that path a safe step or a safe partial trace too.@ -310,3 +310,4 @@first (frame_on_stack), and an epilogue is unwound here instead (unwind_epilogue) */typedef struct { uint64_t lo, hi; } StackRange;/* a thread's committed stack (its TEB's StackLimit to StackBase), read while it is suspended; {0, 0} when it can't be */static StackRange thread_stack(HANDLE th) {Both samplers can enter this existing lazy lookup at once. Resolve and publish
querybefore starting the sampler threads.@ -337,6 +338,111 @@ static StackRange own_stack(void) {return r;}static int on_stack(StackRange s, uint64_t p) { return p >= s.lo && p + 8 <= s.hi; }p + 8can wrap and accept a corrupt RSP. Reusespan_onhere and make actual stack reads fail cleanly on inaccessible pages inside the interval.@ -340,0 +352,4 @@const BYTE *b = (const BYTE *)ctx->Rip;uint64_t rsp = ctx->Rsp;int32_t d;if (b[0] == 0x48 && (b[1] == 0x83 || b[1] == 0x81) && b[2] == 0xc4) { /* add rsp, imm8 / imm32 */Bound every instruction fetch by the function end and handle unreadable pages. A saved RIP near a page boundary can make this lookahead or the unbounded pop scan fault.
@ -340,0 +360,4 @@b += len;} else if ((b[0] & 0xfe) == 0x48 && b[1] == 0x8d && frame_reg && ((b[0] & 1u) << 3 | (b[2] & 7u)) == frame_reg &&((b[2] & 0xf8) == 0x60 || (b[2] & 0xf8) == 0xa0)) { /* lea rsp, [frame register + disp8 / disp32] */const int len = (b[2] & 0xf8) == 0x60 ? 4 : 7;An r12-based LEA has a SIB byte before its displacement. With a displacement of 0x58 this consumes the SIB as the offset and the displacement as an extra pop.
@ -340,0 +381,4 @@int64_t to = (int64_t)((uint64_t)b - image) + (b[0] == 0xeb ? 2 : 5);if (b[0] == 0xeb) to += (int8_t)b[1];else memcpy(&d, b + 1, 4), to += d;out = to <= (int64_t)f->BeginAddress || to >= (int64_t)f->EndAddress;A jump outside this RUNTIME_FUNCTION can enter a chained range of the same function with the frame still allocated. The indirect mask also accepts register dispatch. Keep custom emulation to the AVX return suffix.
@ -340,0 +387,4 @@if (!span_on(s, rsp, 8 * (n + 1))) return -1;for (b = pops; b < popped; rsp += 8) {const int rex = (b[0] & 0xf0) == 0x40;reg[rex ? (b[0] & 1u) << 3 | (b[1] & 7u) : b[0] & 7u] = *(const uint64_t *)rsp;Indexing from
&ctx->Raxcrosses distinct C struct members. Use named members or a register-to-member mapping for the restores and frame-register reads.@ -354,0 +460,4 @@const int epilogue = unwind_epilogue(ctx, s, image, f, info[3] & 15);if (epilogue) return epilogue > 0 && (on_stack(s, ctx->Rsp) || !ctx->Rip);}if (!frame_on_stack(ctx, s, image, rf)) return 0;A body preflight does not cover native epilogue handling driven by version-2 metadata. The chain reads themselves can fault too. Contain the native unwind or fail closed on records this code cannot validate exactly.
@ -53,0 +141,4 @@const StackRange s = own_stack();CONTEXT b = body_ctx;b.Rsp = s.hi - 0x100;if (unwind_step(&b, s)) return 14;These bounds cases leave offset saves, XMM reads, frame-register setup and chained entries untested. Exercise each with a known context and a protected boundary.
@ -53,0 +161,4 @@int vzero = 0; /* (the shape this is about: pops, vzeroupper, ret) */for (const BYTE *b = (const BYTE *)begin; b + 5 <= (const BYTE *)end; b++)vzero |= (b[0] & 0xf8) == 0x58 && b[1] == 0xc5 && b[2] == 0xf8 && b[3] == 0x77 && b[4] == 0xc3;if (!vzero) printf("note: this compiler's avx_frame has no vzeroupper between its pops and its ret\n");This can pass on a compiler that never emits the affected epilogue. Use a fixture with a guaranteed pop, vzeroupper and return sequence.
@ -53,0 +162,4 @@for (const BYTE *b = (const BYTE *)begin; b + 5 <= (const BYTE *)end; b++)vzero |= (b[0] & 0xf8) == 0x58 && b[1] == 0xc5 && b[2] == 0xf8 && b[3] == 0x77 && b[4] == 0xc3;if (!vzero) printf("note: this compiler's avx_frame has no vzeroupper between its pops and its ret\n");AddVectoredExceptionHandler(1, on_step);Check handler registration and the thread wait and exit-code calls. Remove the handler and close the handle on completion so setup failures produce a clear test error.
@ -72,0 +202,4 @@returnexe = Path(tmp) / "walk_steps.exe"run = Path(tmp) / "walk_steps.c"run.write_text(helpers(src) + "static uint64_t prologue_size;\n" + DRIVER, encoding="utf-8")Honor
CCunless--ccis explicit. With a different Clang on PATH, the Windows runtime check can otherwise be skipped and still print PASS.@ -72,0 +203,4 @@exe = Path(tmp) / "walk_steps.exe"run = Path(tmp) / "walk_steps.c"run.write_text(helpers(src) + "static uint64_t prologue_size;\n" + DRIVER, encoding="utf-8")r = subprocess.run([cc, "-std=gnu11", "-O2", "-mavx", "-Wall", "-Werror", "-Wno-unused-function", str(run), "-o",Check AVX availability before executing an AVX binary, or use a baseline-safe fixture that decodes the instruction bytes without executing them.
Recommendation: Request changes. The Windows fixture supports fixing the AVX epilogue case. The new code also takes over ordinary epilogues and interprets unwind records it does not understand, so the walk can still produce wrong caller frames.
Does it help: Yes in the author's earlier Windows fixture. The baseline recorded 8 wrong steps out of 53, and the patch recorded 0 out of 53. The author reports that the rebase preserved the diff. Exact-head gameplay and time spent with each sampled thread suspended are still unmeasured.
Reviewed:
7231d69ef136e2696108dc53de3067479847c54dagainst merge base2e1f6c32d30ec3582093743a968c8447bb435962. Queried verified revision-matched Graphify maps forunwind_stepand checked its callers in source. Read both changed files' diffs and the test and CI evidence. This was a source review.Checked and fine: The test checks the caller PC, SP and nonvolatile integer registers across instruction stops. Both commits have assistance trailers. No new switches or public formats. Linux uses a separate unwind path. On Windows the single-thread sampler, the all-thread sampler, slow-frame capture, and the legacy crash log all use this helper, including when their walks enter Windows or driver modules.
Blocking
host/mkw_glue.c:378recognizes opcode0xc2(ret imm16), but the success path at line 394 advances RSP by eight bytes only. If the stop is on aret 16, the next step receives an RSP 16 bytes below the caller's actual stack pointer and may publish a wrong frame. Limit the custom handler to the reportedvzeroupperfollowed by plainretand keep other cases on a fault-contained native path, or correctly decode and validate the full adjustment.host/mkw_glue.c:459enters the custom decoder without checking the unwind-record version. The helper treats the fourth byte as a frame-register/offset field and the preflight treats the payload as two-byte unwind slots. In valid V3 records those fields carry operation/epilogue counts and a different payload layout. The sampler walks into loaded modules, whose unwind format can differ from the game's toolchain. Gate the custom decoder on the supported record format before interpreting these fields, including chained records. Use a fault-contained native step or stop the walk for an unsupported format. Microsoft x64 unwind information V3These are additional cases for the narrower handler already suggested in the existing exact-head review.
Testing: The earlier authored changed-check run had six passes and four shared baseline failures. The exact-head Forgejo description check passed, with source and workflow checks pending in the review snapshot. After the source changes, verify the exact-head AVX fixture, include negative cases for ordinary returns and unsupported unwind records, and measure stack-walk suspension cost in the reported Windows gameplay scenario. The existing Windows Market request can check profiling behavior on another CPU/GPU after that; it cannot establish unwind semantics by itself.
Overlaps and merge order: #1343 is the merged predecessor. #1947 makes handwritten Windows unwind records relevant to future coverage. This PR does not require the assembly kit.
@ -340,0 +375,4 @@}const BYTE *popped = b;if (b[0] == 0xc5 && b[1] == 0xf8 && b[2] == 0x77) b += 3; /* vzeroupper (clang's, before the ret) */int out = b[0] == 0xc3 || b[0] == 0xc2 || (b[0] == 0xf3 && b[1] == 0xc3) || /* ret, ret imm16, rep ret */This accepts RET imm16, but the success path restores RSP as return slot + 8. The next unwind step will use the wrong RSP for a nonzero immediate. Restrict the custom case to the affected vzeroupper + plain RET sequence, or decode and validate the full adjustment.
@ -351,3 +458,2 @@const BYTE *info = (const BYTE *)(image + rf->UnwindData);const unsigned frame_reg = info[3] & 15;if (frame_reg && !on_stack(s, (&ctx->Rax)[frame_reg])) return 0;if (ctx->Rip - image - rf->BeginAddress >= info[1]) { /* (past the prologue: an epilogue is run here) */Check the unwind-record version before using these byte positions. In version 3, info[3] holds op/epilogue counts and the payload has a different layout. An ordinary sampler step into a loaded module can therefore interpret counts as register numbers. Keep unsupported versions, including chained records, on a fault-contained native path or stop at that frame.
7231d69ef1186660c677WIP: fix(host): the samplers' stack walk unwinds clang's vzeroupper epilogues itself and checks every read of a frameto WIP: fix(host): the stack walks run clang's AVX return suffix themselves and every step under a fault guardThanks for the review (61976). The branch is rebased onto
main@f1f2dc5b(the two earlier commits unchanged,git range-diff), with the checks inf3ed9d4band the fix in186660c6; head186660c6. Where futondestroyer's review asked for the same thing (a narrower handler), I followed it.186660c6. Every byte the decoder reads is inside theRUNTIME_FUNCTION's range (the pop scan stops at 16 pops or the range's end), and the whole step, decoder included, runs under a vectored fault guard that ends the walk on a fault.186660c6. The custom path is only pops,vzeroupper, plainret; no jump of any kind is taken there. Theavx_nativecase stops on ajmp r11in a body with its frame allocated: wrong atf3ed9d4b, the caller at this head.186660c6by not decodingleaany more (at theleathe body's unwind is still right, and Windows does it). Thelea_sibcase (lea rsp, [r12 + 0x58]) was unwound wrong atf3ed9d4band gives the caller at this head, at theleaand at the pop after it.&ctx->Raxindexing: fixed in186660c6:gpr()maps register numbers toCONTEXT's named members, for the pops and the frame register.186660c6:frame_on_stackis gone; ordinary frames take one guardedRtlVirtualUnwind, a record that is not version 1 or 2 ends the walk. Thebad_chaincase (a chain into unmapped unwind data) crashed atf3ed9d4bin the preflight and ends the walk at this head;version3ends it too.186660c6:on_stackisspan_on(subtraction), and reads of a no-access page inside the range are contained by the guard. Thewrap,no_access_suffixandno_access_bodycases failed atf3ed9d4b(on_stacktook UINT64_MAX - 7; the two no-access cases exited 0xc0000005) and pass at this head.186660c6:walk_initresolvesNtQueryInformationThread(and registers the guard) on the main thread inhos_commit_initand again before the samplers'CreateThreads inhos_prof_start;thread_stackonly reads it. Checked by the test's source checks (no state inthread_stack,walk_initbefore the firstCreateThread).186660c6:host_stacksteps through the same guardedunwind_step(set byhos_crash_record_unwinderat startup) and no longer callsRtlVirtualUnwind; without it (the record's own fixture) it walks frame pointers. Checked by a source check andtests/test_crash_record.py(which stops at a renderer source check before it builds the record, onmain@f1f2dc5btoo, so here only a-fsyntax-onlycompile with the host flags covers the file); no runtime case puts a bad frame under the record.f3ed9d4b:epi_avxis assembled with the exact suffix and its own unwind data, andavx_suffixunwinds every stop on its pops and itsvzeroupper(the compiler-made frame stays as extra coverage, and now fails if it has novzeroupperstop).frame_on_stack) are removed in186660c6, and the reads go toRtlVirtualUnwindunder the guard. Covered instead: a frame-register frame (lea_sib), saves inside a protected page within the stack's range (no_access_body, which faults without the guard), and chained data (bad_chain). No case with XMM saves or a valid chain was added.f3ed9d4b: the authored cases are never executed; the compiler-made frame istarget("avx")in a baseline build and runs only when CPUID and XGETBV report AVX (else "skip avx_steps").CC: fixed inf3ed9d4b:--cc, thenCC, thenLONGSHOT_MINGW's clang, then clang/gcc on PATH, astests/test_fastfp_regions.py;RUN_ALL_ARGSdropped; the last line says "runtime" or "source only".f3ed9d4b: the registration, the wait (120 s) andGetExitCodeThreadare checked, the handler removed and the handle closed.Checks (Windows 11, i7-9700, llvm-mingw clang 23.1.5, each through the PC's job queue):
tests/test_sampler_stack_walk.pyatf3ed9d4bfails 8 of 11 cases (avx_native,ret_imm16,lea_sib,version3,wrap;bad_chain,no_access_suffix,no_access_bodyexit 0xc0000005); at186660c6all 11 pass, "PASS (runtime: every case ran)", and the stepped AVX frame gives "53 steps of avx_frame unwound to its caller (7 of them pops, 1 the vzeroupper)", with--ccand with the default compiler.run_all.py --strict --changed origin/mainat the head: 7 passed, 5 failed, the 5 (test_crash_record,test_crash_dialog,test_profiling_mode,test_renderer_new_handler,test_slow_frames) failing onmain@f1f2dc5btoo.host/mkw_glue.candhost/hos_crash_record.cpass a-fsyntax-onlycompile with the host build's flags. Not run: the game.Waiting on me: the gameplay A/B in Castle Town's market with the time each sampled thread stays suspended, sweeps a second, stack depths and frame-time tails (Still to test); the Linux check and host build; CI on this head.
@futondestroyer thanks for the review (62708). Fixed in
186660c6(checks inf3ed9d4b), rebased ontomain@f1f2dc5b, head186660c6:ret imm16): the walk now runs only the reported suffix itself: pops,vzeroupper(c5 f8 77), then a plainret(c3), every byte inside the function's own range.ret imm16, jumps, theadd/leabefore the pops and epilogues without thevzerouppergo toRtlVirtualUnwind, which now runs under a fault guard (a vectored handler; a fault ends the walk). The test'sret_imm16case (pop rbx, vzeroupper,ret 16) failed atf3ed9d4b(the old decoder gave an RSP 16 bytes low) and passes at this head;plain_returnchecks an ordinary pop/ret epilogue and itsret, left to Windows.RUNTIME_FUNCTION's range), and the preflight that interpreted the codes is gone. Before the native step, a record that is not version 1 or 2 ends the walk; chained parents are read byRtlVirtualUnwindunder the guard. The test'sversion3case (a hand-made version 3 record) andbad_chaincase (a chain into unmapped unwind data) end the walk without a fault; atf3ed9d4bbad_chaincrashed (exit 0xc0000005) in the old preflight andversion3returned a frame.Checks (Windows 11, i7-9700, llvm-mingw clang 23.1.5, each through the PC's job queue):
tests/test_sampler_stack_walk.pyatf3ed9d4bfails 8 of 11 cases (avx_native,ret_imm16,lea_sib,version3,wrap;bad_chain,no_access_suffix,no_access_bodyexit 0xc0000005); at186660c6all 11 pass, "PASS (runtime: every case ran)", and the stepped AVX frame gives "53 steps of avx_frame unwound to its caller (7 of them pops, 1 the vzeroupper)", with--ccand with the default compiler.run_all.py --strict --changed origin/mainat the head: 7 passed, 5 failed, the 5 (test_crash_record,test_crash_dialog,test_profiling_mode,test_renderer_new_handler,test_slow_frames) failing onmain@f1f2dc5btoo.host/mkw_glue.candhost/hos_crash_record.cpass a-fsyntax-onlycompile with the host build's flags. Not run: the game.Not done yet: the gameplay run with the time each sampled thread stays suspended (Still to test), the Linux check, Tier 1 and CI on this head.
@Beanstalk This fixes the host's stack walks on Windows (host/mkw_glue.c): the samplers' walk unwinds clang's vzeroupper epilogues itself and checks every read of a frame, so a profile no longer crashes in RtlVirtualUnwind. It acts only with HOS_PROFILE=1 (or the other samplers, the slow-frame recorder, or after a crash); no gameplay path changes. It has not run in the game on these PCs yet. Please play Castle Town Market for two minutes with HOS_PROFILE=1 on both sides, the PR against main on the same PC, on Windows PCs with an AMD processor, with an AMD GPU, with an Intel GPU and with an NVIDIA GPU. The PR side must not crash (main may: that is the bug) and must write a readable profile (some stacks may be shorter); frame time p99 within main's run-to-run spread.
@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.Description: Risks now states the fault guard's worst case exactly. A fault inside a sampler's step is dispatched while the sampled thread is suspended. If that thread was holding the lock on the vectored-handler list, both threads hang; it is not only a delay. Risks also says what bounds it in this tree: handlers are added at start-up and later only by debugging switches, and none is removed. No code change; head stays
186660c6.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.