From 5ee09ed36ca0ed02cf0f2520f81bb33a2435acb8 Mon Sep 17 00:00:00 2001 From: antoine Date: Thu, 7 Jan 2021 15:47:45 +0100 Subject: [PATCH 01/10] Adds a DeviceMapBuilder struct --- mythril/src/kmain.rs | 31 +++++++++++++++--------------- mythril/src/virtdev/acpi.rs | 2 +- mythril/src/virtdev/com.rs | 2 +- mythril/src/virtdev/debug.rs | 2 +- mythril/src/virtdev/dma.rs | 2 +- mythril/src/virtdev/ignore.rs | 2 +- mythril/src/virtdev/ioapic.rs | 2 +- mythril/src/virtdev/keyboard.rs | 2 +- mythril/src/virtdev/mod.rs | 23 +++++++++++++++++----- mythril/src/virtdev/pci.rs | 2 +- mythril/src/virtdev/pic.rs | 2 +- mythril/src/virtdev/pit.rs | 2 +- mythril/src/virtdev/pos.rs | 2 +- mythril/src/virtdev/qemu_fw_cfg.rs | 2 +- mythril/src/virtdev/rtc.rs | 2 +- mythril/src/vm.rs | 6 ++++++ 16 files changed, 53 insertions(+), 33 deletions(-) diff --git a/mythril/src/kmain.rs b/mythril/src/kmain.rs index 839c511..5f41863 100644 --- a/mythril/src/kmain.rs +++ b/mythril/src/kmain.rs @@ -79,45 +79,46 @@ fn build_vm( acpi.add_sdt(madt).unwrap(); - let device_map = config.virtual_devices_mut(); + // let device_map = config.virtual_devices_mut(); + let mut builder = config.device_map_builder(); - device_map + builder .register_device(virtdev::acpi::AcpiRuntime::new(0x600).unwrap()) .unwrap(); - device_map + builder .register_device(virtdev::debug::DebugPort::new(0x402)) .unwrap(); - device_map + builder .register_device(virtdev::com::Uart8250::new(0x3F8)) .unwrap(); - device_map + builder .register_device(virtdev::vga::VgaController::new()) .unwrap(); - device_map + builder .register_device(virtdev::dma::Dma8237::new()) .unwrap(); - device_map + builder .register_device(virtdev::ignore::IgnoredDevice::new()) .unwrap(); - device_map + builder .register_device(virtdev::pci::PciRootComplex::new()) .unwrap(); - device_map + builder .register_device(virtdev::pic::Pic8259::new()) .unwrap(); - device_map + builder .register_device(virtdev::keyboard::Keyboard8042::new()) .unwrap(); - device_map + builder .register_device(virtdev::pit::Pit8254::new()) .unwrap(); - device_map + builder .register_device(virtdev::pos::ProgrammableOptionSelect::new()) .unwrap(); - device_map + builder .register_device(virtdev::rtc::CmosRtc::new(cfg.memory)) .unwrap(); - device_map + builder .register_device(virtdev::ioapic::IoApic::new()) .unwrap(); @@ -151,7 +152,7 @@ fn build_vm( ) .unwrap(); - device_map.register_device(fw_cfg_builder.build()).unwrap(); + builder.register_device(fw_cfg_builder.build()).unwrap(); vm::VirtualMachine::new(cfg.cpus[0].raw, config, info) .expect("Failed to create vm") diff --git a/mythril/src/virtdev/acpi.rs b/mythril/src/virtdev/acpi.rs index d0dc188..5bd8796 100644 --- a/mythril/src/virtdev/acpi.rs +++ b/mythril/src/virtdev/acpi.rs @@ -42,7 +42,7 @@ impl AcpiRuntime { } impl EmulatedDevice for AcpiRuntime { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo( Self::FADT_SMI_COMMAND..=Self::FADT_SMI_COMMAND, diff --git a/mythril/src/virtdev/com.rs b/mythril/src/virtdev/com.rs index 80b79b6..b786e90 100644 --- a/mythril/src/virtdev/com.rs +++ b/mythril/src/virtdev/com.rs @@ -54,7 +54,7 @@ impl Uart8250 { } impl EmulatedDevice for Uart8250 { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { vec![DeviceRegion::PortIo(self.base_port..=self.base_port + 7)] } diff --git a/mythril/src/virtdev/debug.rs b/mythril/src/virtdev/debug.rs index f6443a4..9c30d1c 100644 --- a/mythril/src/virtdev/debug.rs +++ b/mythril/src/virtdev/debug.rs @@ -18,7 +18,7 @@ impl DebugPort { } impl EmulatedDevice for DebugPort { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { vec![DeviceRegion::PortIo(self.port..=self.port)] } diff --git a/mythril/src/virtdev/dma.rs b/mythril/src/virtdev/dma.rs index 12ad61b..9946dd3 100644 --- a/mythril/src/virtdev/dma.rs +++ b/mythril/src/virtdev/dma.rs @@ -31,7 +31,7 @@ impl Dma8237 { } impl EmulatedDevice for Dma8237 { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo( Self::DMA1_CHAN2_ADDR..=Self::DMA1_MASTER_CLEAR, diff --git a/mythril/src/virtdev/ignore.rs b/mythril/src/virtdev/ignore.rs index c8a9d03..9b80e17 100644 --- a/mythril/src/virtdev/ignore.rs +++ b/mythril/src/virtdev/ignore.rs @@ -17,7 +17,7 @@ impl IgnoredDevice { } impl EmulatedDevice for IgnoredDevice { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { vec![ // Ignore #IGNNE stuff DeviceRegion::PortIo(241..=241), diff --git a/mythril/src/virtdev/ioapic.rs b/mythril/src/virtdev/ioapic.rs index 83b3341..31441a0 100644 --- a/mythril/src/virtdev/ioapic.rs +++ b/mythril/src/virtdev/ioapic.rs @@ -15,7 +15,7 @@ impl IoApic { } impl EmulatedDevice for IoApic { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { vec![ DeviceRegion::MemIo( GuestPhysAddr::new(0xfec00000)..=GuestPhysAddr::new(0xfec010f0), diff --git a/mythril/src/virtdev/keyboard.rs b/mythril/src/virtdev/keyboard.rs index 1b2e986..f58dc79 100644 --- a/mythril/src/virtdev/keyboard.rs +++ b/mythril/src/virtdev/keyboard.rs @@ -17,7 +17,7 @@ impl Keyboard8042 { } impl EmulatedDevice for Keyboard8042 { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo(Self::PS2_DATA..=Self::PS2_DATA), DeviceRegion::PortIo(Self::PS2_STATUS..=Self::PS2_STATUS), diff --git a/mythril/src/virtdev/mod.rs b/mythril/src/virtdev/mod.rs index 059816b..603c425 100644 --- a/mythril/src/virtdev/mod.rs +++ b/mythril/src/virtdev/mod.rs @@ -160,6 +160,20 @@ impl DeviceInteraction for GuestPhysAddr { } } +pub struct DeviceMapBuilder<'config> { + pub vm_config: &'config mut VirtualMachineConfig +} + +impl<'config> DeviceMapBuilder<'config> { + pub fn register_device( + &mut self, + dev: Arc>, + ) -> Result<()> { + let services = dev.read().services(self.vm_config); + self.vm_config.virtual_devices_mut().register_services(services) + } +} + /// A structure for looking up `EmulatedDevice`s by port or address #[derive(Default)] pub struct DeviceMap { @@ -176,11 +190,10 @@ impl DeviceMap { op.find_device(self) } - pub fn register_device( + pub fn register_services( &mut self, - dev: Arc>, + services: Vec, ) -> Result<()> { - let services = dev.read().services(); for region in services.into_iter() { match region { DeviceRegion::PortIo(val) => { @@ -221,7 +234,7 @@ impl DeviceMap { } pub trait EmulatedDevice: Send + Sync { - fn services(&self) -> Vec; + fn services(&self, vm_config: VirtualMachineConfig) -> Vec; fn on_event(&mut self, _event: Event) -> Result<()> { Ok(()) @@ -498,7 +511,7 @@ mod test { } impl EmulatedDevice for DummyDevice { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { self.services .iter() .map(|x| DeviceRegion::PortIo(x.clone())) diff --git a/mythril/src/virtdev/pci.rs b/mythril/src/virtdev/pci.rs index ea67e33..d7f45ed 100644 --- a/mythril/src/virtdev/pci.rs +++ b/mythril/src/virtdev/pci.rs @@ -189,7 +189,7 @@ impl PciRootComplex { } impl EmulatedDevice for PciRootComplex { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo( Self::PCI_CONFIG_ADDRESS..=Self::PCI_CONFIG_ADDRESS, diff --git a/mythril/src/virtdev/pic.rs b/mythril/src/virtdev/pic.rs index d016a51..1cec9e8 100644 --- a/mythril/src/virtdev/pic.rs +++ b/mythril/src/virtdev/pic.rs @@ -30,7 +30,7 @@ impl Pic8259 { } impl EmulatedDevice for Pic8259 { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo( Self::PIC_MASTER_COMMAND..=Self::PIC_MASTER_DATA, diff --git a/mythril/src/virtdev/pit.rs b/mythril/src/virtdev/pit.rs index f83f0f2..aa326a1 100644 --- a/mythril/src/virtdev/pit.rs +++ b/mythril/src/virtdev/pit.rs @@ -259,7 +259,7 @@ impl Pit8254 { } impl EmulatedDevice for Pit8254 { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo(PIT_COUNTER_0..=PIT_MODE_CONTROL), DeviceRegion::PortIo(PIT_PS2_CTRL_B..=PIT_PS2_CTRL_B), diff --git a/mythril/src/virtdev/pos.rs b/mythril/src/virtdev/pos.rs index f1a99ef..1153745 100644 --- a/mythril/src/virtdev/pos.rs +++ b/mythril/src/virtdev/pos.rs @@ -24,7 +24,7 @@ impl ProgrammableOptionSelect { // Currently we don't actually implement any of this, but I don't think we // need to either (kvm doesn't seem to) impl EmulatedDevice for ProgrammableOptionSelect { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { vec![DeviceRegion::PortIo( Self::POS_ARBITRATION_CLOCK..=Self::POS_ADAPTER_ENABLE_SETUP, )] diff --git a/mythril/src/virtdev/qemu_fw_cfg.rs b/mythril/src/virtdev/qemu_fw_cfg.rs index 1bb05fd..3bfc414 100644 --- a/mythril/src/virtdev/qemu_fw_cfg.rs +++ b/mythril/src/virtdev/qemu_fw_cfg.rs @@ -388,7 +388,7 @@ impl QemuFwCfg { } impl EmulatedDevice for QemuFwCfg { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo( Self::FW_CFG_PORT_SEL..=Self::FW_CFG_PORT_DATA, diff --git a/mythril/src/virtdev/rtc.rs b/mythril/src/virtdev/rtc.rs index d89cacb..be16f7c 100644 --- a/mythril/src/virtdev/rtc.rs +++ b/mythril/src/virtdev/rtc.rs @@ -167,7 +167,7 @@ impl CmosRtc { //TODO: support the NMI masking stuff impl EmulatedDevice for CmosRtc { - fn services(&self) -> Vec { + fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { vec![DeviceRegion::PortIo(Self::RTC_ADDRESS..=Self::RTC_DATA)] } diff --git a/mythril/src/vm.rs b/mythril/src/vm.rs index 37a6635..54d9f4a 100644 --- a/mythril/src/vm.rs +++ b/mythril/src/vm.rs @@ -274,6 +274,12 @@ impl VirtualMachineConfig { &mut self.virtual_devices } + pub fn device_map_builder(&mut self) -> DeviceMapBuilder<'_> { + DeviceMapBuilder { + vm_config: self + } + } + pub fn physical_devices(&self) -> &PhysicalDeviceConfig { &self.physical_devices } From ac972355799a3bd58320d789d4248fcfda66e042 Mon Sep 17 00:00:00 2001 From: Paul Cacheux Date: Thu, 7 Jan 2021 16:11:58 +0100 Subject: [PATCH 02/10] Bug fixes --- mythril/src/virtdev/acpi.rs | 4 +- mythril/src/virtdev/com.rs | 4 +- mythril/src/virtdev/debug.rs | 4 +- mythril/src/virtdev/dma.rs | 4 +- mythril/src/virtdev/ignore.rs | 4 +- mythril/src/virtdev/ioapic.rs | 4 +- mythril/src/virtdev/keyboard.rs | 4 +- mythril/src/virtdev/mod.rs | 182 +++++++++++++++-------------- mythril/src/virtdev/pci.rs | 7 +- mythril/src/virtdev/pic.rs | 4 +- mythril/src/virtdev/pit.rs | 7 +- mythril/src/virtdev/pos.rs | 4 +- mythril/src/virtdev/qemu_fw_cfg.rs | 7 +- mythril/src/virtdev/rtc.rs | 4 +- mythril/src/virtdev/vga.rs | 7 +- mythril/src/vm.rs | 6 +- 16 files changed, 139 insertions(+), 117 deletions(-) diff --git a/mythril/src/virtdev/acpi.rs b/mythril/src/virtdev/acpi.rs index 5bd8796..e9a3334 100644 --- a/mythril/src/virtdev/acpi.rs +++ b/mythril/src/virtdev/acpi.rs @@ -1,6 +1,6 @@ -use crate::error::Result; use crate::time; use crate::virtdev::{DeviceEvent, DeviceRegion, EmulatedDevice, Event, Port}; +use crate::{error::Result, vm::VirtualMachineConfig}; use alloc::sync::Arc; use alloc::vec::Vec; use spin::RwLock; @@ -42,7 +42,7 @@ impl AcpiRuntime { } impl EmulatedDevice for AcpiRuntime { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo( Self::FADT_SMI_COMMAND..=Self::FADT_SMI_COMMAND, diff --git a/mythril/src/virtdev/com.rs b/mythril/src/virtdev/com.rs index b786e90..9b52ec9 100644 --- a/mythril/src/virtdev/com.rs +++ b/mythril/src/virtdev/com.rs @@ -1,9 +1,9 @@ -use crate::error::Result; use crate::physdev::com::*; use crate::vcpu; use crate::virtdev::{ DeviceEvent, DeviceEventResponse, DeviceRegion, EmulatedDevice, Event, Port, }; +use crate::{error::Result, vm::VirtualMachineConfig}; use alloc::sync::Arc; use alloc::vec::Vec; use core::convert::TryInto; @@ -54,7 +54,7 @@ impl Uart8250 { } impl EmulatedDevice for Uart8250 { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![DeviceRegion::PortIo(self.base_port..=self.base_port + 7)] } diff --git a/mythril/src/virtdev/debug.rs b/mythril/src/virtdev/debug.rs index 9c30d1c..dd1786c 100644 --- a/mythril/src/virtdev/debug.rs +++ b/mythril/src/virtdev/debug.rs @@ -1,7 +1,7 @@ -use crate::error::Result; use crate::virtdev::{ DeviceEvent, DeviceEventResponse, DeviceRegion, EmulatedDevice, Event, Port, }; +use crate::{error::Result, vm::VirtualMachineConfig}; use alloc::sync::Arc; use alloc::vec::Vec; use core::convert::TryInto; @@ -18,7 +18,7 @@ impl DebugPort { } impl EmulatedDevice for DebugPort { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![DeviceRegion::PortIo(self.port..=self.port)] } diff --git a/mythril/src/virtdev/dma.rs b/mythril/src/virtdev/dma.rs index 9946dd3..7ff91b6 100644 --- a/mythril/src/virtdev/dma.rs +++ b/mythril/src/virtdev/dma.rs @@ -1,5 +1,5 @@ -use crate::error::Result; use crate::virtdev::{DeviceRegion, EmulatedDevice, Event, Port}; +use crate::{error::Result, vm::VirtualMachineConfig}; use alloc::sync::Arc; use alloc::vec::Vec; use spin::RwLock; @@ -31,7 +31,7 @@ impl Dma8237 { } impl EmulatedDevice for Dma8237 { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo( Self::DMA1_CHAN2_ADDR..=Self::DMA1_MASTER_CLEAR, diff --git a/mythril/src/virtdev/ignore.rs b/mythril/src/virtdev/ignore.rs index 9b80e17..37d6b7f 100644 --- a/mythril/src/virtdev/ignore.rs +++ b/mythril/src/virtdev/ignore.rs @@ -1,5 +1,5 @@ -use crate::error::Result; use crate::virtdev::{DeviceRegion, EmulatedDevice, Event}; +use crate::{error::Result, vm::VirtualMachineConfig}; use alloc::sync::Arc; use alloc::vec::Vec; use spin::RwLock; @@ -17,7 +17,7 @@ impl IgnoredDevice { } impl EmulatedDevice for IgnoredDevice { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![ // Ignore #IGNNE stuff DeviceRegion::PortIo(241..=241), diff --git a/mythril/src/virtdev/ioapic.rs b/mythril/src/virtdev/ioapic.rs index 31441a0..b61e6c5 100644 --- a/mythril/src/virtdev/ioapic.rs +++ b/mythril/src/virtdev/ioapic.rs @@ -1,6 +1,6 @@ -use crate::error::Result; use crate::memory::GuestPhysAddr; use crate::virtdev::{DeviceRegion, EmulatedDevice, Event}; +use crate::{error::Result, vm::VirtualMachineConfig}; use alloc::sync::Arc; use alloc::vec::Vec; use spin::RwLock; @@ -15,7 +15,7 @@ impl IoApic { } impl EmulatedDevice for IoApic { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![ DeviceRegion::MemIo( GuestPhysAddr::new(0xfec00000)..=GuestPhysAddr::new(0xfec010f0), diff --git a/mythril/src/virtdev/keyboard.rs b/mythril/src/virtdev/keyboard.rs index f58dc79..2a99f6a 100644 --- a/mythril/src/virtdev/keyboard.rs +++ b/mythril/src/virtdev/keyboard.rs @@ -1,5 +1,5 @@ -use crate::error::Result; use crate::virtdev::{DeviceEvent, DeviceRegion, EmulatedDevice, Event, Port}; +use crate::{error::Result, vm::VirtualMachineConfig}; use alloc::sync::Arc; use alloc::vec::Vec; use spin::RwLock; @@ -17,7 +17,7 @@ impl Keyboard8042 { } impl EmulatedDevice for Keyboard8042 { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo(Self::PS2_DATA..=Self::PS2_DATA), DeviceRegion::PortIo(Self::PS2_STATUS..=Self::PS2_STATUS), diff --git a/mythril/src/virtdev/mod.rs b/mythril/src/virtdev/mod.rs index 603c425..9ed7f44 100644 --- a/mythril/src/virtdev/mod.rs +++ b/mythril/src/virtdev/mod.rs @@ -1,6 +1,9 @@ -use crate::error::{Error, Result}; use crate::memory::{GuestAddressSpaceViewMut, GuestPhysAddr}; use crate::vcpu; +use crate::{ + error::{Error, Result}, + vm::VirtualMachineConfig, +}; use alloc::collections::btree_map::BTreeMap; use alloc::sync::Arc; use alloc::vec::Vec; @@ -161,7 +164,7 @@ impl DeviceInteraction for GuestPhysAddr { } pub struct DeviceMapBuilder<'config> { - pub vm_config: &'config mut VirtualMachineConfig + pub vm_config: &'config mut VirtualMachineConfig, } impl<'config> DeviceMapBuilder<'config> { @@ -169,8 +172,10 @@ impl<'config> DeviceMapBuilder<'config> { &mut self, dev: Arc>, ) -> Result<()> { - let services = dev.read().services(self.vm_config); - self.vm_config.virtual_devices_mut().register_services(services) + let services = dev.read().services(&self.vm_config); + self.vm_config + .virtual_devices_mut() + .register_services(services, dev) } } @@ -193,6 +198,7 @@ impl DeviceMap { pub fn register_services( &mut self, services: Vec, + dev: Arc>, ) -> Result<()> { for region in services.into_iter() { match region { @@ -234,7 +240,7 @@ impl DeviceMap { } pub trait EmulatedDevice: Send + Sync { - fn services(&self, vm_config: VirtualMachineConfig) -> Vec; + fn services(&self, vm_config: &VirtualMachineConfig) -> Vec; fn on_event(&mut self, _event: Event) -> Result<()> { Ok(()) @@ -511,7 +517,10 @@ mod test { } impl EmulatedDevice for DummyDevice { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services( + &self, + _vm_config: &VirtualMachineConfig, + ) -> Vec { self.services .iter() .map(|x| DeviceRegion::PortIo(x.clone())) @@ -519,101 +528,104 @@ mod test { } } - #[test] - fn test_device_map() { - let mut map = DeviceMap::default(); - let com = Uart8250::new(0); - map.register_device(com).unwrap(); - let _dev = map.find_device(0u16).unwrap(); + /* + #[test] + fn test_device_map() { + let mut map = DeviceMap::default(); + let com = Uart8250::new(0); + map.register_device(com).unwrap(); + let _dev = map.find_device(0u16).unwrap(); - assert_eq!(map.find_device(10u16).is_none(), true); - } + assert_eq!(map.find_device(10u16).is_none(), true); + } - #[test] - fn test_write_request_try_from() { - let val: Result = - [0x12, 0x34, 0x56, 0x78][..].try_into(); - assert_eq!(val.is_ok(), true); + #[test] + fn test_write_request_try_from() { + let val: Result = + [0x12, 0x34, 0x56, 0x78][..].try_into(); + assert_eq!(val.is_ok(), true); - let val: Result = [0x12, 0x34, 0x56][..].try_into(); - assert_eq!(val.is_err(), true); + let val: Result = [0x12, 0x34, 0x56][..].try_into(); + assert_eq!(val.is_err(), true); - let val: PortWriteRequest = - [0x12, 0x34, 0x56, 0x78][..].try_into().unwrap(); - assert_eq!(val.as_u32(), 0x12345678); + let val: PortWriteRequest = + [0x12, 0x34, 0x56, 0x78][..].try_into().unwrap(); + assert_eq!(val.as_u32(), 0x12345678); - let val: PortWriteRequest = [0x12, 0x34][..].try_into().unwrap(); - assert_eq!(val.as_u32(), 0x1234); - } + let val: PortWriteRequest = [0x12, 0x34][..].try_into().unwrap(); + assert_eq!(val.as_u32(), 0x1234); + } - #[test] - fn test_portio_value_read() { - let mut arr = [0x00, 0x00]; - let mut val = PortReadRequest::TwoBytes(&mut arr); - val.copy_from_u32(0x1234u32); - assert_eq!([0x12, 0x34], val.as_slice()); - assert_eq!(0x1234, u16::from_be_bytes(arr)); - } + #[test] + fn test_portio_value_read() { + let mut arr = [0x00, 0x00]; + let mut val = PortReadRequest::TwoBytes(&mut arr); + val.copy_from_u32(0x1234u32); + assert_eq!([0x12, 0x34], val.as_slice()); + assert_eq!(0x1234, u16::from_be_bytes(arr)); + } - #[test] - fn test_conflicting_portio_device() { - let mut map = DeviceMap::default(); - let com = Uart8250::new(0); - map.register_device(com).unwrap(); - let com = Uart8250::new(0); + #[test] + fn test_conflicting_portio_device() { + let mut map = DeviceMap::default(); + let com = Uart8250::new(0); + map.register_device(com).unwrap(); + let com = Uart8250::new(0); - assert!(map.register_device(com).is_err()); - } + assert!(map.register_device(com).is_err()); + } - #[test] - fn test_fully_overlapping_portio_device() { - // region 2 fully inside region 1 - let services = vec![0..=10, 2..=8]; - let dummy = DummyDevice::new(services); - let mut map = DeviceMap::default(); + #[test] + fn test_fully_overlapping_portio_device() { + // region 2 fully inside region 1 + let services = vec![0..=10, 2..=8]; + let dummy = DummyDevice::new(services); + let mut map = DeviceMap::default(); - assert!(map.register_device(dummy).is_err()); - } + assert!(map.register_device(dummy).is_err()); + } - #[test] - fn test_fully_encompassing_portio_device() { - // region 1 fully inside region 2 - let services = vec![2..=8, 0..=10]; - let dummy = DummyDevice::new(services); - let mut map = DeviceMap::default(); + #[test] + fn test_fully_encompassing_portio_device() { + // region 1 fully inside region 2 + let services = vec![2..=8, 0..=10]; + let dummy = DummyDevice::new(services); + let mut map = DeviceMap::default(); - assert!(map.register_device(dummy).is_err()); - } + assert!(map.register_device(dummy).is_err()); + } - #[test] - fn test_partially_overlapping_tail_portio_device() { - // region 1 and region 2 partially overlap at the tail of region 1 and - // the start of region 2 - let services = vec![0..=4, 3..=8]; - let dummy = DummyDevice::new(services); - let mut map = DeviceMap::default(); + #[test] + fn test_partially_overlapping_tail_portio_device() { + // region 1 and region 2 partially overlap at the tail of region 1 and + // the start of region 2 + let services = vec![0..=4, 3..=8]; + let dummy = DummyDevice::new(services); + let mut map = DeviceMap::default(); - assert!(map.register_device(dummy).is_err()); - } + assert!(map.register_device(dummy).is_err()); + } - #[test] - fn test_partially_overlapping_head_portio_device() { - // region 1 and region 2 partially overlap at the start of region 1 and - // the tail of region 2 - let services = vec![3..=8, 0..=4]; - let dummy = DummyDevice::new(services); - let mut map = DeviceMap::default(); + #[test] + fn test_partially_overlapping_head_portio_device() { + // region 1 and region 2 partially overlap at the start of region 1 and + // the tail of region 2 + let services = vec![3..=8, 0..=4]; + let dummy = DummyDevice::new(services); + let mut map = DeviceMap::default(); - assert!(map.register_device(dummy).is_err()); - } + assert!(map.register_device(dummy).is_err()); + } - #[test] - fn test_non_overlapping_portio_device() { - // region 1 and region 2 don't overlap - let services = vec![0..=3, 4..=8]; - let dummy = DummyDevice::new(services); - let mut map = DeviceMap::default(); + #[test] + fn test_non_overlapping_portio_device() { + // region 1 and region 2 don't overlap + let services = vec![0..=3, 4..=8]; + let dummy = DummyDevice::new(services); + let mut map = DeviceMap::default(); - assert!(map.register_device(dummy).is_ok()); - } + assert!(map.register_device(dummy).is_ok()); + } + + */ } diff --git a/mythril/src/virtdev/pci.rs b/mythril/src/virtdev/pci.rs index d7f45ed..7f56043 100644 --- a/mythril/src/virtdev/pci.rs +++ b/mythril/src/virtdev/pci.rs @@ -1,5 +1,8 @@ -use crate::error::{Error, Result}; use crate::virtdev::{DeviceEvent, DeviceRegion, EmulatedDevice, Event, Port}; +use crate::{ + error::{Error, Result}, + vm::VirtualMachineConfig, +}; use alloc::collections::btree_map::BTreeMap; use alloc::sync::Arc; use alloc::vec::Vec; @@ -189,7 +192,7 @@ impl PciRootComplex { } impl EmulatedDevice for PciRootComplex { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo( Self::PCI_CONFIG_ADDRESS..=Self::PCI_CONFIG_ADDRESS, diff --git a/mythril/src/virtdev/pic.rs b/mythril/src/virtdev/pic.rs index 1cec9e8..4c17774 100644 --- a/mythril/src/virtdev/pic.rs +++ b/mythril/src/virtdev/pic.rs @@ -1,5 +1,5 @@ -use crate::error::Result; use crate::virtdev::{DeviceEvent, DeviceRegion, EmulatedDevice, Event, Port}; +use crate::{error::Result, vm::VirtualMachineConfig}; use alloc::sync::Arc; use alloc::vec::Vec; use core::convert::TryInto; @@ -30,7 +30,7 @@ impl Pic8259 { } impl EmulatedDevice for Pic8259 { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo( Self::PIC_MASTER_COMMAND..=Self::PIC_MASTER_DATA, diff --git a/mythril/src/virtdev/pit.rs b/mythril/src/virtdev/pit.rs index aa326a1..ed6f20e 100644 --- a/mythril/src/virtdev/pit.rs +++ b/mythril/src/virtdev/pit.rs @@ -1,10 +1,13 @@ -use crate::error::{Error, Result}; use crate::physdev::pit::*; use crate::time; use crate::virtdev::{ DeviceEvent, DeviceRegion, EmulatedDevice, Event, Port, PortReadRequest, PortWriteRequest, }; +use crate::{ + error::{Error, Result}, + vm::VirtualMachineConfig, +}; use alloc::sync::Arc; use alloc::vec::Vec; @@ -259,7 +262,7 @@ impl Pit8254 { } impl EmulatedDevice for Pit8254 { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo(PIT_COUNTER_0..=PIT_MODE_CONTROL), DeviceRegion::PortIo(PIT_PS2_CTRL_B..=PIT_PS2_CTRL_B), diff --git a/mythril/src/virtdev/pos.rs b/mythril/src/virtdev/pos.rs index 1153745..045ef72 100644 --- a/mythril/src/virtdev/pos.rs +++ b/mythril/src/virtdev/pos.rs @@ -1,5 +1,5 @@ -use crate::error::Result; use crate::virtdev::{DeviceEvent, DeviceRegion, EmulatedDevice, Event, Port}; +use crate::{error::Result, vm::VirtualMachineConfig}; use alloc::sync::Arc; use alloc::vec::Vec; use spin::RwLock; @@ -24,7 +24,7 @@ impl ProgrammableOptionSelect { // Currently we don't actually implement any of this, but I don't think we // need to either (kvm doesn't seem to) impl EmulatedDevice for ProgrammableOptionSelect { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![DeviceRegion::PortIo( Self::POS_ARBITRATION_CLOCK..=Self::POS_ADAPTER_ENABLE_SETUP, )] diff --git a/mythril/src/virtdev/qemu_fw_cfg.rs b/mythril/src/virtdev/qemu_fw_cfg.rs index 3bfc414..b9d26ac 100644 --- a/mythril/src/virtdev/qemu_fw_cfg.rs +++ b/mythril/src/virtdev/qemu_fw_cfg.rs @@ -1,4 +1,3 @@ -use crate::error::{Error, Result}; use crate::memory::{ GuestAccess, GuestAddressSpaceViewMut, GuestPhysAddr, GuestVirtAddr, PrivilegeLevel, @@ -7,6 +6,10 @@ use crate::virtdev::{ DeviceEvent, DeviceRegion, EmulatedDevice, Event, Port, PortReadRequest, PortWriteRequest, }; +use crate::{ + error::{Error, Result}, + vm::VirtualMachineConfig, +}; use alloc::collections::BTreeMap; use alloc::sync::Arc; use alloc::vec::Vec; @@ -388,7 +391,7 @@ impl QemuFwCfg { } impl EmulatedDevice for QemuFwCfg { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![ DeviceRegion::PortIo( Self::FW_CFG_PORT_SEL..=Self::FW_CFG_PORT_DATA, diff --git a/mythril/src/virtdev/rtc.rs b/mythril/src/virtdev/rtc.rs index be16f7c..f5c6203 100644 --- a/mythril/src/virtdev/rtc.rs +++ b/mythril/src/virtdev/rtc.rs @@ -1,8 +1,8 @@ -use crate::error::Result; use crate::virtdev::{ DeviceEvent, DeviceRegion, EmulatedDevice, Event, Port, PortReadRequest, PortWriteRequest, }; +use crate::{error::Result, vm::VirtualMachineConfig}; use alloc::sync::Arc; use alloc::vec::Vec; use core::convert::{TryFrom, TryInto}; @@ -167,7 +167,7 @@ impl CmosRtc { //TODO: support the NMI masking stuff impl EmulatedDevice for CmosRtc { - fn services(&self, _vm_config: VirtualMachineConfig) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![DeviceRegion::PortIo(Self::RTC_ADDRESS..=Self::RTC_DATA)] } diff --git a/mythril/src/virtdev/vga.rs b/mythril/src/virtdev/vga.rs index 39f6c12..5cbeb11 100644 --- a/mythril/src/virtdev/vga.rs +++ b/mythril/src/virtdev/vga.rs @@ -1,8 +1,11 @@ -use crate::error::{Error, Result}; use crate::virtdev::{ DeviceEvent, DeviceRegion, EmulatedDevice, Event, Port, PortReadRequest, PortWriteRequest, }; +use crate::{ + error::{Error, Result}, + vm::VirtualMachineConfig, +}; use alloc::sync::Arc; use alloc::vec::Vec; use core::convert::{TryFrom, TryInto}; @@ -128,7 +131,7 @@ impl VgaController { } impl EmulatedDevice for VgaController { - fn services(&self) -> Vec { + fn services(&self, _vm_config: &VirtualMachineConfig) -> Vec { vec![ // vga stuff DeviceRegion::PortIo(Self::VGA_INDEX..=Self::VGA_DATA), diff --git a/mythril/src/vm.rs b/mythril/src/vm.rs index 54d9f4a..8822150 100644 --- a/mythril/src/vm.rs +++ b/mythril/src/vm.rs @@ -1,4 +1,3 @@ -use crate::apic; use crate::boot_info::BootInfo; use crate::error::{Error, Result}; use crate::interrupt; @@ -13,6 +12,7 @@ use crate::time; use crate::virtdev::{ DeviceEvent, DeviceInteraction, DeviceMap, Event, ResponseEventArray, }; +use crate::{apic, virtdev::DeviceMapBuilder}; use alloc::boxed::Box; use alloc::collections::BTreeMap; use alloc::string::String; @@ -275,9 +275,7 @@ impl VirtualMachineConfig { } pub fn device_map_builder(&mut self) -> DeviceMapBuilder<'_> { - DeviceMapBuilder { - vm_config: self - } + DeviceMapBuilder { vm_config: self } } pub fn physical_devices(&self) -> &PhysicalDeviceConfig { From 1574e138ba9cbef0cf316af32b7e6c4c82836911 Mon Sep 17 00:00:00 2001 From: Paul Cacheux Date: Thu, 7 Jan 2021 16:27:48 +0100 Subject: [PATCH 03/10] Re-add commented out test working now with config --- mythril/src/virtdev/mod.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/mythril/src/virtdev/mod.rs b/mythril/src/virtdev/mod.rs index 9ed7f44..3d3284e 100644 --- a/mythril/src/virtdev/mod.rs +++ b/mythril/src/virtdev/mod.rs @@ -499,7 +499,7 @@ impl<'a> fmt::Display for MemReadRequest<'a> { #[cfg(test)] mod test { use super::*; - use crate::virtdev::com::*; + use crate::{virtdev::com::*, vm::PhysicalDeviceConfig}; use core::convert::TryInto; // This is just a dummy device so we can have arbitrary port ranges @@ -528,17 +528,25 @@ mod test { } } - /* - #[test] - fn test_device_map() { - let mut map = DeviceMap::default(); - let com = Uart8250::new(0); - map.register_device(com).unwrap(); - let _dev = map.find_device(0u16).unwrap(); + #[test] + fn test_device_map() { + let mut config = VirtualMachineConfig::new( + vec![0.into()], + 1024, + PhysicalDeviceConfig::default(), + ); - assert_eq!(map.find_device(10u16).is_none(), true); - } + let mut builder = config.device_map_builder(); + let com = Uart8250::new(0); + builder.register_device(com).unwrap(); + + let map = config.virtual_devices(); + let _dev = map.find_device(0u16).unwrap(); + assert_eq!(map.find_device(10u16).is_none(), true); + } + + /* #[test] fn test_write_request_try_from() { let val: Result = From 46b1de99c070c93c703c0eb2a1b087848871f47e Mon Sep 17 00:00:00 2001 From: antoine Date: Thu, 14 Jan 2021 09:59:39 +0100 Subject: [PATCH 04/10] Corrects virtdev tests --- mythril/src/virtdev/mod.rs | 186 ++++++++++++++++++++++--------------- 1 file changed, 110 insertions(+), 76 deletions(-) diff --git a/mythril/src/virtdev/mod.rs b/mythril/src/virtdev/mod.rs index 3d3284e..08e2f0f 100644 --- a/mythril/src/virtdev/mod.rs +++ b/mythril/src/virtdev/mod.rs @@ -546,94 +546,128 @@ mod test { assert_eq!(map.find_device(10u16).is_none(), true); } - /* - #[test] - fn test_write_request_try_from() { - let val: Result = - [0x12, 0x34, 0x56, 0x78][..].try_into(); - assert_eq!(val.is_ok(), true); - - let val: Result = [0x12, 0x34, 0x56][..].try_into(); - assert_eq!(val.is_err(), true); - - let val: PortWriteRequest = - [0x12, 0x34, 0x56, 0x78][..].try_into().unwrap(); - assert_eq!(val.as_u32(), 0x12345678); - - let val: PortWriteRequest = [0x12, 0x34][..].try_into().unwrap(); - assert_eq!(val.as_u32(), 0x1234); - } + + #[test] + fn test_write_request_try_from() { + let val: Result = + [0x12, 0x34, 0x56, 0x78][..].try_into(); + assert_eq!(val.is_ok(), true); - #[test] - fn test_portio_value_read() { - let mut arr = [0x00, 0x00]; - let mut val = PortReadRequest::TwoBytes(&mut arr); - val.copy_from_u32(0x1234u32); - assert_eq!([0x12, 0x34], val.as_slice()); - assert_eq!(0x1234, u16::from_be_bytes(arr)); - } + let val: Result = [0x12, 0x34, 0x56][..].try_into(); + assert_eq!(val.is_err(), true); - #[test] - fn test_conflicting_portio_device() { - let mut map = DeviceMap::default(); - let com = Uart8250::new(0); - map.register_device(com).unwrap(); - let com = Uart8250::new(0); + let val: PortWriteRequest = + [0x12, 0x34, 0x56, 0x78][..].try_into().unwrap(); + assert_eq!(val.as_u32(), 0x12345678); - assert!(map.register_device(com).is_err()); - } + let val: PortWriteRequest = [0x12, 0x34][..].try_into().unwrap(); + assert_eq!(val.as_u32(), 0x1234); + } + + #[test] + fn test_portio_value_read() { + let mut arr = [0x00, 0x00]; + let mut val = PortReadRequest::TwoBytes(&mut arr); + val.copy_from_u32(0x1234u32); + assert_eq!([0x12, 0x34], val.as_slice()); + assert_eq!(0x1234, u16::from_be_bytes(arr)); + } + + #[test] + fn test_conflicting_portio_device() { + let mut config = VirtualMachineConfig::new( + vec![0.into()], + 1024, + PhysicalDeviceConfig::default(), + ); - #[test] - fn test_fully_overlapping_portio_device() { - // region 2 fully inside region 1 - let services = vec![0..=10, 2..=8]; - let dummy = DummyDevice::new(services); - let mut map = DeviceMap::default(); + let mut builder = config.device_map_builder(); + let com = Uart8250::new(0); + builder.register_device(com).unwrap(); + let com = Uart8250::new(0); - assert!(map.register_device(dummy).is_err()); - } + assert!(builder.register_device(com).is_err()); + } + + #[test] + fn test_fully_overlapping_portio_device() { + // region 2 fully inside region 1 + let services = vec![0..=10, 2..=8]; + let dummy = DummyDevice::new(services); + let mut config = VirtualMachineConfig::new( + vec![0.into()], + 1024, + PhysicalDeviceConfig::default(), + ); - #[test] - fn test_fully_encompassing_portio_device() { - // region 1 fully inside region 2 - let services = vec![2..=8, 0..=10]; - let dummy = DummyDevice::new(services); - let mut map = DeviceMap::default(); + let mut builder = config.device_map_builder(); - assert!(map.register_device(dummy).is_err()); - } + assert!(builder.register_device(dummy).is_err()); + } + + #[test] + fn test_fully_encompassing_portio_device() { + // region 1 fully inside region 2 + let services = vec![2..=8, 0..=10]; + let dummy = DummyDevice::new(services); + let mut config = VirtualMachineConfig::new( + vec![0.into()], + 1024, + PhysicalDeviceConfig::default(), + ); - #[test] - fn test_partially_overlapping_tail_portio_device() { - // region 1 and region 2 partially overlap at the tail of region 1 and - // the start of region 2 - let services = vec![0..=4, 3..=8]; - let dummy = DummyDevice::new(services); - let mut map = DeviceMap::default(); + let mut builder = config.device_map_builder(); - assert!(map.register_device(dummy).is_err()); - } + assert!(builder.register_device(dummy).is_err()); + } + + #[test] + fn test_partially_overlapping_tail_portio_device() { + // region 1 and region 2 partially overlap at the tail of region 1 and + // the start of region 2 + let services = vec![0..=4, 3..=8]; + let dummy = DummyDevice::new(services); + let mut config = VirtualMachineConfig::new( + vec![0.into()], + 1024, + PhysicalDeviceConfig::default(), + ); - #[test] - fn test_partially_overlapping_head_portio_device() { - // region 1 and region 2 partially overlap at the start of region 1 and - // the tail of region 2 - let services = vec![3..=8, 0..=4]; - let dummy = DummyDevice::new(services); - let mut map = DeviceMap::default(); + let mut builder = config.device_map_builder(); - assert!(map.register_device(dummy).is_err()); - } + assert!(builder.register_device(dummy).is_err()); + } + + #[test] + fn test_partially_overlapping_head_portio_device() { + // region 1 and region 2 partially overlap at the start of region 1 and + // the tail of region 2 + let services = vec![3..=8, 0..=4]; + let dummy = DummyDevice::new(services); + let mut config = VirtualMachineConfig::new( + vec![0.into()], + 1024, + PhysicalDeviceConfig::default(), + ); - #[test] - fn test_non_overlapping_portio_device() { - // region 1 and region 2 don't overlap - let services = vec![0..=3, 4..=8]; - let dummy = DummyDevice::new(services); - let mut map = DeviceMap::default(); + let mut builder = config.device_map_builder(); - assert!(map.register_device(dummy).is_ok()); - } + assert!(builder.register_device(dummy).is_err()); + } + + #[test] + fn test_non_overlapping_portio_device() { + // region 1 and region 2 don't overlap + let services = vec![0..=3, 4..=8]; + let dummy = DummyDevice::new(services); + let mut config = VirtualMachineConfig::new( + vec![0.into()], + 1024, + PhysicalDeviceConfig::default(), + ); - */ + let mut builder = config.device_map_builder(); + + assert!(builder.register_device(dummy).is_ok()); + } } From 88d84cd04324648de821ffb2e5c1205533652736 Mon Sep 17 00:00:00 2001 From: Mael Guillossou Date: Thu, 14 Jan 2021 10:08:29 +0100 Subject: [PATCH 05/10] Default config for test module --- mythril/src/virtdev/mod.rs | 53 +++++++++++++------------------------- 1 file changed, 18 insertions(+), 35 deletions(-) diff --git a/mythril/src/virtdev/mod.rs b/mythril/src/virtdev/mod.rs index 08e2f0f..f9112d7 100644 --- a/mythril/src/virtdev/mod.rs +++ b/mythril/src/virtdev/mod.rs @@ -516,6 +516,17 @@ mod test { } } + impl VirtualMachineConfig { + // Default config used for testing + pub fn default() -> VirtualMachineConfig { + VirtualMachineConfig::new( + vec![0.into()], + 1024, + PhysicalDeviceConfig::default(), + ) + } + } + impl EmulatedDevice for DummyDevice { fn services( &self, @@ -530,11 +541,7 @@ mod test { #[test] fn test_device_map() { - let mut config = VirtualMachineConfig::new( - vec![0.into()], - 1024, - PhysicalDeviceConfig::default(), - ); + let mut config = VirtualMachineConfig::default(); let mut builder = config.device_map_builder(); let com = Uart8250::new(0); @@ -575,11 +582,7 @@ mod test { #[test] fn test_conflicting_portio_device() { - let mut config = VirtualMachineConfig::new( - vec![0.into()], - 1024, - PhysicalDeviceConfig::default(), - ); + let mut config = VirtualMachineConfig::default(); let mut builder = config.device_map_builder(); let com = Uart8250::new(0); @@ -594,11 +597,7 @@ mod test { // region 2 fully inside region 1 let services = vec![0..=10, 2..=8]; let dummy = DummyDevice::new(services); - let mut config = VirtualMachineConfig::new( - vec![0.into()], - 1024, - PhysicalDeviceConfig::default(), - ); + let mut config = VirtualMachineConfig::default(); let mut builder = config.device_map_builder(); @@ -610,11 +609,7 @@ mod test { // region 1 fully inside region 2 let services = vec![2..=8, 0..=10]; let dummy = DummyDevice::new(services); - let mut config = VirtualMachineConfig::new( - vec![0.into()], - 1024, - PhysicalDeviceConfig::default(), - ); + let mut config = VirtualMachineConfig::default(); let mut builder = config.device_map_builder(); @@ -627,11 +622,7 @@ mod test { // the start of region 2 let services = vec![0..=4, 3..=8]; let dummy = DummyDevice::new(services); - let mut config = VirtualMachineConfig::new( - vec![0.into()], - 1024, - PhysicalDeviceConfig::default(), - ); + let mut config = VirtualMachineConfig::default(); let mut builder = config.device_map_builder(); @@ -644,11 +635,7 @@ mod test { // the tail of region 2 let services = vec![3..=8, 0..=4]; let dummy = DummyDevice::new(services); - let mut config = VirtualMachineConfig::new( - vec![0.into()], - 1024, - PhysicalDeviceConfig::default(), - ); + let mut config = VirtualMachineConfig::default(); let mut builder = config.device_map_builder(); @@ -660,11 +647,7 @@ mod test { // region 1 and region 2 don't overlap let services = vec![0..=3, 4..=8]; let dummy = DummyDevice::new(services); - let mut config = VirtualMachineConfig::new( - vec![0.into()], - 1024, - PhysicalDeviceConfig::default(), - ); + let mut config = VirtualMachineConfig::default(); let mut builder = config.device_map_builder(); From ad85cb98a550d9bf745f22a5b02d348a08d0ae6c Mon Sep 17 00:00:00 2001 From: Paul Cacheux Date: Thu, 14 Jan 2021 14:30:12 +0100 Subject: [PATCH 06/10] Fix fmt errors --- mythril/src/virtdev/mod.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/mythril/src/virtdev/mod.rs b/mythril/src/virtdev/mod.rs index f9112d7..f21db0c 100644 --- a/mythril/src/virtdev/mod.rs +++ b/mythril/src/virtdev/mod.rs @@ -524,7 +524,7 @@ mod test { 1024, PhysicalDeviceConfig::default(), ) - } + } } impl EmulatedDevice for DummyDevice { @@ -553,7 +553,6 @@ mod test { assert_eq!(map.find_device(10u16).is_none(), true); } - #[test] fn test_write_request_try_from() { let val: Result = @@ -570,7 +569,7 @@ mod test { let val: PortWriteRequest = [0x12, 0x34][..].try_into().unwrap(); assert_eq!(val.as_u32(), 0x1234); } - + #[test] fn test_portio_value_read() { let mut arr = [0x00, 0x00]; @@ -579,7 +578,7 @@ mod test { assert_eq!([0x12, 0x34], val.as_slice()); assert_eq!(0x1234, u16::from_be_bytes(arr)); } - + #[test] fn test_conflicting_portio_device() { let mut config = VirtualMachineConfig::default(); @@ -591,7 +590,7 @@ mod test { assert!(builder.register_device(com).is_err()); } - + #[test] fn test_fully_overlapping_portio_device() { // region 2 fully inside region 1 @@ -603,7 +602,7 @@ mod test { assert!(builder.register_device(dummy).is_err()); } - + #[test] fn test_fully_encompassing_portio_device() { // region 1 fully inside region 2 @@ -615,7 +614,7 @@ mod test { assert!(builder.register_device(dummy).is_err()); } - + #[test] fn test_partially_overlapping_tail_portio_device() { // region 1 and region 2 partially overlap at the tail of region 1 and @@ -628,7 +627,7 @@ mod test { assert!(builder.register_device(dummy).is_err()); } - + #[test] fn test_partially_overlapping_head_portio_device() { // region 1 and region 2 partially overlap at the start of region 1 and @@ -641,7 +640,7 @@ mod test { assert!(builder.register_device(dummy).is_err()); } - + #[test] fn test_non_overlapping_portio_device() { // region 1 and region 2 don't overlap From a4742fd958c3b166654600e6e27b0ca8c4948dfe Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 15 Jan 2021 10:25:36 +0100 Subject: [PATCH 07/10] Modifies the default VirtualMachineConfig method used for tests --- mythril/src/kmain.rs | 1 - mythril/src/virtdev/mod.rs | 30 ++++++++++++++---------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/mythril/src/kmain.rs b/mythril/src/kmain.rs index 5f41863..a7ea4f4 100644 --- a/mythril/src/kmain.rs +++ b/mythril/src/kmain.rs @@ -79,7 +79,6 @@ fn build_vm( acpi.add_sdt(madt).unwrap(); - // let device_map = config.virtual_devices_mut(); let mut builder = config.device_map_builder(); builder diff --git a/mythril/src/virtdev/mod.rs b/mythril/src/virtdev/mod.rs index f21db0c..0e13cbb 100644 --- a/mythril/src/virtdev/mod.rs +++ b/mythril/src/virtdev/mod.rs @@ -516,15 +516,13 @@ mod test { } } - impl VirtualMachineConfig { - // Default config used for testing - pub fn default() -> VirtualMachineConfig { - VirtualMachineConfig::new( - vec![0.into()], - 1024, - PhysicalDeviceConfig::default(), - ) - } + // Default config used for testing + pub fn get_test_config() -> VirtualMachineConfig { + VirtualMachineConfig::new( + vec![0.into()], + 1024, + PhysicalDeviceConfig::default(), + ) } impl EmulatedDevice for DummyDevice { @@ -541,7 +539,7 @@ mod test { #[test] fn test_device_map() { - let mut config = VirtualMachineConfig::default(); + let mut config = get_test_config(); let mut builder = config.device_map_builder(); let com = Uart8250::new(0); @@ -581,7 +579,7 @@ mod test { #[test] fn test_conflicting_portio_device() { - let mut config = VirtualMachineConfig::default(); + let mut config = get_test_config(); let mut builder = config.device_map_builder(); let com = Uart8250::new(0); @@ -596,7 +594,7 @@ mod test { // region 2 fully inside region 1 let services = vec![0..=10, 2..=8]; let dummy = DummyDevice::new(services); - let mut config = VirtualMachineConfig::default(); + let mut config = get_test_config(); let mut builder = config.device_map_builder(); @@ -608,7 +606,7 @@ mod test { // region 1 fully inside region 2 let services = vec![2..=8, 0..=10]; let dummy = DummyDevice::new(services); - let mut config = VirtualMachineConfig::default(); + let mut config = get_test_config(); let mut builder = config.device_map_builder(); @@ -621,7 +619,7 @@ mod test { // the start of region 2 let services = vec![0..=4, 3..=8]; let dummy = DummyDevice::new(services); - let mut config = VirtualMachineConfig::default(); + let mut config = get_test_config(); let mut builder = config.device_map_builder(); @@ -634,7 +632,7 @@ mod test { // the tail of region 2 let services = vec![3..=8, 0..=4]; let dummy = DummyDevice::new(services); - let mut config = VirtualMachineConfig::default(); + let mut config = get_test_config(); let mut builder = config.device_map_builder(); @@ -646,7 +644,7 @@ mod test { // region 1 and region 2 don't overlap let services = vec![0..=3, 4..=8]; let dummy = DummyDevice::new(services); - let mut config = VirtualMachineConfig::default(); + let mut config = get_test_config(); let mut builder = config.device_map_builder(); From eaa86f9a106b7ae1673a0eebf69cbda6e13b1507 Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 15 Jan 2021 11:00:25 +0100 Subject: [PATCH 08/10] Passes check fmt test --- mythril/src/virtdev/mod.rs | 11 ++++------- mythril/src/vm.rs | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/mythril/src/virtdev/mod.rs b/mythril/src/virtdev/mod.rs index b83c07d..35bfbcd 100644 --- a/mythril/src/virtdev/mod.rs +++ b/mythril/src/virtdev/mod.rs @@ -1,8 +1,8 @@ use crate::error::{Error, Result}; use crate::memory::{GuestAddressSpaceView, GuestPhysAddr}; -use alloc::collections::btree_map::BTreeMap; -use crate::vm::VirtualMachineConfig; use crate::percore::CoreId; +use crate::vm::VirtualMachineConfig; +use alloc::collections::btree_map::BTreeMap; use alloc::sync::Arc; use alloc::vec::Vec; use arrayvec::ArrayVec; @@ -521,11 +521,8 @@ mod test { // Default config used for testing pub fn get_test_config() -> VirtualMachineConfig { let cpus = [CoreId::from(0)]; - VirtualMachineConfig::new( - &cpus, - 1024, - PhysicalDeviceConfig::default(), - ).expect("Couldn't create a test VirtualMachineConfig") + VirtualMachineConfig::new(&cpus, 1024, PhysicalDeviceConfig::default()) + .expect("Couldn't create a test VirtualMachineConfig") } impl EmulatedDevice for DummyDevice { diff --git a/mythril/src/vm.rs b/mythril/src/vm.rs index 46a4c91..de83058 100644 --- a/mythril/src/vm.rs +++ b/mythril/src/vm.rs @@ -13,10 +13,10 @@ use crate::percore; use crate::physdev; use crate::time; use crate::vcpu; +use crate::virtdev::DeviceMapBuilder; use crate::virtdev::{ self, DeviceEvent, DeviceInteraction, DeviceMap, Event, ResponseEventArray, }; -use crate::virtdev::DeviceMapBuilder; use alloc::boxed::Box; use alloc::collections::BTreeMap; use alloc::string::String; From 1655c9369c5dd4c350455200a6d5db46ee24b9c3 Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 15 Jan 2021 11:22:28 +0100 Subject: [PATCH 09/10] Moves unused import into test module --- mythril/src/virtdev/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mythril/src/virtdev/mod.rs b/mythril/src/virtdev/mod.rs index 35bfbcd..a82e0cb 100644 --- a/mythril/src/virtdev/mod.rs +++ b/mythril/src/virtdev/mod.rs @@ -1,6 +1,5 @@ use crate::error::{Error, Result}; use crate::memory::{GuestAddressSpaceView, GuestPhysAddr}; -use crate::percore::CoreId; use crate::vm::VirtualMachineConfig; use alloc::collections::btree_map::BTreeMap; use alloc::sync::Arc; @@ -501,6 +500,7 @@ impl<'a> fmt::Display for MemReadRequest<'a> { #[cfg(test)] mod test { use super::*; + use crate::percore::CoreId; use crate::{virtdev::com::*, vm::PhysicalDeviceConfig}; use core::convert::TryInto; From b2342e6d111ae6b2fe01962e7290c2dcb40adf1f Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 15 Jan 2021 11:41:27 +0100 Subject: [PATCH 10/10] Adds missing doc --- mythril/src/vm.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mythril/src/vm.rs b/mythril/src/vm.rs index de83058..3e01f0a 100644 --- a/mythril/src/vm.rs +++ b/mythril/src/vm.rs @@ -350,10 +350,12 @@ impl VirtualMachineConfig { &mut self.virtual_devices } + /// Creates a DeviceMapBuilder based on this config pub fn device_map_builder(&mut self) -> DeviceMapBuilder<'_> { DeviceMapBuilder { vm_config: self } } + /// Access the physical_devices `PhysicalDeviceConfig` attribute pub fn physical_devices(&self) -> &PhysicalDeviceConfig { &self.physical_devices }