Skip to content

Commit fb8939e

Browse files
rmalmaintokatoka
andauthored
Fix fork bug in libafl qemu (#3109)
* fix fork bug * lol * fix * lower it; we're gonna move from this anyway --------- Co-authored-by: Dongjia "toka" Zhang <tokazerkje@outlook.com>
1 parent bfc55c9 commit fb8939e

File tree

7 files changed

+19
-8
lines changed

7 files changed

+19
-8
lines changed

fuzzers/binary_only/fuzzbench_fork_qemu/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ libafl_targets = { path = "../../../libafl_targets" }
3434
log = { version = "0.4.22", features = ["release_max_level_info"] }
3535
clap = { version = "4.5.18", features = ["default"] }
3636
nix = { version = "0.29.0", features = ["fs"] }
37+
env_logger = "0.11.7"

fuzzers/binary_only/fuzzbench_fork_qemu/Justfile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,18 @@ build:
1919
run: build harness
2020
cargo run \
2121
--profile {{ PROFILE }} \
22-
./{{ FUZZER_NAME }} \
22+
{{ BUILD_DIR }}/harness \
2323
-- \
2424
--libafl-in ../../inprocess/libfuzzer_libpng/corpus \
25-
--libafl-out ./out \
26-
./{{ FUZZER_NAME }}
27-
25+
--libafl-out ./out
2826

2927
[unix]
3028
test: build harness
3129
#!/bin/bash
3230

3331
rm -rf out/
3432
timeout 15s {{ FUZZER }} {{ BUILD_DIR }}/harness -- --libafl-in ../../inprocess/libfuzzer_libpng/corpus --libafl-out out ./harness | tee fuzz_stdout.log
35-
if grep -qa "corpus: 5" fuzz_stdout.log; then
33+
if grep -qa "corpus: 2" fuzz_stdout.log; then
3634
echo "Fuzzer is working"
3735
else
3836
echo "Fuzzer does not generate any testcases or any crashes"

fuzzers/binary_only/fuzzbench_fork_qemu/src/fuzzer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ pub fn main() {
6666
// Needed only on no_std
6767
// unsafe { RegistryBuilder::register::<Tokens>(); }
6868

69+
env_logger::init();
70+
6971
let res = match Command::new(env!("CARGO_PKG_NAME"))
7072
.version(env!("CARGO_PKG_VERSION"))
7173
.author("AFLplusplus team")

libafl/src/executors/inprocess_fork/inner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ where
194194
Ok(ExitKind::Ok)
195195
}
196196
}
197-
_ => Ok(ExitKind::Ok),
197+
_ => panic!("Unexpected waitpid exit: {res:?}"),
198198
}
199199
}
200200
}

libafl/src/executors/inprocess_fork/stateful.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ where
119119
self.inner.pre_run_target_child(fuzzer, state, mgr, input)?;
120120
(self.harness_fn)(&mut self.exposed_executor_state, input);
121121
self.inner.post_run_target_child(fuzzer, state, mgr, input);
122-
Ok(ExitKind::Ok)
122+
123+
unreachable!(
124+
"post_run_target_child should make the process quit. This is a bug."
125+
);
123126
}
124127
Ok(ForkResult::Parent { child }) => {
125128
// Parent

libafl_qemu/src/executor.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ pub type QemuInProcessForkExecutor<'a, C, CM, ED, EM, ET, H, I, OT, S, SM, SP, Z
361361
#[cfg(feature = "fork")]
362362
pub struct QemuForkExecutor<'a, C, CM, ED, EM, ET, H, I, OT, S, SM, SP, Z> {
363363
inner: QemuInProcessForkExecutor<'a, C, CM, ED, EM, ET, H, I, OT, S, SM, SP, Z>,
364+
first_exec: bool,
364365
}
365366

366367
#[cfg(feature = "fork")]
@@ -425,6 +426,7 @@ where
425426
timeout,
426427
shmem_provider,
427428
)?,
429+
first_exec: true,
428430
})
429431
}
430432

@@ -475,7 +477,10 @@ where
475477
mgr: &mut EM,
476478
input: &I,
477479
) -> Result<ExitKind, Error> {
478-
self.inner.exposed_executor_state.first_exec(state);
480+
if self.first_exec {
481+
self.inner.exposed_executor_state.first_exec(state);
482+
self.first_exec = false;
483+
}
479484

480485
self.inner.exposed_executor_state.pre_exec(state, input);
481486

libafl_qemu/src/qemu/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,9 @@ impl Qemu {
651651
pub unsafe fn run(&self) -> Result<QemuExitReason, QemuExitError> {
652652
unsafe {
653653
QEMU_IS_RUNNING = true;
654+
log::trace!("[{}] Qemu running", std::process::id());
654655
self.run_inner();
656+
log::trace!("[{}] Qemu running done.", std::process::id());
655657
QEMU_IS_RUNNING = false;
656658
}
657659

0 commit comments

Comments
 (0)