Skip to content

Commit 02fc773

Browse files
committed
Add trait to abstract over sandboxes in guest function calling
This is useful to allow host-side code to work on both core Hyperlight sandboxes and wrapped sandboxes like Hyperlight Wasm. Signed-off-by: Lucy Menon <[email protected]>
1 parent 16d8a22 commit 02fc773

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

src/hyperlight_host/src/func/call_ctx.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use hyperlight_common::flatbuffer_wrappers::function_types::{
2020
use tracing::{instrument, Span};
2121

2222
use super::guest_dispatch::call_function_on_guest;
23+
use crate::sandbox::Callable;
2324
use crate::{MultiUseSandbox, Result};
2425
/// A context for calling guest functions.
2526
///
@@ -50,6 +51,31 @@ impl MultiUseGuestCallContext {
5051
Self { sbox }
5152
}
5253

54+
/// Close out the context and get back the internally-stored
55+
/// `MultiUseSandbox`. Future contexts opened by the returned sandbox
56+
/// will have guest state restored.
57+
#[instrument(err(Debug), skip(self), parent = Span::current())]
58+
pub fn finish(mut self) -> Result<MultiUseSandbox> {
59+
self.sbox.restore_state()?;
60+
Ok(self.sbox)
61+
}
62+
/// Close out the context and get back the internally-stored
63+
/// `MultiUseSandbox`.
64+
///
65+
/// Note that this method is pub(crate) and does not reset the state of the
66+
/// sandbox.
67+
///
68+
/// It is intended to be used when evolving a MultiUseSandbox to a new state
69+
/// and is not intended to be called publicly. It allows the state of the guest to be altered
70+
/// during the evolution of one sandbox state to another, enabling the new state created
71+
/// to be captured and stored in the Sandboxes state stack.
72+
///
73+
pub(crate) fn finish_no_reset(self) -> MultiUseSandbox {
74+
self.sbox
75+
}
76+
}
77+
78+
impl Callable for MultiUseGuestCallContext {
5379
/// Call the guest function called `func_name` with the given arguments
5480
/// `args`, and expect the return value have the same type as
5581
/// `func_ret_type`.
@@ -61,7 +87,7 @@ impl MultiUseGuestCallContext {
6187
/// If you want to reset state, call `finish()` on this `MultiUseGuestCallContext`
6288
/// and get a new one from the resulting `MultiUseSandbox`
6389
#[instrument(err(Debug),skip(self, args),parent = Span::current())]
64-
pub fn call(
90+
fn call(
6591
&mut self,
6692
func_name: &str,
6793
func_ret_type: ReturnType,
@@ -74,29 +100,6 @@ impl MultiUseGuestCallContext {
74100

75101
call_function_on_guest(&mut self.sbox, func_name, func_ret_type, args)
76102
}
77-
78-
/// Close out the context and get back the internally-stored
79-
/// `MultiUseSandbox`. Future contexts opened by the returned sandbox
80-
/// will have guest state restored.
81-
#[instrument(err(Debug), skip(self), parent = Span::current())]
82-
pub fn finish(mut self) -> Result<MultiUseSandbox> {
83-
self.sbox.restore_state()?;
84-
Ok(self.sbox)
85-
}
86-
/// Close out the context and get back the internally-stored
87-
/// `MultiUseSandbox`.
88-
///
89-
/// Note that this method is pub(crate) and does not reset the state of the
90-
/// sandbox.
91-
///
92-
/// It is intended to be used when evolving a MultiUseSandbox to a new state
93-
/// and is not intended to be called publicly. It allows the state of the guest to be altered
94-
/// during the evolution of one sandbox state to another, enabling the new state created
95-
/// to be captured and stored in the Sandboxes state stack.
96-
///
97-
pub(crate) fn finish_no_reset(self) -> MultiUseSandbox {
98-
self.sbox
99-
}
100103
}
101104

102105
#[cfg(test)]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use hyperlight_common::flatbuffer_wrappers::function_types::{
2+
ParameterValue, ReturnType, ReturnValue,
3+
};
4+
5+
use crate::Result;
6+
7+
/// Trait used by the macros to paper over the differences between hyperlight and hyperlight-wasm
8+
pub trait Callable {
9+
/// Call a guest function dynamically
10+
fn call(
11+
&mut self,
12+
func_name: &str,
13+
func_ret_type: ReturnType,
14+
args: Option<Vec<ParameterValue>>,
15+
) -> Result<ReturnValue>;
16+
}

src/hyperlight_host/src/sandbox/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ pub(crate) mod uninitialized_evolve;
4848
/// Metric definitions for Sandbox module.
4949
pub(crate) mod metrics;
5050

51+
/// Trait used by the macros to paper over the differences between hyperlight and hyperlight-wasm
52+
mod callable;
5153
use std::collections::HashMap;
5254

55+
/// Trait used by the macros to paper over the differences between hyperlight and hyperlight-wasm
56+
pub use callable::Callable;
5357
/// Re-export for `SandboxConfiguration` type
5458
pub use config::SandboxConfiguration;
5559
/// Re-export for the `MultiUseSandbox` type

0 commit comments

Comments
 (0)