1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! # sgx-panic-backtrace
//!
//! A small library for printing out panics and backtraces inside an SGX enclave.
//!
//! ## Why
//!
//! + Get backtraces working while we wait for `backtrace-rs` to get fixed : )
//!
//! ## Usage
//!
//! Add `sgx-panic-backtrace` to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! sgx-panic-backtrace = "0.1.0"
//! ```
//!
//!
//! In the enclave, call `sgx_panic_backtrace::set_panic_hook()` in your main
//! function:
//!
//! ```rust,no_run
//! sgx_panic_backtrace::set_panic_hook();
//! ```
//!
//! If the enclave panics (and panic=abort is not turned on!) it will now print
//! out the raw backtrace frames to stdout. These include only the frame index
//! and relative frame instruction pointer offset, which you'll need to symbolize
//! outside the enclave itself.
//!
//! ```bash
//! $ cargo run --target=x86_64-fortanix-unknown-sgx
//!
//! enclave: panicked at 'foo', bar.rs:10:5
//! stack backtrace:
//! 0: 0x1b09d9
//! 1: 0x1396f6
//! 2: 0x10f4cc
//! 3: 0x48b3ef
//! 4: 0x2d540b
//! 5: 0x2d56fa
//! 6: 0x2d531d
//! 7: 0x16c681
//! 8: 0x116fd0
//! 9: 0x13410e
//! ```
//!
//! To get human readable symbol names and locations from these raw ips, you may
//! wish to use the `stack-trace-resolve` utility that comes with the Fortanix
//! EDP.
//!
//! For example:
//!
//! ```bash
//! $ ftxsgx-runner <my-enclave-bin>.sgxs | stack-trace-resolve <my-enclave-bin>
//! ```
use ;
/// Return the base address of the currently loaded SGX enclave binary. Vendoring
/// this lets us avoid requiring the unstable `sgx_platform` feature.
///
/// This is copied from: [std::os::fortanix_sgx::mem::image_base](https://github.com/rust-lang/rust/blob/master/library/std/src/sys/sgx/abi/mem.rs#L37)
// NOTE: Do not remove inline: will result in relocation failure.
/// Trace each frame and print each relative instruction pointer offset. These
/// offsets should be symbolized these outside the enclave.
/// Set a panic hook that will print out the panic and raw backtrace addresses
/// when the enclave panics. These addresses will need to be symbolized to human-
/// readable symbol names and locations outside the enclave with a tool like
/// `addr2line`.