sgxs_loaders/isgx/debugging.rs
1//! Functions here support the interaction with a debugger such as gdb.
2//!
3//! The `ftxsgx_get_baseaddress_from_pointer` function is the main intended entrypoint,
4//! it can be called statically by gdb on a running ftxsgx-loader,
5//! or on your binary if this crate is used as a library.
6
7use std::sync::Mutex;
8use std::sync::OnceLock;
9
10/// Stores an Enclave's baseaddress and size.
11/// We need to keep track of existing Enclaves for debugging,
12/// to be able to find the baseaddress and the enclave given a global pointer.
13struct EnclaveBaseAddress {
14 baseaddress: u64,
15 size: u64,
16}
17
18/// A (cached) registry of currently running enclaves.
19///
20/// Note that the list, at any moment in time, might be outdated:
21/// * a recently started Enclave might not have been registered yet,
22/// * and an enclave that have finished might not have been unregistered yet.
23///
24/// The only allowed way to use this is thus informational, without reliance on correctness.
25/// It is currently used for debugging (see gdb.py).
26#[derive(Default)]
27struct EnclaveRegistry {
28 enclaves: Vec<EnclaveBaseAddress>,
29}
30
31/// A global, static mutex to the EnclaveRegistry.
32///
33/// Global statics are (obviously) an anti-pattern.
34/// Here, we're choosing to use a global/static because the data from the registry
35/// will be requested by gdb, globally, without parameters, on a running process.
36fn enclave_registry_mutex() -> &'static Mutex<EnclaveRegistry> {
37 static BASEADDRESS_MUTEX: OnceLock<Mutex<EnclaveRegistry>> = OnceLock::new();
38 BASEADDRESS_MUTEX.get_or_init(|| Mutex::new(EnclaveRegistry::default()))
39}
40
41pub fn register_new_enclave(baseaddress: u64, size: u64) {
42 enclave_registry_mutex()
43 .lock()
44 .expect("Failed to obtain enclave registry mutex for new enclave")
45 .enclaves
46 .push(EnclaveBaseAddress { baseaddress, size });
47 // This is not much of a correctness check per se, but this ensures
48 // that the `ftxsgx_get_baseaddress_from_pointer` function is not eliminated during compilation,
49 // even in --release mode. See the documentation for that function on why we need this.
50 assert_eq!(
51 ftxsgx_get_baseaddress_from_pointer(baseaddress),
52 baseaddress
53 );
54}
55
56pub fn unregister_terminated_enclave(baseaddress: u64) {
57 enclave_registry_mutex()
58 .lock()
59 .expect("Failed to obtain an enclave registry mutex for termination")
60 .enclaves
61 .retain(|e| e.baseaddress != baseaddress)
62}
63
64/// This function should be accessible by gdb, even if compiled in --release mode.
65/// Same as with the Mutex function above, this needs to be
66/// a global/static because it will be called by gdb, globally, without parameters.
67#[no_mangle]
68#[inline(never)]
69pub extern "C" fn ftxsgx_get_baseaddress_from_pointer(gdb_pointer: u64) -> u64 {
70 let registry = enclave_registry_mutex()
71 .lock()
72 .expect("Failed to obtain enclave registry mutex for calculating the baseaddress");
73 let found = registry
74 .enclaves
75 .iter()
76 .find(|e| e.baseaddress <= gdb_pointer && gdb_pointer < e.baseaddress + e.size);
77 found.map(|enclave| enclave.baseaddress).unwrap_or(0)
78}