Skip to content

Commit eeb654c

Browse files
committed
[host] added GuestEnvironment struct
The GuestEnvironment struct contains two blobs of data. One identifiable as a guest binary, and one undifferentiated guest blob. This GuestEnvironment is now used to create a new sandbox in place of just a guest binary. There are TryFrom impls to be able to convert from a guest binary to a GuestEnvironment, so this isn't a breaking change. Signed-off-by: danbugs <[email protected]>
1 parent 1c7fc4d commit eeb654c

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::func::{ParameterTuple, SupportedReturnType};
3030
#[cfg(feature = "build-metadata")]
3131
use crate::log_build_details;
3232
use crate::mem::exe::ExeInfo;
33+
use crate::mem::memory_region::MemoryRegionFlags;
3334
use crate::mem::mgr::{STACK_COOKIE_LEN, SandboxMemoryManager};
3435
use crate::mem::shared_mem::ExclusiveSharedMemory;
3536
use crate::sandbox::SandboxConfiguration;
@@ -123,15 +124,71 @@ impl
123124
}
124125
}
125126

126-
/// A `GuestBinary` is either a buffer containing the binary or a path to the binary
127-
#[derive(Debug)]
127+
/// A `GuestBinary` is either a buffer or the file path to some data (e.g., a guest binary).
128+
#[derive(Debug, Clone)]
128129
pub enum GuestBinary<'a> {
129-
/// A buffer containing the guest binary
130+
/// A buffer containing the GuestBinary
130131
Buffer(&'a [u8]),
131-
/// A path to the guest binary
132+
/// A path to the GuestBinary
132133
FilePath(String),
133134
}
134135

136+
/// A `GuestBlob` containing data and the permissions for its use.
137+
#[derive(Debug, Clone)]
138+
pub struct GuestBlob<'a> {
139+
/// The data contained in the blob.
140+
pub data: &'a [u8],
141+
/// The permissions for the blob in memory.
142+
/// By default, it's READ
143+
pub permissions: MemoryRegionFlags,
144+
}
145+
146+
impl<'a> From<&'a [u8]> for GuestBlob<'a> {
147+
fn from(data: &'a [u8]) -> Self {
148+
GuestBlob {
149+
data,
150+
permissions: MemoryRegionFlags::READ,
151+
}
152+
}
153+
}
154+
155+
/// A `GuestEnvironment` is a structure that contains the guest binary and an optional GuestBinary.
156+
#[derive(Debug, Clone)]
157+
pub struct GuestEnvironment<'a> {
158+
/// The guest binary, which can be a file path or a buffer.
159+
pub guest_binary: GuestBinary<'a>,
160+
/// An optional guest blob, which can be used to provide additional data to the guest.
161+
pub init_data: Option<GuestBlob<'a>>,
162+
}
163+
164+
impl<'a> GuestEnvironment<'a> {
165+
/// Creates a new `GuestEnvironment` with the given guest binary and an optional guest blob.
166+
pub fn new(guest_binary: GuestBinary<'a>, init_data: Option<&'a [u8]>) -> Self {
167+
GuestEnvironment {
168+
guest_binary,
169+
init_data: init_data.map(GuestBlob::from),
170+
}
171+
}
172+
}
173+
174+
impl<'a> From<&'a GuestBinary<'a>> for GuestEnvironment<'a> {
175+
fn from(guest_binary: &'a GuestBinary<'a>) -> Self {
176+
GuestEnvironment {
177+
guest_binary: guest_binary.clone(),
178+
init_data: None,
179+
}
180+
}
181+
}
182+
183+
impl<'a> From<GuestBinary<'a>> for GuestEnvironment<'a> {
184+
fn from(bin: GuestBinary<'a>) -> Self {
185+
GuestEnvironment {
186+
guest_binary: bin,
187+
init_data: None,
188+
}
189+
}
190+
}
191+
135192
impl UninitializedSandbox {
136193
/// Create a new sandbox configured to run the binary at path
137194
/// `bin_path`.
@@ -142,17 +199,23 @@ impl UninitializedSandbox {
142199
/// The err attribute is used to emit an error should the Result be an error, it uses the std::`fmt::Debug trait` to print the error.
143200
#[instrument(
144201
err(Debug),
145-
skip(guest_binary),
202+
skip(env),
146203
parent = Span::current()
147204
)]
148-
pub fn new(guest_binary: GuestBinary, cfg: Option<SandboxConfiguration>) -> Result<Self> {
205+
pub fn new<'a>(
206+
env: impl Into<GuestEnvironment<'a>>,
207+
cfg: Option<SandboxConfiguration>,
208+
) -> Result<Self> {
149209
#[cfg(feature = "build-metadata")]
150210
log_build_details();
151211

152212
// hyperlight is only supported on Windows 11 and Windows Server 2022 and later
153213
#[cfg(target_os = "windows")]
154214
check_windows_version()?;
155215

216+
let env: GuestEnvironment<'_> = env.into();
217+
let guest_binary = env.guest_binary;
218+
156219
// If the guest binary is a file make sure it exists
157220
let guest_binary = match guest_binary {
158221
GuestBinary::FilePath(binary_path) => {

0 commit comments

Comments
 (0)