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

Merged
Abdellah-Khanani merged 1 commit from upstream-pr/313 into main 2026-11-24 08:00:13 +00:00
Abdellah-Khanani commented 2026-11-24 02:14:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Abdellah-Khanani on 2026-11-24T02:14:41Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573068496 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/313). Originally posted by [Abdellah-Khanani](https://github.com/Abdellah-Khanani) on 2026-11-24T02:14:41Z. --- `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)
arminkhaleghirod commented 2026-11-24 06:55:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by arminkhaleghirod on 2026-11-24T03:04:17Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7120743548 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/313#issuecomment-7120743548). Originally posted by [arminkhaleghirod](https://github.com/arminkhaleghirod) on 2026-11-24T03:04:17Z. --- Confirmed on Windows 11. On main, `unwinds_like_the_system_walk` crashes every run in debug. With this PR it passes.
mercuryVM commented 2026-11-24 07:37:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T07:37:36Z.

Review: APPROVED


Tested b5c6678 on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (264 passed, 0 failed), debug profile.

  • I confirmed the root cause in the registry source of windows-sys 0.61.2. The x86_64 CONTEXT is #[repr(C)] with no align, and its M128A fields are plain #[repr(C)] too, so the struct's alignment is 8. RtlCaptureContext/RtlVirtualUnwind use aligned 16-byte stores (movaps/fxsave-style), so an 8-off stack slot faults.
  • On this machine, unpatched main happens to get an aligned slot: unwinds_like_the_system_walk passes there. That fits your observation that it depends on the frame layout, and it is exactly why the alignment should be explicit rather than relied on.
  • Aligned16 mirrors the private Aligned already in stacks.rs. Copying into it inside unwind_into covers both callers (the crash handler and stacks.rs) in one place.

No issues found.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6035356930 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/313#pullrequestreview-6035356930). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T07:37:36Z. **Review:** APPROVED --- Tested b5c6678 on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (264 passed, 0 failed), debug profile. - I confirmed the root cause in the registry source of windows-sys 0.61.2. The x86_64 `CONTEXT` is `#[repr(C)]` with no `align`, and its `M128A` fields are plain `#[repr(C)]` too, so the struct's alignment is 8. `RtlCaptureContext`/`RtlVirtualUnwind` use aligned 16-byte stores (`movaps`/`fxsave`-style), so an 8-off stack slot faults. - On this machine, unpatched main happens to get an aligned slot: `unwinds_like_the_system_walk` passes there. That fits your observation that it depends on the frame layout, and it is exactly why the alignment should be explicit rather than relied on. - `Aligned16` mirrors the private `Aligned` already in `stacks.rs`. Copying into it inside `unwind_into` covers both callers (the crash handler and `stacks.rs`) in one place. No issues found. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-24 08:02:06 +00:00
qntic commented 2026-11-24 08:02:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by qntic on 2026-11-24T07:49:44Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7125416556 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/313#issuecomment-7125416556). Originally posted by [qntic](https://github.com/qntic) on 2026-11-24T07:49:44Z. --- 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.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!313
No description provided.