Skip to content

Commit 94d2c37

Browse files
authored
Merge pull request #152 from VardhanThigle/Vardhan/x86_64-fortanix-unknown-sgx-support
Supporting Backtrace crate for x86_64-fortanix-unknown-sgx.
2 parents b8d8d94 + 5bf1992 commit 94d2c37

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ gimli = { version = "0.16.0", optional = true }
3232
memmap = { version = "0.7.0", optional = true }
3333
object = { version = "0.9.0", optional = true }
3434

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

3838
[target.'cfg(windows)'.dependencies]

src/backtrace/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ impl fmt::Debug for Frame {
104104
}
105105

106106
cfg_if! {
107-
if #[cfg(all(unix,
107+
if #[cfg(any(all(unix,
108108
not(target_os = "emscripten"),
109109
not(all(target_os = "ios", target_arch = "arm")),
110-
feature = "libunwind"))] {
110+
feature = "libunwind"),
111+
target_env="sgx"))] {
111112
mod libunwind;
112113
use self::libunwind::trace as trace_imp;
113114
use self::libunwind::Frame as FrameImp;

src/capture.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,22 @@ impl fmt::Debug for Backtrace {
229229
};
230230

231231
for (idx, frame) in iter.enumerate() {
232-
let ip = frame.ip();
232+
// To reduce TCB size in Sgx enclave, we do not want to implement symbol resolution functionality.
233+
// Rather, we can print the offset of the address here, which could be later mapped to
234+
// correct function.
235+
let ip: *mut c_void;
236+
#[cfg(target_env = "sgx")]
237+
{
238+
ip = usize::wrapping_sub(
239+
frame.ip() as _,
240+
std::os::fortanix_sgx::mem::image_base() as _,
241+
) as _;
242+
}
243+
#[cfg(not(target_env = "sgx"))]
244+
{
245+
ip = frame.ip();
246+
}
247+
233248
write!(fmt, "\n{:4}: ", idx)?;
234249

235250
let symbols = match frame.symbols {

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@
7171
#![doc(html_root_url = "https://docs.rs/backtrace")]
7272
#![deny(missing_docs)]
7373
#![no_std]
74+
#![cfg_attr(target_env = "sgx", feature(sgx_platform))]
7475

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

78-
#[cfg(unix)]
79+
#[cfg(any(unix, target_env = "sgx"))]
7980
extern crate libc;
8081
#[cfg(windows)]
8182
extern crate winapi;

0 commit comments

Comments
 (0)