Skip to content

Commit a80a756

Browse files
committed
[*] bring back tests + benchmarks + fuzzing + examples
- added convenience justfile rule to test like CI (w/ different feature combinations), - removed executable_heap feature (heap is always executable now). - fixed check_stack_guard functionality w/ stack cookie (currently broken in main). - fixed logging to get proper output_data_region address (host vs. guest address space when in process vs. in hypervisor). - modified print_output to return the message length. Signed-off-by: danbugs <[email protected]>
1 parent ca42784 commit a80a756

File tree

45 files changed

+1943
-3709
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1943
-3709
lines changed

Justfile

+16-5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ clean-rust:
6262

6363
# Note: most testing recipes take an optional "features" comma separated list argument. If provided, these will be passed to cargo as **THE ONLY FEATURES**, i.e. default features will be disabled.
6464

65+
# run full CI test matrix
66+
test-like-ci config=default-target hypervisor="kvm":
67+
@# with default features
68+
just test {{config}} {{ if hypervisor == "mshv3" {"mshv3"} else {""} }}
69+
70+
@# with only one driver enabled + seccomp + inprocess
71+
just test {{config}} inprocess,seccomp,{{ if hypervisor == "mshv" {"mshv2"} else if hypervisor == "mshv3" {"mshv3"} else {"kvm"} }}
72+
73+
@# make sure certain cargo features compile
74+
cargo check -p hyperlight-host --features crashdump
75+
cargo check -p hyperlight-host --features print_debug
76+
cargo check -p hyperlight-host --features gdb
77+
78+
@# without any driver (should fail to compile)
79+
just test-compilation-fail {{config}}
80+
6581
# runs all tests
6682
test target=default-target features="": (test-unit target features) (test-isolated target features) (test-integration "rust" target features) (test-integration "c" target features) (test-seccomp target features)
6783

@@ -83,11 +99,6 @@ test-isolated target=default-target features="":
8399

84100
# runs integration tests. Guest can either be "rust" or "c"
85101
test-integration guest target=default-target features="":
86-
@# run execute_on_heap test with feature "executable_heap" on and off
87-
{{if os() == "windows" { "$env:" } else { "" } }}GUEST="{{guest}}"{{if os() == "windows" { ";" } else { "" } }} cargo test --profile={{ if target == "debug" { "dev" } else { target } }} --test integration_test execute_on_heap {{ if features =="" {" --features executable_heap"} else {"--features executable_heap," + features} }} -- --ignored
88-
{{if os() == "windows" { "$env:" } else { "" } }}GUEST="{{guest}}"{{if os() == "windows" { ";" } else { "" } }} cargo test --profile={{ if target == "debug" { "dev" } else { target } }} --test integration_test execute_on_heap {{ if features =="" {""} else {"--features " + features} }} -- --ignored
89-
90-
@# run the rest of the integration tests
91102
{{if os() == "windows" { "$env:" } else { "" } }}GUEST="{{guest}}"{{if os() == "windows" { ";" } else { "" } }} cargo test -p hyperlight-host {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} --test '*'
92103

93104
# runs seccomp tests

fuzz/fuzz_targets/guest_call.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,28 @@ limitations under the License.
1919
use std::sync::{Mutex, OnceLock};
2020

2121
use hyperlight_host::func::{ParameterValue, ReturnType};
22-
use hyperlight_host::sandbox::uninitialized::GuestBinary;
22+
use hyperlight_host::sandbox::sandbox_builder::SandboxBuilder;
2323
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2424
use hyperlight_host::sandbox_state::transition::Noop;
25-
use hyperlight_host::{MultiUseSandbox, UninitializedSandbox};
25+
use hyperlight_host::{GuestBinary, MultiUseSandbox};
2626
use hyperlight_testing::simple_guest_for_fuzzing_as_string;
2727
use libfuzzer_sys::fuzz_target;
28+
2829
static SANDBOX: OnceLock<Mutex<MultiUseSandbox>> = OnceLock::new();
2930

