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

Make exit status interpretable by CommandConfigurator #2723

Merged
merged 6 commits into from
Nov 27, 2024
Merged
Changes from 1 commit
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
40 changes: 20 additions & 20 deletions libafl/src/executors/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use core::{
marker::PhantomData,
ops::IndexMut,
};
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
#[cfg(all(feature = "std", unix))]
use std::os::unix::{ffi::OsStrExt, process::ExitStatusExt};
#[cfg(all(feature = "std", target_os = "linux"))]
use std::{
ffi::{CStr, CString},
Expand Down Expand Up @@ -337,38 +337,28 @@ where
OT: Debug + ObserversTuple<I, S>,
{
fn execute_input_with_command(&mut self, state: &mut S, input: &I) -> Result<ExitKind, Error> {
use std::os::unix::prelude::ExitStatusExt;

use wait_timeout::ChildExt;

*state.executions_mut() += 1;
self.observers.pre_exec_child_all(state, input)?;

let mut child = self.configurer.spawn_child(input)?;

let res = match child
let exit_kind = child
.wait_timeout(self.configurer.exec_timeout())
.expect("waiting on child failed")
.map(|status| status.signal())
{
// for reference: https://www.man7.org/linux/man-pages/man7/signal.7.html
Some(Some(9)) => Ok(ExitKind::Oom),
Some(Some(_)) => Ok(ExitKind::Crash),
Some(None) => Ok(ExitKind::Ok),
None => {
.map(|status| self.configurer.exit_kind_from_status(&status))
.unwrap_or_else(|| {
// if this fails, there is not much we can do. let's hope it failed because the process finished
// in the meantime.
drop(child.kill());
// finally, try to wait to properly clean up system resources.
drop(child.wait());
Ok(ExitKind::Timeout)
}
};
ExitKind::Timeout
});

if let Ok(exit_kind) = res {
self.observers
.post_exec_child_all(state, input, &exit_kind)?;
}
self.observers
.post_exec_child_all(state, input, &exit_kind)?;

if let Some(h) = &mut self.configurer.stdout_observer() {
let mut stdout = Vec::new();
Expand All @@ -392,7 +382,7 @@ where
let obs = observers.index_mut(h);
obs.observe_stderr(&stderr);
}
res
Ok(exit_kind)
}
}

Expand Down Expand Up @@ -828,6 +818,16 @@ pub trait CommandConfigurator<I, C = Child>: Sized {
/// Set the timeout duration for execution of the child process.
fn exec_timeout_mut(&mut self) -> &mut Duration;

/// Maps the exit status of the child process to an `ExitKind`.
momvart marked this conversation as resolved.
Show resolved Hide resolved
fn exit_kind_from_status(&self, status: &std::process::ExitStatus) -> ExitKind {
match status.signal() {
// for reference: https://www.man7.org/linux/man-pages/man7/signal.7.html
Some(9) => ExitKind::Oom,
Some(_) => ExitKind::Crash,
None => ExitKind::Ok,
}
}

/// Create an `Executor` from this `CommandConfigurator`.
fn into_executor<OT, S>(self, observers: OT) -> CommandExecutor<OT, S, Self, (), C>
where
Expand Down
Loading