Skip to content
Closed
221 changes: 160 additions & 61 deletions src/bun_core/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,46 @@ impl Drop for MemoryAccessor {
}
}

/// Protection flags of the committed, accessible page containing `address`.
/// `None` for free/reserved pages and for committed pages that would still
/// fault on access: "not MEM_FREE" is not enough to make a read safe, since
/// MEM_RESERVE has no backing and MEM_COMMIT pages can be PAGE_NOACCESS or
/// PAGE_GUARD (the stack guard page after EXCEPTION_STACK_OVERFLOW is exactly
/// this).
Comment thread
robobun marked this conversation as resolved.
Resolved
#[cfg(windows)]
fn accessible_page_protection(address: usize) -> Option<u32> {
use bun_windows_sys::kernel32::{
MEM_COMMIT, MEMORY_BASIC_INFORMATION, PAGE_GUARD, PAGE_NOACCESS, VirtualQuery,
};
// SAFETY: MEMORY_BASIC_INFORMATION is a plain Win32 POD; all-zeros is
// a valid representation.
let mut mbi: MEMORY_BASIC_INFORMATION = unsafe { crate::ffi::zeroed_unchecked() };
// SAFETY: `mbi` is a valid out-param of the size we pass; VirtualQuery
// only inspects the address-space mapping at `address`.
let rc = unsafe {
VirtualQuery(
core::ptr::without_provenance(address),
&raw mut mbi,
core::mem::size_of::<MEMORY_BASIC_INFORMATION>(),
)
};
(rc != 0 && mbi.State == MEM_COMMIT && mbi.Protect & (PAGE_NOACCESS | PAGE_GUARD) == 0)
.then_some(mbi.Protect)
}

/// Whether `address` points into mapped code: an image `.text` section or a
/// JIT/FFI allocation. Return addresses always do; the stack slots a derailed
/// walk would otherwise report (JSValues, heap pointers, counters) never do.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[cfg(windows)]
fn is_executable_memory(address: usize) -> bool {
use bun_windows_sys::kernel32::{
PAGE_EXECUTE, PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY,
};
const EXECUTABLE: u32 =
PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY;
accessible_page_protection(address).is_some_and(|p| p & EXECUTABLE != 0)
}

