From 5bf199261daaecb6d837943a311c784975395120 Mon Sep 17 00:00:00 2001 From: Vardhan Thigle Date: Wed, 9 Jan 2019 13:27:18 +0530 Subject: [PATCH] Supporting Backtrace crate for x86_64-fortanix-unknown-sgx. 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. --- Cargo.toml | 2 +- src/backtrace/mod.rs | 5 +++-- src/capture.rs | 17 ++++++++++++++++- src/lib.rs | 3 ++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 355608b9a..33d4c9e94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/src/backtrace/mod.rs b/src/backtrace/mod.rs index da7af7454..d35b0e121 100644 --- a/src/backtrace/mod.rs +++ b/src/backtrace/mod.rs @@ -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; diff --git a/src/capture.rs b/src/capture.rs index 99044a798..bb180883e 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -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 { diff --git a/src/lib.rs b/src/lib.rs index 49e75f030..f69a873b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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;