From b5c66787dafdb16e3090af49cd17682fd2713d5a Mon Sep 17 00:00:00 2001 From: "Abdellah K." <118073627+Abdellah-Khanani@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:14:00 +0100 Subject: [PATCH] loader: unwind a 16-byte aligned copy of the context 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 --- crates/victory-loader/src/crash.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/crates/victory-loader/src/crash.rs b/crates/victory-loader/src/crash.rs index ac31566..47f402e 100644 --- a/crates/victory-loader/src/crash.rs +++ b/crates/victory-loader/src/crash.rs @@ -512,10 +512,19 @@ fn unwind(context: CONTEXT) -> Vec { frames } +/// A `CONTEXT` at the 16-byte alignment the unwind and capture functions need. +#[repr(C, align(16))] +pub(crate) struct Aligned16(pub(crate) CONTEXT); + /// [`unwind`] for any thread of the process, its stack being `low..high`; /// pushes no more than `MAX_FRAMES`, so it does not allocate when `frames` /// has that capacity (another thread may be suspended holding the heap). -pub(crate) fn unwind_into(mut context: CONTEXT, low: usize, high: usize, frames: &mut Vec) { +pub(crate) fn unwind_into(context: CONTEXT, low: usize, high: usize, frames: &mut Vec) { + // `RtlVirtualUnwind` writes the context with 16-byte aligned stores, and + // windows-sys declares `CONTEXT` without that alignment: a by-value copy on + // the stack can land 8 bytes off and fault (seen in a debug build). + let mut aligned = Aligned16(context); + let context = &mut aligned.0; frames.push(context.Rip); while frames.len() < MAX_FRAMES { // Stop where reading the stack would fault inside this handler. @@ -539,7 +548,7 @@ pub(crate) fn unwind_into(mut context: CONTEXT, low: usize, high: usize, frames: image, context.Rip, entry, - &mut context, + context, &mut data, &mut frame, null_mut(), @@ -591,11 +600,12 @@ mod tests { fn unwinds_like_the_system_walk() { // SAFETY: zeroed CONTEXT is valid; both calls write our buffers. let (frames, walked) = unsafe { - let mut context: CONTEXT = std::mem::zeroed(); - RtlCaptureContext(&mut context); + // `RtlCaptureContext` wants it 16-byte aligned too. + let mut context = Aligned16(std::mem::zeroed()); + RtlCaptureContext(&mut context.0); let mut walked = [null_mut(); 8]; let n = RtlCaptureStackBackTrace(0, 8, walked.as_mut_ptr(), null_mut()); - (unwind(context), walked[..usize::from(n)].to_vec()) + (unwind(context.0), walked[..usize::from(n)].to_vec()) }; // Frame 0 differs (two call sites in this function); callers agree. assert!(walked.len() >= 4, "{walked:?}");