Skip to content

Commit e4a661a

Browse files
authored
Remove sandbox evolving and devolving and replace it with snapshotting API. (#697)
* expose snapshots to the public API Signed-off-by: Jorge Prendes <[email protected]> * remove usage of devolve Signed-off-by: Jorge Prendes <[email protected]> * remove DevolvableSandbox trait Signed-off-by: Jorge Prendes <[email protected]> * Remove dead code Signed-off-by: Jorge Prendes <[email protected]> * replace evolving a MultiUseSandbox with persist_call_guest_function_by_name Signed-off-by: Jorge Prendes <[email protected]> * make all guest function calls persistent Signed-off-by: Jorge Prendes <[email protected]> * remove dead code Signed-off-by: Jorge Prendes <[email protected]> * fix examples Signed-off-by: Jorge Prendes <[email protected]> * Remove MultiUseGuestCallContext Signed-off-by: Jorge Prendes <[email protected]> * remove EvolvableSandbox trait Signed-off-by: Jorge Prendes <[email protected]> * remove TransitionMetadata Signed-off-by: Jorge Prendes <[email protected]> * remove Noop transition Signed-off-by: Jorge Prendes <[email protected]> * remove automatic snapshot stack Signed-off-by: Jorge Prendes <[email protected]> * Remove Sandbox trait Signed-off-by: Jorge Prendes <[email protected]> * fix formatting Signed-off-by: Jorge Prendes <[email protected]> * fix license header Signed-off-by: Jorge Prendes <[email protected]> * fix benchmark to new API Signed-off-by: Jorge Prendes <[email protected]> * fix formatting again Signed-off-by: Jorge Prendes <[email protected]> * address review comments Signed-off-by: Jorge Prendes <[email protected]> --------- Signed-off-by: Jorge Prendes <[email protected]>
1 parent 27c01ea commit e4a661a

File tree

32 files changed

+291
-1102
lines changed

32 files changed

+291
-1102
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ It is followed by an example of a simple guest application using the Hyperlight
3535
```rust
3636
use std::thread;
3737

38-
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
39-
use hyperlight_host::sandbox_state::transition::Noop;
4038
use hyperlight_host::{MultiUseSandbox, UninitializedSandbox};
4139

4240
fn main() -> hyperlight_host::Result<()> {
@@ -54,7 +52,7 @@ fn main() -> hyperlight_host::Result<()> {
5452
// Note: This function is unused by the guest code below, it's just here for demonstration purposes
5553

5654
// Initialize sandbox to be able to call host functions
57-
let mut multi_use_sandbox: MultiUseSandbox = uninitialized_sandbox.evolve(Noop::default())?;
55+
let mut multi_use_sandbox: MultiUseSandbox = uninitialized_sandbox.evolve()?;
5856

5957
// Call a function in the guest
6058
let message = "Hello, World! I am executing inside of a VM :)\n".to_string();

fuzz/fuzz_targets/guest_call.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ use std::sync::{Mutex, OnceLock};
2020

2121
use hyperlight_host::func::{ParameterValue, ReturnType};
2222
use hyperlight_host::sandbox::uninitialized::GuestBinary;
23-
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
24-
use hyperlight_host::sandbox_state::transition::Noop;
2523
use hyperlight_host::{MultiUseSandbox, UninitializedSandbox};
2624
use hyperlight_testing::simple_guest_for_fuzzing_as_string;
2725
use libfuzzer_sys::fuzz_target;
@@ -38,7 +36,7 @@ fuzz_target!(
3836
)
3937
.unwrap();
4038

41-
let mu_sbox: MultiUseSandbox = u_sbox.evolve(Noop::default()).unwrap();
39+
let mu_sbox: MultiUseSandbox = u_sbox.evolve().unwrap();
4240
SANDBOX.set(Mutex::new(mu_sbox)).unwrap();
4341
},
4442

fuzz/fuzz_targets/host_call.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ use std::sync::{Mutex, OnceLock};
2020

2121
use hyperlight_host::func::{ParameterValue, ReturnType};
2222
use hyperlight_host::sandbox::uninitialized::GuestBinary;
23-
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
24-
use hyperlight_host::sandbox_state::transition::Noop;
2523
use hyperlight_host::{HyperlightError, MultiUseSandbox, UninitializedSandbox};
2624
use hyperlight_testing::simple_guest_for_fuzzing_as_string;
2725
use libfuzzer_sys::fuzz_target;
@@ -37,7 +35,7 @@ fuzz_target!(
3735
)
3836
.unwrap();
3937

40-
let mu_sbox: MultiUseSandbox = u_sbox.evolve(Noop::default()).unwrap();
38+
let mu_sbox: MultiUseSandbox = u_sbox.evolve().unwrap();
4139
SANDBOX.set(Mutex::new(mu_sbox)).unwrap();
4240
},
4341

fuzz/fuzz_targets/host_print.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
use std::sync::{Mutex, OnceLock};
44

55
use hyperlight_host::sandbox::uninitialized::GuestBinary;
6-
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
7-
use hyperlight_host::sandbox_state::transition::Noop;
86
use hyperlight_host::{MultiUseSandbox, UninitializedSandbox};
97
use hyperlight_testing::simple_guest_for_fuzzing_as_string;
108
use libfuzzer_sys::{Corpus, fuzz_target};
@@ -23,7 +21,7 @@ fuzz_target!(
2321
)
2422
.unwrap();
2523

26-
let mu_sbox: MultiUseSandbox = u_sbox.evolve(Noop::default()).unwrap();
24+
let mu_sbox: MultiUseSandbox = u_sbox.evolve().unwrap();
2725
SANDBOX.set(Mutex::new(mu_sbox)).unwrap();
2826
},
2927

src/hyperlight_component_util/src/host.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ fn emit_component<'a, 'b, 'c>(s: &'c mut State<'a, 'b>, wn: WitName, ct: &'c Com
347347
pub(crate) rt: ::std::sync::Arc<::std::sync::Mutex<#rtsid<T>>>,
348348
}
349349
pub(crate) fn register_host_functions<I: #ns::#import_trait + ::std::marker::Send + 'static, S: ::hyperlight_host::func::Registerable>(sb: &mut S, i: I) -> ::std::sync::Arc<::std::sync::Mutex<#rtsid<I>>> {
350-
use ::hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
351350
let rts = ::std::sync::Arc::new(::std::sync::Mutex::new(#rtsid::new()));
352351
let #import_id = ::std::sync::Arc::new(::std::sync::Mutex::new(i));
353352
#(#imports)*
@@ -357,14 +356,12 @@ fn emit_component<'a, 'b, 'c>(s: &'c mut State<'a, 'b>, wn: WitName, ct: &'c Com
357356
#(#exports)*
358357
}
359358
impl #ns::#r#trait for ::hyperlight_host::sandbox::UninitializedSandbox {
360-
type Exports<I: #ns::#import_trait + ::std::marker::Send> = #wrapper_name<I, ::hyperlight_host::func::call_ctx::MultiUseGuestCallContext>;
359+
type Exports<I: #ns::#import_trait + ::std::marker::Send> = #wrapper_name<I, ::hyperlight_host::sandbox::initialized_multi_use::MultiUseSandbox>;
361360
fn instantiate<I: #ns::#import_trait + ::std::marker::Send + 'static>(mut self, i: I) -> Self::Exports<I> {
362361
let rts = register_host_functions(&mut self, i);
363-
let noop = ::core::default::Default::default();
364-
let sb = ::hyperlight_host::sandbox_state::sandbox::EvolvableSandbox::evolve(self, noop).unwrap();
365-
let cc = ::hyperlight_host::func::call_ctx::MultiUseGuestCallContext::start(sb);
362+
let sb = self.evolve().unwrap();
366363
#wrapper_name {
367-
sb: cc,
364+
sb,
368365
rt: rts,
369366
}
370367
}

src/hyperlight_host/benches/benchmarks.rs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ use hyperlight_host::GuestBinary;
1919
use hyperlight_host::sandbox::{
2020
Callable, MultiUseSandbox, SandboxConfiguration, UninitializedSandbox,
2121
};
22-
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
23-
use hyperlight_host::sandbox_state::transition::Noop;
2422
use hyperlight_testing::simple_guest_as_string;
2523

2624
fn create_uninit_sandbox() -> UninitializedSandbox {
@@ -29,7 +27,7 @@ fn create_uninit_sandbox() -> UninitializedSandbox {
2927
}
3028

3129
fn create_multiuse_sandbox() -> MultiUseSandbox {
32-
create_uninit_sandbox().evolve(Noop::default()).unwrap()
30+
create_uninit_sandbox().evolve().unwrap()
3331
}
3432

3533
fn guest_call_benchmark(c: &mut Criterion) {
@@ -38,24 +36,20 @@ fn guest_call_benchmark(c: &mut Criterion) {
3836
// Benchmarks a single guest function call.
3937
// The benchmark does **not** include the time to reset the sandbox memory after the call.
4038
group.bench_function("guest_call", |b| {
41-
let mut call_ctx = create_multiuse_sandbox().new_call_context();
39+
let mut sbox = create_multiuse_sandbox();
4240

43-
b.iter(|| {
44-
call_ctx
45-
.call::<String>("Echo", "hello\n".to_string())
46-
.unwrap()
47-
});
41+
b.iter(|| sbox.call::<String>("Echo", "hello\n".to_string()).unwrap());
4842
});
4943

5044
// Benchmarks a single guest function call.
5145
// The benchmark does include the time to reset the sandbox memory after the call.
52-
group.bench_function("guest_call_with_reset", |b| {
53-
let mut sandbox = create_multiuse_sandbox();
46+
group.bench_function("guest_call_with_restore", |b| {
47+
let mut sbox = create_multiuse_sandbox();
48+
let snapshot = sbox.snapshot().unwrap();
5449

5550
b.iter(|| {
56-
sandbox
57-
.call_guest_function_by_name::<String>("Echo", "hello\n".to_string())
58-
.unwrap()
51+
sbox.call::<String>("Echo", "hello\n".to_string()).unwrap();
52+
sbox.restore(&snapshot).unwrap();
5953
});
6054
});
6155

@@ -69,11 +63,13 @@ fn guest_call_benchmark(c: &mut Criterion) {
6963
.register("HostAdd", |a: i32, b: i32| Ok(a + b))
7064
.unwrap();
7165

72-
let multiuse_sandbox: MultiUseSandbox =
73-
uninitialized_sandbox.evolve(Noop::default()).unwrap();
74-
let mut call_ctx = multiuse_sandbox.new_call_context();
66+
let mut multiuse_sandbox: MultiUseSandbox = uninitialized_sandbox.evolve().unwrap();
7567

76-
b.iter(|| call_ctx.call::<i32>("Add", (1_i32, 41_i32)).unwrap());
68+
b.iter(|| {
69+
multiuse_sandbox
70+
.call::<i32>("Add", (1_i32, 41_i32))
71+
.unwrap()
72+
});
7773
});
7874

7975
group.finish();
@@ -99,7 +95,7 @@ fn guest_call_benchmark_large_param(c: &mut Criterion) {
9995
Some(config),
10096
)
10197
.unwrap();
102-
let mut sandbox = sandbox.evolve(Noop::default()).unwrap();
98+
let mut sandbox = sandbox.evolve().unwrap();
10399

104100
b.iter(|| {
105101
sandbox
@@ -139,17 +135,6 @@ fn sandbox_benchmark(c: &mut Criterion) {
139135
b.iter(create_multiuse_sandbox);
140136
});
141137

142-
// Benchmarks the time to create a new sandbox and create a new call context.
143-
// Does **not** include the time to drop the sandbox or the call context.
144-
group.bench_function("create_sandbox_and_call_context", |b| {
145-
b.iter_with_large_drop(|| create_multiuse_sandbox().new_call_context());
146-
});
147-
148-
// Benchmarks the time to create a new sandbox, create a new call context, and drop the call context.
149-
group.bench_function("create_sandbox_and_call_context_and_drop", |b| {
150-
b.iter(|| create_multiuse_sandbox().new_call_context());
151-
});
152-
153138
group.finish();
154139
}
155140

src/hyperlight_host/examples/func_ctx/main.rs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,28 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
use hyperlight_host::func::call_ctx::MultiUseGuestCallContext;
18-
use hyperlight_host::sandbox::{Callable, MultiUseSandbox, UninitializedSandbox};
19-
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
20-
use hyperlight_host::sandbox_state::transition::Noop;
21-
use hyperlight_host::{GuestBinary, Result};
17+
use hyperlight_host::GuestBinary;
18+
use hyperlight_host::sandbox::UninitializedSandbox;
2219
use hyperlight_testing::simple_guest_as_string;
2320

2421
fn main() {
2522
// create a new `MultiUseSandbox` configured to run the `simpleguest.exe`
2623
// test guest binary
27-
let sbox1: MultiUseSandbox = {
28-
let path = simple_guest_as_string().unwrap();
29-
let u_sbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None).unwrap();
30-
u_sbox.evolve(Noop::default())
31-
}
32-
.unwrap();
33-
34-
// create a new call context from the sandbox, then do some calls with it.
35-
let ctx1 = sbox1.new_call_context();
36-
let sbox2 = do_calls(ctx1).unwrap();
37-
// create a new call context from the returned sandbox, then do some calls
38-
// with that one
39-
let ctx2 = sbox2.new_call_context();
40-
do_calls(ctx2).unwrap();
41-
}
42-
43-
/// Given a `MultiUseGuestCallContext` derived from an existing
44-
/// `MultiUseSandbox` configured to run the `simpleguest.exe` test guest
45-
/// binary, do several calls against that binary, print their results, then
46-
/// call `ctx.finish()` and return the resulting `MultiUseSandbox`. Return an `Err`
47-
/// if anything failed.
48-
fn do_calls(mut ctx: MultiUseGuestCallContext) -> Result<MultiUseSandbox> {
49-
let res: String = ctx.call("Echo", "hello".to_string())?;
24+
let path = simple_guest_as_string().unwrap();
25+
let mut sbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None)
26+
.unwrap()
27+
.evolve()
28+
.unwrap();
29+
30+
// Do several calls against a sandbox running the `simpleguest.exe` binary,
31+
// and print their results
32+
let res: String = sbox
33+
.call_guest_function_by_name("Echo", "hello".to_string())
34+
.unwrap();
5035
println!("got Echo res: {res}");
5136

52-
let res: i32 = ctx.call("CallMalloc", 200_i32)?;
37+
let res: i32 = sbox
38+
.call_guest_function_by_name("CallMalloc", 200_i32)
39+
.unwrap();
5340
println!("got CallMalloc res: {res}");
54-
ctx.finish()
5541
}

src/hyperlight_host/examples/guest-debugging/main.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ use std::thread;
1919
use hyperlight_host::sandbox::SandboxConfiguration;
2020
#[cfg(gdb)]
2121
use hyperlight_host::sandbox::config::DebugInfo;
22-
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
23-
use hyperlight_host::sandbox_state::transition::Noop;
2422
use hyperlight_host::{MultiUseSandbox, UninitializedSandbox};
2523

2624
/// Build a sandbox configuration that enables GDB debugging when the `gdb` feature is enabled.
@@ -70,9 +68,8 @@ fn main() -> hyperlight_host::Result<()> {
7068
// Note: This function is unused, it's just here for demonstration purposes
7169

7270
// Initialize sandboxes to be able to call host functions
73-
let mut multi_use_sandbox_dbg: MultiUseSandbox =
74-
uninitialized_sandbox_dbg.evolve(Noop::default())?;
75-
let mut multi_use_sandbox: MultiUseSandbox = uninitialized_sandbox.evolve(Noop::default())?;
71+
let mut multi_use_sandbox_dbg: MultiUseSandbox = uninitialized_sandbox_dbg.evolve()?;
72+
let mut multi_use_sandbox: MultiUseSandbox = uninitialized_sandbox.evolve()?;
7673

7774
// Call guest function
7875
let message =

src/hyperlight_host/examples/hello-world/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ limitations under the License.
1616
#![allow(clippy::disallowed_macros)]
1717
use std::thread;
1818

19-
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
20-
use hyperlight_host::sandbox_state::transition::Noop;
2119
use hyperlight_host::{MultiUseSandbox, UninitializedSandbox};
2220

2321
fn main() -> hyperlight_host::Result<()> {
@@ -37,7 +35,7 @@ fn main() -> hyperlight_host::Result<()> {
3735
// Note: This function is unused, it's just here for demonstration purposes
3836

3937
// Initialize sandbox to be able to call host functions
40-
let mut multi_use_sandbox: MultiUseSandbox = uninitialized_sandbox.evolve(Noop::default())?;
38+
let mut multi_use_sandbox: MultiUseSandbox = uninitialized_sandbox.evolve()?;
4139

4240
// Call guest function
4341
let message = "Hello, World! I am executing inside of a VM :)\n".to_string();

src/hyperlight_host/examples/logging/main.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ extern crate hyperlight_host;
1818

1919
use std::sync::{Arc, Barrier};
2020

21-
use hyperlight_host::sandbox::Callable;
2221
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
23-
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
24-
use hyperlight_host::sandbox_state::transition::Noop;
25-
use hyperlight_host::{GuestBinary, MultiUseSandbox, Result};
22+
use hyperlight_host::{GuestBinary, Result};
2623
use hyperlight_testing::simple_guest_as_string;
2724

2825
fn fn_writer(_msg: String) -> Result<i32> {
@@ -48,10 +45,7 @@ fn main() -> Result<()> {
4845
usandbox.register_print(fn_writer)?;
4946

5047
// Initialize the sandbox.
51-
52-
let no_op = Noop::<UninitializedSandbox, MultiUseSandbox>::default();
53-
54-
let mut multiuse_sandbox = usandbox.evolve(no_op)?;
48+
let mut multiuse_sandbox = usandbox.evolve()?;
5549

5650
// Call a guest function 5 times to generate some log entries.
5751
for _ in 0..5 {
@@ -81,10 +75,7 @@ fn main() -> Result<()> {
8175
UninitializedSandbox::new(GuestBinary::FilePath(hyperlight_guest_path.clone()), None)?;
8276

8377
// Initialize the sandbox.
84-
85-
let no_op = Noop::<UninitializedSandbox, MultiUseSandbox>::default();
86-
87-
let mut multiuse_sandbox = usandbox.evolve(no_op)?;
78+
let mut multiuse_sandbox = usandbox.evolve()?;
8879
let interrupt_handle = multiuse_sandbox.interrupt_handle();
8980
let barrier = Arc::new(Barrier::new(2));
9081
let barrier2 = barrier.clone();
@@ -102,10 +93,10 @@ fn main() -> Result<()> {
10293
// Call a function that gets cancelled by the host function 5 times to generate some log entries.
10394

10495
for _ in 0..NUM_CALLS {
105-
let mut ctx = multiuse_sandbox.new_call_context();
10696
barrier.wait();
107-
ctx.call::<()>("Spin", ()).unwrap_err();
108-
multiuse_sandbox = ctx.finish().unwrap();
97+
multiuse_sandbox
98+
.call_guest_function_by_name::<()>("Spin", ())
99+
.unwrap_err();
109100
}
110101
thread.join().unwrap();
111102

0 commit comments

Comments
 (0)