Skip to content

Commit a835ef5

Browse files
authored
Fix fuzz_host_call target by (#840)
1. Setting sandbox snapshot to none in call_type_erased_guest_function_by_name 2. Restoring to an initial snapshot on each fuzzing iteration to avoid hitting a known memory leak. Signed-off-by: adamperlin <[email protected]>
1 parent 16fabd4 commit a835ef5

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

fuzz/fuzz_targets/host_call.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,57 @@ limitations under the License.
1919
use std::sync::{Mutex, OnceLock};
2020

2121
use hyperlight_host::func::{ParameterValue, ReturnType};
22+
use hyperlight_host::sandbox::SandboxConfiguration;
23+
use hyperlight_host::sandbox::snapshot::Snapshot;
2224
use hyperlight_host::sandbox::uninitialized::GuestBinary;
2325
use hyperlight_host::{HyperlightError, MultiUseSandbox, UninitializedSandbox};
2426
use hyperlight_testing::simple_guest_for_fuzzing_as_string;
2527
use libfuzzer_sys::fuzz_target;
28+
29+
// TODO: this SNAPSHOT is needed because of the memory leak in: https://github.com/hyperlight-dev/hyperlight/issues/826
30+
// This should be removed once the leak is fixed
31+
static SNAPSHOT: OnceLock<Mutex<Snapshot>> = OnceLock::new();
2632
static SANDBOX: OnceLock<Mutex<MultiUseSandbox>> = OnceLock::new();
2733

2834
// This fuzz target tests all combinations of ReturnType and Parameters for `call_guest_function_by_name`.
2935
// For fuzzing efficiency, we create one Sandbox and reuse it for all fuzzing iterations.
3036
fuzz_target!(
3137
init: {
38+
let mut cfg = SandboxConfiguration::default();
39+
cfg.set_output_data_size(64 * 1024); // 64 KB output buffer
40+
cfg.set_input_data_size(64 * 1024); // 64 KB input buffer
3241
let u_sbox = UninitializedSandbox::new(
3342
GuestBinary::FilePath(simple_guest_for_fuzzing_as_string().expect("Guest Binary Missing")),
34-
None
43+
Some(cfg)
3544
)
3645
.unwrap();
3746

38-
let mu_sbox: MultiUseSandbox = u_sbox.evolve().unwrap();
47+
let mut mu_sbox: MultiUseSandbox = u_sbox.evolve().unwrap();
48+
let snapshot = mu_sbox.snapshot().unwrap();
3949
SANDBOX.set(Mutex::new(mu_sbox)).unwrap();
50+
SNAPSHOT.set(Mutex::new(snapshot)).map_err(|_| "Snapshot already set").unwrap();
4051
},
4152

4253
|data: (String, ReturnType, Vec<ParameterValue>)| {
4354
let (host_func_name, host_func_return, mut host_func_params) = data;
4455
let mut sandbox = SANDBOX.get().unwrap().lock().unwrap();
56+
let snapshot = SNAPSHOT.get().unwrap().lock().unwrap();
57+
sandbox.restore(&snapshot).unwrap();
58+
4559
host_func_params.insert(0, ParameterValue::String(host_func_name));
4660
match sandbox.call_type_erased_guest_function_by_name("FuzzHostFunc", host_func_return, host_func_params) {
47-
Err(HyperlightError::GuestAborted(_, message)) if !message.contains("Host Function Not Found") => {
48-
// We don't allow GuestAborted errors, except for the "Host Function Not Found" case
49-
panic!("Guest Aborted: {}", message);
61+
Err(e) => {
62+
match e {
63+
// the following are expected errors and occur frequently since
64+
// we are randomly generating the function name and parameters
65+
// to call with.
66+
HyperlightError::HostFunctionNotFound(_) => {}
67+
HyperlightError::UnexpectedNoOfArguments(_, _) => {},
68+
HyperlightError::ParameterValueConversionFailure(_, _) => {},
69+
70+
// any other error should be reported
71+
_ => panic!("Guest Aborted with Unexpected Error: {:?}", e),
72+
}
5073
}
5174
_ => {}
5275
}

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ impl MultiUseSandbox {
381381
ret_type: ReturnType,
382382
args: Vec<ParameterValue>,
383383
) -> Result<ReturnValue> {
384+
// Reset snapshot since we are mutating the sandbox state
385+
self.snapshot = None;
384386
maybe_time_and_emit_guest_call(func_name, || {
385387
self.call_guest_function_by_name_no_reset(func_name, ret_type, args)
386388
})

0 commit comments

Comments
 (0)