enclave_runner/platform/
internal.rs1use std::ops::RangeInclusive;
8use std::convert::TryFrom;
9
10use crate::{Command, Library};
11use crate::stream_router::{StreamRouter};
12
13
14pub trait EnclavePlatform<T: EnclaveType> {
15 type Loader;
16
17 fn build(self, loader: Self::Loader, configuration: EnclaveConfiguration, type_configuration: T::Configuration) -> Result<T, anyhow::Error>;
18}
19
20pub struct EnclaveConfiguration {
21 pub stream_router: Box<dyn StreamRouter + Send + Sync>,
22 pub forward_panics: bool,
23}
24
25pub trait EnclaveTypePrivate {
26 type ConfigurationBuilder: Default;
27}
28
29pub trait EnclaveType: EnclaveTypePrivate {
30 type Configuration: TryFrom<Self::ConfigurationBuilder, Error = anyhow::Error>;
31}
32
33impl EnclaveType for Command {
34 type Configuration = CommandConfiguration;
35}
36
37impl EnclaveTypePrivate for Command {
38 type ConfigurationBuilder = CommandConfigurationBuilder;
39}
40
41#[non_exhaustive]
42pub struct CommandConfiguration {
43 pub cmd_args: Vec<Vec<u8>>,
44 pub num_worker_threads: usize,
45}
46
47pub struct CommandConfigurationBuilder {
48 pub(crate) cmd_args: Vec<Vec<u8>>,
49 pub(crate) num_worker_threads: Option<usize>,
50}
51
52impl Default for CommandConfigurationBuilder {
53 fn default() -> Self {
54 CommandConfigurationBuilder {
55 cmd_args: vec![b"enclave".to_vec()],
56 num_worker_threads: None,
57 }
58 }
59}
60
61impl TryFrom<CommandConfigurationBuilder> for CommandConfiguration {
62 type Error = anyhow::Error;
63
64 fn try_from(builder: CommandConfigurationBuilder) -> Result<Self, Self::Error> {
65 if let Some(num_worker_threads) = builder.num_worker_threads {
66 const NUM_WORKER_THREADS_RANGE: RangeInclusive<usize> = 1..=65536;
67 anyhow::ensure!(
68 NUM_WORKER_THREADS_RANGE.contains(&num_worker_threads),
69 "`num_worker_threads` must be in range {NUM_WORKER_THREADS_RANGE:?}"
70 );
71 }
72 let num_worker_threads = builder.num_worker_threads.unwrap_or_else(num_cpus::get);
73
74 Ok(CommandConfiguration {
75 cmd_args: builder.cmd_args,
76 num_worker_threads,
77 })
78 }
79}
80
81
82
83impl EnclaveType for Library {
84 type Configuration = LibraryConfiguration;
85}
86
87impl EnclaveTypePrivate for Library {
88 type ConfigurationBuilder = LibraryConfigurationBuilder;
89}
90
91#[non_exhaustive]
92pub struct LibraryConfiguration {
93}
94
95#[derive(Default)]
96pub struct LibraryConfigurationBuilder {
97}
98
99impl TryFrom<LibraryConfigurationBuilder> for LibraryConfiguration {
100 type Error = anyhow::Error;
101
102 fn try_from(_: LibraryConfigurationBuilder) -> Result<Self, Self::Error> {
103 Ok(LibraryConfiguration {})
104 }
105}