hyperlight_guest_bin/
host_comm.rs1use alloc::string::ToString;
18use alloc::vec::Vec;
19
20use hyperlight_common::flatbuffer_wrappers::function_call::FunctionCall;
21use hyperlight_common::flatbuffer_wrappers::function_types::{
22 ParameterValue, ReturnType, ReturnValue,
23};
24use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
25use hyperlight_common::flatbuffer_wrappers::util::get_flatbuffer_result;
26use hyperlight_common::func::{ParameterTuple, SupportedReturnType};
27use hyperlight_guest::error::{HyperlightGuestError, Result};
28
29use crate::GUEST_HANDLE;
30
31pub fn call_host_function<T>(
32 function_name: &str,
33 parameters: Option<Vec<ParameterValue>>,
34 return_type: ReturnType,
35) -> Result<T>
36where
37 T: TryFrom<ReturnValue>,
38{
39 let handle = unsafe { GUEST_HANDLE };
40 handle.call_host_function::<T>(function_name, parameters, return_type)
41}
42
43pub fn call_host<T>(function_name: impl AsRef<str>, args: impl ParameterTuple) -> Result<T>
44where
45 T: SupportedReturnType + TryFrom<ReturnValue>,
46{
47 call_host_function::<T>(function_name.as_ref(), Some(args.into_value()), T::TYPE)
48}
49
50pub fn call_host_function_without_returning_result(
51 function_name: &str,
52 parameters: Option<Vec<ParameterValue>>,
53 return_type: ReturnType,
54) -> Result<()> {
55 let handle = unsafe { GUEST_HANDLE };
56 handle.call_host_function_without_returning_result(function_name, parameters, return_type)
57}
58
59pub fn get_host_return_value_raw() -> Result<ReturnValue> {
60 let handle = unsafe { GUEST_HANDLE };
61 handle.get_host_return_raw()
62}
63
64pub fn get_host_return_value<T: TryFrom<ReturnValue>>() -> Result<T> {
65 let handle = unsafe { GUEST_HANDLE };
66 handle.get_host_return_value::<T>()
67}
68
69pub fn read_n_bytes_from_user_memory(num: u64) -> Result<Vec<u8>> {
70 let handle = unsafe { GUEST_HANDLE };
71 handle.read_n_bytes_from_user_memory(num)
72}
73
74pub fn print_output_with_host_print(function_call: FunctionCall) -> Result<Vec<u8>> {
79 let handle = unsafe { GUEST_HANDLE };
80 if let ParameterValue::String(message) = function_call.parameters.unwrap().remove(0) {
81 let res = handle.call_host_function::<i32>(
82 "HostPrint",
83 Some(Vec::from(&[ParameterValue::String(message)])),
84 ReturnType::Int,
85 )?;
86
87 Ok(get_flatbuffer_result(res))
88 } else {
89 Err(HyperlightGuestError::new(
90 ErrorCode::GuestError,
91 "Wrong Parameters passed to print_output_with_host_print".to_string(),
92 ))
93 }
94}