Skip to content

Commit 57f1716

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 058a4de commit 57f1716

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
@@ -423,9 +423,14 @@ impl MultiUseSandbox {
423423
.get_guest_function_call_result()
424424
})();
425425

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

@@ -504,6 +509,25 @@ mod tests {
504509
use crate::sandbox::SandboxConfiguration;
505510
use crate::{GuestBinary, HyperlightError, MultiUseSandbox, Result, UninitializedSandbox};
506511

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

0 commit comments

Comments
 (0)