|
| 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 | + |
| 17 | +#![no_main] |
| 18 | + |
| 19 | +use std::sync::{Mutex, OnceLock}; |
| 20 | + |
| 21 | +use hyperlight_host::func::{ParameterValue, ReturnType}; |
| 22 | +use hyperlight_host::sandbox::uninitialized::GuestBinary; |
| 23 | +use hyperlight_host::sandbox::SandboxConfiguration; |
| 24 | +use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox; |
| 25 | +use hyperlight_host::sandbox_state::transition::Noop; |
| 26 | +use hyperlight_host::{HyperlightError, MultiUseSandbox, UninitializedSandbox}; |
| 27 | +use hyperlight_testing::simple_guest_as_string; |
| 28 | +use libfuzzer_sys::fuzz_target; |
| 29 | +static SANDBOX: OnceLock<Mutex<MultiUseSandbox>> = OnceLock::new(); |
| 30 | + |
| 31 | +// This fuzz target tests all combinations of ReturnType and Parameters for `call_guest_function_by_name`. |
| 32 | +// For fuzzing efficiency, we create one Sandbox and reuse it for all fuzzing iterations. |
| 33 | +fuzz_target!( |
| 34 | + init: { |
| 35 | + let u_sbox = UninitializedSandbox::new( |
| 36 | + GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")), |
| 37 | + None, |
| 38 | + None, |
| 39 | + None, |
| 40 | + ) |
| 41 | + .unwrap(); |
| 42 | + |
| 43 | + let mu_sbox: MultiUseSandbox = u_sbox.evolve(Noop::default()).unwrap(); |
| 44 | + SANDBOX.set(Mutex::new(mu_sbox)).unwrap(); |
| 45 | + }, |
| 46 | + |
| 47 | + |data: (String, ReturnType, Vec<ParameterValue>)| { |
| 48 | + let (host_func_name, host_func_return, mut host_func_params) = data; |
| 49 | + let mut sandbox = SANDBOX.get().unwrap().lock().unwrap(); |
| 50 | + host_func_params.insert(0, ParameterValue::String(host_func_name)); |
| 51 | + match sandbox.call_guest_function_by_name("FuzzHostFunc", host_func_return, Some(host_func_params)) { |
| 52 | + Err(HyperlightError::GuestAborted(_, message)) if !message.contains("Host Function Not Found") => { |
| 53 | + // We don't allow GuestAborted errors, except for the "Host Function Not Found" case |
| 54 | + panic!("Guest Aborted: {}", message); |
| 55 | + } |
| 56 | + _ => {} |
| 57 | + } |
| 58 | + } |
| 59 | +); |
0 commit comments