Skip to content

Commit d0ac424

Browse files
committed
Use native types when calling host functions
Signed-off-by: Jorge Prendes <[email protected]>
1 parent f3f135a commit d0ac424

File tree

24 files changed

+308
-713
lines changed

24 files changed

+308
-713
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,9 @@ fn main() -> hyperlight_host::Result<()> {
6060
let message = "Hello, World! I am executing inside of a VM :)\n".to_string();
6161
// in order to call a function it first must be defined in the guest and exposed so that
6262
// the host can call it
63-
let result = multi_use_sandbox.call_guest_function_by_name(
63+
let result: i32 = multi_use_sandbox.call_guest_function_by_name(
6464
"PrintOutput",
65-
ReturnType::Int,
66-
Some(vec![ParameterValue::String(message.clone())]),
65+
(message),
6766
);
6867

6968
assert!(result.is_ok());

fuzz/fuzz_targets/guest_call.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ fuzz_target!(
4343
SANDBOX.set(Mutex::new(mu_sbox)).unwrap();
4444
},
4545

46-
|data: (ReturnType, Option<Vec<ParameterValue>>)| {
46+
|data: (ReturnType, Vec<ParameterValue>)| {
4747
let mut sandbox = SANDBOX.get().unwrap().lock().unwrap();
48-
let _ = sandbox.call_guest_function_by_name("PrintOutput", data.0, data.1);
48+
let _ = sandbox.call_type_erased_guest_function_by_name("PrintOutput", data.0, data.1);
4949
}
5050
);

fuzz/fuzz_targets/host_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fuzz_target!(
4646
let (host_func_name, host_func_return, mut host_func_params) = data;
4747
let mut sandbox = SANDBOX.get().unwrap().lock().unwrap();
4848
host_func_params.insert(0, ParameterValue::String(host_func_name));
49-
match sandbox.call_guest_function_by_name("FuzzHostFunc", host_func_return, Some(host_func_params)) {
49+
match sandbox.call_type_erased_guest_function_by_name("FuzzHostFunc", host_func_return, host_func_params) {
5050
Err(HyperlightError::GuestAborted(_, message)) if !message.contains("Host Function Not Found") => {
5151
// We don't allow GuestAborted errors, except for the "Host Function Not Found" case
5252
panic!("Guest Aborted: {}", message);

fuzz/fuzz_targets/host_print.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use std::sync::{Mutex, OnceLock};
44

5-
use hyperlight_host::func::{ParameterValue, ReturnType, ReturnValue};
65
use hyperlight_host::sandbox::uninitialized::GuestBinary;
76
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
87
use hyperlight_host::sandbox_state::transition::Noop;
@@ -29,22 +28,14 @@ fuzz_target!(
2928
SANDBOX.set(Mutex::new(mu_sbox)).unwrap();
3029
},
3130

32-
|data: ParameterValue| -> Corpus {
33-
// only interested in String types
34-
if !matches!(data, ParameterValue::String(_)) {
35-
return Corpus::Reject;
36-
}
37-
31+
|data: String| -> Corpus {
3832
let mut sandbox = SANDBOX.get().unwrap().lock().unwrap();
39-
let res = sandbox.call_guest_function_by_name(
33+
let len: i32 = sandbox.call_guest_function_by_name::<i32>(
4034
"PrintOutput",
41-
ReturnType::Int,
42-
Some(vec![data.clone()]),
43-
);
44-
match res {
45-
Ok(ReturnValue::Int(len)) => assert!(len >= 0),
46-
_ => panic!("Unexpected return value: {:?}", res),
47-
}
35+
data,
36+
)
37+
.expect("Unexpected return value");
38+
assert!(len >= 0);
4839

4940
Corpus::Keep
5041
});

src/hyperlight_host/benches/benchmarks.rs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
use std::time::Duration;
1818

1919
use criterion::{criterion_group, criterion_main, Criterion};
20-
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
2120
use hyperlight_host::sandbox::{MultiUseSandbox, SandboxConfiguration, UninitializedSandbox};
2221
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2322
use hyperlight_host::sandbox_state::transition::Noop;
@@ -41,15 +40,7 @@ fn guest_call_benchmark(c: &mut Criterion) {
4140
group.bench_function("guest_call", |b| {
4241
let mut call_ctx = create_multiuse_sandbox().new_call_context();
4342

44-
b.iter(|| {
45-
call_ctx
46-
.call(
47-
"Echo",
48-
ReturnType::Int,
49-
Some(vec![ParameterValue::String("hello\n".to_string())]),
50-
)
51-
.unwrap()
52-
});
43+
b.iter(|| call_ctx.call::<i32>("Echo", "hello\n".to_string()).unwrap());
5344
});
5445

5546
// Benchmarks a single guest function call.
@@ -59,11 +50,7 @@ fn guest_call_benchmark(c: &mut Criterion) {
5950

6051
b.iter(|| {
6152
sandbox
62-
.call_guest_function_by_name(
63-
"Echo",
64-
ReturnType::Int,
65-
Some(vec![ParameterValue::String("hello\n".to_string())]),
66-
)
53+
.call_guest_function_by_name::<i32>("Echo", "hello\n".to_string())
6754
.unwrap()
6855
});
6956
});
@@ -89,13 +76,9 @@ fn guest_call_benchmark(c: &mut Criterion) {
8976

9077
b.iter(|| {
9178
sandbox
92-
.call_guest_function_by_name(
79+
.call_guest_function_by_name::<()>(
9380
"LargeParameters",
94-
ReturnType::Void,
95-
Some(vec![
96-
ParameterValue::VecBytes(large_vec.clone()),
97-
ParameterValue::String(large_string.clone()),
98-
]),
81+
(large_vec.clone(), large_string.clone()),
9982
)
10083
.unwrap()
10184
});
@@ -115,15 +98,7 @@ fn guest_call_benchmark(c: &mut Criterion) {
11598
uninitialized_sandbox.evolve(Noop::default()).unwrap();
11699
let mut call_ctx = multiuse_sandbox.new_call_context();
117100

118-
b.iter(|| {
119-
call_ctx
120-
.call(
121-
"Add",
122-
ReturnType::Int,
123-
Some(vec![ParameterValue::Int(1), ParameterValue::Int(41)]),
124-
)
125-
.unwrap()
126-
});
101+
b.iter(|| call_ctx.call::<i32>("Add", (1_i32, 41_i32)).unwrap());
127102
});
128103

129104
group.finish();

src/hyperlight_host/examples/func_ctx/main.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
1817
use hyperlight_host::func::call_ctx::MultiUseGuestCallContext;
1918
use hyperlight_host::sandbox::{MultiUseSandbox, UninitializedSandbox};
2019
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2120
use hyperlight_host::sandbox_state::transition::Noop;
22-
use hyperlight_host::{new_error, GuestBinary, Result};
21+
use hyperlight_host::{GuestBinary, Result};
2322
use hyperlight_testing::simple_guest_as_string;
2423

2524
fn main() {
@@ -47,29 +46,10 @@ fn main() {
4746
/// call `ctx.finish()` and return the resulting `MultiUseSandbox`. Return an `Err`
4847
/// if anything failed.
4948
fn do_calls(mut ctx: MultiUseGuestCallContext) -> Result<MultiUseSandbox> {
50-
{
51-
let res1: String = {
52-
let rv = ctx.call(
53-
"Echo",
54-
ReturnType::Int,
55-
Some(vec![ParameterValue::String("hello".to_string())]),
56-
)?;
57-
rv.try_into()
58-
}
59-
.map_err(|e| new_error!("failed to get Echo result: {}", e))?;
60-
println!("got Echo res: {res1}");
61-
}
62-
{
63-
let res2: i32 = {
64-
let rv = ctx.call(
65-
"CallMalloc",
66-
ReturnType::Int,
67-
Some(vec![ParameterValue::Int(200)]),
68-
)?;
69-
rv.try_into()
70-
}
71-
.map_err(|e| new_error!("failed to get CallMalloc result: {}", e))?;
72-
println!("got CallMalloc res: {res2}");
73-
}
49+
let res: String = ctx.call("Echo", "hello".to_string())?;
50+
println!("got Echo res: {res}");
51+
52+
let res: i32 = ctx.call("CallMalloc", 200_i32)?;
53+
println!("got CallMalloc res: {res}");
7454
ctx.finish()
7555
}

src/hyperlight_host/examples/guest-debugging/main.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ limitations under the License.
1616
#![allow(clippy::disallowed_macros)]
1717
use std::thread;
1818

19-
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
2019
#[cfg(gdb)]
2120
use hyperlight_host::sandbox::config::DebugInfo;
2221
use hyperlight_host::sandbox::SandboxConfiguration;
@@ -63,13 +62,12 @@ fn main() -> hyperlight_host::Result<()> {
6362

6463
// Call guest function
6564
let message = "Hello, World! I am executing inside of a VM :)\n".to_string();
66-
let result = multi_use_sandbox.call_guest_function_by_name(
67-
"PrintOutput", // function must be defined in the guest binary
68-
ReturnType::Int,
69-
Some(vec![ParameterValue::String(message.clone())]),
70-
);
71-
72-
assert!(result.is_ok());
65+
multi_use_sandbox
66+
.call_guest_function_by_name::<i32>(
67+
"PrintOutput", // function must be defined in the guest binary
68+
message.clone(),
69+
)
70+
.unwrap();
7371

7472
Ok(())
7573
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ limitations under the License.
1616
#![allow(clippy::disallowed_macros)]
1717
use std::thread;
1818

19-
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
2019
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2120
use hyperlight_host::sandbox_state::transition::Noop;
2221
use hyperlight_host::{MultiUseSandbox, UninitializedSandbox};
@@ -43,13 +42,12 @@ fn main() -> hyperlight_host::Result<()> {
4342

4443
// Call guest function
4544
let message = "Hello, World! I am executing inside of a VM :)\n".to_string();
46-
let result = multi_use_sandbox.call_guest_function_by_name(
47-
"PrintOutput", // function must be defined in the guest binary
48-
ReturnType::Int,
49-
Some(vec![ParameterValue::String(message.clone())]),
50-
);
51-
52-
assert!(result.is_ok());
45+
multi_use_sandbox
46+
.call_guest_function_by_name::<i32>(
47+
"PrintOutput", // function must be defined in the guest binary
48+
message,
49+
)
50+
.unwrap();
5351

5452
Ok(())
5553
}

src/hyperlight_host/examples/logging/main.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ limitations under the License.
1616
#![allow(clippy::disallowed_macros)]
1717
extern crate hyperlight_host;
1818

19-
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
2019
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
2120
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2221
use hyperlight_host::sandbox_state::transition::Noop;
@@ -53,12 +52,9 @@ fn main() -> Result<()> {
5352

5453
// Call a guest function 5 times to generate some log entries.
5554
for _ in 0..5 {
56-
let result = multiuse_sandbox.call_guest_function_by_name(
57-
"Echo",
58-
ReturnType::String,
59-
Some(vec![ParameterValue::String("a".to_string())]),
60-
);
61-
result.unwrap();
55+
multiuse_sandbox
56+
.call_guest_function_by_name::<String>("Echo", "a".to_string())
57+
.unwrap();
6258
}
6359

6460
// Define a message to send to the guest.
@@ -67,12 +63,9 @@ fn main() -> Result<()> {
6763

6864
// Call a guest function that calls the HostPrint host function 5 times to generate some log entries.
6965
for _ in 0..5 {
70-
let result = multiuse_sandbox.call_guest_function_by_name(
71-
"PrintOutput",
72-
ReturnType::Int,
73-
Some(vec![ParameterValue::String(msg.clone())]),
74-
);
75-
result.unwrap();
66+
multiuse_sandbox
67+
.call_guest_function_by_name::<i32>("PrintOutput", msg.clone())
68+
.unwrap();
7669
}
7770
Ok(())
7871
};
@@ -98,10 +91,8 @@ fn main() -> Result<()> {
9891
for _ in 0..5 {
9992
let mut ctx = multiuse_sandbox.new_call_context();
10093

101-
let result = ctx.call("Spin", ReturnType::Void, None);
102-
assert!(result.is_err());
103-
let result = ctx.finish();
104-
multiuse_sandbox = result.unwrap();
94+
ctx.call::<()>("Spin", ()).unwrap_err();
95+
multiuse_sandbox = ctx.finish().unwrap();
10596
}
10697

10798
Ok(())

src/hyperlight_host/examples/metrics/main.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
extern crate hyperlight_host;
1818
use std::thread::{spawn, JoinHandle};
1919

20-
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
2120
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
2221
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2322
use hyperlight_host::sandbox_state::transition::Noop;
@@ -65,12 +64,9 @@ fn do_hyperlight_stuff() {
6564

6665
// Call a guest function 5 times to generate some metrics.
6766
for _ in 0..5 {
68-
let result = multiuse_sandbox.call_guest_function_by_name(
69-
"Echo",
70-
ReturnType::String,
71-
Some(vec![ParameterValue::String("a".to_string())]),
72-
);
73-
assert!(result.is_ok());
67+
multiuse_sandbox
68+
.call_guest_function_by_name::<String>("Echo", "a".to_string())
69+
.unwrap();
7470
}
7571

7672
// Define a message to send to the guest.
@@ -79,12 +75,9 @@ fn do_hyperlight_stuff() {
7975

8076
// Call a guest function that calls the HostPrint host function 5 times to generate some metrics.
8177
for _ in 0..5 {
82-
let result = multiuse_sandbox.call_guest_function_by_name(
83-
"PrintOutput",
84-
ReturnType::Int,
85-
Some(vec![ParameterValue::String(msg.clone())]),
86-
);
87-
assert!(result.is_ok());
78+
multiuse_sandbox
79+
.call_guest_function_by_name::<i32>("PrintOutput", msg.clone())
80+
.unwrap();
8881
}
8982
Ok(())
9083
});
@@ -111,11 +104,8 @@ fn do_hyperlight_stuff() {
111104
for _ in 0..5 {
112105
let mut ctx = multiuse_sandbox.new_call_context();
113106

114-
let result = ctx.call("Spin", ReturnType::Void, None);
115-
assert!(result.is_err());
116-
let result = ctx.finish();
117-
assert!(result.is_ok());
118-
multiuse_sandbox = result.unwrap();
107+
ctx.call::<()>("Spin", ()).unwrap_err();
108+
multiuse_sandbox = ctx.finish().unwrap();
119109
}
120110

121111
for join_handle in join_handles {

src/hyperlight_host/examples/tracing-chrome/main.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616
#![allow(clippy::disallowed_macros)]
17-
use hyperlight_host::func::{ParameterValue, ReturnType, ReturnValue};
1817
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
1918
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2019
use hyperlight_host::sandbox_state::transition::Noop;
@@ -41,13 +40,9 @@ fn main() -> Result<()> {
4140

4241
// do the function call
4342
let current_time = std::time::Instant::now();
44-
let res = sbox.call_guest_function_by_name(
45-
"Echo",
46-
ReturnType::String,
47-
Some(vec![ParameterValue::String("Hello, World!".to_string())]),
48-
)?;
43+
let res: String = sbox.call_guest_function_by_name("Echo", "Hello, World!".to_string())?;
4944
let elapsed = current_time.elapsed();
5045
println!("Function call finished in {:?}.", elapsed);
51-
assert!(matches!(res, ReturnValue::String(s) if s == "Hello, World!"));
46+
assert_eq!(res, "Hello, World!");
5247
Ok(())
5348
}

0 commit comments

Comments
 (0)