Skip to content

Commit 824daa1

Browse files
committed
Add trait to abstract over sandboxes in host function registration
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 4fdab8f commit 824daa1

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/hyperlight_host/src/func/host_functions.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,52 @@ use super::{ParameterTuple, ResultType, SupportedReturnType};
2323
use crate::sandbox::{ExtraAllowedSyscall, UninitializedSandbox};
2424
use crate::{log_then_return, new_error, Result};
2525

26+
/// A sandbox on which (primitive) host functions can be registered
27+
///
28+
pub trait Registerable {
29+
/// Register a primitive host function
30+
fn register_host_function<Args: ParameterTuple, Output: SupportedReturnType>(
31+
&mut self,
32+
name: &str,
33+
hf: impl Into<HostFunction<Output, Args>>,
34+
) -> Result<()>;
35+
/// Register a primitive host function whose worker thread has
36+
/// extra permissive seccomp filters installed
37+
#[cfg(all(feature = "seccomp", target_os = "linux"))]
38+
fn register_host_function_with_syscalls<Args: ParameterTuple, Output: SupportedReturnType>(
39+
&mut self,
40+
name: &str,
41+
hf: impl Into<HostFunction<Output, Args>>,
42+
eas: Vec<ExtraAllowedSyscall>,
43+
) -> Result<()>;
44+
}
45+
impl Registerable for UninitializedSandbox {
46+
fn register_host_function<Args: ParameterTuple, Output: SupportedReturnType>(
47+
&mut self,
48+
name: &str,
49+
hf: impl Into<HostFunction<Output, Args>>,
50+
) -> Result<()> {
51+
let mut hfs = self
52+
.host_funcs
53+
.try_lock()
54+
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?;
55+
(*hfs).register_host_function(name.to_string(), hf.into().into())
56+
}
57+
#[cfg(all(feature = "seccomp", target_os = "linux"))]
58+
fn register_host_function_with_syscalls<Args: ParameterTuple, Output: SupportedReturnType>(
59+
&mut self,
60+
name: &str,
61+
hf: impl Into<HostFunction<Output, Args>>,
62+
eas: Vec<ExtraAllowedSyscall>,
63+
) -> Result<()> {
64+
let mut hfs = self
65+
.host_funcs
66+
.try_lock()
67+
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?;
68+
(*hfs).register_host_function_with_syscalls(name.to_string(), hf.into().into(), eas)
69+
}
70+
}
71+
2672
/// A representation of a host function.
2773
/// This is a thin wrapper around a `Fn(Args) -> Result<Output>`.
2874
#[derive(Clone)]

src/hyperlight_host/src/func/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(crate) mod param_type;
3838
pub(crate) mod ret_type;
3939

4040
/// Re-export for `HostFunction` trait
41-
pub use host_functions::HostFunction;
41+
pub use host_functions::{HostFunction, Registerable};
4242
/// Re-export for `ParameterValue` enum
4343
pub use hyperlight_common::flatbuffer_wrappers::function_types::ParameterValue;
4444
/// Re-export for `ReturnType` enum

0 commit comments

Comments
 (0)