Skip to content

Commit 6415498

Browse files
committed
[host,simpleguest] removed remaining references to HostPrint
- updated our hello-world example, - updated the README, - changed last remaining use of HostPrint to use print (wrapper for using the DebugPrint OutbAction), and - added an extra test in sandbox_builder. Signed-off-by: danbugs <[email protected]>
1 parent 38452e0 commit 6415498

File tree

5 files changed

+68
-64
lines changed

5 files changed

+68
-64
lines changed

README.md

+20-18
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ fn main() -> hyperlight_host::Result<()> {
4848
)?;
4949

5050
// Register a host function
51-
fn sleep_5_secs() -> hyperlight_host::Result<()> {
52-
thread::sleep(std::time::Duration::from_secs(5));
51+
fn host_add() -> hyperlight_host::Result<()> {
52+
5353
Ok(())
5454
}
5555

@@ -92,41 +92,43 @@ use hyperlight_common::flatbuffer_wrappers::function_types::{
9292
ParameterType, ParameterValue, ReturnType,
9393
};
9494
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
95-
use hyperlight_common::flatbuffer_wrappers::util::get_flatbuffer_result_from_int;
9695

9796
use hyperlight_guest::error::{HyperlightGuestError, Result};
9897
use hyperlight_guest::guest_function_definition::GuestFunctionDefinition;
9998
use hyperlight_guest::guest_function_register::register_function;
100-
use hyperlight_guest::host_function_call::{
101-
call_host_function, get_host_value_return_as_int,
102-
};
99+
use hyperlight_common::host_calling::{call_host_function, get_host_return_value};
103100

104-
fn print_output(function_call: &FunctionCall) -> Result<Vec<u8>> {
105-
if let ParameterValue::String(message) = function_call.parameters.clone().unwrap()[0].clone() {
101+
fn add(function_call: &FunctionCall) -> Result<Vec<u8>> {
102+
if let (ParameterValue::Int(a), ParameterValue::Int(b)) = (
103+
function_call.parameters.clone().unwrap()[0].clone(),
104+
function_call.parameters.clone().unwrap()[1].clone(),
105+
) {
106106
call_host_function(
107-
"HostPrint",
108-
Some(Vec::from(&[ParameterValue::String(message.to_string())])),
107+
"HostAdd",
108+
Some(Vec::from(&[ParameterValue::Int(a), ParameterValue::Int(b)])),
109109
ReturnType::Int,
110110
)?;
111-
let result = get_host_value_return_as_int()?;
112-
Ok(get_flatbuffer_result_from_int(result))
111+
112+
let res = get_host_return_value::<i32>()?;
113+
114+
Ok(get_flatbuffer_result(res))
113115
} else {
114116
Err(HyperlightGuestError::new(
115117
ErrorCode::GuestFunctionParameterTypeMismatch,
116-
"Invalid parameters passed to simple_print_output".to_string(),
118+
"Invalid parameters passed to add".to_string(),
117119
))
118120
}
119121
}
120122

121123
#[no_mangle]
122124
pub extern "C" fn hyperlight_main() {
123-
let print_output_def = GuestFunctionDefinition::new(
124-
"PrintOutput".to_string(),
125-
Vec::from(&[ParameterType::String]),
125+
let add_def = GuestFunctionDefinition::new(
126+
"Add".to_string(),
127+
Vec::from(&[ParameterType::Int, ParameterType::Int]),
126128
ReturnType::Int,
127-
print_output as i64,
129+
add as usize,
128130
);
129-
register_function(print_output_def);
131+
register_function(add_def);
130132
}
131133

132134
#[no_mangle]

src/hyperlight_host/examples/hello-world/main.rs

+18-23
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,43 @@ limitations under the License.
1515
*/
1616

1717
use std::sync::{Arc, Mutex};
18-
use std::thread;
1918

2019
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
21-
use hyperlight_host::func::HostFunction0;
20+
use hyperlight_host::func::HostFunction2;
21+
use hyperlight_host::sandbox::sandbox_builder::SandboxBuilder;
2222
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2323
use hyperlight_host::sandbox_state::transition::Noop;
24-
use hyperlight_host::{MultiUseSandbox, UninitializedSandbox};
24+
use hyperlight_host::{GuestBinary, MultiUseSandbox};
25+
use hyperlight_testing::simple_guest_as_string;
2526

2627
fn main() -> hyperlight_host::Result<()> {
2728
// Create an uninitialized sandbox with a guest binary
28-
let mut uninitialized_sandbox = UninitializedSandbox::new(
29-
hyperlight_host::GuestBinary::FilePath(
30-
hyperlight_testing::simple_guest_as_string().unwrap(),
31-
),
32-
None, // default configuration
33-
None, // default run options
34-
None, // default host print function
35-
)?;
29+
let sandbox_builder = SandboxBuilder::new(GuestBinary::FilePath(simple_guest_as_string()?))?;
30+
31+
let mut uninitialized_sandbox = sandbox_builder.build()?;
3632

37-
// Register a host functions
38-
fn sleep_5_secs() -> hyperlight_host::Result<()> {
39-
thread::sleep(std::time::Duration::from_secs(5));
40-
Ok(())
33+
// Register a host function
34+
fn add(a: i32, b: i32) -> hyperlight_host::Result<i32> {
35+
Ok(a + b)
4136
}
37+
let host_function = Arc::new(Mutex::new(add));
38+
host_function.register(&mut uninitialized_sandbox, "HostAdd")?;
4239

43-
let host_function = Arc::new(Mutex::new(sleep_5_secs));
40+
let host_function = Arc::new(Mutex::new(add));
4441

45-
host_function.register(&mut uninitialized_sandbox, "Sleep5Secs")?;
46-
// Note: This function is unused, it's just here for demonstration purposes
42+
host_function.register(&mut uninitialized_sandbox, "HostAdd")?;
4743

4844
// Initialize sandbox to be able to call host functions
4945
let mut multi_use_sandbox: MultiUseSandbox = uninitialized_sandbox.evolve(Noop::default())?;
5046

5147
// Call guest function
52-
let message = "Hello, World! I am executing inside of a VM :)\n".to_string();
5348
let result = multi_use_sandbox.call_guest_function_by_name(
54-
"PrintOutput", // function must be defined in the guest binary
49+
"Add", // function must be defined in the guest binary
5550
ReturnType::Int,
56-
Some(vec![ParameterValue::String(message.clone())]),
57-
);
51+
Some(vec![ParameterValue::Int(1), ParameterValue::Int(41)]),
52+
)?;
5853

59-
assert!(result.is_ok());
54+
println!("Guest function result: 1 + 41 = {:?}", result);
6055

6156
Ok(())
6257
}

src/hyperlight_host/src/sandbox/host_funcs.rs

-15
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,6 @@ impl HostFuncsWrapper {
7979
register_host_function_helper(self, mgr, hfd, func, Some(extra_allowed_syscalls))
8080
}
8181

82-
/// Assuming a host function called `"HostPrint"` exists, and takes a
83-
/// single string parameter, call it with the given `msg` parameter.
84-
///
85-
/// Return `Ok` if the function was found and was of the right signature,
86-
/// and `Err` otherwise.
87-
#[instrument(err(Debug), skip_all, parent = Span::current(), level = "Trace")]
88-
pub(super) fn host_print(&mut self, msg: String) -> Result<i32> {
89-
let res = call_host_func_impl(
90-
self.get_host_funcs(),
91-
"HostPrint",
92-
vec![ParameterValue::String(msg)],
93-
)?;
94-
res.try_into()
95-
.map_err(|_| HostFunctionNotFound("HostPrint".to_string()))
96-
}
9782
/// From the set of registered host functions, attempt to get the one
9883
/// named `name`. If it exists, call it with the given arguments list
9984
/// `args` and return its result.

src/hyperlight_host/src/sandbox/sandbox_builder.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,9 @@ mod tests {
882882
use hyperlight_common::flatbuffer_wrappers::function_types::{
883883
ParameterValue, ReturnType, ReturnValue,
884884
};
885-
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode::GuestFunctionParameterTypeMismatch;
885+
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode::{
886+
GuestFunctionNotFound, GuestFunctionParameterTypeMismatch,
887+
};
886888
use hyperlight_testing::simple_guest_as_string;
887889

888890
use super::*;
@@ -1051,4 +1053,30 @@ mod tests {
10511053

10521054
Ok(())
10531055
}
1056+
1057+
#[test]
1058+
fn test_sandbox_builder_try_calling_nonexistent_guest_function() -> Result<()> {
1059+
// Tests building an uninitialized sandbox w/ the sandbox builder
1060+
let sandbox_builder =
1061+
SandboxBuilder::new(GuestBinary::FilePath(simple_guest_as_string()?))?;
1062+
1063+
let uninitialized_sandbox = sandbox_builder.build()?;
1064+
1065+
// Tests evolving to a multi-use sandbox
1066+
let mut multi_use_sandbox = uninitialized_sandbox.evolve(Noop::default())?;
1067+
1068+
let result = multi_use_sandbox.call_guest_function_by_name(
1069+
"SomeNonExistentFunction",
1070+
ReturnType::Void,
1071+
None,
1072+
);
1073+
1074+
// Should get Error: GuestError(GuestFunctionNotFound, "SomeNonExistentFunction")
1075+
assert!(matches!(
1076+
result,
1077+
Err(HyperlightError::GuestError(GuestFunctionNotFound { .. }, _,))
1078+
));
1079+
1080+
Ok(())
1081+
}
10541082
}

src/tests/rust_guests/simpleguest/src/main.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1127,20 +1127,14 @@ pub fn guest_dispatch_function(function_call: FunctionCall) -> Result<Vec<u8>> {
11271127
1,
11281128
);
11291129

1130-
call_host_function(
1131-
"HostPrint",
1132-
Some(Vec::from(&[ParameterValue::String(message.to_string())])),
1133-
ReturnType::Int,
1134-
)?;
1135-
let result = get_host_return_value::<i32>()?;
1130+
print(message);
11361131
let function_name = function_call.function_name.clone();
11371132
let param_len = function_call.parameters.clone().unwrap_or_default().len();
11381133
let call_type = function_call.function_call_type().clone();
11391134

11401135
if function_name != "ThisIsNotARealFunctionButTheNameIsImportant"
11411136
|| param_len != 0
11421137
|| call_type != FunctionCallType::Guest
1143-
|| result != 100
11441138
{
11451139
return Err(HyperlightGuestError::new(
11461140
ErrorCode::GuestFunctionNotFound,

0 commit comments

Comments
 (0)