Skip to content

Commit 4166a4e

Browse files
author
Vardhan Thigle
committed
Supporting backtrace for x86_64-fortanix-unknown-sgx.
1 parent 7ad470c commit 4166a4e

File tree

2 files changed

+83
-10
lines changed

2 files changed

+83
-10
lines changed

src/libstd/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@
305305
#![feature(maybe_uninit)]
306306
#![cfg_attr(all(target_vendor = "fortanix", target_env = "sgx"),
307307
feature(global_asm, range_contains, slice_index_methods,
308-
decl_macro, coerce_unsized, sgx_platform))]
308+
decl_macro, coerce_unsized, sgx_platform, ptr_wrapping_offset_from))]
309309

310310
#![default_lib_allocator]
311311

src/libstd/sys/sgx/backtrace.rs

+82-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,100 @@
11
use io;
2-
use sys::unsupported;
2+
use error::Error;
3+
use libc;
34
use sys_common::backtrace::Frame;
5+
use unwind as uw;
46

57
pub struct BacktraceContext;
68

7-
pub fn unwind_backtrace(_frames: &mut [Frame])
8-
-> io::Result<(usize, BacktraceContext)>
9-
{
10-
unsupported()
9+
struct Context<'a> {
10+
idx: usize,
11+
frames: &'a mut [Frame],
12+
}
13+
14+
#[derive(Debug)]
15+
struct UnwindError(uw::_Unwind_Reason_Code);
16+
17+
impl Error for UnwindError {
18+
fn description(&self) -> &'static str {
19+
"unexpected return value while unwinding"
20+
}
21+
}
22+
23+
impl ::fmt::Display for UnwindError {
24+
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
25+
write!(f, "{}: {:?}", self.description(), self.0)
26+
}
27+
}
28+
29+
#[inline(never)] // this function call can be skipped it when tracing.
30+
pub fn unwind_backtrace(frames: &mut [Frame]) -> io::Result<(usize, BacktraceContext)> {
31+
let mut cx = Context { idx: 0, frames };
32+
let result_unwind =
33+
unsafe { uw::_Unwind_Backtrace(trace_fn, &mut cx as *mut Context as *mut libc::c_void) };
34+
// See libunwind:src/unwind/Backtrace.c for the return values.
35+
// No, there is no doc.
36+
let res = match result_unwind {
37+
// These return codes seem to be benign and need to be ignored for backtraces
38+
// to show up properly on all tested platforms.
39+
uw::_URC_END_OF_STACK | uw::_URC_FATAL_PHASE1_ERROR | uw::_URC_FAILURE => {
40+
Ok((cx.idx, BacktraceContext))
41+
}
42+
_ => Err(io::Error::new(
43+
io::ErrorKind::Other,
44+
UnwindError(result_unwind),
45+
)),
46+
};
47+
res
1148
}
1249

13-
pub fn resolve_symname<F>(_frame: Frame,
14-
_callback: F,
50+
extern "C" fn trace_fn(
51+
ctx: *mut uw::_Unwind_Context,
52+
arg: *mut libc::c_void,
53+
) -> uw::_Unwind_Reason_Code {
54+
let cx = unsafe { &mut *(arg as *mut Context) };
55+
if cx.idx >= cx.frames.len() {
56+
return uw::_URC_NORMAL_STOP;
57+
}
58+
59+
let mut ip_before_insn = 0;
60+
let mut ip = unsafe { uw::_Unwind_GetIPInfo(ctx, &mut ip_before_insn) as *mut libc::c_void };
61+
if !ip.is_null() && ip_before_insn == 0 {
62+
// this is a non-signaling frame, so `ip` refers to the address
63+
// after the calling instruction. account for that.
64+
ip = (ip as usize - 1) as *mut _;
65+
}
66+
67+
let symaddr = unsafe { uw::_Unwind_FindEnclosingFunction(ip) };
68+
cx.frames[cx.idx] = Frame {
69+
symbol_addr: symaddr as *mut u8,
70+
exact_position: ip as *mut u8,
71+
inline_context: 0,
72+
};
73+
cx.idx += 1;
74+
75+
uw::_URC_NO_REASON
76+
}
77+
78+
extern {
79+
static IMAGE_BASE: u8;
80+
}
81+
82+
83+
// To reduce TCB size in Sgx enclave, we do not want to implement resolve_symname functionality.
84+
// Rather, we print the offset of the address here, which could be later mapped to correct function.
85+
pub fn resolve_symname<F>(frame: Frame,
86+
callback: F,
1587
_: &BacktraceContext) -> io::Result<()>
1688
where F: FnOnce(Option<&str>) -> io::Result<()>
1789
{
18-
unsupported()
90+
callback(Some(&format!("0x{:x}",
91+
(unsafe {frame.symbol_addr.wrapping_offset_from(&IMAGE_BASE)}))))
1992
}
2093

2194
pub fn foreach_symbol_fileline<F>(_: Frame,
2295
_: F,
2396
_: &BacktraceContext) -> io::Result<bool>
2497
where F: FnMut(&[u8], u32) -> io::Result<()>
2598
{
26-
unsupported()
99+
Ok(false)
27100
}

0 commit comments

Comments
 (0)