From 6e7ea03c268fea775b4b6b3edc49936272bd1d13 Mon Sep 17 00:00:00 2001 From: Mohsen Zohrevandi Date: Mon, 13 Nov 2023 16:43:38 -0800 Subject: [PATCH] Adjust frame IP in backtraces relative to image base for SGX target --- library/std/src/backtrace.rs | 5 +++++ library/std/src/sys_common/backtrace.rs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/library/std/src/backtrace.rs b/library/std/src/backtrace.rs index e7110aebdea..c1874ffc5df 100644 --- a/library/std/src/backtrace.rs +++ b/library/std/src/backtrace.rs @@ -327,6 +327,11 @@ impl Backtrace { let _lock = lock(); let mut frames = Vec::new(); let mut actual_start = None; + #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] + { + let image_base = crate::os::fortanix_sgx::mem::image_base(); + backtrace_rs::set_image_base(crate::ptr::from_exposed_addr_mut(image_base as _)); + } unsafe { backtrace_rs::trace_unsynchronized(|frame| { frames.push(BacktraceFrame { diff --git a/library/std/src/sys_common/backtrace.rs b/library/std/src/sys_common/backtrace.rs index 84e2c5d8d7f..e2acc6a2044 100644 --- a/library/std/src/sys_common/backtrace.rs +++ b/library/std/src/sys_common/backtrace.rs @@ -64,6 +64,11 @@ unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt:: let mut first_omit = true; // Start immediately if we're not using a short backtrace. let mut start = print_fmt != PrintFmt::Short; + #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] + { + let image_base = crate::os::fortanix_sgx::mem::image_base(); + backtrace_rs::set_image_base(crate::ptr::from_exposed_addr_mut(image_base as _)); + } backtrace_rs::trace_unsynchronized(|frame| { if print_fmt == PrintFmt::Short && idx > MAX_NB_FRAMES { return false;