From 6e7ea03c268fea775b4b6b3edc49936272bd1d13 Mon Sep 17 00:00:00 2001 From: Mohsen Zohrevandi Date: Mon, 13 Nov 2023 16:43:38 -0800 Subject: [PATCH 1/3] 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 @@ fn create(ip: usize) -> 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; From ec8c3d9992661f078152b0e1f53b1a96fce090ca Mon Sep 17 00:00:00 2001 From: Mohsen Zohrevandi Date: Tue, 14 Nov 2023 13:27:57 -0800 Subject: [PATCH 2/3] Move SGX-specific image base logic to sys_common --- library/std/src/backtrace.rs | 8 ++------ library/std/src/sys_common/backtrace.rs | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/library/std/src/backtrace.rs b/library/std/src/backtrace.rs index c1874ffc5df..7fcf2ee358c 100644 --- a/library/std/src/backtrace.rs +++ b/library/std/src/backtrace.rs @@ -95,7 +95,7 @@ use crate::panic::UnwindSafe; use crate::sync::atomic::{AtomicUsize, Ordering::Relaxed}; use crate::sync::LazyLock; -use crate::sys_common::backtrace::{lock, output_filename}; +use crate::sys_common::backtrace::{lock, output_filename, set_image_base}; use crate::vec::Vec; /// A captured OS thread stack backtrace. @@ -327,11 +327,7 @@ fn create(ip: usize) -> 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 _)); - } + set_image_base(); 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 e2acc6a2044..d680bd33279 100644 --- a/library/std/src/sys_common/backtrace.rs +++ b/library/std/src/sys_common/backtrace.rs @@ -64,11 +64,7 @@ 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 _)); - } + set_image_base(); backtrace_rs::trace_unsynchronized(|frame| { if print_fmt == PrintFmt::Short && idx > MAX_NB_FRAMES { return false; @@ -218,3 +214,14 @@ pub fn output_filename( } fmt::Display::fmt(&file.display(), fmt) } + +#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] +pub fn set_image_base() { + let image_base = crate::os::fortanix_sgx::mem::image_base(); + backtrace_rs::set_image_base(crate::ptr::from_exposed_addr_mut(image_base as _)); +} + +#[cfg(not(all(target_vendor = "fortanix", target_env = "sgx")))] +pub fn set_image_base() { + // nothing to do for platforms other than SGX +} From b576dd2b3ce9450151c1e0fa82e7377bbadc1123 Mon Sep 17 00:00:00 2001 From: Mohsen Zohrevandi Date: Fri, 17 Nov 2023 11:49:23 -0800 Subject: [PATCH 3/3] Use ptr::invalid_mut for SGX image base --- library/std/src/sys_common/backtrace.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys_common/backtrace.rs b/library/std/src/sys_common/backtrace.rs index d680bd33279..adfe721cfa9 100644 --- a/library/std/src/sys_common/backtrace.rs +++ b/library/std/src/sys_common/backtrace.rs @@ -218,7 +218,7 @@ pub fn output_filename( #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] pub fn set_image_base() { 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::set_image_base(crate::ptr::invalid_mut(image_base as _)); } #[cfg(not(all(target_vendor = "fortanix", target_env = "sgx")))]