Skip to content

Commit 2e1efff

Browse files
authored
Remove unused FunctionDefinition and redundant FunctionsMap (#479)
* remove unused FunctionDefinition Signed-off-by: Jorge Prendes <[email protected]> * Remove FunctionsMap Signed-off-by: Jorge Prendes <[email protected]> --------- Signed-off-by: Jorge Prendes <[email protected]>
1 parent cfc394c commit 2e1efff

File tree

3 files changed

+17
-137
lines changed

3 files changed

+17
-137
lines changed

src/hyperlight_host/src/func/host_functions.rs

+3-41
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,14 @@ limitations under the License.
1717
#![allow(non_snake_case)]
1818
use std::sync::{Arc, Mutex};
1919

20-
use hyperlight_common::flatbuffer_wrappers::function_types::{
21-
ParameterType, ParameterValue, ReturnType,
22-
};
20+
use hyperlight_common::flatbuffer_wrappers::function_types::ParameterValue;
2321
use tracing::{instrument, Span};
2422

2523
use super::{HyperlightFunction, SupportedParameterType, SupportedReturnType};
2624
use crate::sandbox::{ExtraAllowedSyscall, UninitializedSandbox};
2725
use crate::HyperlightError::UnexpectedNoOfArguments;
2826
use crate::{log_then_return, new_error, Result};
2927

30-
/// The definition of a function exposed from the host to the guest
31-
#[derive(Debug, Default, Clone, PartialEq, Eq)]
32-
pub struct HostFunctionDefinition {
33-
/// The function name
34-
pub function_name: String,
35-
/// The type of the parameter values for the host function call.
36-
pub parameter_types: Option<Vec<ParameterType>>,
37-
/// The type of the return value from the host function call
38-
pub return_type: ReturnType,
39-
}
40-
41-
impl HostFunctionDefinition {
42-
/// Create a new `HostFunctionDefinition`.
43-
pub fn new(
44-
function_name: String,
45-
parameter_types: Option<Vec<ParameterType>>,
46-
return_type: ReturnType,
47-
) -> Self {
48-
Self {
49-
function_name,
50-
parameter_types,
51-
return_type,
52-
}
53-
}
54-
}
55-
5628
/// Trait for registering a host function
5729
pub trait HostFunction<R, Args> {
5830
/// Register the host function with the given name in the sandbox.
@@ -181,8 +153,6 @@ macro_rules! impl_host_function {
181153
Ok(result.into_value())
182154
});
183155

184-
let parameter_types = Some(vec![$($P::TYPE),*]);
185-
186156
if let Some(_eas) = extra_allowed_syscalls {
187157
if cfg!(all(feature = "seccomp", target_os = "linux")) {
188158
// Register with extra allowed syscalls
@@ -193,11 +163,7 @@ macro_rules! impl_host_function {
193163
.try_lock()
194164
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?
195165
.register_host_function_with_syscalls(
196-
&HostFunctionDefinition::new(
197-
name.to_string(),
198-
parameter_types,
199-
R::TYPE,
200-
),
166+
name.to_string(),
201167
HyperlightFunction::new(func),
202168
_eas,
203169
)?;
@@ -213,11 +179,7 @@ macro_rules! impl_host_function {
213179
.try_lock()
214180
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?
215181
.register_host_function(
216-
&HostFunctionDefinition::new(
217-
name.to_string(),
218-
parameter_types,
219-
R::TYPE,
220-
),
182+
name.to_string(),
221183
HyperlightFunction::new(func),
222184
)?;
223185
}

src/hyperlight_host/src/sandbox/host_funcs.rs

+14-50
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,33 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
use std::collections::HashMap;
1718
use std::io::{IsTerminal, Write};
1819

1920
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnValue};
2021
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
2122
use tracing::{instrument, Span};
2223

23-
use super::{ExtraAllowedSyscall, FunctionsMap};
24-
use crate::func::host_functions::HostFunctionDefinition;
24+
use super::ExtraAllowedSyscall;
2525
use crate::func::HyperlightFunction;
2626
use crate::HyperlightError::HostFunctionNotFound;
2727
use crate::{new_error, Result};
2828

29-
type HostFunctionDetails = Option<Vec<HostFunctionDefinition>>;
30-
3129
#[derive(Default, Clone)]
3230
/// A Wrapper around details of functions exposed by the Host
3331
pub struct HostFuncsWrapper {
34-
functions_map: FunctionsMap,
35-
function_details: HostFunctionDetails,
32+
functions_map: HashMap<String, (HyperlightFunction, Option<Vec<ExtraAllowedSyscall>>)>,
3633
}
3734

3835
impl HostFuncsWrapper {
39-
#[instrument(skip_all, parent = Span::current(), level = "Trace")]
40-
fn get_host_funcs(&self) -> &FunctionsMap {
41-
&self.functions_map
42-
}
43-
#[instrument(skip_all, parent = Span::current(), level = "Trace")]
44-
fn get_host_funcs_mut(&mut self) -> &mut FunctionsMap {
45-
&mut self.functions_map
46-
}
47-
#[instrument(skip_all, parent = Span::current(), level = "Trace")]
48-
fn get_host_func_details(&self) -> &HostFunctionDetails {
49-
&self.function_details
50-
}
51-
#[instrument(skip_all, parent = Span::current(), level = "Trace")]
52-
fn get_host_func_details_mut(&mut self) -> &mut HostFunctionDetails {
53-
&mut self.function_details
54-
}
55-
5636
/// Register a host function with the sandbox.
5737
#[instrument(err(Debug), skip_all, parent = Span::current(), level = "Trace")]
5838
pub(crate) fn register_host_function(
5939
&mut self,
60-
hfd: &HostFunctionDefinition,
40+
name: String,
6141
func: HyperlightFunction,
6242
) -> Result<()> {
63-
register_host_function_helper(self, hfd, func, None)
43+
register_host_function_helper(self, name, func, None)
6444
}
6545

6646
/// Register a host function with the sandbox, with a list of extra syscalls
@@ -69,11 +49,11 @@ impl HostFuncsWrapper {
6949
#[cfg(all(feature = "seccomp", target_os = "linux"))]
7050
pub(crate) fn register_host_function_with_syscalls(
7151
&mut self,
72-
hfd: &HostFunctionDefinition,
52+
name: String,
7353
func: HyperlightFunction,
7454
extra_allowed_syscalls: Vec<ExtraAllowedSyscall>,
7555
) -> Result<()> {
76-
register_host_function_helper(self, hfd, func, Some(extra_allowed_syscalls))
56+
register_host_function_helper(self, name, func, Some(extra_allowed_syscalls))
7757
}
7858

7959
/// Assuming a host function called `"HostPrint"` exists, and takes a
@@ -84,7 +64,7 @@ impl HostFuncsWrapper {
8464
#[instrument(err(Debug), skip_all, parent = Span::current(), level = "Trace")]
8565
pub(super) fn host_print(&mut self, msg: String) -> Result<i32> {
8666
let res = call_host_func_impl(
87-
self.get_host_funcs(),
67+
&self.functions_map,
8868
"HostPrint",
8969
vec![ParameterValue::String(msg)],
9070
)?;
@@ -104,56 +84,40 @@ impl HostFuncsWrapper {
10484
name: &str,
10585
args: Vec<ParameterValue>,
10686
) -> Result<ReturnValue> {
107-
call_host_func_impl(self.get_host_funcs(), name, args)
108-
}
109-
110-
/// Insert a host function into the list of registered host functions.
111-
pub(super) fn insert_host_function(&mut self, host_function: HostFunctionDefinition) {
112-
match &mut self.function_details {
113-
Some(host_functions) => host_functions.push(host_function),
114-
None => {
115-
let host_functions = Vec::from(&[host_function]);
116-
self.function_details = Some(host_functions);
117-
}
118-
}
87+
call_host_func_impl(&self.functions_map, name, args)
11988
}
12089
}
12190

12291
fn register_host_function_helper(
12392
self_: &mut HostFuncsWrapper,
124-
hfd: &HostFunctionDefinition,
93+
name: String,
12594
func: HyperlightFunction,
12695
extra_allowed_syscalls: Option<Vec<ExtraAllowedSyscall>>,
12796
) -> Result<()> {
12897
if let Some(_syscalls) = extra_allowed_syscalls {
12998
#[cfg(all(feature = "seccomp", target_os = "linux"))]
130-
self_
131-
.get_host_funcs_mut()
132-
.insert(hfd.function_name.to_string(), func, Some(_syscalls));
99+
self_.functions_map.insert(name, (func, Some(_syscalls)));
133100

134101
#[cfg(not(all(feature = "seccomp", target_os = "linux")))]
135102
return Err(new_error!(
136103
"Extra syscalls are only supported on Linux with seccomp"
137104
));
138105
} else {
139-
self_
140-
.get_host_funcs_mut()
141-
.insert(hfd.function_name.to_string(), func, None);
106+
self_.functions_map.insert(name, (func, None));
142107
}
143-
self_.insert_host_function(hfd.clone());
144108

145109
Ok(())
146110
}
147111

148112
#[instrument(err(Debug), skip_all, parent = Span::current(), level = "Trace")]
149113
fn call_host_func_impl(
150-
host_funcs: &FunctionsMap,
114+
host_funcs: &HashMap<String, (HyperlightFunction, Option<Vec<ExtraAllowedSyscall>>)>,
151115
name: &str,
152116
args: Vec<ParameterValue>,
153117
) -> Result<ReturnValue> {
154118
// Inner function containing the common logic
155119
fn call_func(
156-
host_funcs: &FunctionsMap,
120+
host_funcs: &HashMap<String, (HyperlightFunction, Option<Vec<ExtraAllowedSyscall>>)>,
157121
name: &str,
158122
args: Vec<ParameterValue>,
159123
) -> Result<ReturnValue> {

src/hyperlight_host/src/sandbox/mod.rs

-46
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ pub mod uninitialized;
4545
/// initialized `Sandbox`es.
4646
pub(crate) mod uninitialized_evolve;
4747

48-
use std::collections::HashMap;
49-
5048
/// Re-export for `SandboxConfiguration` type
5149
pub use config::SandboxConfiguration;
5250
/// Re-export for the `MultiUseSandbox` type
@@ -60,7 +58,6 @@ pub use uninitialized::GuestBinary;
6058
pub use uninitialized::UninitializedSandbox;
6159

6260
use self::mem_mgr::MemMgrWrapper;
63-
use crate::func::HyperlightFunction;
6461
use crate::hypervisor::hypervisor_handler::HypervisorHandler;
6562
#[cfg(target_os = "windows")]
6663
use crate::hypervisor::windows_hypervisor_platform;
@@ -86,49 +83,6 @@ pub fn is_supported_platform() -> bool {
8683
/// Alias for the type of extra allowed syscalls.
8784
pub type ExtraAllowedSyscall = i64;
8885

89-
/// A `HashMap` to map function names to `HyperlightFunction`s and their extra allowed syscalls.
90-
///
91-
/// Note: you cannot add extra syscalls on Windows, but the field is still present to avoid a funky
92-
/// conditional compilation setup. This isn't a big deal as this struct isn't public facing.
93-
#[derive(Clone, Default)]
94-
pub(super) struct FunctionsMap(
95-
HashMap<String, (HyperlightFunction, Option<Vec<ExtraAllowedSyscall>>)>,
96-
);
97-
98-
impl FunctionsMap {
99-
/// Insert a new entry into the map
100-
pub(super) fn insert(
101-
&mut self,
102-
key: String,
103-
value: HyperlightFunction,
104-
extra_syscalls: Option<Vec<ExtraAllowedSyscall>>,
105-
) {
106-
self.0.insert(key, (value, extra_syscalls));
107-
}
108-
109-
/// Get the value associated with the given key, if it exists.
110-
pub(super) fn get(
111-
&self,
112-
key: &str,
113-
) -> Option<&(HyperlightFunction, Option<Vec<ExtraAllowedSyscall>>)> {
114-
self.0.get(key)
115-
}
116-
117-
/// Get the length of the map.
118-
fn len(&self) -> usize {
119-
self.0.len()
120-
}
121-
}
122-
123-
impl PartialEq for FunctionsMap {
124-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
125-
fn eq(&self, other: &Self) -> bool {
126-
self.len() == other.len() && self.0.keys().all(|k| other.0.contains_key(k))
127-
}
128-
}
129-
130-
impl Eq for FunctionsMap {}
131-
13286
/// Determine whether a suitable hypervisor is available to run
13387
/// this sandbox.
13488
///

0 commit comments

Comments
 (0)