Skip to content

Commit c1330be

Browse files
Rollup merge of rust-lang#117895 - mzohreva:mz/fix-sgx-backtrace, r=Mark-Simulacrum
Adjust frame IP in backtraces relative to image base for SGX target This is followup to rust-lang/backtrace-rs#566. The backtraces printed by `panic!` or generated by `std::backtrace::Backtrace` in SGX target are not usable. The frame addresses need to be relative to image base address so they can be used for symbol resolution. Here's an example panic backtrace generated before this change: ``` $ cargo r --target x86_64-fortanix-unknown-sgx ... stack backtrace: 0: 0x7f8fe401d3a5 - <unknown> 1: 0x7f8fe4034780 - <unknown> 2: 0x7f8fe401c5a3 - <unknown> 3: 0x7f8fe401d1f5 - <unknown> 4: 0x7f8fe401e6f6 - <unknown> ``` Here's the same panic after this change: ``` $ cargo +stage1 r --target x86_64-fortanix-unknown-sgx stack backtrace: 0: 0x198bf - <unknown> 1: 0x3d181 - <unknown> 2: 0x26164 - <unknown> 3: 0x19705 - <unknown> 4: 0x1ef36 - <unknown> ``` cc `@jethrogb` and `@workingjubilee`
2 parents 9c3f143 + b576dd2 commit c1330be

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Diff for: library/std/src/backtrace.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ use crate::fmt;
9595
use crate::panic::UnwindSafe;
9696
use crate::sync::atomic::{AtomicUsize, Ordering::Relaxed};
9797
use crate::sync::LazyLock;
98-
use crate::sys_common::backtrace::{lock, output_filename};
98+
use crate::sys_common::backtrace::{lock, output_filename, set_image_base};
9999
use crate::vec::Vec;
100100

101101
/// A captured OS thread stack backtrace.
@@ -327,6 +327,7 @@ impl Backtrace {
327327
let _lock = lock();
328328
let mut frames = Vec::new();
329329
let mut actual_start = None;
330+
set_image_base();
330331
unsafe {
331332
backtrace_rs::trace_unsynchronized(|frame| {
332333
frames.push(BacktraceFrame {

Diff for: library/std/src/sys_common/backtrace.rs

+12
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::
6464
let mut first_omit = true;
6565
// Start immediately if we're not using a short backtrace.
6666
let mut start = print_fmt != PrintFmt::Short;
67+
set_image_base();
6768
backtrace_rs::trace_unsynchronized(|frame| {
6869
if print_fmt == PrintFmt::Short && idx > MAX_NB_FRAMES {
6970
return false;
@@ -213,3 +214,14 @@ pub fn output_filename(
213214
}
214215
fmt::Display::fmt(&file.display(), fmt)
215216
}
217+
218+
#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
219+
pub fn set_image_base() {
220+
let image_base = crate::os::fortanix_sgx::mem::image_base();
221+
backtrace_rs::set_image_base(crate::ptr::invalid_mut(image_base as _));
222+
}
223+
224+
#[cfg(not(all(target_vendor = "fortanix", target_env = "sgx")))]
225+
pub fn set_image_base() {
226+
// nothing to do for platforms other than SGX
227+
}

0 commit comments

Comments
 (0)