From 148a9ed5ce8acedbcc1f99215850c4f6f768da21 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Fri, 24 Jan 2020 09:34:51 +0100 Subject: [PATCH] vmm: Fix map_err losing the inner error Signed-off-by: Sebastien Boeuf --- vmm/src/cpu.rs | 4 ++-- vmm/src/memory_manager.rs | 11 +++++------ vmm/src/vm.rs | 27 ++++++++++++++++++--------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/vmm/src/cpu.rs b/vmm/src/cpu.rs index 7610951b2..7e4901da6 100644 --- a/vmm/src/cpu.rs +++ b/vmm/src/cpu.rs @@ -109,7 +109,7 @@ pub enum Error { VcpuUnhandledKvmExit, /// Failed to join on vCPU threads - ThreadCleanup, + ThreadCleanup(std::boxed::Box), /// Cannot add legacy device to Bus. BusError(devices::BusError), @@ -478,7 +478,7 @@ impl VcpuState { fn join_thread(&mut self) -> Result<()> { if let Some(handle) = self.handle.take() { - handle.join().map_err(|_| Error::ThreadCleanup)? + handle.join().map_err(Error::ThreadCleanup)? } Ok(()) diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index b6fa5bbe9..b3b4db098 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -78,6 +78,9 @@ pub enum Error { /// The requested hotplug memory addition is not a valid size InvalidSize, + + /// Failed to set the user memory region. + SetUserMemoryRegion(kvm_ioctls::Error), } pub fn get_host_cpu_phys_bits() -> u8 { @@ -411,12 +414,8 @@ impl MemoryManager { }; // Safe because the guest regions are guaranteed not to overlap. - unsafe { - self.fd - .set_user_memory_region(mem_region) - .map_err(|e| io::Error::from_raw_os_error(e.errno())) - } - .map_err(|_: io::Error| Error::GuestMemory(MmapError::NoMemoryRegion))?; + unsafe { self.fd.set_user_memory_region(mem_region) } + .map_err(Error::SetUserMemoryRegion)?; // Mark the pages as mergeable if explicitly asked for. if mergeable { diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index c77ff01d5..b83af0dae 100755 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -78,7 +78,16 @@ pub enum Error { KernelLoad(linux_loader::loader::Error), /// Cannot load the command line in memory - CmdLine, + LoadCmdLine(linux_loader::loader::Error), + + /// Cannot modify the command line + CmdLineInsertStr(linux_loader::cmdline::Error), + + /// Cannot convert command line into CString + CmdLineCString(std::ffi::NulError), + + /// Cannot configure system + ConfigureSystem(arch::Error), PoisonedState, @@ -110,7 +119,7 @@ pub enum Error { SignalHandlerSpawn(io::Error), /// Failed to join on vCPU threads - ThreadCleanup, + ThreadCleanup(std::boxed::Box), /// Failed to create a new KVM instance KvmNew(kvm_ioctls::Error), @@ -363,12 +372,12 @@ impl Vm { let mut cmdline = Cmdline::new(arch::CMDLINE_MAX_SIZE); cmdline .insert_str(self.config.lock().unwrap().cmdline.args.clone()) - .map_err(|_| Error::CmdLine)?; + .map_err(Error::CmdLineInsertStr)?; for entry in self.devices.cmdline_additions() { - cmdline.insert_str(entry).map_err(|_| Error::CmdLine)?; + cmdline.insert_str(entry).map_err(Error::CmdLineInsertStr)?; } - let cmdline_cstring = CString::new(cmdline).map_err(|_| Error::CmdLine)?; + let cmdline_cstring = CString::new(cmdline).map_err(Error::CmdLineCString)?; let guest_memory = self.memory_manager.lock().as_ref().unwrap().guest_memory(); let mem = guest_memory.load_full(); let entry_addr = match linux_loader::loader::Elf::load( @@ -395,7 +404,7 @@ impl Vm { arch::layout::CMDLINE_START, &cmdline_cstring, ) - .map_err(|_| Error::CmdLine)?; + .map_err(Error::LoadCmdLine)?; let boot_vcpus = self.cpu_manager.lock().unwrap().boot_vcpus(); let _max_vcpus = self.cpu_manager.lock().unwrap().max_vcpus(); @@ -422,7 +431,7 @@ impl Vm { Some(hdr), rsdp_addr, ) - .map_err(|_| Error::CmdLine)?; + .map_err(Error::ConfigureSystem)?; let load_addr = entry_addr .kernel_load @@ -441,7 +450,7 @@ impl Vm { None, rsdp_addr, ) - .map_err(|_| Error::CmdLine)?; + .map_err(Error::ConfigureSystem)?; Ok(entry_addr.kernel_load) } @@ -476,7 +485,7 @@ impl Vm { // Wait for all the threads to finish for thread in self.threads.drain(..) { - thread.join().map_err(|_| Error::ThreadCleanup)? + thread.join().map_err(Error::ThreadCleanup)? } *state = new_state;