Skip to content

Commit 8e15bf9

Browse files
committed
Convert mem::shared_mem module to use thiserror structured errors
It will be used from the hvf code, which needs to return a structured error itself. Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com>
1 parent ef6dc2d commit 8e15bf9

8 files changed

Lines changed: 307 additions & 232 deletions

File tree

src/hyperlight_host/src/error.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ pub enum HyperlightError {
4242
/// Anyhow error
4343
#[error("Anyhow Error was returned: {0}")]
4444
AnyhowError(#[from] anyhow::Error),
45-
/// Memory access out of bounds
46-
#[error("Offset: {0} out of bounds, Max is: {1}")]
47-
BoundsCheckFailed(u64, usize),
4845

4946
/// Checked Add Overflow
5047
#[error("Couldn't add offset to base address. Offset: {0}, Base Address: {1}")]
@@ -154,18 +151,6 @@ pub enum HyperlightError {
154151
#[error("Memory Access Violation at address {0:#x} of type {1}, but memory is marked as {2}")]
155152
MemoryAccessViolation(u64, MemoryRegionFlags, MemoryRegionFlags),
156153

157-
/// Memory Allocation Failed.
158-
#[error("Memory Allocation Failed with OS Error {0:?}.")]
159-
MemoryAllocationFailed(Option<i32>),
160-
161-
/// Memory Protection Failed
162-
#[error("Memory Protection Failed with OS Error {0:?}.")]
163-
MemoryProtectionFailed(Option<i32>),
164-
165-
/// Memory region size mismatch
166-
#[error("Memory region size mismatch: host size {0:?}, guest size {1:?} region {2:?}")]
167-
MemoryRegionSizeMismatch(usize, usize, String),
168-
169154
/// The memory request exceeds the maximum size allowed
170155
#[error("Memory requested {0} exceeds maximum size allowed {1}")]
171156
MemoryRequestTooBig(usize, usize),
@@ -179,14 +164,6 @@ pub enum HyperlightError {
179164
#[error("Metric Not Found {0:?}.")]
180165
MetricNotFound(&'static str),
181166

182-
/// mmap Failed.
183-
#[error("mmap failed with os error {0:?}")]
184-
MmapFailed(Option<i32>),
185-
186-
/// mprotect Failed.
187-
#[error("mprotect failed with os error {0:?}")]
188-
MprotectFailed(Option<i32>),
189-
190167
/// No Hypervisor was found for Sandbox.
191168
#[error("No Hypervisor was found for Sandbox")]
192169
NoHypervisorFound(),
@@ -240,6 +217,10 @@ pub enum HyperlightError {
240217
#[error("Failed To Convert Return Value {0:?} to {1:?}")]
241218
ReturnValueConversionFailure(ReturnValue, &'static str),
242219

220+
/// Error creating or operating on memory shared with the guest
221+
#[error("Failed to execute shared memory operation: {0}")]
222+
SharedMemory(#[from] crate::mem::shared_mem::SharedMemoryError),
223+
243224
/// Tried to restore a snapshot into a sandbox whose memory
244225
/// layout is not compatible with the snapshot's.
245226
#[error("Snapshot memory layout is not compatible with this sandbox")]
@@ -347,7 +328,6 @@ impl HyperlightError {
347328
| HyperlightError::PoisonedSandbox
348329
| HyperlightError::ExecutionAccessViolation(_)
349330
| HyperlightError::MemoryAccessViolation(_, _, _)
350-
| HyperlightError::MemoryRegionSizeMismatch(_, _, _)
351331
// HyperlightVmError::Restore is already handled manually in restore(), but we mark it
352332
// as poisoning here too for defense in depth.
353333
| HyperlightError::HyperlightVmError(HyperlightVmError::Restore(_)) => true,
@@ -365,7 +345,6 @@ impl HyperlightError {
365345

366346
// All other errors do not poison the sandbox.
367347
HyperlightError::AnyhowError(_)
368-
| HyperlightError::BoundsCheckFailed(_, _)
369348
| HyperlightError::CheckedAddOverflow(_, _)
370349
| HyperlightError::CStringConversionError(_)
371350
| HyperlightError::Error(_)
@@ -386,13 +365,9 @@ impl HyperlightError {
386365
| HyperlightError::InvalidFlatBuffer(_)
387366
| HyperlightError::JsonConversionFailure(_)
388367
| HyperlightError::LockAttemptFailed(_)
389-
| HyperlightError::MemoryAllocationFailed(_)
390-
| HyperlightError::MemoryProtectionFailed(_)
391368
| HyperlightError::MemoryRequestTooBig(_, _)
392369
| HyperlightError::MemoryRequestTooSmall(_, _)
393370
| HyperlightError::MetricNotFound(_)
394-
| HyperlightError::MmapFailed(_)
395-
| HyperlightError::MprotectFailed(_)
396371
| HyperlightError::NoHypervisorFound()
397372
| HyperlightError::NoMemorySnapshot
398373
| HyperlightError::ParameterValueConversionFailure(_, _)
@@ -409,7 +384,8 @@ impl HyperlightError {
409384
| HyperlightError::UnexpectedParameterValueType(_, _)
410385
| HyperlightError::UnexpectedReturnValueType(_, _)
411386
| HyperlightError::UTF8StringConversionFailure(_)
412-
| HyperlightError::VectorCapacityIncorrect(_, _, _) => false,
387+
| HyperlightError::VectorCapacityIncorrect(_, _, _)
388+
| HyperlightError::SharedMemory(_) => false,
413389

414390
#[cfg(target_os = "windows")]
415391
HyperlightError::CrossBeamReceiveError(_) => false,

src/hyperlight_host/src/hypervisor/gdb/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,12 @@ impl<'a> DebugMemoryView<'a> {
159159
.mem_mgr
160160
.shared_mem
161161
.copy_from_slice(data, resolved.offset)
162-
.map_err(|e| DebugMemoryAccessError::CopyFailed(Box::new(e))),
162+
.map_err(|e| DebugMemoryAccessError::CopyFailed(Box::new(e.into()))),
163163
BaseGpaRegion::Scratch(()) => self
164164
.mem_mgr
165165
.scratch_mem
166166
.copy_from_slice(data, resolved.offset)
167-
.map_err(|e| DebugMemoryAccessError::CopyFailed(Box::new(e))),
167+
.map_err(|e| DebugMemoryAccessError::CopyFailed(Box::new(e.into()))),
168168
_ => Err(DebugMemoryAccessError::WriteToReadOnly),
169169
}
170170
}

src/hyperlight_host/src/hypervisor/virtual_machine/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub(crate) enum VmExit {
178178
}
179179

180180
/// VM error
181-
#[derive(Debug, Clone, thiserror::Error)]
181+
#[derive(Debug, thiserror::Error)]
182182
pub enum VmError {
183183
#[error("Failed to create vm: {0}")]
184184
CreateVm(#[from] CreateVmError),
@@ -241,7 +241,7 @@ pub enum CreateVmError {
241241
}
242242

243243
/// RunVCPU error
244-
#[derive(Debug, Clone, thiserror::Error)]
244+
#[derive(Debug, thiserror::Error)]
245245
pub enum RunVcpuError {
246246
#[error("Failed to decode message type: {0}")]
247247
DecodeIOMessage(u32),
@@ -343,7 +343,7 @@ pub enum RegisterError {
343343
ConversionFailed(String),
344344
}
345345

346-
#[derive(Debug, Clone, thiserror::Error)]
346+
#[derive(Debug, thiserror::Error)]
347347
pub enum ResetVcpuError {
348348
#[error("Single-operation vcpu reset not supported on this hypervisor")]
349349
NotSupported,
@@ -356,7 +356,7 @@ pub enum ResetVcpuError {
356356
}
357357

358358
/// Map memory error
359-
#[derive(Debug, Clone, thiserror::Error)]
359+
#[derive(Debug, thiserror::Error)]
360360
pub enum MapMemoryError {
361361
#[cfg(target_os = "windows")]
362362
#[error("Address conversion failed: {0}")]
@@ -381,7 +381,7 @@ pub enum MapMemoryError {
381381
}
382382

383383
/// Unmap memory error
384-
#[derive(Debug, Clone, thiserror::Error)]
384+
#[derive(Debug, thiserror::Error)]
385385
pub enum UnmapMemoryError {
386386
#[error("Hypervisor error: {0}")]
387387
Hypervisor(HypervisorError),

src/hyperlight_host/src/mem/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub(crate) trait ReadableSharedMemory {
158158
#[cfg(readable_shared_mem)]
159159
impl ReadableSharedMemory for &HostSharedMemory {
160160
fn copy_to_slice(&self, slice: &mut [u8], offset: usize) -> Result<()> {
161-
HostSharedMemory::copy_to_slice(self, slice, offset)
161+
Ok(HostSharedMemory::copy_to_slice(self, slice, offset)?)
162162
}
163163
}
164164
/// Coherence workaround for the blanket impl below.

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,12 @@ impl SandboxMemoryManager<HostSharedMemory> {
390390
/// Reads a host function call from memory
391391
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
392392
pub(crate) fn get_host_function_call(&mut self) -> Result<FunctionCall> {
393-
self.scratch_mem.try_pop_buffer_into::<FunctionCall>(
394-
self.layout.get_output_data_buffer_scratch_host_offset(),
395-
self.layout.output_data_size(),
396-
)
393+
self.scratch_mem
394+
.try_pop_buffer_into::<FunctionCall>(
395+
self.layout.get_output_data_buffer_scratch_host_offset(),
396+
self.layout.output_data_size(),
397+
)
398+
.map_err(From::from)
397399
}
398400

399401
/// Writes a host function call result to memory
@@ -405,11 +407,13 @@ impl SandboxMemoryManager<HostSharedMemory> {
405407
let mut builder = FlatBufferBuilder::new();
406408
let data = res.encode(&mut builder);
407409

408-
self.scratch_mem.push_buffer(
409-
self.layout.get_input_data_buffer_scratch_host_offset(),
410-
self.layout.input_data_size(),
411-
data,
412-
)
410+
self.scratch_mem
411+
.push_buffer(
412+
self.layout.get_input_data_buffer_scratch_host_offset(),
413+
self.layout.input_data_size(),
414+
data,
415+
)
416+
.map_err(From::from)
413417
}
414418

415419
/// Writes a guest function call to memory
@@ -434,19 +438,23 @@ impl SandboxMemoryManager<HostSharedMemory> {
434438
/// A function call result can be either an error or a successful return value.
435439
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
436440
pub(crate) fn get_guest_function_call_result(&mut self) -> Result<FunctionCallResult> {
437-
self.scratch_mem.try_pop_buffer_into::<FunctionCallResult>(
438-
self.layout.get_output_data_buffer_scratch_host_offset(),
439-
self.layout.output_data_size(),
440-
)
441+
self.scratch_mem
442+
.try_pop_buffer_into::<FunctionCallResult>(
443+
self.layout.get_output_data_buffer_scratch_host_offset(),
444+
self.layout.output_data_size(),
445+
)
446+
.map_err(From::from)
441447
}
442448

443449
/// Read guest log data from the `SharedMemory` contained within `self`
444450
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
445451
pub(crate) fn read_guest_log_data(&mut self) -> Result<GuestLogData> {
446-
self.scratch_mem.try_pop_buffer_into::<GuestLogData>(
447-
self.layout.get_output_data_buffer_scratch_host_offset(),
448-
self.layout.output_data_size(),
449-
)
452+
self.scratch_mem
453+
.try_pop_buffer_into::<GuestLogData>(
454+
self.layout.get_output_data_buffer_scratch_host_offset(),
455+
self.layout.output_data_size(),
456+
)
457+
.map_err(From::from)
450458
}
451459

452460
pub(crate) fn clear_io_buffers(&mut self) {
@@ -527,7 +535,9 @@ impl SandboxMemoryManager<HostSharedMemory> {
527535
fn update_scratch_bookkeeping_item(&mut self, offset: u64, value: u64) -> Result<()> {
528536
let scratch_size = self.scratch_mem.mem_size();
529537
let base_offset = scratch_size - offset as usize;
530-
self.scratch_mem.write::<u64>(base_offset, value)
538+
self.scratch_mem
539+
.write::<u64>(base_offset, value)
540+
.map_err(From::from)
531541
}
532542

533543
fn update_scratch_bookkeeping(&mut self) -> Result<()> {

0 commit comments

Comments
 (0)