Skip to content

Commit

Permalink
Supporting Backtrace crate for x86_64-fortanix-unknown-sgx.
Browse files Browse the repository at this point in the history
1. Backtracing is supported via libunwind which is linked to x86_64-fortanix-unknown-sgx.
2. To reduce enclave TCB size, we do not support symbol resolution for this target.
   We rather, display the offset of each function, which could be resolved later.
  • Loading branch information
Vardhan Thigle committed Jan 10, 2019
1 parent b8d8d94 commit 5bf1992
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ gimli = { version = "0.16.0", optional = true }
memmap = { version = "0.7.0", optional = true }
object = { version = "0.9.0", optional = true }

[target.'cfg(unix)'.dependencies]
[target.'cfg(any(unix, target_env = "sgx"))'.dependencies]
libc = { version = "0.2.45", default-features = false }

[target.'cfg(windows)'.dependencies]
Expand Down
5 changes: 3 additions & 2 deletions src/backtrace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ impl fmt::Debug for Frame {
}

cfg_if! {
if #[cfg(all(unix,
if #[cfg(any(all(unix,
not(target_os = "emscripten"),
not(all(target_os = "ios", target_arch = "arm")),
feature = "libunwind"))] {
feature = "libunwind"),
target_env="sgx"))] {
mod libunwind;
use self::libunwind::trace as trace_imp;
use self::libunwind::Frame as FrameImp;
Expand Down
17 changes: 16 additions & 1 deletion src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,22 @@ impl fmt::Debug for Backtrace {
};

for (idx, frame) in iter.enumerate() {
let ip = frame.ip();
// To reduce TCB size in Sgx enclave, we do not want to implement symbol resolution functionality.
// Rather, we can print the offset of the address here, which could be later mapped to
// correct function.
let ip: *mut c_void;
#[cfg(target_env = "sgx")]
{
ip = usize::wrapping_sub(
frame.ip() as _,
std::os::fortanix_sgx::mem::image_base() as _,
) as _;
}
#[cfg(not(target_env = "sgx"))]
{
ip = frame.ip();
}

write!(fmt, "\n{:4}: ", idx)?;

let symbols = match frame.symbols {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@
#![doc(html_root_url = "https://docs.rs/backtrace")]
#![deny(missing_docs)]
#![no_std]
#![cfg_attr(target_env = "sgx", feature(sgx_platform))]

#[cfg(feature = "std")]
#[macro_use] extern crate std;

#[cfg(unix)]
#[cfg(any(unix, target_env = "sgx"))]
extern crate libc;
#[cfg(windows)]
extern crate winapi;
Expand Down

0 comments on commit 5bf1992

Please sign in to comment.