@@ -458,12 +458,12 @@ impl Vmm {
458458
459459 // Add the kernel command line to the boot parameters.
460460 bootparams. hdr . cmd_line_ptr = CMDLINE_START as u32 ;
461- bootparams. hdr . cmdline_size = self . kernel_cfg . cmdline . as_str ( ) . len ( ) as u32 + 1 ;
461+ bootparams. hdr . cmdline_size = self . kernel_cfg . cmdline . as_string ( ) . unwrap ( ) . len ( ) as u32 + 1 ;
462462
463463 // Load the kernel command line into guest memory.
464- let mut cmdline = Cmdline :: new ( 4096 ) ;
464+ let mut cmdline = Cmdline :: new ( 4096 ) . unwrap ( ) ;
465465 cmdline
466- . insert_str ( self . kernel_cfg . cmdline . as_str ( ) )
466+ . insert_str ( self . kernel_cfg . cmdline . as_string ( ) . unwrap ( ) )
467467 . map_err ( Error :: Cmdline ) ?;
468468
469469 load_cmdline (
@@ -724,7 +724,7 @@ impl Vmm {
724724 let cmdline = & self . kernel_cfg . cmdline ;
725725 let fdt = self
726726 . fdt_builder
727- . with_cmdline ( cmdline. as_str ( ) . to_string ( ) )
727+ . with_cmdline ( cmdline. as_string ( ) . unwrap ( ) )
728728 . with_num_vcpus ( self . num_vcpus . try_into ( ) . unwrap ( ) )
729729 . with_mem_size ( mem_size)
730730 . create_fdt ( )
@@ -750,6 +750,7 @@ mod tests {
750750 #[ cfg( target_arch = "x86_64" ) ]
751751 use std:: path:: Path ;
752752 use std:: path:: PathBuf ;
753+ use std:: fs:: write;
753754 #[ cfg( target_arch = "x86_64" ) ]
754755 use vm_memory:: {
755756 bytes:: ByteValued ,
@@ -956,20 +957,38 @@ mod tests {
956957 matches!( vmm. load_kernel( ) . unwrap_err( ) , Error :: IO ( e) if e. kind( ) == ErrorKind :: NotFound )
957958 ) ;
958959
959- // Test case: kernel image is invalid.
960+ // Test case: kernel image is invalid. This test has two flavors. In the first
961+ // we try to load an empty file, in the second a file which has all zeros.
960962 let mut vmm_config = default_vmm_config ( ) ;
961963 let temp_file = TempFile :: new ( ) . unwrap ( ) ;
962964 vmm_config. kernel_config . path = PathBuf :: from ( temp_file. as_path ( ) ) ;
963965 let mut vmm = mock_vmm ( vmm_config) ;
964966
965967 let err = vmm. load_kernel ( ) . unwrap_err ( ) ;
968+ #[ cfg( target_arch = "x86_64" ) ]
969+ assert ! ( matches!(
970+ err,
971+ Error :: KernelLoad ( loader:: Error :: Elf ( loader:: elf:: Error :: ReadElfHeader ) )
972+ ) ) ;
973+ #[ cfg( target_arch = "aarch64" ) ]
974+ assert ! ( matches!(
975+ err,
976+ Error :: KernelLoad ( loader:: Error :: Pe ( loader:: pe:: Error :: ReadImageHeader ) )
977+ ) ) ;
978+
979+ let temp_file_path = PathBuf :: from ( temp_file. as_path ( ) ) ;
980+ let buffer: Vec < u8 > = vec ! [ 0_u8 ; 1024 ] ;
981+ write ( temp_file_path, buffer) . unwrap ( ) ;
982+ let err = vmm. load_kernel ( ) . unwrap_err ( ) ;
983+
966984 #[ cfg( target_arch = "x86_64" ) ]
967985 assert ! ( matches!(
968986 err,
969987 Error :: KernelLoad ( loader:: Error :: Bzimage (
970988 loader:: bzimage:: Error :: InvalidBzImage
971989 ) )
972990 ) ) ;
991+
973992 #[ cfg( target_arch = "aarch64" ) ]
974993 assert ! ( matches!(
975994 err,
@@ -1011,15 +1030,16 @@ mod tests {
10111030 let mut vmm_config = default_vmm_config ( ) ;
10121031 vmm_config. kernel_config . path = default_elf_path ( ) ;
10131032 let mut vmm = mock_vmm ( vmm_config) ;
1014- assert_eq ! ( vmm. kernel_cfg. cmdline. as_str ( ) , DEFAULT_KERNEL_CMDLINE ) ;
1033+ assert_eq ! ( vmm. kernel_cfg. cmdline. as_string ( ) . unwrap ( ) , DEFAULT_KERNEL_CMDLINE ) ;
10151034 vmm. add_serial_console ( ) . unwrap ( ) ;
10161035 #[ cfg( target_arch = "x86_64" ) ]
1017- assert ! ( vmm. kernel_cfg. cmdline. as_str ( ) . contains( "console=ttyS0" ) ) ;
1036+ assert ! ( vmm. kernel_cfg. cmdline. as_string ( ) . unwrap ( ) . contains( "console=ttyS0" ) ) ;
10181037 #[ cfg( target_arch = "aarch64" ) ]
10191038 assert ! ( vmm
10201039 . kernel_cfg
10211040 . cmdline
1022- . as_str( )
1041+ . as_string( )
1042+ . unwrap( )
10231043 . contains( "earlycon=uart,mmio" ) ) ;
10241044 }
10251045
@@ -1118,7 +1138,7 @@ mod tests {
11181138 assert_eq ! ( vmm. block_devices. len( ) , 1 ) ;
11191139 #[ cfg( target_arch = "aarch64" ) ]
11201140 assert_eq ! ( vmm. fdt_builder. virtio_device_len( ) , 1 ) ;
1121- assert ! ( vmm. kernel_cfg. cmdline. as_str ( ) . contains( "virtio" ) ) ;
1141+ assert ! ( vmm. kernel_cfg. cmdline. as_string ( ) . unwrap ( ) . contains( "virtio" ) ) ;
11221142
11231143 let invalid_block_config = BlockConfig {
11241144 // Let's create the tempfile directly here so that it gets out of scope immediately
@@ -1151,7 +1171,7 @@ mod tests {
11511171 assert_eq ! ( vmm. net_devices. len( ) , 1 ) ;
11521172 #[ cfg( target_arch = "aarch64" ) ]
11531173 assert_eq ! ( vmm. fdt_builder. virtio_device_len( ) , 1 ) ;
1154- assert ! ( vmm. kernel_cfg. cmdline. as_str ( ) . contains( "virtio" ) ) ;
1174+ assert ! ( vmm. kernel_cfg. cmdline. as_string ( ) . unwrap ( ) . contains( "virtio" ) ) ;
11551175 }
11561176 }
11571177
@@ -1172,7 +1192,7 @@ mod tests {
11721192 let cmdline = & vmm. kernel_cfg . cmdline ;
11731193 let fdt = vmm
11741194 . fdt_builder
1175- . with_cmdline ( cmdline. as_str ( ) . to_string ( ) )
1195+ . with_cmdline ( cmdline. as_string ( ) . unwrap ( ) . to_string ( ) )
11761196 . with_num_vcpus ( vmm. num_vcpus . try_into ( ) . unwrap ( ) )
11771197 . with_mem_size ( mem_size)
11781198 . with_serial_console ( 0x40000000 , 0x1000 )
0 commit comments