enclave_runner/
command.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;
8
9use anyhow::Error;
10use sgxs::loader::{Load, MappingInfo};
11
12use crate::loader::{EnclaveBuilder, ErasedTcs};
13use crate::usercalls::EnclaveState;
14use crate::usercalls::UsercallExtension;
15use std::os::raw::c_void;
16
17#[derive(Debug)]
18pub struct Command {
19    main: ErasedTcs,
20    threads: Vec<ErasedTcs>,
21    address: usize,
22    size: usize,
23    usercall_ext: Option<Box<dyn UsercallExtension>>,
24    forward_panics: bool,
25    force_time_usercalls: bool,
26    cmd_args: Vec<Vec<u8>>,
27    num_worker_threads: usize,
28}
29
30impl MappingInfo for Command {
31    fn address(&self) -> *mut c_void {
32        self.address as _
33    }
34
35    fn size(&self) -> usize {
36        self.size
37    }
38}
39
40impl Command {
41    /// # Panics
42    /// Panics if the number of TCSs is 0.
43    pub(crate) fn internal_new(
44        mut tcss: Vec<ErasedTcs>,
45        address: *mut c_void,
46        size: usize,
47        usercall_ext: Option<Box<dyn UsercallExtension>>,
48        forward_panics: bool,
49        force_time_usercalls: bool,
50        cmd_args: Vec<Vec<u8>>,
51        num_worker_threads: usize,
52    ) -> Command {
53        let main = tcss.remove(0);
54        Command {
55            main,
56            threads: tcss,
57            address: address as _,
58            size,
59            usercall_ext,
60            forward_panics,
61            force_time_usercalls,
62            cmd_args,
63            num_worker_threads,
64        }
65    }
66
67    pub fn new<P: AsRef<Path>, L: Load>(enclave_path: P, loader: &mut L) -> Result<Command, Error> {
68        EnclaveBuilder::new(enclave_path.as_ref()).build(loader)
69    }
70
71    pub fn run(self) -> Result<(), Error> {
72        EnclaveState::main_entry(self.main, self.threads, self.usercall_ext, self.forward_panics, self.force_time_usercalls, self.cmd_args, self.num_worker_threads)
73    }
74}