Skip to content

Commit f9836b1

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 f9836b1

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,52 @@ impl
123123
}
124124
}
125125

126-
/// A `GuestBinary` is either a buffer containing the binary or a path to the binary
127-
#[derive(Debug)]
126+
/// A `GuestBinary` is either a buffer or the file path to some data (e.g., a guest binary).
127+
#[derive(Debug, Clone)]
128128
pub enum GuestBinary<'a> {
129-
/// A buffer containing the guest binary
129+
/// A buffer containing the GuestBinary
130130
Buffer(&'a [u8]),
131-
/// A path to the guest binary
131+
/// A path to the GuestBinary
132132
FilePath(String),
133133
}
134134

135+
/// A `GuestEnvironment` is a structure that contains the guest binary and an optional GuestBinary.
136+
#[derive(Debug, Clone)]
137+
pub struct GuestEnvironment<'a> {
138+
/// The guest binary, which can be a file path or a buffer.
139+
pub guest_binary: GuestBinary<'a>,
140+
/// An optional guest blob, which can be used to provide additional data to the guest.
141+
pub guest_blob: Option<&'a [u8]>,
142+
}
143+
144+
impl<'a> GuestEnvironment<'a> {
145+
/// Creates a new `GuestEnvironment` with the given guest binary and an optional guest blob.
146+
pub fn new(guest_binary: GuestBinary<'a>, guest_blob: Option<&'a [u8]>) -> Self {
147+
GuestEnvironment {
148+
guest_binary,
149+
guest_blob,
150+
}
151+
}
152+
}
153+
154+
impl<'a> From<&'a GuestBinary<'a>> for GuestEnvironment<'a> {
155+
fn from(guest_binary: &'a GuestBinary<'a>) -> Self {
156+
GuestEnvironment {
157+
guest_binary: guest_binary.clone(),
158+
guest_blob: None,
159+
}
160+
}
161+
}
162+
163+
impl<'a> From<GuestBinary<'a>> for GuestEnvironment<'a> {
164+
fn from(bin: GuestBinary<'a>) -> Self {
165+
GuestEnvironment {
166+
guest_binary: bin,
167+
guest_blob: None,
168+
}
169+
}
170+
}
171+
135172
impl UninitializedSandbox {
136173
/// Create a new sandbox configured to run the binary at path
137174
/// `bin_path`.
@@ -142,17 +179,23 @@ impl UninitializedSandbox {
142179
/// 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.
143180
#[instrument(
144181
err(Debug),
145-
skip(guest_binary),
182+
skip(env),
146183
parent = Span::current()
147184
)]
148-
pub fn new(guest_binary: GuestBinary, cfg: Option<SandboxConfiguration>) -> Result<Self> {
185+
pub fn new<'a>(
186+
env: impl Into<GuestEnvironment<'a>>,
187+
cfg: Option<SandboxConfiguration>,
188+
) -> Result<Self> {
149189
#[cfg(feature = "build-metadata")]
150190
log_build_details();
151191

152192
// hyperlight is only supported on Windows 11 and Windows Server 2022 and later
153193
#[cfg(target_os = "windows")]
154194
check_windows_version()?;
155195

196+
let env: GuestEnvironment<'_> = env.into();
197+
let guest_binary = env.guest_binary;
198+
156199
// If the guest binary is a file make sure it exists
157200
let guest_binary = match guest_binary {
158201
GuestBinary::FilePath(binary_path) => {

0 commit comments

Comments
 (0)