Skip to content

Commit caa40e3

Browse files
committed
[*] remove uses of panic/unwrap/expect
Satisfying clippy in release by removing panic/unwrap/expect usages from the new APIs Signed-off-by: danbugs <[email protected]>
1 parent a80a756 commit caa40e3

File tree

23 files changed

+269
-261
lines changed

23 files changed

+269
-261
lines changed

fuzz/fuzz_targets/host_call.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ limitations under the License.
1919
use std::sync::{Mutex, OnceLock};
2020

2121
use hyperlight_host::func::{ParameterValue, ReturnType};
22+
use hyperlight_host::sandbox::sandbox_builder::SandboxBuilder;
2223
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2324
use hyperlight_host::sandbox_state::transition::Noop;
24-
use hyperlight_host::{HyperlightError, MultiUseSandbox};
25+
use hyperlight_host::{GuestBinary, HyperlightError, MultiUseSandbox};
2526
use hyperlight_testing::simple_guest_for_fuzzing_as_string;
2627
use libfuzzer_sys::fuzz_target;
28+
2729
static SANDBOX: OnceLock<Mutex<MultiUseSandbox>> = OnceLock::new();
2830

2931
// This fuzz target tests all combinations of ReturnType and Parameters for `call_guest_function_by_name`.

src/hyperlight_common/src/host_calling.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ use crate::PEB;
1414
/// This usually requires a host function to be called first using `call_host_function`.
1515
pub fn get_host_return_value<T: TryFrom<ReturnValue>>() -> Result<T> {
1616
let input_data_section: InputDataSection =
17-
unsafe { (*PEB).clone() }.get_input_data_region().into();
18-
let return_value = input_data_section
19-
.try_pop_shared_input_data_into::<ReturnValue>()
20-
.expect("Unable to deserialize a return value from host");
17+
unsafe { (*PEB).clone() }.get_input_data_region()?.into();
18+
let return_value = input_data_section.try_pop_shared_input_data_into::<ReturnValue>()?;
2119

2220
T::try_from(return_value).map_err(|_| {
2321
anyhow::anyhow!(
@@ -42,24 +40,22 @@ pub fn call_host_function(
4240
return_type,
4341
);
4442

45-
let host_function_call_buffer: Vec<u8> = host_function_call
46-
.try_into()
47-
.expect("Unable to serialize host function call");
43+
let host_function_call_buffer: Vec<u8> = host_function_call.try_into()?;
4844

4945
let output_data_section: OutputDataSection =
50-
unsafe { (*PEB).clone() }.get_output_data_region().into();
46+
unsafe { (*PEB).clone() }.get_output_data_region()?.into();
5147
output_data_section.push_shared_output_data(host_function_call_buffer)?;
5248

53-
outb(OutBAction::CallFunction as u16, 0);
54-
55-
Ok(())
49+
outb(OutBAction::CallFunction as u16, 0)
5650
}
5751

5852
/// Uses `hloutb` to issue multiple `DebugPrint` `OutBAction`s to print a message.
59-
pub fn print(message: &str) {
53+
pub fn print(message: &str) -> Result<()> {
6054
for byte in message.bytes() {
61-
outb(OutBAction::DebugPrint as u16, byte);
55+
outb(OutBAction::DebugPrint as u16, byte)?;
6256
}
57+
58+
Ok(())
6359
}
6460

6561
/// Exposes a C API to allow the guest to print a string, byte by byte
@@ -68,5 +64,6 @@ pub fn print(message: &str) {
6864
/// This function is not thread safe and assumes `outb` is safe to call directly.
6965
#[no_mangle]
7066
pub unsafe extern "C" fn _putchar(c: c_char) {
71-
outb(OutBAction::DebugPrint as u16, c as u8);
67+
#[allow(clippy::expect_used)] // allow `expect` over C API functions
68+
outb(OutBAction::DebugPrint as u16, c as u8).expect("Failed to print character");
7269
}

src/hyperlight_common/src/outb.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn hloutb(port: u16, val: u8) {
3737
}
3838
}
3939

40-
pub fn outb(port: u16, value: u8) {
40+
pub fn outb(port: u16, value: u8) -> Result<()> {
4141
unsafe {
4242
match RUNNING_MODE {
4343
RunMode::Hypervisor => {
@@ -53,12 +53,14 @@ pub fn outb(port: u16, value: u8) {
5353
} else if let Some(outb_func) = OUTB_HANDLER {
5454
outb_func(port, value);
5555
} else {
56-
panic!("Tried to call outb without hypervisor and without outb function ptrs");
56+
bail!("Tried to call outb without hypervisor and without outb function ptrs");
5757
}
5858
}
5959
_ => {
60-
panic!("Tried to call outb in invalid runmode");
60+
bail!("Tried to call outb in invalid runmode");
6161
}
6262
}
6363
}
64+
65+
Ok(())
6466
}

0 commit comments

Comments
 (0)