Skip to content

Commit 77945b3

Browse files
committed
Change fuzzing to fuzz return type and parameters
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 63fa7c6 commit 77945b3

File tree

9 files changed

+32
-26
lines changed

9 files changed

+32
-26
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"Cargo.toml",
44
// guest crates for testing, not part of the workspace
55
"src/tests/rust_guests/simpleguest/Cargo.toml",
6-
"src/tests/rust_guests/callbackguest/Cargo.toml"
6+
"src/tests/rust_guests/callbackguest/Cargo.toml",
7+
"src/hyperlight_host/fuzz/Cargo.toml"
78
]
89
}

Cargo.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hyperlight_common/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ anyhow = { version = "1.0.96", default-features = false }
2020
log = "0.4.25"
2121
tracing = { version = "0.1.41", optional = true }
2222
strum = {version = "0.27", default-features = false, features = ["derive"]}
23+
arbitrary = {version = "1.4.1", optional = true, features = ["derive"]}
2324

2425
[features]
2526
default = ["tracing"]
27+
fuzzing = ["dep:arbitrary"]
2628

2729
[dev-dependencies]
2830
hyperlight-testing = { workspace = true }

src/hyperlight_common/src/flatbuffer_wrappers/function_types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::flatbuffers::hyperlight::generated::{
3232
};
3333

3434
/// Supported parameter types with values for function calling.
35+
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
3536
#[derive(Debug, Clone, PartialEq)]
3637
pub enum ParameterValue {
3738
/// i32
@@ -104,6 +105,7 @@ pub enum ReturnValue {
104105
}
105106

106107
/// Supported return types from function calling.
108+
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
107109
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
108110
#[repr(C)]
109111
pub enum ReturnType {

src/hyperlight_common/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
#![no_std]
17+
// We use Arbitrary during fuzzing, which requires std
18+
#![cfg_attr(not(feature = "fuzzing"), no_std)]
1819

1920
extern crate alloc;
2021

src/hyperlight_host/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ mshv3 = ["dep:mshv-bindings3", "dep:mshv-ioctls3"]
132132
inprocess = []
133133
# This enables easy debug in the guest
134134
gdb = ["dep:gdbstub", "dep:gdbstub_arch"]
135+
fuzzing = ["hyperlight-common/fuzzing"]
135136

136137
[[bench]]
137138
name = "benchmarks"

src/hyperlight_host/fuzz/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ cargo-fuzz = true
1010
[dependencies]
1111
libfuzzer-sys = "0.4"
1212
hyperlight-testing = { workspace = true }
13-
hyperlight-host = { workspace = true, default-features = true }
13+
hyperlight-host = { workspace = true, default-features = true, features = ["fuzzing"]}
1414

1515
[[bin]]
1616
name = "fuzz_target_1"

src/hyperlight_host/fuzz/README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,18 @@ This directory contains the fuzzing infrastructure for Hyperlight. We use `cargo
44

55
You can run the fuzzers with:
66
```sh
7-
cargo +nightly-2023-11-28-x86_64-unknown-linux-gnu fuzz run --release <fuzzer_name>
7+
just fuzz
88
```
99

10-
> Note: Because nightly toolchains are not stable, we pin the nightly version to `2023-11-28`. To install this toolchain, run:
11-
> ```sh
12-
> rustup toolchain install nightly-2023-11-28-x86_64-unknown-linux-gnu
13-
> ```
14-
1510
As per Microsoft's Offensive Research & Security Engineering (MORSE) team, all host exposed functions that receive or interact with guest data must be continuously fuzzed for, at least, 500 million fuzz test cases without any crashes. Because `cargo-fuzz` doesn't support setting a maximum number of iterations; instead, we use the `--max_total_time` flag to set a maximum time to run the fuzzer. We have a GitHub action (acting like a CRON job) that runs the fuzzers for 24 hours every week.
1611

17-
Currently, we only fuzz the `PrintOutput` function. We plan to add more fuzzers in the future.
12+
Currently, we only fuzz the parameters and return type to a hardcoded `PrintOutput` guest function. We plan to add more fuzzers in the future.
1813

1914
## On Failure
2015

2116
If you encounter a failure, you can re-run an entire seed (i.e., group of inputs) with:
2217
```sh
23-
cargo +nightly-2023-11-28-x86_64-unknown-linux-gnu fuzz run --release <fuzzer_name> -- -seed=<seed-number>
18+
cargo +nightly fuzz run <fuzzer_name> -- -seed=<seed-number>
2419
```
2520

2621
The seed number can be seed in a specific run, like:
@@ -29,5 +24,5 @@ The seed number can be seed in a specific run, like:
2924
Or, if repro-ing a failure from CI, you can download the artifact from the fuzzing run, and run it like:
3025

3126
```sh
32-
cargo +nightly-2023-11-28-x86_64-unknown-linux-gnu fuzz run --release -O <fuzzer_name> <fuzzer-input (e.g., fuzz/artifacts/fuzz_target_1/crash-93c522e64ee822034972ccf7026d3a8f20d5267c>
27+
cargo +nightly fuzz run -O <fuzzer_name> <fuzzer-input (e.g., fuzz/artifacts/fuzz_target_1/crash-93c522e64ee822034972ccf7026d3a8f20d5267c>
3328
```

src/hyperlight_host/fuzz/fuzz_targets/fuzz_target_1.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use hyperlight_host::{MultiUseSandbox, UninitializedSandbox};
2424
use hyperlight_testing::simple_guest_as_string;
2525
use libfuzzer_sys::fuzz_target;
2626

27-
fuzz_target!(|data: &[u8]| {
27+
fuzz_target!(|data: (ReturnType, Option<Vec<ParameterValue>>)| {
2828
let u_sbox = UninitializedSandbox::new(
2929
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")),
3030
None,
@@ -33,18 +33,7 @@ fuzz_target!(|data: &[u8]| {
3333
)
3434
.unwrap();
3535

36-
let mu_sbox: MultiUseSandbox = u_sbox.evolve(Noop::default()).unwrap();
36+
let mut mu_sbox: MultiUseSandbox = u_sbox.evolve(Noop::default()).unwrap();
3737

38-
let msg = String::from_utf8_lossy(data).to_string();
39-
let len = msg.len() as i32;
40-
let mut ctx = mu_sbox.new_call_context();
41-
let result = ctx
42-
.call(
43-
"PrintOutput",
44-
ReturnType::Int,
45-
Some(vec![ParameterValue::String(msg.clone())]),
46-
)
47-
.unwrap();
48-
49-
assert_eq!(result, ReturnValue::Int(len));
38+
let _ = mu_sbox.call_guest_function_by_name("PrintOutput", data.0, data.1);
5039
});

0 commit comments

Comments
 (0)