enclave_runner/
library.rs

1/* Copyright (c) Fortanix, Inc.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7use std::path::Path;
8use std::sync::Arc;
9
10use anyhow::Error;
11use sgxs::loader::{Load, MappingInfo};
12
13use crate::loader::{EnclaveBuilder, ErasedTcs};
14use crate::usercalls::EnclaveState;
15use crate::usercalls::UsercallExtension;
16use std::fmt;
17use std::os::raw::c_void;
18
19pub struct Library {
20    enclave: Arc<EnclaveState>,
21    address: *mut c_void,
22    size: usize,
23}
24
25impl fmt::Debug for Library {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        f.debug_struct("Library")
28            .field("address", &self.address)
29            .field("size", &self.size)
30            .finish()
31    }
32}
33
34impl MappingInfo for Library {
35    fn address(&self) -> *mut c_void {
36        self.address
37    }
38
39    fn size(&self) -> usize {
40        self.size
41    }
42}
43
44impl Library {
45    pub(crate) fn internal_new(
46        tcss: Vec<ErasedTcs>,
47        address: *mut c_void,
48        size: usize,
49        usercall_ext: Option<Box<dyn UsercallExtension>>,
50        forward_panics: bool,
51        force_time_usercalls: bool,
52    ) -> Library {
53        Library {
54            enclave: EnclaveState::library(tcss, usercall_ext, forward_panics, force_time_usercalls),
55            address,
56            size,
57        }
58    }
59
60    pub fn new<P: AsRef<Path>, L: Load>(enclave_path: P, loader: &mut L) -> Result<Library, Error> {
61        EnclaveBuilder::new(enclave_path.as_ref()).build_library(loader)
62    }
63
64    /// If this library's TCSs are all currently servicing other calls, this
65    /// function will block until a TCS becomes available.
66    ///
67    /// # Safety
68    ///
69    /// The caller must ensure that the parameters passed-in match what the
70    /// enclave is expecting.
71    pub unsafe fn call(
72        &self,
73        p1: u64,
74        p2: u64,
75        p3: u64,
76        p4: u64,
77        p5: u64,
78    ) -> Result<(u64, u64), Error> {
79        EnclaveState::library_entry(&self.enclave, p1, p2, p3, p4, p5)
80    }
81}