Skip to content

Commit e42058c

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 <danilochiarlone@gmail.com>
1 parent 5208cfe commit e42058c

File tree

1 file changed

+59
-5
lines changed

1 file changed

+59
-5
lines changed

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 59 additions & 5 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,62 @@ impl
123124
}
124125
}
125126

126-
/// A `GuestBinary` is either a buffer containing the binary or a path to the binary
127+
/// A `GuestBinary` is either a buffer or the file path to some data (e.g., a guest binary).
127128
#[derive(Debug)]
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)]
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)]
157+
pub struct GuestEnvironment<'a, 'b> {
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<'b>>,
162+
}
163+
164+
impl<'a, 'b> GuestEnvironment<'a, 'b> {
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<&'b [u8]>) -> Self {
167+
GuestEnvironment {
168+
guest_binary,
169+
init_data: init_data.map(GuestBlob::from),
170+
}
171+
}
172+
}
173+
174+
impl<'a> From<GuestBinary<'a>> for GuestEnvironment<'a, '_> {
175+
fn from(guest_binary: GuestBinary<'a>) -> Self {
176+
GuestEnvironment {
177+
guest_binary,
178+
init_data: None,
179+
}
180+
}
181+
}
182+
135183
impl UninitializedSandbox {
136184
/// Create a new sandbox configured to run the binary at path
137185
/// `bin_path`.
@@ -142,17 +190,23 @@ impl UninitializedSandbox {
142190
/// 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.
143191
#[instrument(
144192
err(Debug),
145-
skip(guest_binary),
193+
skip(env),
146194
parent = Span::current()
147195
)]
148-
pub fn new(guest_binary: GuestBinary, cfg: Option<SandboxConfiguration>) -> Result<Self> {
196+
pub fn new<'a, 'b>(
197+
env: impl Into<GuestEnvironment<'a, 'b>>,
198+
cfg: Option<SandboxConfiguration>,
199+
) -> Result<Self> {
149200
#[cfg(feature = "build-metadata")]
150201
log_build_details();
151202

152203
// hyperlight is only supported on Windows 11 and Windows Server 2022 and later
153204
#[cfg(target_os = "windows")]
154205
check_windows_version()?;
155206

207+
let env: GuestEnvironment<'_, '_> = env.into();
208+
let guest_binary = env.guest_binary;
209+
156210
// If the guest binary is a file make sure it exists
157211
let guest_binary = match guest_binary {
158212
GuestBinary::FilePath(binary_path) => {

0 commit comments

Comments
 (0)