hyperlight_guest/
host_functions.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 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    // get host function details
32    let host_function_details = get_host_function_details();
33
34    // check if there are any host functions
35    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    // check if function w/ given name exists
43    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() // if no parameters (and no mismatches), return empty vector
71    };
72
73    let function_call_parameter_types = function_call_fparameters
74        .iter()
75        .map(|p| p.into())
76        .collect::<Vec<ParameterType>>();
77
78    // Verify that the function call has the correct parameter types.
79    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}