hyperlight_host/func/mod.rs
1/*
2Copyright 2024 The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use crate::{new_error, Result};
18/// Context structures used to allow the user to call one or more guest
19/// functions on the same Hyperlight sandbox instance, all from within the
20/// same state and mutual exclusion context.
21pub mod call_ctx;
22/// Functionality to dispatch a call from the host to the guest
23pub(crate) mod guest_dispatch;
24/// Functionality to check for errors after a guest call
25pub(crate) mod guest_err;
26/// Definitions and functionality to enable guest-to-host function calling,
27/// also called "host functions"
28///
29/// This module includes functionality to do the following
30///
31/// - Define several prototypes for what a host function must look like,
32/// including the number of arguments (arity) they can have, supported argument
33/// types, and supported return types
34/// - Registering host functions to be callable by the guest
35/// - Dynamically dispatching a call from the guest to the appropriate
36/// host function
37pub mod host_functions;
38/// Definitions and functionality for supported parameter types
39pub(crate) mod param_type;
40/// Definitions and functionality for supported return types
41pub mod ret_type;
42
43use std::sync::{Arc, Mutex};
44
45/// Re-export for `ParameterValue` enum
46pub use hyperlight_common::flatbuffer_wrappers::function_types::ParameterValue;
47/// Re-export for `ReturnType` enum
48pub use hyperlight_common::flatbuffer_wrappers::function_types::ReturnType;
49/// Re-export for `ReturnType` enum
50pub use hyperlight_common::flatbuffer_wrappers::function_types::ReturnValue;
51pub use param_type::SupportedParameterType;
52pub use ret_type::SupportedReturnType;
53use tracing::{instrument, Span};
54
55type HLFunc = Arc<Mutex<Box<dyn FnMut(Vec<ParameterValue>) -> Result<ReturnValue> + Send>>>;
56
57/// Generic HyperlightFunction
58#[derive(Clone)]
59pub struct HyperlightFunction(HLFunc);
60
61impl HyperlightFunction {
62 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
63 pub(crate) fn new<F>(f: F) -> Self
64 where
65 F: FnMut(Vec<ParameterValue>) -> Result<ReturnValue> + Send + 'static,
66 {
67 Self(Arc::new(Mutex::new(Box::new(f))))
68 }
69
70 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
71 pub(crate) fn call(&self, args: Vec<ParameterValue>) -> Result<ReturnValue> {
72 let mut f = self
73 .0
74 .try_lock()
75 .map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?;
76 f(args)
77 }
78}
79
80/// Re-export for `HostFunction0` trait
81pub use host_functions::HostFunction0;
82/// Re-export for `HostFunction1` trait
83pub use host_functions::HostFunction1;
84/// Re-export for `HostFunction10` trait
85pub use host_functions::HostFunction10;
86/// Re-export for `HostFunction2` trait
87pub use host_functions::HostFunction2;
88/// Re-export for `HostFunction3` trait
89pub use host_functions::HostFunction3;
90/// Re-export for `HostFunction4` trait
91pub use host_functions::HostFunction4;
92/// Re-export for `HostFunction5` trait
93pub use host_functions::HostFunction5;
94/// Re-export for `HostFunction6` trait
95pub use host_functions::HostFunction6;
96/// Re-export for `HostFunction7` trait
97pub use host_functions::HostFunction7;
98/// Re-export for `HostFunction8` trait
99pub use host_functions::HostFunction8;
100/// Re-export for `HostFunction9` trait
101pub use host_functions::HostFunction9;