Skip to main content

nitro_enclaves/launch/
types.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use super::error::*;
4
5use bitflags::bitflags;
6
7/// The image type of the enclave.
8#[derive(Debug)]
9pub enum ImageType<'a> {
10    /// Enclave Image Format.
11    Eif(&'a [u8]),
12}
13
14/// Data related to setting enclave memory.
15#[derive(Debug)]
16pub struct MemoryInfo<'a> {
17    /// Enclave image type.
18    pub image_type: ImageType<'a>,
19
20    /// Amount of memory (in MiB) to allocate to the enclave.
21    pub size_mib: usize,
22}
23
24impl<'a> MemoryInfo<'a> {
25    pub fn new(image_type: ImageType<'a>, size_mib: usize) -> Self {
26        Self {
27            image_type,
28            size_mib,
29        }
30    }
31}
32
33bitflags! {
34    /// Configuration flags for starting an enclave.
35    #[repr(transparent)]
36    #[derive(Copy, Clone, Default)]
37    pub struct StartFlags: u64 {
38        /// Start enclave in debug mode.
39        const DEBUG = 1;
40    }
41}
42
43/// Calculate an enclave's poll timeout from its image size and the amount of memory allocated to
44/// it.
45pub struct PollTimeout(pub i32);
46
47impl TryFrom<(&[u8], usize)> for PollTimeout {
48    type Error = LaunchError;
49
50    fn try_from(args: (&[u8], usize)) -> Result<Self, Self::Error> {
51        let mul = 60 * 1000; // One minute in milliseconds.
52        let size = args.0.len();
53
54        let file: i32 = ((1 + (size - 1) / (6 << 30)) as i32).saturating_mul(mul);
55        let alloc: i32 = ((1 + (args.1 - 1) / (100 << 30)) as i32).saturating_mul(mul);
56
57        Ok(Self(file + alloc))
58    }
59}
60
61impl From<PollTimeout> for i32 {
62    fn from(arg: PollTimeout) -> i32 {
63        arg.0
64    }
65}