Skip to content

Commit bc09c1f

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 824daa1 commit bc09c1f

File tree

11 files changed

+71
-28
lines changed

11 files changed

+71
-28
lines changed

src/hyperlight_host/benches/benchmarks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ limitations under the License.
1515
*/
1616

1717
use criterion::{criterion_group, criterion_main, Criterion};
18-
use hyperlight_host::sandbox::{MultiUseSandbox, SandboxConfiguration, UninitializedSandbox};
18+
use hyperlight_host::sandbox::{
19+
Callable, MultiUseSandbox, SandboxConfiguration, UninitializedSandbox,
20+
};
1921
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2022
use hyperlight_host::sandbox_state::transition::Noop;
2123
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
@@ -15,7 +15,7 @@ limitations under the License.
1515
*/
1616

1717
use hyperlight_host::func::call_ctx::MultiUseGuestCallContext;
18-
use hyperlight_host::sandbox::{MultiUseSandbox, UninitializedSandbox};
18+
use hyperlight_host::sandbox::{Callable, MultiUseSandbox, UninitializedSandbox};
1919
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2020
use hyperlight_host::sandbox_state::transition::Noop;
2121
use hyperlight_host::{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 @@ extern crate hyperlight_host;
1919
use std::sync::{Arc, Barrier};
2020

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
@@ -19,6 +19,7 @@ use std::sync::{Arc, Barrier};
1919
use std::thread::{spawn, JoinHandle};
2020

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/tracing-otlp/main.rs

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

2929
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
30+
use hyperlight_host::sandbox::Callable;
3031
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
3132
use hyperlight_host::sandbox_state::transition::Noop;
3233
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
@@ -20,6 +20,7 @@ use std::sync::{Arc, Barrier};
2020
use std::thread::{spawn, JoinHandle};
2121

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::{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
@@ -17,6 +17,7 @@ limitations under the License.
1717
use tracing::{instrument, Span};
1818

1919
use super::{ParameterTuple, SupportedReturnType};
20+
use crate::sandbox::Callable;
2021
use crate::{MultiUseSandbox, Result};
2122
/// A context for calling guest functions.
2223
///
@@ -47,6 +48,31 @@ impl MultiUseGuestCallContext {
4748
Self { sbox }
4849
}
4950

51+
/// Close out the context and get back the internally-stored
52+
/// `MultiUseSandbox`. Future contexts opened by the returned sandbox
53+
/// will have guest state restored.
54+
#[instrument(err(Debug), skip(self), parent = Span::current())]
55+
pub fn finish(mut self) -> Result<MultiUseSandbox> {
56+
self.sbox.restore_state()?;
57+
Ok(self.sbox)
58+
}
59+
/// Close out the context and get back the internally-stored
60+
/// `MultiUseSandbox`.
61+
///
62+
/// Note that this method is pub(crate) and does not reset the state of the
63+
/// sandbox.
64+
///
65+
/// It is intended to be used when evolving a MultiUseSandbox to a new state
66+
/// and is not intended to be called publicly. It allows the state of the guest to be altered
67+
/// during the evolution of one sandbox state to another, enabling the new state created
68+
/// to be captured and stored in the Sandboxes state stack.
69+
///
70+
pub(crate) fn finish_no_reset(self) -> MultiUseSandbox {
71+
self.sbox
72+
}
73+
}
74+
75+
impl Callable for MultiUseGuestCallContext {
5076
/// Call the guest function called `func_name` with the given arguments
5177
/// `args`, and expect the return value have the same type as
5278
/// `func_ret_type`.
@@ -58,7 +84,7 @@ impl MultiUseGuestCallContext {
5884
/// If you want to reset state, call `finish()` on this `MultiUseGuestCallContext`
5985
/// and get a new one from the resulting `MultiUseSandbox`
6086
#[instrument(err(Debug),skip(self, args),parent = Span::current())]
61-
pub fn call<Output: SupportedReturnType>(
87+
fn call<Output: SupportedReturnType>(
6288
&mut self,
6389
func_name: &str,
6490
args: impl ParameterTuple,
@@ -75,29 +101,6 @@ impl MultiUseGuestCallContext {
75101
);
76102
Output::from_value(ret?)
77103
}
78-
79-
/// Close out the context and get back the internally-stored
80-
/// `MultiUseSandbox`. Future contexts opened by the returned sandbox
81-
/// will have guest state restored.
82-
#[instrument(err(Debug), skip(self), parent = Span::current())]
83-
pub fn finish(mut self) -> Result<MultiUseSandbox> {
84-
self.sbox.restore_state()?;
85-
Ok(self.sbox)
86-
}
87-
/// Close out the context and get back the internally-stored
88-
/// `MultiUseSandbox`.
89-
///
90-
/// Note that this method is pub(crate) and does not reset the state of the
91-
/// sandbox.
92-
///
93-
/// It is intended to be used when evolving a MultiUseSandbox to a new state
94-
/// and is not intended to be called publicly. It allows the state of the guest to be altered
95-
/// during the evolution of one sandbox state to another, enabling the new state created
96-
/// to be captured and stored in the Sandboxes state stack.
97-
///
98-
pub(crate) fn finish_no_reset(self) -> MultiUseSandbox {
99-
self.sbox
100-
}
101104
}
102105

103106
#[cfg(test)]
@@ -108,6 +111,7 @@ mod tests {
108111
use hyperlight_testing::simple_guest_as_string;
109112

110113
use super::MultiUseGuestCallContext;
114+
use crate::sandbox::Callable;
111115
use crate::sandbox_state::sandbox::EvolvableSandbox;
112116
use crate::sandbox_state::transition::Noop;
113117
use crate::{GuestBinary, HyperlightError, MultiUseSandbox, Result, UninitializedSandbox};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2024 The Hyperlight Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
use crate::func::{ParameterTuple, SupportedReturnType};
18+
use crate::Result;
19+
20+
/// Trait used by the macros to paper over the differences between hyperlight and hyperlight-wasm
21+
pub trait Callable {
22+
/// Call a guest function dynamically
23+
fn call<Output: SupportedReturnType>(
24+
&mut self,
25+
func_name: &str,
26+
args: impl ParameterTuple,
27+
) -> Result<Output>;
28+
}

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

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

323323
use crate::func::call_ctx::MultiUseGuestCallContext;
324-
use crate::sandbox::SandboxConfiguration;
324+
use crate::sandbox::{Callable, SandboxConfiguration};
325325
use crate::sandbox_state::sandbox::{DevolvableSandbox, EvolvableSandbox};
326326
use crate::sandbox_state::transition::{MultiUseContextCallback, Noop};
327327
use crate::{GuestBinary, HyperlightError, MultiUseSandbox, Result, UninitializedSandbox};

src/hyperlight_host/src/sandbox/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ pub mod uninitialized;
3737
/// initialized `Sandbox`es.
3838
pub(crate) mod uninitialized_evolve;
3939

40+
/// Trait used by the macros to paper over the differences between hyperlight and hyperlight-wasm
41+
mod callable;
42+
43+
/// Trait used by the macros to paper over the differences between hyperlight and hyperlight-wasm
44+
pub use callable::Callable;
4045
/// Re-export for `SandboxConfiguration` type
4146
pub use config::SandboxConfiguration;
4247
/// Re-export for the `MultiUseSandbox` type

0 commit comments

Comments
 (0)