3031
// This fuzz target tests all combinations of ReturnType and Parameters for `call_guest_function_by_name`.
3132
// For fuzzing efficiency, we create one Sandbox and reuse it for all fuzzing iterations.
3233
fuzz_target!(
3334
init: {
34-
35-
let u_sbox = UninitializedSandbox::new(
36-
GuestBinary::FilePath(simple_guest_for_fuzzing_as_string().expect("Guest Binary Missing")),
37-
None,
38-
None,
39-
None,
35+
let u_sbox = SandboxBuilder::new(
36+
GuestBinary::FilePath(
37+
simple_guest_for_fuzzing_as_string()
38+
.expect("Guest Binary Missing")
39+
)
4040
)
41-
.unwrap();
41+
.expect("Failed to create sandbox")
42+
.build()
43+
.expect("Failed to build sandbox");
4244

4345
let mu_sbox: MultiUseSandbox = u_sbox.evolve(Noop::default()).unwrap();
4446
SANDBOX.set(Mutex::new(mu_sbox)).unwrap();

fuzz/fuzz_targets/host_call.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ limitations under the License.
1919
use std::sync::{Mutex, OnceLock};
2020

2121
use hyperlight_host::func::{ParameterValue, ReturnType};
22-
use hyperlight_host::sandbox::uninitialized::GuestBinary;
23-
use hyperlight_host::sandbox::SandboxConfiguration;
2422
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2523
use hyperlight_host::sandbox_state::transition::Noop;
26-
use hyperlight_host::{HyperlightError, MultiUseSandbox, UninitializedSandbox};
24+
use hyperlight_host::{HyperlightError, MultiUseSandbox};
2725
use hyperlight_testing::simple_guest_for_fuzzing_as_string;
2826
use libfuzzer_sys::fuzz_target;
2927
static SANDBOX: OnceLock<Mutex<MultiUseSandbox>> = OnceLock::new();
@@ -32,13 +30,15 @@ static SANDBOX: OnceLock<Mutex<MultiUseSandbox>> = OnceLock::new();
3230
// For fuzzing efficiency, we create one Sandbox and reuse it for all fuzzing iterations.
3331
fuzz_target!(
3432
init: {
35-
let u_sbox = UninitializedSandbox::new(
36-
GuestBinary::FilePath(simple_guest_for_fuzzing_as_string().expect("Guest Binary Missing")),
37-
None,
38-
None,
39-
None,
33+
let u_sbox = SandboxBuilder::new(
34+
GuestBinary::FilePath(
35+
simple_guest_for_fuzzing_as_string()
36+
.expect("Guest Binary Missing")
37+
)
4038
)
41-
.unwrap();
39+
.expect("Failed to create sandbox")
40+
.build()
41+
.expect("Failed to build sandbox");
4242

4343
let mu_sbox: MultiUseSandbox = u_sbox.evolve(Noop::default()).unwrap();
4444
SANDBOX.set(Mutex::new(mu_sbox)).unwrap();

fuzz/fuzz_targets/host_print.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
use std::sync::{Mutex, OnceLock};
44

55
use hyperlight_host::func::{ParameterValue, ReturnType, ReturnValue};
6-
use hyperlight_host::sandbox::uninitialized::GuestBinary;
6+
use hyperlight_host::sandbox::sandbox_builder::SandboxBuilder;
77
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
88
use hyperlight_host::sandbox_state::transition::Noop;
9-
use hyperlight_host::{MultiUseSandbox, UninitializedSandbox};
9+
use hyperlight_host::{GuestBinary, MultiUseSandbox};
1010
use hyperlight_testing::simple_guest_for_fuzzing_as_string;
1111
use libfuzzer_sys::{fuzz_target, Corpus};
1212

@@ -18,13 +18,15 @@ static SANDBOX: OnceLock<Mutex<MultiUseSandbox>> = OnceLock::new();
1818
// For fuzzing efficiency, we create one Sandbox and reuse it for all fuzzing iterations.
1919
fuzz_target!(
2020
init: {
21-
let u_sbox = UninitializedSandbox::new(
22-
GuestBinary::FilePath(simple_guest_for_fuzzing_as_string().expect("Guest Binary Missing")),
23-
None,
24-
None,
25-
None,
21+
let u_sbox = SandboxBuilder::new(
22+
GuestBinary::FilePath(
23+
simple_guest_for_fuzzing_as_string()
24+
.expect("Guest Binary Missing")
25+
)
2626
)
27-
.unwrap();
27+
.expect("Failed to create sandbox")
28+
.build()
29+
.expect("Failed to build sandbox");
2830

2931
let mu_sbox: MultiUseSandbox = u_sbox.evolve(Noop::default()).unwrap();
3032
SANDBOX.set(Mutex::new(mu_sbox)).unwrap();

src/hyperlight_common/src/peb.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ pub struct MemoryRegion {
2727
#[repr(C)]
2828
#[derive(Clone, Default)]
2929
pub struct HyperlightPEB {
30+
/// The minimum stack address is the lowest address of the stack.
3031
pub min_stack_address: u64,
32+
3133
// - Host configured fields
3234
/// Hyperlight supports two primary modes:
3335
/// 1. Hypervisor mode
@@ -166,7 +168,7 @@ impl HyperlightPEB {
166168

167169
/// Sets the guest stack data region.
168170
/// - HyperlightPEB is always set with a default size for stack from the guest binary, there's an
169-
/// option to override this size with the `size_override` parameter.
171+
/// option to override this size with the `size_override` parameter.
170172
171173
pub fn set_guest_stack_data_region(&mut self, offset: u64, size_override: Option<u64>) {
172174
let size = size_override.unwrap_or_else(|| {
@@ -237,7 +239,7 @@ impl HyperlightPEB {
237239

238240
/// Sets the guest heap data region.
239241
/// - HyperlightPEB is always set with a default size for heap from the guest binary, there's an
240-
/// option to override this size with the `size_override` parameter.
242+
/// option to override this size with the `size_override` parameter.
241243
pub fn set_guest_heap_data_region(&mut self, offset: u64, size_override: Option<u64>) {
242244
let size = size_override.unwrap_or_else(|| {
243245
self.guest_heap_data

src/hyperlight_guest/src/entrypoint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub extern "win64" fn entrypoint(peb_address: u64, seed: u64, max_log_level: u64
8686

8787
// The guest sets the address to a "guest function dispatch" function, which is a function
8888
// that is called by the host to dispatch calls to guest functions.
89-
(*PEB).set_guest_function_dispatch_ptr(dispatch_function as u64);
89+
(*PEB).set_guest_function_dispatch_ptr(dispatch_function as usize as u64);
9090

9191
// Set up the guest heap
9292
HEAP_ALLOCATOR

src/hyperlight_guest/src/guest_error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) fn write_error(error_code: ErrorCode, message: Option<&str>) {
3030
let output_data: OutputDataSection = peb.get_output_data_region().into();
3131

3232
let guest_error = GuestError::new(
33-
error_code.clone(),
33+
error_code,
3434
message.map_or("".to_string(), |m| m.to_string()),
3535
);
3636

src/hyperlight_guest/src/guest_function_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn internal_dispatch_function() -> Result<()> {
9393
.expect("Function call deserialization failed");
9494

9595
let result_vec = call_guest_function(function_call).inspect_err(|e| {
96-
set_error(e.kind.clone(), e.message.as_str());
96+
set_error(e.kind, e.message.as_str());
9797
})?;
9898

9999
output_data_section

src/hyperlight_guest/src/logging.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ fn write_log_data(
4444
.try_into()
4545
.expect("Failed to convert GuestLogData to bytes");
4646

47-
let output_data_section: OutputDataSection = unsafe { (*PEB).clone() }
48-
.get_output_data_guest_region()
49-
.into();
47+
let output_data_section: OutputDataSection =
48+
unsafe { (*PEB).clone() }.get_output_data_region().into();
5049

5150
output_data_section
5251
.push_shared_output_data(bytes)

src/hyperlight_guest_capi/src/dispatch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use core::mem;
77
use hyperlight_common::flatbuffer_wrappers::function_call::FunctionCall;
88
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterType, ReturnType};
99
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
10+
use hyperlight_common::host_calling::call_host_function;
1011
use hyperlight_guest::error::{HyperlightGuestError, Result};
1112
use hyperlight_guest::guest_function_definition::GuestFunctionDefinition;
1213
use hyperlight_guest::guest_function_register::GuestFunctionRegister;
13-
use hyperlight_guest::host_function_call::call_host_function;
1414

1515
use crate::types::{FfiFunctionCall, FfiVec};
1616
static mut REGISTERED_C_GUEST_FUNCTIONS: GuestFunctionRegister = GuestFunctionRegister::new();

src/hyperlight_guest_capi/src/flatbuffer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use alloc::boxed::Box;
22
use core::ffi::{c_char, CStr};
33

44
use hyperlight_common::flatbuffer_wrappers::util::get_flatbuffer_result;
5-
use hyperlight_guest::host_function_call::get_host_return_value;
5+
use hyperlight_common::host_calling::get_host_return_value;
66

77
use crate::types::FfiVec;
88

src/hyperlight_host/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ built = { version = "0.7.7", features = ["chrono", "git2"] }
121121
default = ["kvm", "mshv2", "seccomp"]
122122
seccomp = ["dep:seccompiler"]
123123
function_call_metrics = []
124-
executable_heap = []
125124
# This feature enables printing of debug information to stdout in debug builds
126125
print_debug = []
127126
crashdump = ["dep:tempfile"] # Dumps the VM state to a file on unexpected errors or crashes. The path of the file will be printed on stdout and logged. This feature can only be used in debug builds.

src/hyperlight_host/benches/benchmarks.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ use std::sync::{Arc, Mutex};
1919
use criterion::{criterion_group, criterion_main, Criterion};
2020
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
2121
use hyperlight_host::func::HostFunction2;
22+
use hyperlight_host::sandbox::sandbox_builder::SandboxBuilder;
2223
use hyperlight_host::sandbox::{MultiUseSandbox, UninitializedSandbox};
2324
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2425
use hyperlight_host::sandbox_state::transition::Noop;
2526
use hyperlight_host::GuestBinary;
2627
use hyperlight_testing::simple_guest_as_string;
2728

2829
fn create_uninit_sandbox() -> UninitializedSandbox {
29-
let path = simple_guest_as_string().unwrap();
30-
UninitializedSandbox::new(GuestBinary::FilePath(path), None, None, None).unwrap()
30+
SandboxBuilder::new(GuestBinary::FilePath(
31+
simple_guest_as_string().expect("Guest Binary Missing"),
32+
))
33+
.expect("Failed to create sandbox")
34+
.build()
35+
.expect("Failed to build sandbox")
3136
}
3237

3338
fn create_multiuse_sandbox() -> MultiUseSandbox {

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

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
/*
2-
Copyright 2024 The Hyperlight Authors.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
16-
1+
// /*
2+
// Copyright 2024 The Hyperlight Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// */
1716
use hyperlight_host::func::{ParameterValue, ReturnType, ReturnValue};
17+
use hyperlight_host::sandbox::sandbox_builder::SandboxBuilder;
1818
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
1919
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2020
use hyperlight_host::sandbox_state::transition::Noop;
@@ -33,8 +33,7 @@ fn main() -> Result<()> {
3333
simple_guest_as_string().expect("Cannot find the guest binary at the expected location.");
3434

3535
// Create a new sandbox.
36-
let usandbox =
37-
UninitializedSandbox::new(GuestBinary::FilePath(simple_guest_path), None, None, None)?;
36+
let usandbox = SandboxBuilder::new(GuestBinary::FilePath(simple_guest_path))?.build()?;
3837

3938
let mut sbox = usandbox
4039
.evolve(Noop::<UninitializedSandbox, MultiUseSandbox>::default())

src/hyperlight_host/examples/func_ctx/main.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
/*
2-
Copyright 2024 The Hyperlight Authors.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
16-
1+
// /*
2+
// Copyright 2024 The Hyperlight Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// */
1716
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
1817
use hyperlight_host::func::call_ctx::MultiUseGuestCallContext;
19-
use hyperlight_host::sandbox::{MultiUseSandbox, UninitializedSandbox};
18+
use hyperlight_host::sandbox::sandbox_builder::SandboxBuilder;
19+
use hyperlight_host::sandbox::MultiUseSandbox;
2020
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2121
use hyperlight_host::sandbox_state::transition::Noop;
2222
use hyperlight_host::{new_error, GuestBinary, Result};
@@ -27,8 +27,10 @@ fn main() {
2727
// test guest binary
2828
let sbox1: MultiUseSandbox = {
2929
let path = simple_guest_as_string().unwrap();
30-
let u_sbox =
31-
UninitializedSandbox::new(GuestBinary::FilePath(path), None, None, None).unwrap();
30+
let u_sbox = SandboxBuilder::new(GuestBinary::FilePath(path.clone()))
31+
.expect("Failed to create sandbox")
32+
.build()
33+
.expect("Failed to build sandbox");
3234
u_sbox.evolve(Noop::default())
3335
}
3436
.unwrap();

0 commit comments

Comments
 (0)