diff --git a/fuzzers/qemu_systemmode/src/fuzzer_classic.rs b/fuzzers/qemu_systemmode/src/fuzzer_classic.rs index e932c7f2b47..ef01ecf414e 100644 --- a/fuzzers/qemu_systemmode/src/fuzzer_classic.rs +++ b/fuzzers/qemu_systemmode/src/fuzzer_classic.rs @@ -1,11 +1,7 @@ //! A fuzzer using qemu in systemmode for binary-only coverage of kernels //! use core::{ptr::addr_of_mut, time::Duration}; -use std::{ - env, - path::{Path, PathBuf}, - process, -}; +use std::{env, path::PathBuf, process}; use libafl::{ corpus::{Corpus, InMemoryCorpus, OnDiskCorpus}, @@ -89,20 +85,18 @@ pub fn fuzz() { let mut run_client = |state: Option<_>, mut mgr, _core_id| { // Initialize QEMU let qemu = Qemu::builder() - .machine(qemu_config::Machine::new("mps2-an385")) + .machine("mps2-an385") .monitor(qemu_config::Monitor::null) - .kernel(qemu_config::Kernel::new(Path::new( - "target/classic/example.elf", - ))) + .kernel("target/classic/example.elf") .serial(qemu_config::Serial::null) - .no_graphic(qemu_config::NoGraphic::ENABLE) - .snapshot(qemu_config::Snapshot::ENABLE) - .drives(vec![qemu_config::Drive::builder() + .no_graphic(true) + .snapshot(true) + .drives([qemu_config::Drive::builder() .interface(qemu_config::DriveInterface::none) .format(qemu_config::DiskImageFileFormat::qcow2) - .file(PathBuf::from("target/classic/dummy.qcow2")) + .file("target/classic/dummy.qcow2") .build()]) - .start_cpu(qemu_config::StartCPU::DISABLE) + .start_cpu(false) .build() .expect("Failed to initialized QEMU"); diff --git a/libafl_qemu/src/qemu/qemu_config.rs b/libafl_qemu/src/qemu/qemu_config.rs index 292ea46f0d9..f688693a071 100644 --- a/libafl_qemu/src/qemu/qemu_config.rs +++ b/libafl_qemu/src/qemu/qemu_config.rs @@ -48,7 +48,7 @@ pub enum DiskImageFileFormat { #[derive(Debug, Clone, Default, TypedBuilder)] pub struct Drive { - #[builder(default, setter(strip_option))] + #[builder(default, setter(strip_option, into))] file: Option, #[builder(default, setter(strip_option))] format: Option, @@ -114,11 +114,10 @@ impl Display for Bios { } } -impl Bios { - #[must_use] - pub fn new(path: &Path) -> Self { +impl> From for Bios { + fn from(path: R) -> Self { Self { - path: path.to_path_buf(), + path: path.as_ref().to_path_buf(), } } } @@ -134,11 +133,10 @@ impl Display for Kernel { } } -impl Kernel { - #[must_use] - pub fn new(path: &Path) -> Self { +impl> From for Kernel { + fn from(path: R) -> Self { Self { - path: path.to_path_buf(), + path: path.as_ref().to_path_buf(), } } } @@ -154,11 +152,10 @@ impl Display for LoadVM { } } -impl LoadVM { - #[must_use] - pub fn new(path: &Path) -> Self { +impl> From for LoadVM { + fn from(path: R) -> Self { Self { - path: path.to_path_buf(), + path: path.as_ref().to_path_buf(), } } } @@ -174,11 +171,10 @@ impl Display for Machine { } } -impl Machine { - #[must_use] - pub fn new(name: &str) -> Self { +impl> From for Machine { + fn from(name: R) -> Self { Self { - name: name.to_string(), + name: name.as_ref().to_string(), } } } @@ -191,6 +187,16 @@ pub enum Snapshot { DISABLE, } +impl From for Snapshot { + fn from(snapshot: bool) -> Self { + if snapshot { + Snapshot::ENABLE + } else { + Snapshot::DISABLE + } + } +} + #[derive(Debug, Clone, strum_macros::Display)] pub enum StartCPU { #[strum(serialize = "")] @@ -199,6 +205,16 @@ pub enum StartCPU { DISABLE, } +impl From for StartCPU { + fn from(start_cpu: bool) -> Self { + if start_cpu { + StartCPU::ENABLE + } else { + StartCPU::DISABLE + } + } +} + #[derive(Debug, Clone, strum_macros::Display)] pub enum NoGraphic { #[strum(serialize = "-nographic")] @@ -207,6 +223,16 @@ pub enum NoGraphic { DISABLE, } +impl From for NoGraphic { + fn from(no_graphic: bool) -> Self { + if no_graphic { + NoGraphic::ENABLE + } else { + NoGraphic::DISABLE + } + } +} + #[derive(Debug, Clone)] pub enum RamSize { MB(u32), @@ -241,6 +267,16 @@ pub enum VgaPci { DISABLE, } +impl From for VgaPci { + fn from(vga_pci: bool) -> Self { + if vga_pci { + VgaPci::ENABLE + } else { + VgaPci::DISABLE + } + } +} + #[cfg(emulation_mode = "usermode")] #[derive(Debug, Clone)] pub struct Program { @@ -255,11 +291,10 @@ impl Display for Program { } #[cfg(emulation_mode = "usermode")] -impl Program { - #[must_use] - pub fn new(path: &Path) -> Self { +impl> From for Program { + fn from(path: R) -> Self { Self { - path: path.to_path_buf(), + path: path.as_ref().to_path_buf(), } } } @@ -270,19 +305,19 @@ pub struct QemuConfig { #[cfg(emulation_mode = "systemmode")] #[builder(default, setter(strip_option))] accelerator: Option, - #[builder(default, setter(strip_option))] + #[builder(default, setter(strip_option, into))] bios: Option, - #[builder(default)] + #[builder(default, setter(into))] drives: Vec, - #[builder(default, setter(strip_option))] + #[builder(default, setter(strip_option, into))] kernel: Option, - #[builder(default, setter(strip_option))] + #[builder(default, setter(strip_option, into))] load_vm: Option, - #[builder(default, setter(strip_option))] + #[builder(default, setter(strip_option, into))] machine: Option, #[builder(default, setter(strip_option))] monitor: Option, - #[builder(default, setter(strip_option))] + #[builder(default, setter(strip_option, into))] no_graphic: Option, #[builder(default, setter(strip_option))] ram_size: Option, @@ -290,15 +325,16 @@ pub struct QemuConfig { serial: Option, #[builder(default, setter(strip_option))] smp_cpus: Option, - #[builder(default, setter(strip_option))] + #[builder(default, setter(strip_option, into))] snapshot: Option, - #[builder(default, setter(strip_option))] + #[builder(default, setter(strip_option, into))] vga_pci: Option, - #[builder(default, setter(strip_option))] + #[builder(default, setter(strip_option, into))] start_cpu: Option, #[cfg(emulation_mode = "usermode")] + #[builder(setter(into))] program: Program, -} // Adding something here? Please leave program as the last field +} // Adding something here? Please leave Program as the last field impl From for Result { fn from(config: QemuConfig) -> Self { @@ -323,10 +359,7 @@ mod test { #[cfg(emulation_mode = "usermode")] fn usermode() { let program = "/bin/pwd"; - let qemu = Qemu::builder() - .program(Program::new(Path::new("/bin/pwd"))) - .build() - .unwrap(); + let qemu = Qemu::builder().program("/bin/pwd").build().unwrap(); let config = qemu.get_config().unwrap(); assert_eq!(config.to_string().trim(), program.trim()); }