hyperlight_guest/
host_functions.rs1use alloc::format;
18use alloc::string::ToString;
19use alloc::vec::Vec;
20use core::slice::from_raw_parts;
21
22use hyperlight_common::flatbuffer_wrappers::function_call::FunctionCall;
23use hyperlight_common::flatbuffer_wrappers::function_types::ParameterType;
24use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
25use hyperlight_common::flatbuffer_wrappers::host_function_details::HostFunctionDetails;
26
27use crate::error::{HyperlightGuestError, Result};
28use crate::P_PEB;
29
30pub(crate) fn validate_host_function_call(function_call: &FunctionCall) -> Result<()> {
31 let host_function_details = get_host_function_details();
33
34 if host_function_details.host_functions.is_none() {
36 return Err(HyperlightGuestError::new(
37 ErrorCode::GuestError,
38 "No host functions found".to_string(),
39 ));
40 }
41
42 let host_function = if let Some(host_function) =
44 host_function_details.find_by_function_name(&function_call.function_name)
45 {
46 host_function
47 } else {
48 return Err(HyperlightGuestError::new(
49 ErrorCode::GuestError,
50 format!(
51 "Host Function Not Found: {}",
52 function_call.function_name.clone()
53 ),
54 ));
55 };
56
57 let function_call_fparameters = if let Some(parameters) = function_call.parameters.clone() {
58 parameters
59 } else {
60 if host_function.parameter_types.is_some() {
61 return Err(HyperlightGuestError::new(
62 ErrorCode::GuestError,
63 format!(
64 "Incorrect parameter count for function: {}",
65 function_call.function_name.clone()
66 ),
67 ));
68 }
69
70 Vec::new() };
72
73 let function_call_parameter_types = function_call_fparameters
74 .iter()
75 .map(|p| p.into())
76 .collect::<Vec<ParameterType>>();
77
78 host_function
80 .verify_equal_parameter_types(&function_call_parameter_types)
81 .map_err(|_| {
82 HyperlightGuestError::new(
83 ErrorCode::GuestError,
84 format!(
85 "Incorrect parameter type for function: {}",
86 function_call.function_name.clone()
87 ),
88 )
89 })?;
90
91 Ok(())
92}
93
94pub fn get_host_function_details() -> HostFunctionDetails {
95 let peb_ptr = unsafe { P_PEB.unwrap() };
96
97 let host_function_details_buffer =
98 unsafe { (*peb_ptr).hostFunctionDefinitions.fbHostFunctionDetails } as *const u8;
99 let host_function_details_size =
100 unsafe { (*peb_ptr).hostFunctionDefinitions.fbHostFunctionDetailsSize };
101
102 let host_function_details_slice: &[u8] = unsafe {
103 from_raw_parts(
104 host_function_details_buffer,
105 host_function_details_size as usize,
106 )
107 };
108
109 host_function_details_slice
110 .try_into()
111 .expect("Failed to convert buffer to HostFunctionDetails")
112}