Skip to content

Attempt to backtrace through jit frames on x86_64 Windows #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/backtrace/dbghelp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,31 @@ pub unsafe fn trace(cb: &mut FnMut(&super::Frame) -> bool) {
Err(()) => return, // oh well...
};

// On x86_64 and ARM64 we opt to not use the default `Sym*` functions from
// dbghelp for getting the function table and module base. Instead we use
// the `RtlLookupFunctionEntry` function in kernel32 which will account for
// JIT compiler frames as well. These should be equivalent, but using
// `Rtl*` allows us to backtrace through JIT frames.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just thinking that we should perhaps mention that this is limited to in-process walking only, which is fine for backtrace's use case.

cfg_if::cfg_if! {
if #[cfg(target_pointer_width = "64")] {
use core::ptr;

unsafe extern "system" fn function_table_access(_process: HANDLE, addr: DWORD64) -> PVOID {
let mut base = 0;
RtlLookupFunctionEntry(addr, &mut base, ptr::null_mut()).cast()
}

unsafe extern "system" fn get_module_base(_process: HANDLE, addr: DWORD64) -> DWORD64 {
let mut base = 0;
RtlLookupFunctionEntry(addr, &mut base, ptr::null_mut());
base
}
} else {
let function_table_access = dbghelp.SymFunctionTableAccess64();
let get_module_base = dbghelp.SymGetModuleBase64();
}
}

// Attempt to use `StackWalkEx` if we can, but fall back to `StackWalk64`
// since it's in theory supported on more systems.
match (*dbghelp.dbghelp()).StackWalkEx() {
Expand All @@ -113,8 +138,8 @@ pub unsafe fn trace(cb: &mut FnMut(&super::Frame) -> bool) {
frame_ptr,
&mut context.0 as *mut CONTEXT as *mut _,
None,
Some(dbghelp.SymFunctionTableAccess64()),
Some(dbghelp.SymGetModuleBase64()),
Some(function_table_access),
Some(get_module_base),
None,
0,
) == TRUE
Expand All @@ -141,8 +166,8 @@ pub unsafe fn trace(cb: &mut FnMut(&super::Frame) -> bool) {
frame_ptr,
&mut context.0 as *mut CONTEXT as *mut _,
None,
Some(dbghelp.SymFunctionTableAccess64()),
Some(dbghelp.SymGetModuleBase64()),
Some(function_table_access),
Some(get_module_base),
None,
) == TRUE
{
Expand Down
19 changes: 19 additions & 0 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ cfg_if::cfg_if! {
pub use self::winapi::HINSTANCE;
pub use self::winapi::FARPROC;
pub use self::winapi::LPSECURITY_ATTRIBUTES;
#[cfg(target_pointer_width = "64")]
pub use self::winapi::PUNWIND_HISTORY_TABLE;
#[cfg(target_pointer_width = "64")]
pub use self::winapi::PRUNTIME_FUNCTION;

mod winapi {
pub use winapi::ctypes::*;
Expand All @@ -35,6 +39,10 @@ cfg_if::cfg_if! {
pub type HINSTANCE = *mut c_void;
pub type FARPROC = *mut c_void;
pub type LPSECURITY_ATTRIBUTES = *mut c_void;
#[cfg(target_pointer_width = "64")]
pub type PRUNTIME_FUNCTION = *mut c_void;
#[cfg(target_pointer_width = "64")]
pub type PUNWIND_HISTORY_TABLE = *mut c_void;
}
}

Expand Down Expand Up @@ -374,6 +382,17 @@ ffi! {
}
}

#[cfg(target_pointer_width = "64")]
ffi! {
extern "system" {
pub fn RtlLookupFunctionEntry(
ControlPc: DWORD64,
ImageBase: PDWORD64,
HistoryTable: PUNWIND_HISTORY_TABLE,
) -> PRUNTIME_FUNCTION;
}
}

#[cfg(target_arch = "aarch64")]
ffi! {
#[repr(C, align(16))]
Expand Down