Skip to content

Commit

Permalink
Polish the API
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcondiro committed Jul 8, 2024
1 parent c417e2e commit 179da1b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 49 deletions.
22 changes: 8 additions & 14 deletions fuzzers/qemu_systemmode/src/fuzzer_classic.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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");

Expand Down
103 changes: 68 additions & 35 deletions libafl_qemu/src/qemu/qemu_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
#[builder(default, setter(strip_option))]
format: Option<DiskImageFileFormat>,
Expand Down Expand Up @@ -114,11 +114,10 @@ impl Display for Bios {
}
}

impl Bios {
#[must_use]
pub fn new(path: &Path) -> Self {
impl<R: AsRef<Path>> From<R> for Bios {
fn from(path: R) -> Self {
Self {
path: path.to_path_buf(),
path: path.as_ref().to_path_buf(),
}
}
}
Expand All @@ -134,11 +133,10 @@ impl Display for Kernel {
}
}

impl Kernel {
#[must_use]
pub fn new(path: &Path) -> Self {
impl<R: AsRef<Path>> From<R> for Kernel {
fn from(path: R) -> Self {
Self {
path: path.to_path_buf(),
path: path.as_ref().to_path_buf(),
}
}
}
Expand All @@ -154,11 +152,10 @@ impl Display for LoadVM {
}
}

impl LoadVM {
#[must_use]
pub fn new(path: &Path) -> Self {
impl<R: AsRef<Path>> From<R> for LoadVM {
fn from(path: R) -> Self {
Self {
path: path.to_path_buf(),
path: path.as_ref().to_path_buf(),
}
}
}
Expand All @@ -174,11 +171,10 @@ impl Display for Machine {
}
}

impl Machine {
#[must_use]
pub fn new(name: &str) -> Self {
impl<R: AsRef<str>> From<R> for Machine {
fn from(name: R) -> Self {
Self {
name: name.to_string(),
name: name.as_ref().to_string(),
}
}
}
Expand All @@ -191,6 +187,16 @@ pub enum Snapshot {
DISABLE,
}

impl From<bool> 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 = "")]
Expand All @@ -199,6 +205,16 @@ pub enum StartCPU {
DISABLE,
}

impl From<bool> 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")]
Expand All @@ -207,6 +223,16 @@ pub enum NoGraphic {
DISABLE,
}

impl From<bool> for NoGraphic {
fn from(no_graphic: bool) -> Self {
if no_graphic {
NoGraphic::ENABLE
} else {
NoGraphic::DISABLE
}
}
}

#[derive(Debug, Clone)]
pub enum RamSize {
MB(u32),
Expand Down Expand Up @@ -241,6 +267,16 @@ pub enum VgaPci {
DISABLE,
}

impl From<bool> 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 {
Expand All @@ -255,11 +291,10 @@ impl Display for Program {
}

#[cfg(emulation_mode = "usermode")]
impl Program {
#[must_use]
pub fn new(path: &Path) -> Self {
impl<R: AsRef<Path>> From<R> for Program {
fn from(path: R) -> Self {
Self {
path: path.to_path_buf(),
path: path.as_ref().to_path_buf(),
}
}
}
Expand All @@ -270,35 +305,36 @@ pub struct QemuConfig {
#[cfg(emulation_mode = "systemmode")]
#[builder(default, setter(strip_option))]
accelerator: Option<Accelerator>,
#[builder(default, setter(strip_option))]
#[builder(default, setter(strip_option, into))]
bios: Option<Bios>,
#[builder(default)]
#[builder(default, setter(into))]
drives: Vec<Drive>,
#[builder(default, setter(strip_option))]
#[builder(default, setter(strip_option, into))]
kernel: Option<Kernel>,
#[builder(default, setter(strip_option))]
#[builder(default, setter(strip_option, into))]
load_vm: Option<LoadVM>,
#[builder(default, setter(strip_option))]
#[builder(default, setter(strip_option, into))]
machine: Option<Machine>,
#[builder(default, setter(strip_option))]
monitor: Option<Monitor>,
#[builder(default, setter(strip_option))]
#[builder(default, setter(strip_option, into))]
no_graphic: Option<NoGraphic>,
#[builder(default, setter(strip_option))]
ram_size: Option<RamSize>,
#[builder(default, setter(strip_option))]
serial: Option<Serial>,
#[builder(default, setter(strip_option))]
smp_cpus: Option<SmpCpus>,
#[builder(default, setter(strip_option))]
#[builder(default, setter(strip_option, into))]
snapshot: Option<Snapshot>,
#[builder(default, setter(strip_option))]
#[builder(default, setter(strip_option, into))]
vga_pci: Option<VgaPci>,
#[builder(default, setter(strip_option))]
#[builder(default, setter(strip_option, into))]
start_cpu: Option<StartCPU>,
#[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<QemuConfig> for Result<Qemu, QemuInitError> {
fn from(config: QemuConfig) -> Self {
Expand All @@ -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());
}
Expand Down

0 comments on commit 179da1b

Please sign in to comment.