Skip to content

Commit 7ab2149

Browse files
committed
improve error handling and avoid panicking
- replace `expect` statements with logic to propagate error up the call chain - improve log messages and comments Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent 251cdb4 commit 7ab2149

File tree

3 files changed

+44
-47
lines changed

3 files changed

+44
-47
lines changed

src/hyperlight_host/src/hypervisor/gdb/x86_64_target.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ impl HyperlightSandboxTarget {
9090
}
9191

9292
/// Sends an event to the Hypervisor that tells it to disable debugging
93-
/// and continue executing until end
93+
/// and continue executing
9494
/// Note: The method waits for a confirmation message
9595
pub fn disable_debug(&mut self) -> Result<(), GdbTargetError> {
96-
log::info!("Disable debugging and continue until end");
96+
log::info!("Disable debugging and resume execution");
9797

9898
match self.send_command(DebugMsg::DisableDebug)? {
9999
DebugResponse::DisableDebug => Ok(()),

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ mod debug {
128128
/// The first 4 debug registers are used to set the addresses
129129
/// The 4th and 5th debug registers are obsolete and not used
130130
/// The 7th debug register is used to enable the breakpoints
131-
/// For more information see: https://en.wikipedia.org/wiki/X86_debug_register
131+
/// For more information see: DEBUG REGISTERS chapter in the architecture
132+
/// manual
132133
fn set_debug_config(&mut self, vcpu_fd: &VcpuFd, step: bool) -> Result<()> {
133134
let addrs = &self.hw_breakpoints;
134135

@@ -417,7 +418,7 @@ mod debug {
417418
let save_data = debug
418419
.sw_breakpoints
419420
.remove(&addr)
420-
.expect("Expected the hashmap to contain the address");
421+
.ok_or_else(|| new_error!("Expected the hashmap to contain the address"))?;
421422

422423
(true, Some(save_data))
423424
} else {
@@ -481,19 +482,12 @@ mod debug {
481482
dbg_mem_access_fn: Arc<Mutex<dyn DbgMemAccessHandlerCaller>>,
482483
) -> Result<DebugResponse> {
483484
match req {
484-
DebugMsg::AddHwBreakpoint(addr) => {
485-
let res = self
486-
.add_hw_breakpoint(addr)
487-
.expect("Add hw breakpoint error");
488-
Ok(DebugResponse::AddHwBreakpoint(res))
489-
}
490-
DebugMsg::AddSwBreakpoint(addr) => {
491-
let res = self
492-
.add_sw_breakpoint(addr, dbg_mem_access_fn)
493-
.expect("Add sw breakpoint error");
494-
495-
Ok(DebugResponse::AddSwBreakpoint(res))
496-
}
485+
DebugMsg::AddHwBreakpoint(addr) => self
486+
.add_hw_breakpoint(addr)
487+
.map(DebugResponse::AddHwBreakpoint),
488+
DebugMsg::AddSwBreakpoint(addr) => self
489+
.add_sw_breakpoint(addr, dbg_mem_access_fn)
490+
.map(DebugResponse::AddSwBreakpoint),
497491
DebugMsg::Continue => {
498492
self.set_single_step(false)?;
499493
Ok(DebugResponse::Continue)
@@ -520,21 +514,16 @@ mod debug {
520514
}
521515
DebugMsg::ReadRegisters => {
522516
let mut regs = X86_64Regs::default();
523-
self.read_regs(&mut regs).expect("Read Regs error");
524-
Ok(DebugResponse::ReadRegisters(regs))
525-
}
526-
DebugMsg::RemoveHwBreakpoint(addr) => {
527-
let res = self
528-
.remove_hw_breakpoint(addr)
529-
.expect("Remove hw breakpoint error");
530-
Ok(DebugResponse::RemoveHwBreakpoint(res))
531-
}
532-
DebugMsg::RemoveSwBreakpoint(addr) => {
533-
let res = self
534-
.remove_sw_breakpoint(addr, dbg_mem_access_fn)
535-
.expect("Remove sw breakpoint error");
536-
Ok(DebugResponse::RemoveSwBreakpoint(res))
517+
518+
self.read_regs(&mut regs)
519+
.map(|_| DebugResponse::ReadRegisters(regs))
537520
}
521+
DebugMsg::RemoveHwBreakpoint(addr) => self
522+
.remove_hw_breakpoint(addr)
523+
.map(DebugResponse::RemoveHwBreakpoint),
524+
DebugMsg::RemoveSwBreakpoint(addr) => self
525+
.remove_sw_breakpoint(addr, dbg_mem_access_fn)
526+
.map(DebugResponse::RemoveSwBreakpoint),
538527
DebugMsg::Step => {
539528
self.set_single_step(true)?;
540529
Ok(DebugResponse::Step)
@@ -544,10 +533,9 @@ mod debug {
544533

545534
Ok(DebugResponse::WriteAddr)
546535
}
547-
DebugMsg::WriteRegisters(regs) => {
548-
self.write_regs(&regs).expect("Write Regs error");
549-
Ok(DebugResponse::WriteRegisters)
550-
}
536+
DebugMsg::WriteRegisters(regs) => self
537+
.write_regs(&regs)
538+
.map(|_| DebugResponse::WriteRegisters),
551539
}
552540
}
553541

@@ -557,9 +545,12 @@ mod debug {
557545
.as_mut()
558546
.ok_or_else(|| new_error!("Debug is not enabled"))?;
559547

560-
gdb_conn
561-
.recv()
562-
.map_err(|e| new_error!("Got an error while waiting to receive a message: {:?}", e))
548+
gdb_conn.recv().map_err(|e| {
549+
new_error!(
550+
"Got an error while waiting to receive a message from the gdb thread: {:?}",
551+
e
552+
)
553+
})
563554
}
564555

565556
pub fn send_dbg_msg(&mut self, cmd: DebugResponse) -> Result<()> {
@@ -570,9 +561,12 @@ mod debug {
570561
.as_mut()
571562
.ok_or_else(|| new_error!("Debug is not enabled"))?;
572563

573-
gdb_conn
574-
.send(cmd)
575-
.map_err(|e| new_error!("Got an error while sending a response message {:?}", e))
564+
gdb_conn.send(cmd).map_err(|e| {
565+
new_error!(
566+
"Got an error while sending a response message to the gdb thread: {:?}",
567+
e
568+
)
569+
})
576570
}
577571
}
578572
}
@@ -854,11 +848,12 @@ impl Hypervisor for KVMDriver {
854848
}
855849
}
856850
#[cfg(gdb)]
857-
Ok(VcpuExit::Debug(_)) => {
858-
let reason = self.get_stop_reason()?;
859-
860-
HyperlightExit::Debug(reason)
861-
}
851+
Ok(VcpuExit::Debug(_)) => match self.get_stop_reason() {
852+
Ok(reason) => HyperlightExit::Debug(reason),
853+
Err(e) => {
854+
log_then_return!("Error getting stop reason: {:?}", e);
855+
}
856+
},
862857
Err(e) => match e.errno() {
863858
// In case of the gdb feature, the timeout is not enabled, this
864859
// exit is because of a signal sent from the gdb thread to the

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ impl VirtualCPU {
232232
match hv.run() {
233233
#[cfg(gdb)]
234234
Ok(HyperlightExit::Debug(stop_reason)) => {
235-
hv.handle_debug(dbg_mem_access_fn.clone(), stop_reason)?;
235+
if let Err(e) = hv.handle_debug(dbg_mem_access_fn.clone(), stop_reason) {
236+
log_then_return!(e);
237+
}
236238
}
237239

238240
Ok(HyperlightExit::Halt()) => {

0 commit comments

Comments
 (0)