From b7e10ca7af5b68f57413a62c3ebe0fb1c3584046 Mon Sep 17 00:00:00 2001 From: Valentin Huber Date: Tue, 21 May 2024 13:19:02 +0200 Subject: [PATCH] Making StdOutObserver and StdErrObserver implement Observer (#2236) * making stdout and stderr observers implement observer * fixing imports --- libafl/src/executors/command.rs | 6 ++++++ libafl/src/observers/stdio.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/libafl/src/executors/command.rs b/libafl/src/executors/command.rs index 4b44d00951..2614b711eb 100644 --- a/libafl/src/executors/command.rs +++ b/libafl/src/executors/command.rs @@ -236,6 +236,7 @@ where use wait_timeout::ChildExt; *state.executions_mut() += 1; + self.observers.pre_exec_child_all(state, input)?; let mut child = self.configurer.spawn_child(input)?; @@ -258,6 +259,11 @@ where } }; + if let Ok(exit_kind) = res { + self.observers + .post_exec_child_all(state, input, &exit_kind)?; + } + if let Some(ref mut ob) = &mut self.configurer.stdout_observer_mut() { let mut stdout = Vec::new(); child.stdout.as_mut().ok_or_else(|| { diff --git a/libafl/src/observers/stdio.rs b/libafl/src/observers/stdio.rs index 43ee321435..e73c378d41 100644 --- a/libafl/src/observers/stdio.rs +++ b/libafl/src/observers/stdio.rs @@ -8,6 +8,8 @@ use std::vec::Vec; use libafl_bolts::Named; use serde::{Deserialize, Serialize}; +use crate::{inputs::UsesInput, observers::Observer, state::State, Error}; + /// An observer that captures stdout of a target. /// Only works for supported executors. #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -41,6 +43,20 @@ impl Named for StdOutObserver { } } +impl Observer for StdOutObserver +where + S: State, +{ + fn pre_exec_child( + &mut self, + _state: &mut S, + _input: &::Input, + ) -> Result<(), Error> { + self.stdout = None; + Ok(()) + } +} + /// An observer that captures stderr of a target. /// Only works for supported executors. #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -73,3 +89,17 @@ impl Named for StdErrObserver { &self.name } } + +impl Observer for StdErrObserver +where + S: State, +{ + fn pre_exec_child( + &mut self, + _state: &mut S, + _input: &::Input, + ) -> Result<(), Error> { + self.stderr = None; + Ok(()) + } +}