@@ -30,6 +30,7 @@ use crate::func::{ParameterTuple, SupportedReturnType};
30
30
#[ cfg( feature = "build-metadata" ) ]
31
31
use crate :: log_build_details;
32
32
use crate :: mem:: exe:: ExeInfo ;
33
+ use crate :: mem:: memory_region:: MemoryRegionFlags ;
33
34
use crate :: mem:: mgr:: { STACK_COOKIE_LEN , SandboxMemoryManager } ;
34
35
use crate :: mem:: shared_mem:: ExclusiveSharedMemory ;
35
36
use crate :: sandbox:: SandboxConfiguration ;
@@ -123,15 +124,62 @@ impl
123
124
}
124
125
}
125
126
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).
127
128
#[ derive( Debug ) ]
128
129
pub enum GuestBinary < ' a > {
129
- /// A buffer containing the guest binary
130
+ /// A buffer containing the GuestBinary
130
131
Buffer ( & ' a [ u8 ] ) ,
131
- /// A path to the guest binary
132
+ /// A path to the GuestBinary
132
133
FilePath ( String ) ,
133
134
}
134
135
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
+
135
183
impl UninitializedSandbox {
136
184
/// Create a new sandbox configured to run the binary at path
137
185
/// `bin_path`.
@@ -142,17 +190,23 @@ impl UninitializedSandbox {
142
190
/// 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.
143
191
#[ instrument(
144
192
err( Debug ) ,
145
- skip( guest_binary ) ,
193
+ skip( env ) ,
146
194
parent = Span :: current( )
147
195
) ]
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 > {
149
200
#[ cfg( feature = "build-metadata" ) ]
150
201
log_build_details ( ) ;
151
202
152
203
// hyperlight is only supported on Windows 11 and Windows Server 2022 and later
153
204
#[ cfg( target_os = "windows" ) ]
154
205
check_windows_version ( ) ?;
155
206
207
+ let env: GuestEnvironment < ' _ , ' _ > = env. into ( ) ;
208
+ let guest_binary = env. guest_binary ;
209
+
156
210
// If the guest binary is a file make sure it exists
157
211
let guest_binary = match guest_binary {
158
212
GuestBinary :: FilePath ( binary_path) => {
0 commit comments