fn is_valid_memory(address: usize) -> bool {
let page_size = bun_alloc::page_size();
let aligned_address = address & !(page_size - 1);
Expand All @@ -176,35 +216,16 @@ fn is_valid_memory(address: usize) -> bool {
#[cfg(windows)]
{
use bun_windows_sys::kernel32::{
MEM_COMMIT, MEMORY_BASIC_INFORMATION, PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE,
PAGE_EXECUTE_WRITECOPY, PAGE_GUARD, PAGE_NOACCESS, PAGE_READONLY, PAGE_READWRITE,
PAGE_WRITECOPY, VirtualQuery,
PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY, PAGE_READONLY,
PAGE_READWRITE, PAGE_WRITECOPY,
};
// SAFETY: MEMORY_BASIC_INFORMATION is a plain Win32 POD; all-zeros is
// a valid representation.
let mut mbi: MEMORY_BASIC_INFORMATION = unsafe { crate::ffi::zeroed_unchecked() };
// SAFETY: `mbi` is a valid out-param of the size we pass; VirtualQuery
// only inspects the address-space mapping at `aligned_address`.
let rc = unsafe {
VirtualQuery(
core::ptr::without_provenance(aligned_address),
&raw mut mbi,
core::mem::size_of::<MEMORY_BASIC_INFORMATION>(),
)
};
// "Not MEM_FREE" is not enough to make a read safe: MEM_RESERVE has no
// backing, and MEM_COMMIT pages can be PAGE_NOACCESS or PAGE_GUARD (the
// stack guard page after EXCEPTION_STACK_OVERFLOW is exactly this).
const READABLE: u32 = PAGE_READONLY
| PAGE_READWRITE
| PAGE_WRITECOPY
| PAGE_EXECUTE_READ
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_WRITECOPY;
rc != 0
&& mbi.State == MEM_COMMIT
&& mbi.Protect & (PAGE_NOACCESS | PAGE_GUARD) == 0
&& mbi.Protect & READABLE != 0
accessible_page_protection(aligned_address).is_some_and(|p| p & READABLE != 0)
}
#[cfg(not(windows))]
{
Expand Down Expand Up @@ -338,6 +359,99 @@ pub(crate) fn capture_current(first_address: Option<usize>, out: &mut [usize]) -
n
}

#[cfg(windows)]
fn context_pc_sp(ctx: &bun_windows_sys::CONTEXT) -> (u64, u64) {
#[cfg(target_arch = "x86_64")]
{
(ctx.Rip, ctx.Rsp)
}
#[cfg(target_arch = "aarch64")]
{
(ctx.Pc, ctx.Sp)
}
}

/// Step out of a leaf function (no unwind info because it never touches the
/// stack): the return address is still where the call left it, `[Rsp]` on x64
/// and `Lr` on ARM64 (`bl` pushes nothing, so `Sp` is correctly left alone).
/// Only frame 0 can be a leaf; a return address never points into a function
/// that makes no calls. Declines, leaving `ctx` untouched, when the slot does
/// not hold a code address: the faulting code is then offlineasm (see
/// [`unwind_frame_pointer`]) and the slot is scratch.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[cfg(windows)]
fn unwind_leaf(ctx: &mut bun_windows_sys::CONTEXT, ma: &mut MemoryAccessor) -> bool {
#[cfg(target_arch = "x86_64")]
{
if ctx.Rsp & 7 != 0 {
return false;
}
let Some(return_pc) = ma.load_usize(ctx.Rsp as usize) else {
return false;
};
if !is_executable_memory(return_pc) {
return false;
}
ctx.Rip = return_pc as u64;
ctx.Rsp += 8;
true
}
#[cfg(target_arch = "aarch64")]
{
let _ = ma;
if ctx.Lr == ctx.Pc || !is_executable_memory(ctx.Lr as usize) {
return false;
}
ctx.Pc = ctx.Lr;
true
}
}

/// Step out of a frame that has no unwind info and is not a leaf. In bun that
/// is JSC's offlineasm code (the LLInt opcode handlers and the `vmEntryTo*`
/// trampolines carry no `.seh_*` directives, and for a PC inside the image
/// `RtlLookupFunctionEntry` consults only its static `.pdata`) and tinycc's
/// `bun:ffi` trampolines. Both keep the frame-pointer register (`rbp`/`x29`)
/// on a frame whose first two slots are the caller's frame pointer and the
/// return address: the `CallFrame` layout, which is also what the unwind info
/// JSC registers for its JIT pool describes (`registerJITUnwindInfo` in
/// WebKit's `ExecutableAllocator.cpp`). The Rtl unwinder restores that
/// register when it steps out of a compiled frame, so it holds the right frame
/// here however many C++ and JIT frames sat above. A frame pointer below the
/// stack pointer, or with unreadable slots, holds something else: stop.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[cfg(windows)]
fn unwind_frame_pointer(ctx: &mut bun_windows_sys::CONTEXT, ma: &mut MemoryAccessor) -> bool {
#[cfg(target_arch = "x86_64")]
let (frame, sp) = (ctx.Rbp, ctx.Rsp);
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
#[cfg(target_arch = "aarch64")]
let (frame, sp) = (ctx.Fp, ctx.Sp);
const SLOT: u64 = core::mem::size_of::<usize>() as u64;
if frame % SLOT != 0 || frame < sp {
return false;
}
let Some(caller_sp) = frame.checked_add(2 * SLOT) else {
return false;
};
let (Some(caller_frame), Some(return_pc)) = (
ma.load_usize(frame as usize),
ma.load_usize((frame + SLOT) as usize),
) else {
return false;
};
#[cfg(target_arch = "x86_64")]
{
ctx.Rbp = caller_frame as u64;
ctx.Rip = return_pc as u64;
ctx.Rsp = caller_sp;
}
#[cfg(target_arch = "aarch64")]
{
ctx.Fp = caller_frame as u64;
ctx.Pc = return_pc as u64;
ctx.Sp = caller_sp;
}
true
}

/// Capture a faulting thread's call stack from the fault context. `pc` is the
/// exact faulting instruction (`ExceptionAddress` / `mcontext` PC) and becomes
/// frame 0.
Expand All @@ -349,9 +463,13 @@ pub(crate) fn capture_current(first_address: Option<usize>, out: &mut [usize]) -
/// Windows: `fp` is the `*const CONTEXT` the VEH received (`EXCEPTION_POINTERS
/// ::ContextRecord`). The walk is `RtlLookupFunctionEntry` + `RtlVirtualUnwind`
/// seeded from that context, so it starts at the fault frame and the handler's
/// own frames are never in the chain. `rbp` is not a reliable frame pointer
/// across the prebuilt JavaScriptCore/LLInt code, so an fp-walk would derail;
/// `.pdata` is always emitted, so the Rtl unwinder works everywhere.
/// own frames are never in the chain. Compiled code always has unwind info and
/// JSC registers it for the JIT pool; the frames that lack it are JSC's
/// offlineasm (LLInt, `vmEntryToJavaScript`), which every JS-initiated crash
/// passes through, and those are stepped over via the frame pointer
/// ([`unwind_frame_pointer`]). A pure frame-pointer walk is still not an
/// option: the prebuilt C++ does not maintain one, and it is the Rtl unwinder
/// stepping through those frames that keeps the frame-pointer register valid.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub fn capture_from_context(pc: usize, fp: usize, out: &mut [usize]) -> usize {
if out.is_empty() {
return 0;
Expand All @@ -369,50 +487,27 @@ pub fn capture_from_context(pc: usize, fp: usize, out: &mut [usize]) -> usize {
// valid for the duration of the handler. Copy so RtlVirtualUnwind can
// mutate freely without touching the kernel's record.
let mut ctx: CONTEXT = unsafe { *(fp as *const CONTEXT) };
let mut ma = MemoryAccessor::INIT;
// Termination: on x64 RtlVirtualUnwind sets Pc = 0 at the end of the
// chain; on ARM64 it can leave the CONTEXT entirely unchanged, so a
// step that changed neither Pc nor Sp is also terminal. An Sp-only or
// Pc-only check truncates legitimate stacks: an ARM64 fault at prolog
// offset 0 (or a .pdata-bearing zero-stack leaf) sets Pc = Lr while
// leaving Sp unchanged, and directly-recursive frames share a return
// Pc. The `n < out.len()` cap is the ultimate bound.
// Pc. A step that produces a non-code Pc has lost the chain (a smashed
// return address, or a fallback applied to a frame it does not fit);
// the trace ends at the last frame that was actually recovered rather
// than continuing into whatever the stack slots happen to hold. The
// `n < out.len()` cap is the ultimate bound.
Comment thread
robobun marked this conversation as resolved.
Resolved
while n < out.len() {
#[cfg(target_arch = "x86_64")]
let (control_pc, control_sp) = (ctx.Rip, ctx.Rsp);
#[cfg(target_arch = "aarch64")]
let (control_pc, control_sp) = (ctx.Pc, ctx.Sp);
let (control_pc, control_sp) = context_pc_sp(&ctx);
let mut image_base: u64 = 0;
// SAFETY: `control_pc` is a code address from the fault context;
// `image_base` is valid for write; history table may be null.
let rf = unsafe {
ntdll::RtlLookupFunctionEntry(control_pc, &mut image_base, core::ptr::null_mut())
};
if rf.is_null() {
// Leaf function with no `.pdata` entry: manually pop the
// return address. On x64 it is at `[Rsp]`; on ARM64 it is in
// `Lr`. An ARM64 leaf allocates no stack (bl does not push),
// so `Sp` is correctly left unchanged here.
#[cfg(target_arch = "x86_64")]
{
if ctx.Rsp & 7 != 0 || !is_valid_memory(ctx.Rsp as usize) {
break;
}
// SAFETY: is_valid_memory confirmed the page at `Rsp` is
// committed and readable (not reserved/guard/noaccess),
// and `Rsp` is 8-aligned.
unsafe {
ctx.Rip = *(ctx.Rsp as *const u64);
}
ctx.Rsp += 8;
}
#[cfg(target_arch = "aarch64")]
{
if ctx.Lr == control_pc {
break;
}
ctx.Pc = ctx.Lr;
}
} else {
if !rf.is_null() {
let mut handler_data: *mut core::ffi::c_void = core::ptr::null_mut();
let mut establisher_frame: u64 = 0;
// SAFETY: `rf` and `image_base` came from RtlLookupFunctionEntry
Expand All @@ -430,12 +525,16 @@ pub fn capture_from_context(pc: usize, fp: usize, out: &mut [usize]) -> usize {
core::ptr::null_mut(),
);
}
} else if !(n == 1 && unwind_leaf(&mut ctx, &mut ma))
&& !unwind_frame_pointer(&mut ctx, &mut ma)
{
break;
}
#[cfg(target_arch = "x86_64")]
let (next_pc, next_sp) = (ctx.Rip, ctx.Rsp);
#[cfg(target_arch = "aarch64")]
let (next_pc, next_sp) = (ctx.Pc, ctx.Sp);
if next_pc == 0 || (next_pc == control_pc && next_sp == control_sp) {
let (next_pc, next_sp) = context_pc_sp(&ctx);
if next_pc == 0
|| (next_pc == control_pc && next_sp == control_sp)
|| !is_executable_memory(next_pc as usize)
{
break;
}
out[n] = next_pc as usize;
Expand Down
8 changes: 6 additions & 2 deletions src/windows_sys/externs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ pub struct M128A {
}

/// `_CONTEXT` for AMD64 (winnt.h). Full layout so `RtlVirtualUnwind` can
/// mutate it in place during a stack walk. Only `Rip`/`Rsp` are read by bun.
/// mutate it in place during a stack walk. Only `Rip`/`Rsp`/`Rbp` are read by
/// bun.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[cfg(all(windows, target_arch = "x86_64"))]
#[repr(C, align(16))]
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -629,10 +630,11 @@ pub struct CONTEXT {
const _: () = {
assert!(core::mem::size_of::<CONTEXT>() == 1232);
assert!(core::mem::offset_of!(CONTEXT, Rsp) == 0x98);
assert!(core::mem::offset_of!(CONTEXT, Rbp) == 0xa0);
assert!(core::mem::offset_of!(CONTEXT, Rip) == 0xf8);
};

/// `_ARM64_NT_CONTEXT` (winnt.h). Only `Pc`/`Sp`/`Lr` are read by bun.
/// `_ARM64_NT_CONTEXT` (winnt.h). Only `Fp`/`Lr`/`Sp`/`Pc` are read by bun.
#[cfg(all(windows, target_arch = "aarch64"))]
#[repr(C, align(16))]
#[derive(Clone, Copy)]
Expand All @@ -656,6 +658,7 @@ pub struct CONTEXT {
#[cfg(all(windows, target_arch = "aarch64"))]
const _: () = {
assert!(core::mem::size_of::<CONTEXT>() == 912);
assert!(core::mem::offset_of!(CONTEXT, Fp) == 0xf0);
assert!(core::mem::offset_of!(CONTEXT, Lr) == 0xf8);
assert!(core::mem::offset_of!(CONTEXT, Sp) == 0x100);
assert!(core::mem::offset_of!(CONTEXT, Pc) == 0x108);
Expand Down Expand Up @@ -840,6 +843,7 @@ pub mod kernel32 {
pub const PAGE_READONLY: u32 = 0x02;
pub const PAGE_READWRITE: u32 = 0x04;
pub const PAGE_WRITECOPY: u32 = 0x08;
pub const PAGE_EXECUTE: u32 = 0x10;
pub const PAGE_EXECUTE_READ: u32 = 0x20;
pub const PAGE_EXECUTE_READWRITE: u32 = 0x40;
pub const PAGE_EXECUTE_WRITECOPY: u32 = 0x80;
Expand Down
Loading