Skip to content
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

Adjust frame IP in backtraces relative to image base for SGX target #117895

Merged
merged 3 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion library/std/src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ use crate::fmt;
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.
Expand Down Expand Up @@ -327,6 +327,7 @@ impl Backtrace {
let _lock = lock();
let mut frames = Vec::new();
let mut actual_start = None;
set_image_base();
unsafe {
backtrace_rs::trace_unsynchronized(|frame| {
frames.push(BacktraceFrame {
Expand Down
12 changes: 12 additions & 0 deletions library/std/src/sys_common/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +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;
set_image_base();
backtrace_rs::trace_unsynchronized(|frame| {
if print_fmt == PrintFmt::Short && idx > MAX_NB_FRAMES {
return false;
Expand Down Expand Up @@ -213,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 _));
Copy link
Member

Choose a reason for hiding this comment

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

Is this API implying that the backtrace generation is going to do more than math with the pointer value here? i.e., read/write from it? It seems odd to me to call from_exposed_addr here, vs. invalid_mut, which feels like it more appropriately describes the opaque nature of this pointer and theoretically hints that no one should read from it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, backtrace is only using this for adjusting frame IPs. Having read the docs in https://doc.rust-lang.org/nightly/std/ptr/index.html I was under the impression that doing pointer arithmetic also requires strict provenance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I would not expect arithmetic with non-unsafe functions to ever require provenance. It looks like that code is operating on usize even, so it's not even doing pointer arithmetic I think.

}

#[cfg(not(all(target_vendor = "fortanix", target_env = "sgx")))]
pub fn set_image_base() {
// nothing to do for platforms other than SGX
}