Skip to content

Commit 5d4faf5

Browse files
committed
Remove and prevent use of panic in hyperlight-host
Signed-off-by: Simon Davies <[email protected]>
1 parent 93cf05e commit 5d4faf5

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

src/hyperlight_host/src/hypervisor/windows_hypervisor_platform.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,28 @@ impl VMPartition {
104104
};
105105

106106
regions.iter().try_for_each(|region| unsafe {
107+
let flags = region
108+
.flags
109+
.iter()
110+
.map(|flag| match flag {
111+
MemoryRegionFlags::NONE => Ok(WHvMapGpaRangeFlagNone),
112+
MemoryRegionFlags::READ => Ok(WHvMapGpaRangeFlagRead),
113+
MemoryRegionFlags::WRITE => Ok(WHvMapGpaRangeFlagWrite),
114+
MemoryRegionFlags::EXECUTE => Ok(WHvMapGpaRangeFlagExecute),
115+
MemoryRegionFlags::STACK_GUARD => Ok(WHvMapGpaRangeFlagNone),
116+
_ => Err(new_error!("Invalid Memory Region Flag")),
117+
})
118+
.collect::<Result<Vec<WHV_MAP_GPA_RANGE_FLAGS>>>()?
119+
.iter()
120+
.fold(WHvMapGpaRangeFlagNone, |acc, flag| acc | *flag); // collect using bitwise OR
121+
107122
let res = whvmapgparange2_func(
108123
self.0,
109124
process_handle,
110125
region.host_region.start as *const c_void,
111126
region.guest_region.start as u64,
112127
(region.guest_region.end - region.guest_region.start) as u64,
113-
region
114-
.flags
115-
.iter()
116-
.filter_map(|flag| match flag {
117-
MemoryRegionFlags::NONE => Some(WHvMapGpaRangeFlagNone),
118-
MemoryRegionFlags::READ => Some(WHvMapGpaRangeFlagRead),
119-
MemoryRegionFlags::WRITE => Some(WHvMapGpaRangeFlagWrite),
120-
MemoryRegionFlags::EXECUTE => Some(WHvMapGpaRangeFlagExecute),
121-
MemoryRegionFlags::STACK_GUARD => None,
122-
_ => panic!("Invalid flag"),
123-
})
124-
.fold(WHvMapGpaRangeFlagNone, |acc, flag| acc | flag), // collect using bitwise OR,
128+
flags,
125129
);
126130
if res.is_err() {
127131
return Err(new_error!("Call to WHvMapGpaRange2 failed"));

src/hyperlight_host/src/lib.rs

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

17+
#![cfg_attr(not(any(test, debug_assertions)), warn(clippy::panic))]
1718
#![cfg_attr(not(any(test, debug_assertions)), warn(clippy::expect_used))]
1819

1920
use std::sync::Once;

src/hyperlight_host/src/sandbox_state/sandbox.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ limitations under the License.
1515
*/
1616

1717
use std::fmt::Debug;
18-
use std::panic;
1918

2019
use tracing::{instrument, Span};
2120

2221
use super::transition::TransitionMetadata;
23-
use crate::Result;
22+
use crate::{new_error, Result};
2423

2524
/// The minimal functionality of a Hyperlight sandbox. Most of the types
2625
/// and operations within this crate require `Sandbox` implementations.
@@ -48,7 +47,9 @@ pub trait Sandbox: Sized + Debug {
4847
// The default implementation is provided so that types that implement Sandbox (e.g. JSSandbox) but do not need to implement this trait do not need to provide an implementation
4948
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
5049
fn check_stack_guard(&self) -> Result<bool> {
51-
panic!("check_stack_guard not implemented for this type");
50+
Err(new_error!(
51+
"check_stack_guard not implemented for this type"
52+
))
5253
}
5354
}
5455

0 commit comments

Comments
 (0)