Skip to content

Commit 8c67b94

Browse files
authored
Revert temporary fuzz fix (#1000)
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent 554ac77 commit 8c67b94

File tree

6 files changed

+48
-15
lines changed

6 files changed

+48
-15
lines changed

Cargo.lock

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

fuzz/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ cargo-fuzz = true
1111
libfuzzer-sys = "0.4"
1212
hyperlight-testing = { workspace = true }
1313
hyperlight-host = { workspace = true, default-features = true, features = ["fuzzing"]}
14+
hyperlight-common = {workspace = true}
1415

1516
[[bin]]
1617
name = "fuzz_host_print"

fuzz/fuzz_targets/host_call.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@ limitations under the License.
1818

1919
use std::sync::{Mutex, OnceLock};
2020

21+
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
2122
use hyperlight_host::func::{ParameterValue, ReturnType};
2223
use hyperlight_host::sandbox::SandboxConfiguration;
23-
use hyperlight_host::sandbox::snapshot::Snapshot;
2424
use hyperlight_host::sandbox::uninitialized::GuestBinary;
2525
use hyperlight_host::{HyperlightError, MultiUseSandbox, UninitializedSandbox};
2626
use hyperlight_testing::simple_guest_for_fuzzing_as_string;
2727
use libfuzzer_sys::fuzz_target;
2828

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();
3229
static SANDBOX: OnceLock<Mutex<MultiUseSandbox>> = OnceLock::new();
3330

3431
// This fuzz target tests all combinations of ReturnType and Parameters for `call_guest_function_by_name`.
@@ -44,27 +41,25 @@ fuzz_target!(
4441
)
4542
.unwrap();
4643

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

5348
|data: (String, ReturnType, Vec<ParameterValue>)| {
5449
let (host_func_name, host_func_return, mut host_func_params) = data;
5550
let mut sandbox = SANDBOX.get().unwrap().lock().unwrap();
56-
let snapshot = SNAPSHOT.get().unwrap().lock().unwrap();
57-
sandbox.restore(&snapshot).unwrap();
58-
59-
host_func_params.insert(0, ParameterValue::String(host_func_name));
51+
host_func_params.insert(0, ParameterValue::String(host_func_name.clone()));
6052
if let Err(e) = sandbox.call_type_erased_guest_function_by_name("FuzzHostFunc", host_func_return, host_func_params) {
6153
match e {
6254
// the following are expected errors and occur frequently since
6355
// we are randomly generating the function name and parameters
6456
// to call with.
6557
HyperlightError::HostFunctionNotFound(_) => {}
58+
HyperlightError::GuestError(ErrorCode::HostFunctionError, msg) if msg == format!("HostFunction {} was not found", host_func_name) => {}
6659
HyperlightError::UnexpectedNoOfArguments(_, _) => {},
60+
HyperlightError::GuestError(ErrorCode::HostFunctionError, msg) if msg.contains("The number of arguments to the function is wrong") => {}
6761
HyperlightError::ParameterValueConversionFailure(_, _) => {},
62+
HyperlightError::GuestError(ErrorCode::HostFunctionError, msg) if msg.contains("Failed To Convert Parameter Value") => {}
6863

6964
// any other error should be reported
7065
_ => panic!("Guest Aborted with Unexpected Error: {:?}", e),

src/hyperlight_guest/src/guest_handle/host_comm.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ impl GuestHandle {
8888
}
8989
}
9090

91+
pub fn get_host_return_raw(&self) -> Result<ReturnValue> {
92+
let inner = self
93+
.try_pop_shared_input_data_into::<FunctionCallResult>()
94+
.expect("Unable to deserialize a return value from host")
95+
.into_inner();
96+
97+
match inner {
98+
Ok(ret) => Ok(ret),
99+
Err(e) => Err(HyperlightGuestError {
100+
kind: e.code,
101+
message: e.message,
102+
}),
103+
}
104+
}
105+
91106
/// Call a host function without reading its return value from shared mem.
92107
/// This is used by both the Rust and C APIs to reduce code duplication.
93108
///

src/hyperlight_guest_bin/src/host_comm.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ pub fn call_host_function_without_returning_result(
5454
handle.call_host_function_without_returning_result(function_name, parameters, return_type)
5555
}
5656

57+
pub fn get_host_return_value_raw() -> Result<ReturnValue> {
58+
let handle = unsafe { GUEST_HANDLE };
59+
handle.get_host_return_raw()
60+
}
61+
5762
pub fn get_host_return_value<T: TryFrom<ReturnValue>>() -> Result<T> {
5863
let handle = unsafe { GUEST_HANDLE };
5964
handle.get_host_return_value::<T>()

src/tests/rust_guests/simpleguest/src/main.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use core::ptr::write_volatile;
3636

3737
use hyperlight_common::flatbuffer_wrappers::function_call::{FunctionCall, FunctionCallType};
3838
use hyperlight_common::flatbuffer_wrappers::function_types::{
39-
ParameterType, ParameterValue, ReturnType,
39+
ParameterType, ParameterValue, ReturnType, ReturnValue,
4040
};
4141
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
4242
use hyperlight_common::flatbuffer_wrappers::guest_log_level::LogLevel;
@@ -47,8 +47,8 @@ use hyperlight_guest::exit::{abort_with_code, abort_with_code_and_message};
4747
use hyperlight_guest_bin::guest_function::definition::GuestFunctionDefinition;
4848
use hyperlight_guest_bin::guest_function::register::register_function;
4949
use hyperlight_guest_bin::host_comm::{
50-
call_host_function, call_host_function_without_returning_result, print_output_with_host_print,
51-
read_n_bytes_from_user_memory,
50+
call_host_function, call_host_function_without_returning_result, get_host_return_value_raw,
51+
print_output_with_host_print, read_n_bytes_from_user_memory,
5252
};
5353
use hyperlight_guest_bin::memory::malloc;
5454
use hyperlight_guest_bin::{MIN_STACK_ADDRESS, guest_logger};
@@ -1665,7 +1665,23 @@ fn fuzz_host_function(func: FunctionCall) -> Result<Vec<u8>> {
16651665
func.expected_return_type,
16661666
)
16671667
.expect("failed to call host function");
1668-
Ok(get_flatbuffer_result(()))
1668+
1669+
let host_return = get_host_return_value_raw();
1670+
match host_return {
1671+
Ok(return_value) => match return_value {
1672+
ReturnValue::Int(i) => Ok(get_flatbuffer_result(i)),
1673+
ReturnValue::UInt(i) => Ok(get_flatbuffer_result(i)),
1674+
ReturnValue::Long(i) => Ok(get_flatbuffer_result(i)),
1675+
ReturnValue::ULong(i) => Ok(get_flatbuffer_result(i)),
1676+
ReturnValue::Float(i) => Ok(get_flatbuffer_result(i)),
1677+
ReturnValue::Double(i) => Ok(get_flatbuffer_result(i)),
1678+
ReturnValue::String(str) => Ok(get_flatbuffer_result(str.as_str())),
1679+
ReturnValue::Bool(bool) => Ok(get_flatbuffer_result(bool)),
1680+
ReturnValue::Void(()) => Ok(get_flatbuffer_result(())),
1681+
ReturnValue::VecBytes(byte) => Ok(get_flatbuffer_result(byte.as_slice())),
1682+
},
1683+
Err(e) => Err(e),
1684+
}
16691685
}
16701686

16711687
#[no_mangle]

0 commit comments

Comments
 (0)