loader: unwind a 16-byte aligned copy of the context #313

Manually merged
github merged 1 commit from gh-pr/313/head into gh-pr/313/base 2026-11-26 03:58:56 +00:00
Owner

@⁠Abdellah-Khanani on GitHub · 2026-11-24 02:14 UTC · original
Abdellah-Khanani:loader-aligned-unwind → main · +15 −5 · 1 files · 1 commits · merged 2026-11-24 08:00 UTC

unwind_into hands its by-value CONTEXT to RtlVirtualUnwind, and unwinds_like_the_system_walk hands a stack CONTEXT to RtlCaptureContext. Both functions want the context 16-byte aligned. windows-sys 0.61 declares the x64 CONTEXT as plain #[repr(C)], with no align(16), so on the stack it can land 8 bytes off.

In a debug build here it does. cargo test --workspace on main (d2a4bae) stops the whole victory-loader test binary with thread 'crash::tests::unwinds_like_the_system_walk' has overflowed its stack and STATUS_ACCESS_VIOLATION, so the other 17 loader tests are not counted. A temporary print showed the test's context at an address ending in ...838. Release builds happen to get an aligned slot and pass.

Aligning only the test's context is not enough: the test still crashes inside unwind_into, on its own copy. The same copy is what the crash handler (unwind(*context)) and stacks.rs (unwind_into(context.0, …)) unwind with, so a debug or diagnostics build can fault while writing a crash report.

What is in it

  • crates/victory-loader/src/crash.rs: an Aligned16(CONTEXT) wrapper (#[repr(C, align(16))], like the private Aligned in stacks.rs for GetThreadContext). unwind_into copies its context into one and unwinds that. unwinds_like_the_system_walk captures into one.

Tested

  • Windows 11 Pro 22631, Ryzen 5 2600, AMD Radeon RX 5700 XT (driver 32.0.21045.29003), Rust 1.98.1 from rust-toolchain.toml.
  • cargo test --workspace --no-fail-fast on main d2a4bae: 246 passed, 0 failed, 4 ignored, plus the victory-loader lib binary aborted (0xc0000005, the stack-overflow message above).
  • The same with this change: 264 passed, 0 failed, 4 ignored, exit code 0.
  • cargo test -p victory-loader, debug and --release: 18 + 2 passed in both.
  • With only the test's context aligned (no change to unwind_into), the debug test still crashes the same way.
  • cargo fmt --check -p victory-loader: no new differences (10 before and after, all in lines this does not touch).

Not tested

  • The crash handler on a real crash, and VICTORY_STACKS, in a diagnostics build.
  • Any title.

AI use

  • Claude Code (Claude Opus 5.5) found the cause with a temporary print, wrote the change and this description, and ran the tests above on my machine. I reviewed the diff.

🤖 Generated with Claude Code

**@⁠Abdellah-Khanani** on GitHub · 2026-11-24 02:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/313) `Abdellah-Khanani:loader-aligned-unwind` → `main` · +15 −5 · 1 files · 1 commits · merged 2026-11-24 08:00 UTC `unwind_into` hands its by-value `CONTEXT` to `RtlVirtualUnwind`, and `unwinds_like_the_system_walk` hands a stack `CONTEXT` to `RtlCaptureContext`. Both functions want the context 16-byte aligned. windows-sys 0.61 declares the x64 `CONTEXT` as plain `#[repr(C)]`, with no `align(16)`, so on the stack it can land 8 bytes off. In a debug build here it does. `cargo test --workspace` on `main` (d2a4bae) stops the whole `victory-loader` test binary with `thread 'crash::tests::unwinds_like_the_system_walk' has overflowed its stack` and `STATUS_ACCESS_VIOLATION`, so the other 17 loader tests are not counted. A temporary print showed the test's context at an address ending in `...838`. Release builds happen to get an aligned slot and pass. Aligning only the test's context is not enough: the test still crashes inside `unwind_into`, on its own copy. The same copy is what the crash handler (`unwind(*context)`) and `stacks.rs` (`unwind_into(context.0, …)`) unwind with, so a debug or diagnostics build can fault while writing a crash report. **What is in it** - `crates/victory-loader/src/crash.rs`: an `Aligned16(CONTEXT)` wrapper (`#[repr(C, align(16))]`, like the private `Aligned` in `stacks.rs` for `GetThreadContext`). `unwind_into` copies its context into one and unwinds that. `unwinds_like_the_system_walk` captures into one. **Tested** - Windows 11 Pro 22631, Ryzen 5 2600, AMD Radeon RX 5700 XT (driver 32.0.21045.29003), Rust 1.98.1 from `rust-toolchain.toml`. - `cargo test --workspace --no-fail-fast` on `main` d2a4bae: 246 passed, 0 failed, 4 ignored, plus the `victory-loader` lib binary aborted (`0xc0000005`, the stack-overflow message above). - The same with this change: **264 passed, 0 failed, 4 ignored**, exit code 0. - `cargo test -p victory-loader`, debug and `--release`: 18 + 2 passed in both. - With only the test's context aligned (no change to `unwind_into`), the debug test still crashes the same way. - `cargo fmt --check -p victory-loader`: no new differences (10 before and after, all in lines this does not touch). **Not tested** - The crash handler on a real crash, and `VICTORY_STACKS`, in a diagnostics build. - Any title. **AI use** - Claude Code (Claude Opus 5.5) found the cause with a temporary print, wrote the change and this description, and ran the tests above on my machine. I reviewed the diff. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
RtlVirtualUnwind and RtlCaptureContext want a 16-byte aligned CONTEXT,
and windows-sys 0.61 declares the x64 CONTEXT without align(16). In a
debug build the by-value copy in unwind_into, and the test's stack
context, landed 8 bytes off (an address ending in ...838), so
unwinds_like_the_system_walk crashed the whole victory-loader test binary
with a stack-overflow message and STATUS_ACCESS_VIOLATION. Aligning only
the test's context was not enough: the copy in unwind_into, which the
crash handler and stacks.rs also unwind with, faulted as well.

