Skip to content

Commit d420230

Browse files
authored
Use native types when calling host functions (#494)
* Use native types when calling host functions Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com> * allow mprotect by default Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com> * address PR feedback Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com> --------- Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
1 parent f94e7a7 commit d420230

File tree

25 files changed

+310
-709
lines changed

25 files changed

+310
-709
lines changed

README.md

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

6867
assert!(result.is_ok());

fuzz/fuzz_targets/guest_call.rs

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

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

fuzz/fuzz_targets/host_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fuzz_target!(
4545
let (host_func_name, host_func_return, mut host_func_params) = data;
4646
let mut sandbox = SANDBOX.get().unwrap().lock().unwrap();
4747
host_func_params.insert(0, ParameterValue::String(host_func_name));
48-
match sandbox.call_guest_function_by_name("FuzzHostFunc", host_func_return, Some(host_func_params)) {
48+
match sandbox.call_type_erased_guest_function_by_name("FuzzHostFunc", host_func_return, host_func_params) {
4949
Err(HyperlightError::GuestAborted(_, message)) if !message.contains("Host Function Not Found") => {
5050
// We don't allow GuestAborted errors, except for the "Host Function Not Found" case
5151
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;
@@ -28,22 +27,14 @@ fuzz_target!(
2827
SANDBOX.set(Mutex::new(mu_sbox)).unwrap();
2928
},
3029

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

4839
Corpus::Keep
4940
});

src/hyperlight_host/benches/benchmarks.rs

Lines changed: 5 additions & 26 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;
@@ -43,11 +42,7 @@ fn guest_call_benchmark(c: &mut Criterion) {
4342

4443
b.iter(|| {
4544
call_ctx
46-
.call(
47-
"Echo",
48-
ReturnType::Int,
49-
Some(vec![ParameterValue::String("hello\n".to_string())]),
50-
)
45+
.call::<String>("Echo", "hello\n".to_string())
5146
.unwrap()
5247
});
5348
});
@@ -59,11 +54,7 @@ fn guest_call_benchmark(c: &mut Criterion) {
5954

6055
b.iter(|| {
6156
sandbox
62-
.call_guest_function_by_name(
63-
"Echo",
64-
ReturnType::Int,
65-
Some(vec![ParameterValue::String("hello\n".to_string())]),
66-
)
57+
.call_guest_function_by_name::<String>("Echo", "hello\n".to_string())
6758
.unwrap()
6859
});
6960
});
@@ -88,13 +79,9 @@ fn guest_call_benchmark(c: &mut Criterion) {
8879

8980
b.iter(|| {
9081
sandbox
91-
.call_guest_function_by_name(
82+
.call_guest_function_by_name::<()>(
9283
"LargeParameters",
93-
ReturnType::Void,
94-
Some(vec![
95-
ParameterValue::VecBytes(large_vec.clone()),
96-
ParameterValue::String(large_string.clone()),
97-
]),
84+
(large_vec.clone(), large_string.clone()),
9885
)
9986
.unwrap()
10087
});
@@ -114,15 +101,7 @@ fn guest_call_benchmark(c: &mut Criterion) {
114101
uninitialized_sandbox.evolve(Noop::default()).unwrap();
115102
let mut call_ctx = multiuse_sandbox.new_call_context();
116103

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

128107
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;
@@ -62,13 +61,12 @@ fn main() -> hyperlight_host::Result<()> {
6261

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

7371
Ok(())
7472
}

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};
@@ -42,13 +41,12 @@ fn main() -> hyperlight_host::Result<()> {
4241

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

5351
Ok(())
5452
}

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
};
@@ -95,10 +88,8 @@ fn main() -> Result<()> {
9588
for _ in 0..5 {
9689
let mut ctx = multiuse_sandbox.new_call_context();
9790

98-
let result = ctx.call("Spin", ReturnType::Void, None);
99-
assert!(result.is_err());
100-
let result = ctx.finish();
101-
multiuse_sandbox = result.unwrap();
91+
ctx.call::<()>("Spin", ()).unwrap_err();
92+
multiuse_sandbox = ctx.finish().unwrap();
10293
}
10394

10495
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
});
@@ -108,11 +101,8 @@ fn do_hyperlight_stuff() {
108101
for _ in 0..5 {
109102
let mut ctx = multiuse_sandbox.new_call_context();
110103

111-
let result = ctx.call("Spin", ReturnType::Void, None);
112-
assert!(result.is_err());
113-
let result = ctx.finish();
114-
assert!(result.is_ok());
115-
multiuse_sandbox = result.unwrap();
104+
ctx.call::<()>("Spin", ()).unwrap_err();
105+
multiuse_sandbox = ctx.finish().unwrap();
116106
}
117107

118108
for join_handle in join_handles {

0 commit comments

Comments
 (0)