Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for env_logger for qemu binary only fuzzers #2817

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzzers/binary_only/qemu_cmin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ vergen-git2 = "1.0.1"

[dependencies]
clap = { version = "4.5.18", features = ["derive", "string"] }
env_logger = { version = "0.11.5" }
libafl = { path = "../../../libafl" }
libafl_bolts = { path = "../../../libafl_bolts" }
libafl_qemu = { path = "../../../libafl_qemu", features = ["usermode"] }
Expand Down
13 changes: 7 additions & 6 deletions fuzzers/binary_only/qemu_cmin/src/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub struct FuzzerOptions {
pub const MAX_INPUT_SIZE: usize = 1048576; // 1MB

pub fn fuzz() -> Result<(), Error> {
env_logger::init();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use the simple logger from libafl?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for now, we are using a mix of env_logger and libafl logger.
maybe we could create some kind of StdLogger and unify logging usage?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean everybody is free to use whatever they want, that's what libraries are for :D
but if you just want to see the logs and don't care about the extra stuff env logger provides there's no reason for the extra dependency

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to be able to set the log output level at runtime rather than compile time. I don't think that the simple logger supports that?

let mut options = FuzzerOptions::parse();

let corpus_dir = PathBuf::from(options.input);
Expand All @@ -107,10 +108,10 @@ pub fn fuzz() -> Result<(), Error> {
.expect("Failed to read dir entry");

let program = env::args().next().unwrap();
log::debug!("Program: {program:}");
log::info!("Program: {program:}");

options.args.insert(0, program);
log::debug!("ARGS: {:#?}", options.args);
log::info!("ARGS: {:#?}", options.args);

env::remove_var("LD_LIBRARY_PATH");
let qemu = Qemu::init(&options.args).unwrap();
Expand All @@ -121,21 +122,21 @@ pub fn fuzz() -> Result<(), Error> {
let test_one_input_ptr = elf
.resolve_symbol("LLVMFuzzerTestOneInput", qemu.load_addr())
.expect("Symbol LLVMFuzzerTestOneInput not found");
log::debug!("LLVMFuzzerTestOneInput @ {test_one_input_ptr:#x}");
log::info!("LLVMFuzzerTestOneInput @ {test_one_input_ptr:#x}");

qemu.entry_break(test_one_input_ptr);

let pc: GuestReg = qemu.read_reg(Regs::Pc).unwrap();
log::debug!("Break at {pc:#x}");
log::info!("Break at {pc:#x}");

let ret_addr: GuestAddr = qemu.read_return_address().unwrap();
log::debug!("Return address = {ret_addr:#x}");
log::info!("Return address = {ret_addr:#x}");
qemu.set_breakpoint(ret_addr);

let input_addr = qemu
.map_private(0, MAX_INPUT_SIZE, MmapPerms::ReadWrite)
.unwrap();
log::debug!("Placing input at {input_addr:#x}");
log::info!("Placing input at {input_addr:#x}");

let stack_ptr: GuestAddr = qemu.read_reg(Regs::Sp).unwrap();

Expand Down
1 change: 1 addition & 0 deletions fuzzers/binary_only/qemu_coverage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ vergen-git2 = "1.0.1"

[dependencies]
clap = { version = "4.5.18", features = ["derive", "string"] }
env_logger = { version = "0.11.5" }
libafl = { path = "../../../libafl" }
libafl_bolts = { path = "../../../libafl_bolts" }
libafl_qemu = { path = "../../../libafl_qemu", features = ["usermode"] }
Expand Down
19 changes: 10 additions & 9 deletions fuzzers/binary_only/qemu_coverage/src/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub struct FuzzerOptions {
pub const MAX_INPUT_SIZE: usize = 1048576; // 1MB

pub fn fuzz() {
env_logger::init();
let mut options = FuzzerOptions::parse();

let corpus_files = options
Expand All @@ -116,10 +117,10 @@ pub fn fuzz() {
let files_per_core = (num_files as f64 / num_cores as f64).ceil() as usize;

let program = env::args().next().unwrap();
log::debug!("Program: {program:}");
log::info!("Program: {program:}");

options.args.insert(0, program);
log::debug!("ARGS: {:#?}", options.args);
log::info!("ARGS: {:#?}", options.args);

env::remove_var("LD_LIBRARY_PATH");

Expand All @@ -131,12 +132,12 @@ pub fn fuzz() {
let test_one_input_ptr = elf
.resolve_symbol("LLVMFuzzerTestOneInput", qemu.load_addr())
.expect("Symbol LLVMFuzzerTestOneInput not found");
log::debug!("LLVMFuzzerTestOneInput @ {test_one_input_ptr:#x}");
log::info!("LLVMFuzzerTestOneInput @ {test_one_input_ptr:#x}");

qemu.entry_break(test_one_input_ptr);

for m in qemu.mappings() {
log::debug!(
log::info!(
"Mapping: 0x{:016x}-0x{:016x}, {}",
m.start(),
m.end(),
Expand All @@ -145,17 +146,17 @@ pub fn fuzz() {
}

let pc: GuestReg = qemu.read_reg(Regs::Pc).unwrap();
log::debug!("Break at {pc:#x}");
log::info!("Break at {pc:#x}");

let ret_addr: GuestAddr = qemu.read_return_address().unwrap();
log::debug!("Return address = {ret_addr:#x}");
log::info!("Return address = {ret_addr:#x}");

qemu.set_breakpoint(ret_addr);

let input_addr = qemu
.map_private(0, MAX_INPUT_SIZE, MmapPerms::ReadWrite)
.unwrap();
log::debug!("Placing input at {input_addr:#x}");
log::info!("Placing input at {input_addr:#x}");

let stack_ptr: GuestAddr = qemu.read_reg(Regs::Sp).unwrap();

Expand Down Expand Up @@ -267,10 +268,10 @@ pub fn fuzz() {
println!("Failed to load initial corpus at {:?}", &options.input_dir);
process::exit(0);
});
log::debug!("We imported {} inputs from disk.", state.corpus().count());
log::info!("We imported {} inputs from disk.", state.corpus().count());
}

log::debug!("Processed {} inputs from disk.", files.len());
log::info!("Processed {} inputs from disk.", files.len());

mgr.send_exiting()?;
Err(Error::ShuttingDown)?
Expand Down
1 change: 1 addition & 0 deletions fuzzers/binary_only/qemu_launcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ vergen-git2 = "1.0.1"

[dependencies]
clap = { version = "4.5.18", features = ["derive", "string"] }
env_logger = { version = "0.11.5" }
libafl = { path = "../../../libafl", features = ["tui_monitor"] }
libafl_bolts = { path = "../../../libafl_bolts", features = [
"errors_backtrace",
Expand Down
4 changes: 2 additions & 2 deletions fuzzers/binary_only/qemu_launcher/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ impl Client<'_> {
let core_id = client_description.core_id();
let mut args = self.args()?;
Harness::edit_args(&mut args);
log::debug!("ARGS: {:#?}", args);
log::info!("ARGS: {:#?}", args);

let mut env = self.env();
Harness::edit_env(&mut env);
log::debug!("ENV: {:#?}", env);
log::info!("ENV: {:#?}", env);

let is_asan = self.options.is_asan_core(core_id);
let is_asan_guest = self.options.is_asan_guest_core(core_id);
Expand Down
1 change: 1 addition & 0 deletions fuzzers/binary_only/qemu_launcher/src/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct Fuzzer {

impl Fuzzer {
pub fn new() -> Fuzzer {
env_logger::init();
let options = FuzzerOptions::parse();
options.validate();
Fuzzer { options }
Expand Down
4 changes: 2 additions & 2 deletions fuzzers/binary_only/qemu_launcher/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ impl Harness {
/// Initialize the emulator, run to the entrypoint (or jump there) and return the [`Harness`] struct
pub fn init(qemu: Qemu) -> Result<Harness, Error> {
let start_pc = Self::start_pc(qemu)?;
log::debug!("start_pc @ {start_pc:#x}");
log::info!("start_pc @ {start_pc:#x}");

qemu.entry_break(start_pc);

let ret_addr: GuestAddr = qemu
.read_return_address()
.map_err(|e| Error::unknown(format!("Failed to read return address: {e:?}")))?;
log::debug!("ret_addr = {ret_addr:#x}");
log::info!("ret_addr = {ret_addr:#x}");
qemu.set_breakpoint(ret_addr);

let input_addr = qemu
Expand Down
Loading