-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fuzzing target to fuzz return type and parameters (#301)
* Adds fuzzing target to fuzz the ParameterValue and ReturnType of both guest and host function calls. Rename existing target to host_print. Move fuzz directory to root directory. Signed-off-by: Ludvig Liljenberg <lliljenberg@microsoft.com> * Fix leaky stack discovered by fuzzing host functions Signed-off-by: Ludvig Liljenberg <lliljenberg@microsoft.com> --------- Signed-off-by: Ludvig Liljenberg <lliljenberg@microsoft.com>
- Loading branch information
Showing
23 changed files
with
252 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[package] | ||
name = "hyperlight-fuzz" | ||
version = "0.0.0" | ||
publish = false | ||
edition = { workspace = true } | ||
|
||
[package.metadata] | ||
cargo-fuzz = true | ||
|
||
[dependencies] | ||
libfuzzer-sys = "0.4" | ||
hyperlight-testing = { workspace = true } | ||
hyperlight-host = { workspace = true, default-features = true, features = ["fuzzing"]} | ||
|
||
[[bin]] | ||
name = "host_print" | ||
path = "fuzz_targets/host_print.rs" | ||
test = false | ||
doc = false | ||
bench = false | ||
|
||
[[bin]] | ||
name = "guest_call" | ||
path = "fuzz_targets/guest_call.rs" | ||
test = false | ||
doc = false | ||
bench = false | ||
|
||
[[bin]] | ||
name = "host_call" | ||
path = "fuzz_targets/host_call.rs" | ||
test = false | ||
doc = false | ||
bench = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright 2024 The Hyperlight Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#![no_main] | ||
|
||
use std::sync::{Mutex, OnceLock}; | ||
|
||
use hyperlight_host::func::{ParameterValue, ReturnType}; | ||
use hyperlight_host::sandbox::uninitialized::GuestBinary; | ||
use hyperlight_host::sandbox::SandboxConfiguration; | ||
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox; | ||
use hyperlight_host::sandbox_state::transition::Noop; | ||
use hyperlight_host::{HyperlightError, MultiUseSandbox, UninitializedSandbox}; | ||
use hyperlight_testing::simple_guest_as_string; | ||
use libfuzzer_sys::fuzz_target; | ||
static SANDBOX: OnceLock<Mutex<MultiUseSandbox>> = OnceLock::new(); | ||
|
||
// This fuzz target tests all combinations of ReturnType and Parameters for `call_guest_function_by_name`. | ||
// For fuzzing efficiency, we create one Sandbox and reuse it for all fuzzing iterations. | ||
fuzz_target!( | ||
init: { | ||
let u_sbox = UninitializedSandbox::new( | ||
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")), | ||
None, | ||
None, | ||
None, | ||
) | ||
.unwrap(); | ||
|
||
let mu_sbox: MultiUseSandbox = u_sbox.evolve(Noop::default()).unwrap(); | ||
SANDBOX.set(Mutex::new(mu_sbox)).unwrap(); | ||
}, | ||
|
||
|data: (String, ReturnType, Vec<ParameterValue>)| { | ||
let (host_func_name, host_func_return, mut host_func_params) = data; | ||
let mut sandbox = SANDBOX.get().unwrap().lock().unwrap(); | ||
host_func_params.insert(0, ParameterValue::String(host_func_name)); | ||
match sandbox.call_guest_function_by_name("FuzzHostFunc", host_func_return, Some(host_func_params)) { | ||
Err(HyperlightError::GuestAborted(_, message)) if !message.contains("Host Function Not Found") => { | ||
// We don't allow GuestAborted errors, except for the "Host Function Not Found" case | ||
panic!("Guest Aborted: {}", message); | ||
} | ||
_ => {} | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#![no_main] | ||
|
||
use std::sync::{Mutex, OnceLock}; | ||
|
||
use hyperlight_host::func::{ParameterValue, ReturnType, ReturnValue}; | ||
use hyperlight_host::sandbox::uninitialized::GuestBinary; | ||
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox; | ||
use hyperlight_host::sandbox_state::transition::Noop; | ||
use hyperlight_host::{MultiUseSandbox, UninitializedSandbox}; | ||
use hyperlight_testing::simple_guest_as_string; | ||
use libfuzzer_sys::{fuzz_target, Corpus}; | ||
|
||
static SANDBOX: OnceLock<Mutex<MultiUseSandbox>> = OnceLock::new(); | ||
|
||
// This fuzz target is used to test the HostPrint host function. We generate | ||
// an arbitrary ParameterValue::String, which is passed to the guest, which passes | ||
// it without modification to the host function. | ||
// For fuzzing efficiency, we create one Sandbox and reuse it for all fuzzing iterations. | ||
fuzz_target!( | ||
init: { | ||
let u_sbox = UninitializedSandbox::new( | ||
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")), | ||
None, | ||
None, | ||
None, | ||
) | ||
.unwrap(); | ||
|
||
let mu_sbox: MultiUseSandbox = u_sbox.evolve(Noop::default()).unwrap(); | ||
SANDBOX.set(Mutex::new(mu_sbox)).unwrap(); | ||
}, | ||
|
||
|data: ParameterValue| -> Corpus { | ||
// only interested in String types | ||
if !matches!(data, ParameterValue::String(_)) { | ||
return Corpus::Reject; | ||
} | ||
|
||
let mut sandbox = SANDBOX.get().unwrap().lock().unwrap(); | ||
let res = sandbox.call_guest_function_by_name( | ||
"PrintOutput", | ||
ReturnType::Int, | ||
Some(vec![data.clone()]), | ||
); | ||
match res { | ||
Ok(ReturnValue::Int(len)) => assert!(len >= 0), | ||
_ => panic!("Unexpected return value: {:?}", res), | ||
} | ||
|
||
Corpus::Keep | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
[toolchain] | ||
channel = "1.81.0" | ||
# if you update this, don't forget to change the pinned version | ||
# of nightly we use in the fuzzing workflow. | ||
channel = "1.81.0" |
Oops, something went wrong.