@@ -957,4 +957,67 @@ mod tests {
957957
958958 Ok ( ( ) )
959959 }
960+
961+ #[ test]
962+ #[ cfg( crashdump) ]
963+ fn test_sandbox_builder_crashdump ( ) -> Result < ( ) > {
964+ // Capture list of files in /tmp before the test
965+ let tmp_dir = Path :: new ( "/tmp" ) ;
966+ let before_files: std:: collections:: HashSet < _ > = std:: fs:: read_dir ( tmp_dir)
967+ . expect ( "Failed to read /tmp directory" )
968+ . map ( |e| e. unwrap ( ) . file_name ( ) )
969+ . collect ( ) ;
970+
971+ // Setup guest sandbox
972+ let sandbox_builder =
973+ SandboxBuilder :: new ( GuestBinary :: FilePath ( simple_guest_as_string ( ) ?) ) ?;
974+
975+ let mut uninitialized_sandbox = sandbox_builder. build ( ) ?;
976+
977+ // Register host function
978+ fn add ( a : i32 , b : i32 ) -> Result < i32 > {
979+ Ok ( a + b)
980+ }
981+ let host_function = Arc :: new ( Mutex :: new ( add) ) ;
982+ host_function. register ( & mut uninitialized_sandbox, "HostAdd" ) ?;
983+
984+ // Evolve to multi-use sandbox
985+ let mut multi_use_sandbox = uninitialized_sandbox. evolve ( Noop :: default ( ) ) ?;
986+
987+ // Call the guest function expected to crash
988+ let result = multi_use_sandbox. call_guest_function_by_name (
989+ "StackOverflow" ,
990+ ReturnType :: Void ,
991+ Some ( vec ! [ ParameterValue :: Int ( 512 ) ] ) ,
992+ ) ;
993+
994+ assert ! ( result. is_err( ) ) ;
995+
996+ // Capture list of files in /tmp after the crash
997+ let after_files: std:: collections:: HashSet < _ > = std:: fs:: read_dir ( tmp_dir)
998+ . expect ( "Failed to read /tmp directory" )
999+ . map ( |e| e. unwrap ( ) . file_name ( ) )
1000+ . collect ( ) ;
1001+
1002+ // Find the new files created
1003+ let new_files: Vec < _ > = after_files
1004+ . difference ( & before_files)
1005+ . filter ( |f| f. to_string_lossy ( ) . ends_with ( ".dmp" ) )
1006+ . collect ( ) ;
1007+
1008+ assert ! ( !new_files. is_empty( ) , "No crashdump file was created." ) ;
1009+
1010+ // Check the crashdump file(s)
1011+ for file_name in new_files {
1012+ let file_path = tmp_dir. join ( file_name) ;
1013+ let metadata = std:: fs:: metadata ( & file_path) ?;
1014+ assert ! (
1015+ metadata. len( ) > 0 ,
1016+ "Crashdump file is empty: {:?}" ,
1017+ file_path
1018+ ) ;
1019+ }
1020+
1021+ Ok ( ( ) )
1022+ }
9601023}
0 commit comments