Skip to content

Commit 1c34866

Browse files
committed
crashdump: add SandboxMetadata field that store relevant data about the sandbox
- only store the binary path for now Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent 8eee79a commit 1c34866

File tree

8 files changed

+122
-13
lines changed

8 files changed

+122
-13
lines changed

src/hyperlight_host/src/hypervisor/crashdump.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub(crate) struct CrashDumpContext<'a> {
2020
regs: [u64; 27],
2121
xsave: Vec<u8>,
2222
entry: u64,
23+
binary: Option<String>,
24+
filename: Option<String>,
2325
}
2426

2527
impl<'a> CrashDumpContext<'a> {
@@ -28,12 +30,16 @@ impl<'a> CrashDumpContext<'a> {
2830
regs: [u64; 27],
2931
xsave: Vec<u8>,
3032
entry: u64,
33+
binary: Option<String>,
34+
filename: Option<String>,
3135
) -> Self {
3236
Self {
3337
regions,
3438
regs,
3539
xsave,
3640
entry,
41+
binary,
42+
filename,
3743
}
3844
}
3945
}
@@ -64,6 +70,21 @@ impl GuestView {
6470
})
6571
.collect();
6672

73+
let filename = if let Some(mut binary) = ctx.filename.clone() {
74+
binary.push('\0');
75+
76+
binary
77+
} else {
78+
"\0".to_string()
79+
};
80+
81+
let cmd = if let Some(mut binary) = ctx.binary.clone() {
82+
binary.push('\0');
83+
binary
84+
} else {
85+
"\0".to_string()
86+
};
87+
6788
let mut components = vec![];
6889
if !ctx.xsave.is_empty() {
6990
components.push(ArchComponentState {
@@ -78,7 +99,7 @@ impl GuestView {
7899
tid: 1,
79100
uid: 0, // User ID
80101
gid: 0, // Group ID
81-
comm: "\0".to_string(),
102+
comm: filename,
82103
ppid: 0, // Parent PID
83104
pgrp: 0, // Process group ID
84105
nice: 0, // Nice value
@@ -91,7 +112,7 @@ impl GuestView {
91112
session: 0, // Session ID of the process
92113
sighold: 0, // Blocked signal
93114
sigpend: 0, // Pending signal
94-
cmd_line: "\0".to_string(),
115+
cmd_line: cmd,
95116

96117
arch_state: Box::new(ArchState {
97118
gpr_state: ctx.regs.to_vec(),

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ use mshv_bindings::{
4848
};
4949
use mshv_ioctls::{Mshv, VcpuFd, VmFd};
5050
use tracing::{instrument, Span};
51-
5251
#[cfg(crashdump)]
53-
use super::crashdump;
52+
use {super::crashdump, crate::sandbox::uninitialized::SandboxMetadata, std::path::Path};
53+
5454
use super::fpu::{FP_CONTROL_WORD_DEFAULT, FP_TAG_WORD_DEFAULT, MXCSR_DEFAULT};
5555
#[cfg(gdb)]
5656
use super::gdb::{DebugCommChannel, DebugMsg, DebugResponse, GuestDebug, MshvDebug};
@@ -296,6 +296,8 @@ pub(super) struct HypervLinuxDriver {
296296
debug: Option<MshvDebug>,
297297
#[cfg(gdb)]
298298
gdb_conn: Option<DebugCommChannel<DebugResponse, DebugMsg>>,
299+
#[cfg(crashdump)]
300+
metadata: SandboxMetadata,
299301
}
300302

301303
impl HypervLinuxDriver {
@@ -314,6 +316,7 @@ impl HypervLinuxDriver {
314316
rsp_ptr: GuestPtr,
315317
pml4_ptr: GuestPtr,
316318
#[cfg(gdb)] gdb_conn: Option<DebugCommChannel<DebugResponse, DebugMsg>>,
319+
#[cfg(crashdump)] metadata: SandboxMetadata,
317320
) -> Result<Self> {
318321
let mshv = Mshv::new()?;
319322
let pr = Default::default();
@@ -393,6 +396,8 @@ impl HypervLinuxDriver {
393396
debug,
394397
#[cfg(gdb)]
395398
gdb_conn,
399+
#[cfg(crashdump)]
400+
metadata,
396401
})
397402
}
398403

@@ -695,11 +700,20 @@ impl Hypervisor for HypervLinuxDriver {
695700
regs[25] = sregs.fs.selector as u64; // fs
696701
regs[26] = sregs.gs.selector as u64; // gs
697702

703+
let filename = self.metadata.binary_path.clone().map(|path| {
704+
Path::new(&path)
705+
.file_name()
706+
.map(|name| name.to_os_string().into_string().unwrap())
707+
.unwrap()
708+
});
709+
698710
Ok(crashdump::CrashDumpContext::new(
699711
&self.mem_regions,
700712
regs,
701713
xsave.buffer.to_vec(),
702714
self.entrypoint,
715+
self.metadata.binary_path.clone(),
716+
filename,
703717
))
704718
}
705719

@@ -820,6 +834,8 @@ mod tests {
820834
pml4_ptr,
821835
#[cfg(gdb)]
822836
None,
837+
#[cfg(crashdump)]
838+
SandboxMetadata { binary_path: None },
823839
)
824840
.unwrap();
825841
}

src/hyperlight_host/src/hypervisor/hyperv_windows.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ use windows::Win32::System::Hypervisor::{
2727
WHV_MEMORY_ACCESS_TYPE, WHV_PARTITION_HANDLE, WHV_REGISTER_VALUE, WHV_RUN_VP_EXIT_CONTEXT,
2828
WHV_RUN_VP_EXIT_REASON, WHV_X64_SEGMENT_REGISTER, WHV_X64_SEGMENT_REGISTER_0,
2929
};
30-
3130
#[cfg(crashdump)]
32-
use super::crashdump;
31+
use {super::crashdump, crate::sandbox::uninitialized::SandboxMetadata, std::path::Path};
32+
3333
use super::fpu::{FP_TAG_WORD_DEFAULT, MXCSR_DEFAULT};
3434
#[cfg(gdb)]
3535
use super::handlers::DbgMemAccessHandlerWrapper;
@@ -58,6 +58,8 @@ pub(crate) struct HypervWindowsDriver {
5858
entrypoint: u64,
5959
orig_rsp: GuestPtr,
6060
mem_regions: Vec<MemoryRegion>,
61+
#[cfg(crashdump)]
62+
metadata: SandboxMetadata,
6163
}
6264
/* This does not automatically impl Send/Sync because the host
6365
* address of the shared memory region is a raw pointer, which are
@@ -68,6 +70,7 @@ unsafe impl Send for HypervWindowsDriver {}
6870
unsafe impl Sync for HypervWindowsDriver {}
6971

7072
impl HypervWindowsDriver {
73+
#[allow(clippy::too_many_arguments)]
7174
#[instrument(err(Debug), skip_all, parent = Span::current(), level = "Trace")]
7275
pub(crate) fn new(
7376
mem_regions: Vec<MemoryRegion>,
@@ -77,6 +80,7 @@ impl HypervWindowsDriver {
7780
entrypoint: u64,
7881
rsp: u64,
7982
mmap_file_handle: HandleWrapper,
83+
#[cfg(crashdump)] metadata: SandboxMetadata,
8084
) -> Result<Self> {
8185
// create and setup hypervisor partition
8286
let mut partition = VMPartition::new(1)?;
@@ -104,6 +108,8 @@ impl HypervWindowsDriver {
104108
entrypoint,
105109
orig_rsp: GuestPtr::try_from(RawPtr::from(rsp))?,
106110
mem_regions,
111+
#[cfg(crashdump)]
112+
metadata,
107113
})
108114
}
109115

@@ -524,11 +530,20 @@ impl Hypervisor for HypervWindowsDriver {
524530
regs[25] = unsafe { sregs.fs.Segment.Selector } as u64; // fs
525531
regs[26] = unsafe { sregs.gs.Segment.Selector } as u64; // gs
526532

533+
let filename = self.metadata.binary_path.clone().map(|path| {
534+
Path::new(&path)
535+
.file_name()
536+
.map(|name| name.to_os_string().into_string().unwrap())
537+
.unwrap()
538+
});
539+
527540
Ok(crashdump::CrashDumpContext::new(
528541
&self.mem_regions,
529542
regs,
530543
xsave,
531544
self.entrypoint,
545+
self.metadata.binary_path.clone(),
546+
filename,
532547
))
533548
}
534549
}

src/hyperlight_host/src/hypervisor/hypervisor_handler.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ use crate::mem::shared_mem::{GuestSharedMemory, HostSharedMemory, SharedMemory};
5151
#[cfg(gdb)]
5252
use crate::sandbox::config::DebugInfo;
5353
use crate::sandbox::hypervisor::{get_available_hypervisor, HypervisorType};
54+
#[cfg(crashdump)]
55+
use crate::sandbox::uninitialized::SandboxMetadata;
5456
#[cfg(target_os = "linux")]
5557
use crate::signal_handlers::setup_signal_handlers;
5658
use crate::HyperlightError::{
@@ -238,6 +240,7 @@ impl HypervisorHandler {
238240
&mut self,
239241
sandbox_memory_manager: SandboxMemoryManager<GuestSharedMemory>,
240242
#[cfg(gdb)] debug_info: Option<DebugInfo>,
243+
#[cfg(crashdump)] metadata: SandboxMetadata,
241244
) -> Result<()> {
242245
let configuration = self.configuration.clone();
243246
#[cfg(target_os = "windows")]
@@ -304,6 +307,8 @@ impl HypervisorHandler {
304307
configuration.outb_handler.clone(),
305308
#[cfg(gdb)]
306309
&debug_info,
310+
#[cfg(crashdump)]
311+
&metadata,
307312
)?);
308313
}
309314
let hv = hv.as_mut().ok_or_else(|| new_error!("Hypervisor not set"))?;
@@ -835,6 +840,7 @@ fn set_up_hypervisor_partition(
835840
#[allow(unused_variables)] // parameter only used for in-process mode
836841
outb_handler: OutBHandlerWrapper,
837842
#[cfg(gdb)] debug_info: &Option<DebugInfo>,
843+
#[cfg(crashdump)] metadata: &SandboxMetadata,
838844
) -> Result<Box<dyn Hypervisor>> {
839845
let mem_size = u64::try_from(mgr.shared_mem.mem_size())?;
840846
let mut regions = mgr.layout.get_memory_regions(&mgr.shared_mem)?;
@@ -924,6 +930,8 @@ fn set_up_hypervisor_partition(
924930
pml4_ptr,
925931
#[cfg(gdb)]
926932
gdb_conn,
933+
#[cfg(crashdump)]
934+
metadata.clone(),
927935
)?;
928936
Ok(Box::new(hv))
929937
}
@@ -937,6 +945,8 @@ fn set_up_hypervisor_partition(
937945
rsp_ptr.absolute()?,
938946
#[cfg(gdb)]
939947
gdb_conn,
948+
#[cfg(crashdump)]
949+
metadata.clone(),
940950
)?;
941951
Ok(Box::new(hv))
942952
}
@@ -954,6 +964,8 @@ fn set_up_hypervisor_partition(
954964
entrypoint_ptr.absolute()?,
955965
rsp_ptr.absolute()?,
956966
HandleWrapper::from(mmap_file_handle),
967+
#[cfg(crashdump)]
968+
metadata.clone(),
957969
)?;
958970
Ok(Box::new(hv))
959971
}

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ use kvm_ioctls::Cap::UserMemory;
2424
use kvm_ioctls::{Kvm, VcpuExit, VcpuFd, VmFd};
2525
use log::LevelFilter;
2626
use tracing::{instrument, Span};
27-
2827
#[cfg(crashdump)]
29-
use super::crashdump;
28+
use {super::crashdump, crate::sandbox::uninitialized::SandboxMetadata, std::path::Path};
29+
3030
use super::fpu::{FP_CONTROL_WORD_DEFAULT, FP_TAG_WORD_DEFAULT, MXCSR_DEFAULT};
3131
#[cfg(gdb)]
3232
use super::gdb::{DebugCommChannel, DebugMsg, DebugResponse, GuestDebug, KvmDebug, VcpuStopReason};
@@ -288,6 +288,8 @@ pub(super) struct KVMDriver {
288288
debug: Option<KvmDebug>,
289289
#[cfg(gdb)]
290290
gdb_conn: Option<DebugCommChannel<DebugResponse, DebugMsg>>,
291+
#[cfg(crashdump)]
292+
metadata: SandboxMetadata,
291293
}
292294

293295
impl KVMDriver {
@@ -301,6 +303,7 @@ impl KVMDriver {
301303
entrypoint: u64,
302304
rsp: u64,
303305
#[cfg(gdb)] gdb_conn: Option<DebugCommChannel<DebugResponse, DebugMsg>>,
306+
#[cfg(crashdump)] metadata: SandboxMetadata,
304307
) -> Result<Self> {
305308
let kvm = Kvm::new()?;
306309

@@ -352,6 +355,8 @@ impl KVMDriver {
352355
debug,
353356
#[cfg(gdb)]
354357
gdb_conn,
358+
#[cfg(crashdump)]
359+
metadata,
355360
};
356361

357362
Ok(ret)
@@ -619,6 +624,13 @@ impl Hypervisor for KVMDriver {
619624
regs[25] = sregs.fs.selector as u64; // fs
620625
regs[26] = sregs.gs.selector as u64; // gs
621626

627+
let filename = self.metadata.binary_path.clone().map(|path| {
628+
Path::new(&path)
629+
.file_name()
630+
.map(|name| name.to_os_string().into_string().unwrap())
631+
.unwrap()
632+
});
633+
622634
Ok(crashdump::CrashDumpContext::new(
623635
&self.mem_regions,
624636
regs,
@@ -629,6 +641,8 @@ impl Hypervisor for KVMDriver {
629641
acc
630642
}),
631643
self.entrypoint,
644+
self.metadata.binary_path.clone(),
645+
filename,
632646
))
633647
}
634648

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ pub(crate) mod tests {
350350
};
351351
use crate::mem::ptr::RawPtr;
352352
use crate::sandbox::uninitialized::GuestBinary;
353+
#[cfg(crashdump)]
354+
use crate::sandbox::uninitialized::SandboxMetadata;
353355
use crate::sandbox::{SandboxConfiguration, UninitializedSandbox};
354356
use crate::{new_error, Result};
355357

@@ -411,6 +413,8 @@ pub(crate) mod tests {
411413
gshm,
412414
#[cfg(gdb)]
413415
None,
416+
#[cfg(crashdump)]
417+
SandboxMetadata { binary_path: None },
414418
)?;
415419

416420
hv_handler.execute_hypervisor_handler_action(HypervisorHandlerAction::Initialise)

0 commit comments

Comments
 (0)