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
/* Copyright (c) Fortanix, Inc.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::path::Path;

use failure::Error;
use sgxs::loader::{Load, MappingInfo};

use crate::loader::{EnclaveBuilder, ErasedTcs};
use crate::usercalls::EnclaveState;
use crate::usercalls::UsercallExtension;
use std::os::raw::c_void;

#[derive(Debug)]
pub struct Command {
    main: ErasedTcs,
    threads: Vec<ErasedTcs>,
    address: usize,
    size: usize,
    usercall_ext: Option<Box<dyn UsercallExtension>>,
    forward_panics: bool,
    cmd_args: Vec<Vec<u8>>,
}

impl MappingInfo for Command {
    fn address(&self) -> *mut c_void {
        self.address as _
    }

    fn size(&self) -> usize {
        self.size
    }
}

impl Command {
    /// # Panics
    /// Panics if the number of TCSs is 0.
    pub(crate) fn internal_new(
        mut tcss: Vec<ErasedTcs>,
        address: *mut c_void,
        size: usize,
        usercall_ext: Option<Box<dyn UsercallExtension>>,
        forward_panics: bool,
        cmd_args: Vec<Vec<u8>>,
    ) -> Command {
        let main = tcss.remove(0);
        Command {
            main,
            threads: tcss,
            address: address as _,
            size,
            usercall_ext,
            forward_panics,
            cmd_args,
        }
    }

    pub fn new<P: AsRef<Path>, L: Load>(enclave_path: P, loader: &mut L) -> Result<Command, Error> {
        EnclaveBuilder::new(enclave_path.as_ref()).build(loader)
    }

    pub fn run(self) -> Result<(), Error> {
        EnclaveState::main_entry(self.main, self.threads, self.usercall_ext, self.forward_panics, self.cmd_args)
    }
}