Skip to main content

enclave_runner_sgx/
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 enclave_runner::stream_router::StreamRouter;
12use sgxs::loader::Load;
13
14use crate::loader::{EnclaveBuilder, ErasedTcs};
15use crate::usercalls::EnclaveState;
16use std::os::raw::c_void;
17
18/// If this library's TCSs are all currently servicing other calls, the `call`
19/// function will block until a TCS becomes available.
20pub struct Library {
21    _private: (),
22}
23
24impl Library {
25    pub(crate) fn internal_new(
26        tcss: Vec<ErasedTcs>,
27        _address: *mut c_void,
28        _size: usize,
29        stream_router: Box<dyn StreamRouter>,
30        forward_panics: bool,
31        force_time_usercalls: bool,
32    ) -> enclave_runner::Library {
33        let enclave =
34            EnclaveState::library(tcss, stream_router, forward_panics, force_time_usercalls);
35        (Arc::new(move |p1, p2, p3, p4, p5| {
36            EnclaveState::library_entry(&enclave, p1, p2, p3, p4, p5)
37        }) as Arc<dyn Fn(u64, u64, u64, u64, u64) -> _>)
38            .into()
39    }
40
41    pub fn new<P: AsRef<Path>, L: Load>(
42        enclave_path: P,
43        loader: &mut L,
44    ) -> Result<enclave_runner::Library, Error> {
45        enclave_runner::EnclaveBuilder::new(EnclaveBuilder::new(enclave_path.as_ref()))
46            .build(loader)
47    }
48}