@@ -23,7 +23,7 @@ use elfcore::{
2323 ReadProcessMemory , ThreadView , VaProtection , VaRegion ,
2424} ;
2525
26- use super :: Hypervisor ;
26+ use crate :: hypervisor :: hyperlight_vm :: HyperlightVm ;
2727use crate :: mem:: memory_region:: { MemoryRegion , MemoryRegionFlags } ;
2828use crate :: { Result , new_error} ;
2929
@@ -262,7 +262,7 @@ impl ReadProcessMemory for GuestMemReader {
262262///
263263/// # Returns
264264/// * `Result<()>`: Success or error
265- pub ( crate ) fn generate_crashdump ( hv : & dyn Hypervisor ) -> Result < ( ) > {
265+ pub ( crate ) fn generate_crashdump ( hv : & HyperlightVm ) -> Result < ( ) > {
266266 // Get crash context from hypervisor
267267 let ctx = hv
268268 . crashdump_context ( )
@@ -349,28 +349,23 @@ fn core_dump_file_path(dump_dir: Option<String>) -> String {
349349/// Returns:
350350/// * `Result<usize>`: The number of bytes written to the core dump file.
351351fn checked_core_dump (
352- ctx : Option < CrashDumpContext > ,
352+ ctx : CrashDumpContext ,
353353 get_writer : impl FnOnce ( ) -> Result < Box < dyn Write > > ,
354354) -> Result < usize > {
355- let mut nbytes = 0 ;
356- // If the HV returned a context it means we can create a core dump
357- // This is the case when the sandbox has been configured at runtime to allow core dumps
358- if let Some ( ctx) = ctx {
359- log:: info!( "Creating core dump file..." ) ;
360-
361- // Set up data sources for the core dump
362- let guest_view = GuestView :: new ( & ctx) ;
363- let memory_reader = GuestMemReader :: new ( & ctx) ;
364-
365- // Create and write core dump
366- let core_builder = CoreDumpBuilder :: from_source ( guest_view, memory_reader) ;
367-
368- let writer = get_writer ( ) ?;
369- // Write the core dump directly to the file
370- nbytes = core_builder
371- . write ( writer)
372- . map_err ( |e| new_error ! ( "Failed to write core dump: {:?}" , e) ) ?;
373- }
355+ log:: info!( "Creating core dump file..." ) ;
356+
357+ // Set up data sources for the core dump
358+ let guest_view = GuestView :: new ( & ctx) ;
359+ let memory_reader = GuestMemReader :: new ( & ctx) ;
360+
361+ // Create and write core dump
362+ let core_builder = CoreDumpBuilder :: from_source ( guest_view, memory_reader) ;
363+
364+ let writer = get_writer ( ) ?;
365+ // Write the core dump directly to the file
366+ let nbytes = core_builder
367+ . write ( writer)
368+ . map_err ( |e| new_error ! ( "Failed to write core dump: {:?}" , e) ) ?;
374369
375370 Ok ( nbytes)
376371}
@@ -424,17 +419,6 @@ mod test {
424419 assert ! ( path. starts_with( & temp_dir) ) ;
425420 }
426421
427- /// Test core is not created when the context is None
428- #[ test]
429- fn test_crashdump_not_created_when_context_is_none ( ) {
430- // Call the function with None context
431- let result = checked_core_dump ( None , || Ok ( Box :: new ( std:: io:: empty ( ) ) ) ) ;
432-
433- // Check if the result is ok and the number of bytes is 0
434- assert ! ( result. is_ok( ) ) ;
435- assert_eq ! ( result. unwrap( ) , 0 ) ;
436- }
437-
438422 /// Test the core dump creation with no regions fails
439423 #[ test]
440424 fn test_crashdump_write_fails_when_no_regions ( ) {
@@ -451,7 +435,7 @@ mod test {
451435 let get_writer = || Ok ( Box :: new ( std:: io:: empty ( ) ) as Box < dyn Write > ) ;
452436
453437 // Call the function
454- let result = checked_core_dump ( Some ( ctx) , get_writer) ;
438+ let result = checked_core_dump ( ctx, get_writer) ;
455439
456440 // Check if the result is an error
457441 // This should fail because there are no regions
@@ -482,7 +466,7 @@ mod test {
482466 let get_writer = || Ok ( Box :: new ( std:: io:: empty ( ) ) as Box < dyn Write > ) ;
483467
484468 // Call the function
485- let result = checked_core_dump ( Some ( ctx) , get_writer) ;
469+ let result = checked_core_dump ( ctx, get_writer) ;
486470
487471 // Check if the result is ok and the number of bytes is 0
488472 assert ! ( result. is_ok( ) ) ;
0 commit comments