hyperlight_guest/
guest_function_definition.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::String;
19use alloc::vec::Vec;
20
21use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterType, ReturnType};
22use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
23
24use crate::error::{HyperlightGuestError, Result};
25
26/// The definition of a function exposed from the guest to the host
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct GuestFunctionDefinition {
29    /// The function name
30    pub function_name: String,
31    /// The type of the parameter values for the host function call.
32    pub parameter_types: Vec<ParameterType>,
33    /// The type of the return value from the host function call
34    pub return_type: ReturnType,
35    /// The function pointer to the guest function
36    pub function_pointer: usize,
37}
38
39impl GuestFunctionDefinition {
40    /// Create a new `GuestFunctionDefinition`.
41    pub fn new(
42        function_name: String,
43        parameter_types: Vec<ParameterType>,
44        return_type: ReturnType,
45        function_pointer: usize,
46    ) -> Self {
47        Self {
48            function_name,
49            parameter_types,
50            return_type,
51            function_pointer,
52        }
53    }
54
55    /// Verify that `self` has same signature as the provided `parameter_types`.
56    pub fn verify_parameters(&self, parameter_types: &[ParameterType]) -> Result<()> {
57        // Verify that the function does not have more than `MAX_PARAMETERS` parameters.
58        const MAX_PARAMETERS: usize = 11;
59        if parameter_types.len() > MAX_PARAMETERS {
60            return Err(HyperlightGuestError::new(
61                ErrorCode::GuestError,
62                format!(
63                    "Function {} has too many parameters: {} (max allowed is {}).",
64                    self.function_name,
65                    parameter_types.len(),
66                    MAX_PARAMETERS
67                ),
68            ));
69        }
70
71        if self.parameter_types.len() != parameter_types.len() {
72            return Err(HyperlightGuestError::new(
73                ErrorCode::GuestFunctionIncorrecNoOfParameters,
74                format!(
75                    "Called function {} with {} parameters but it takes {}.",
76                    self.function_name,
77                    parameter_types.len(),
78                    self.parameter_types.len()
79                ),
80            ));
81        }
82
83        for (i, parameter_type) in self.parameter_types.iter().enumerate() {
84            if parameter_type != &parameter_types[i] {
85                return Err(HyperlightGuestError::new(
86                    ErrorCode::GuestFunctionParameterTypeMismatch,
87                    format!(
88                        "Expected parameter type {:?} for parameter index {} of function {} but got {:?}.",
89                        parameter_type, i, self.function_name, parameter_types[i]
90                    ),
91                ));
92            }
93        }
94
95        Ok(())
96    }
97}