Skip to content

Commit fd48da2

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 7baab16 commit fd48da2

File tree

12 files changed

+57
-29
lines changed

12 files changed

+57
-29
lines changed

src/hyperlight_host/benches/benchmarks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::sync::{Arc, Mutex};
1919
use criterion::{criterion_group, criterion_main, Criterion};
2020
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
2121
use hyperlight_host::func::HostFunction2;
22-
use hyperlight_host::sandbox::{MultiUseSandbox, UninitializedSandbox};
22+
use hyperlight_host::sandbox::{Callable, MultiUseSandbox, UninitializedSandbox};
2323
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2424
use hyperlight_host::sandbox_state::transition::Noop;
2525
use hyperlight_host::GuestBinary;

src/hyperlight_host/examples/func_ctx/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
1818
use hyperlight_host::func::call_ctx::MultiUseGuestCallContext;
19-
use hyperlight_host::sandbox::{MultiUseSandbox, UninitializedSandbox};
19+
use hyperlight_host::sandbox::{Callable, MultiUseSandbox, UninitializedSandbox};
2020
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2121
use hyperlight_host::sandbox_state::transition::Noop;
2222
use hyperlight_host::{new_error, GuestBinary, Result};

src/hyperlight_host/examples/logging/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::sync::{Arc, Mutex};
1919

2020
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
2121
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
22+
use hyperlight_host::sandbox::Callable;
2223
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2324
use hyperlight_host::sandbox_state::transition::Noop;
2425
use hyperlight_host::{GuestBinary, MultiUseSandbox, Result};

src/hyperlight_host/examples/metrics/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::thread::{spawn, JoinHandle};
2020

2121
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
2222
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
23+
use hyperlight_host::sandbox::Callable;
2324
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2425
use hyperlight_host::sandbox_state::transition::Noop;
2526
use hyperlight_host::{set_metrics_registry, GuestBinary, MultiUseSandbox, Result};

src/hyperlight_host/examples/otlp_tracing/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use std::sync::{Arc, Mutex};
2929
use std::thread::{self, spawn, JoinHandle};
3030

3131
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
32+
use hyperlight_host::sandbox::Callable;
3233
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
3334
use hyperlight_host::sandbox_state::transition::Noop;
3435
use hyperlight_host::{GuestBinary, MultiUseSandbox, Result as HyperlightResult};

src/hyperlight_host/examples/tracing/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::sync::{Arc, Mutex};
2121
use std::thread::{spawn, JoinHandle};
2222

2323
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
24+
use hyperlight_host::sandbox::Callable;
2425
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2526
use hyperlight_host::sandbox_state::transition::Noop;
2627
use hyperlight_host::{GuestBinary, MultiUseSandbox, Result};

src/hyperlight_host/src/func/call_ctx.rs

Lines changed: 28 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)]
@@ -109,6 +112,7 @@ mod tests {
109112
};
110113
use hyperlight_testing::simple_guest_as_string;
111114

115+
use crate::sandbox::Callable;
112116
use crate::sandbox_state::sandbox::EvolvableSandbox;
113117
use crate::sandbox_state::transition::Noop;
114118
use crate::{GuestBinary, HyperlightError, MultiUseSandbox, Result, UninitializedSandbox};

src/hyperlight_host/src/func/guest_dispatch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ mod tests {
113113
use super::*;
114114
use crate::func::call_ctx::MultiUseGuestCallContext;
115115
use crate::func::host_functions::HostFunction0;
116-
use crate::sandbox::is_hypervisor_present;
117116
use crate::sandbox::uninitialized::GuestBinary;
117+
use crate::sandbox::{is_hypervisor_present, Callable};
118118
use crate::sandbox_state::sandbox::EvolvableSandbox;
119119
use crate::sandbox_state::transition::Noop;
120120
use crate::{new_error, HyperlightError, MultiUseSandbox, Result, UninitializedSandbox};
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/initialized_multi_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ mod tests {
263263
use hyperlight_testing::simple_guest_as_string;
264264

265265
use crate::func::call_ctx::MultiUseGuestCallContext;
266-
use crate::sandbox::SandboxConfiguration;
266+
use crate::sandbox::{Callable, SandboxConfiguration};
267267
use crate::sandbox_state::sandbox::{DevolvableSandbox, EvolvableSandbox};
268268
use crate::sandbox_state::transition::{MultiUseContextCallback, Noop};
269269
use crate::{GuestBinary, MultiUseSandbox, UninitializedSandbox};

0 commit comments

Comments
 (0)