unwind_into now copies its context into an Aligned16 wrapper and unwinds
that; the test captures into one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:58:55 +00:00
github manually merged commit b5c66787da into gh-pr/313/base 2026-11-26 03:58:56 +00:00
Author
Owner

@⁠arminkhaleghirod on GitHub · 2026-11-24 03:04 UTC · original

Confirmed on Windows 11. On main, unwinds_like_the_system_walk crashes every run in debug. With this PR it passes.

**@⁠arminkhaleghirod** on GitHub · 2026-11-24 03:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/313#issuecomment-7120743548) Confirmed on Windows 11. On main, `unwinds_like_the_system_walk` crashes every run in debug. With this PR it passes.
Author
Owner

@⁠qntic on GitHub · 2026-11-24 07:49 UTC · original

Independent test on a Zen 3 machine: confirms the fix.

Environment: Windows 11 22631, AMD Ryzen 5 5600X (Zen 3), AMD Radeon RX 6600 XT, 32 GB, Rust 1.98.1 from rust-toolchain.toml, debug, --locked. This PR cherry-picked onto d2a4bae (tree identical to b5c6678).

Before (d2a4bae): cargo test --locked -q -p victory-loader --lib aborts in 3 of 3 runs with thread 'crash::tests::unwinds_like_the_system_walk' has overflowed its stack and 0xc0000005, STATUS_ACCESS_VIOLATION. The test alone aborts the same way.

After (this PR):

  • victory-loader lib: 18 passed, 0 failed, in 3 of 3 runs.
  • The test alone: passes.
  • cargo test --workspace --locked --no-fail-fast: 264 passed, 0 failed, 4 ignored, exit code 0. These are the same totals as in the PR description, on a different CPU generation (Zen 3 here, Zen+ there).

Not tested: a release build of the loader tests, the crash handler on a real crash, VICTORY_STACKS, any title.

**@⁠qntic** on GitHub · 2026-11-24 07:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/313#issuecomment-7125416556) Independent test on a Zen 3 machine: confirms the fix. **Environment:** Windows 11 22631, AMD Ryzen 5 5600X (Zen 3), AMD Radeon RX 6600 XT, 32 GB, Rust 1.98.1 from `rust-toolchain.toml`, debug, `--locked`. This PR cherry-picked onto `d2a4bae` (tree identical to `b5c6678`). **Before (`d2a4bae`):** `cargo test --locked -q -p victory-loader --lib` aborts in 3 of 3 runs with `thread 'crash::tests::unwinds_like_the_system_walk' has overflowed its stack` and `0xc0000005, STATUS_ACCESS_VIOLATION`. The test alone aborts the same way. **After (this PR):** - `victory-loader` lib: 18 passed, 0 failed, in 3 of 3 runs. - The test alone: passes. - `cargo test --workspace --locked --no-fail-fast`: **264 passed, 0 failed, 4 ignored**, exit code 0. These are the same totals as in the PR description, on a different CPU generation (Zen 3 here, Zen+ there). **Not tested:** a release build of the loader tests, the crash handler on a real crash, `VICTORY_STACKS`, any title.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!313
No description provided.