Skip to content

Commit 288e66a

Browse files
committed
Only clear io buffer after unsuccesfull guest call.
Signed-off-by: Ludvig Liljenberg <[email protected]> Undo stuff that breaks unwinding Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 87af189 commit 288e66a

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,14 @@ impl MultiUseSandbox {
425425
.get_guest_function_call_result()
426426
})();
427427

428-
// TODO: Do we want to allow re-entrant guest function calls?
429-
self.get_mgr_wrapper_mut().as_mut().clear_io_buffers();
430-
428+
// In the happy path we do not need to clear io-buffers from the host because:
429+
// - the serialized guest function call is zeroed out by the guest during deserialization, see call to `try_pop_shared_input_data_into::<FunctionCall>()`
430+
// - the serialized guest function result is zeroed out by us (the host) during deserialization, see `get_guest_function_call_result`
431+
// - any serialized host function call are zeroed out by us (the host) during deserialization, see `get_host_function_call`
432+
// - any serialized host function result is zeroed out by the guest during deserialization, see `get_host_return_value`
433+
if res.is_err() {
434+
self.get_mgr_wrapper_mut().as_mut().clear_io_buffers();
435+
}
431436
res
432437
}
433438

@@ -506,6 +511,25 @@ mod tests {
506511
use crate::sandbox::SandboxConfiguration;
507512
use crate::{GuestBinary, HyperlightError, MultiUseSandbox, Result, UninitializedSandbox};
508513

514+
/// Make sure input/output buffers are properly reset after guest call (with host call)
515+
#[test]
516+
fn io_buffer_reset() {
517+
let mut cfg = SandboxConfiguration::default();
518+
cfg.set_input_data_size(4096);
519+
cfg.set_output_data_size(4096);
520+
let path = simple_guest_as_string().unwrap();
521+
let mut sandbox =
522+
UninitializedSandbox::new(GuestBinary::FilePath(path), Some(cfg)).unwrap();
523+
sandbox.register("HostAdd", |a: i32, b: i32| a + b).unwrap();
524+
let mut sandbox = sandbox.evolve().unwrap();
525+
526+
// will exhaust io if leaky
527+
for _ in 0..1000 {
528+
let result = sandbox.call::<i32>("Add", (5i32, 10i32)).unwrap();
529+
assert_eq!(result, 15);
530+
}
531+
}
532+
509533
/// Tests that call_guest_function_by_name restores the state correctly
510534
#[test]
511535
fn test_call_guest_function_by_name() {

0 commit comments

Comments
 (0)