Skip to content

Commit fa8a576

Browse files
wtdcodetokatoka
andauthored
Introduce HasAflStyleTargetArguments and restore parse_afl_cmdline for CommandExecutor (#3125)
* Introduce HasAflStyleTargetArguments and restore parse_afl_cmdline for CommandExecutor * clippy * Fix imports * Move envs to the shared trait * Move to a standalone file * Format * Gate via std and unix * Fix * clippy * move InputLocation to afl_args * clippy * Fix * fmt * Fix more * Shall last missing * Rename to AflTargetArgs * Fmt * move AflTargetArgs to libafl_bolts and adapt CMainArgsBuilder to use it * use_stdin is no longer needed * leaveout * Fix again * Renaming * Leave out lib.rs * Leave out fmt --------- Co-authored-by: Dongjia "toka" Zhang <[email protected]>
1 parent a7d735c commit fa8a576

File tree

16 files changed

+467
-520
lines changed

16 files changed

+467
-520
lines changed

fuzzers/baby/backtrace_baby_fuzzers/forkserver_executor/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use libafl_bolts::{
2525
rands::StdRand,
2626
shmem::{ShMem, ShMemProvider},
2727
tuples::tuple_list,
28-
AsSliceMut,
28+
AsSliceMut, TargetArgs,
2929
};
3030

3131
pub fn main() {

fuzzers/forkserver/forkserver_libafl_cc/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use libafl_bolts::{
2222
rands::StdRand,
2323
shmem::{ShMem, ShMemProvider, UnixShMemProvider},
2424
tuples::{tuple_list, Handled, Merge},
25-
AsSliceMut, Truncate,
25+
AsSliceMut, TargetArgs, Truncate,
2626
};
2727
use libafl_targets::EDGES_MAP_DEFAULT_SIZE;
2828
use nix::sys::signal::Signal;

fuzzers/forkserver/forkserver_simple/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use libafl::{
1919
state::{HasCorpus, StdState},
2020
};
2121
use libafl_bolts::{
22-
AsSliceMut, Truncate, current_nanos,
22+
AsSliceMut, TargetArgs, Truncate, current_nanos,
2323
rands::StdRand,
2424
shmem::{ShMem, ShMemProvider, UnixShMemProvider},
2525
tuples::{Handled, Merge, tuple_list},

fuzzers/forkserver/fuzzbench_forkserver/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use libafl_bolts::{
3838
rands::StdRand,
3939
shmem::{ShMem, ShMemProvider, UnixShMemProvider},
4040
tuples::{tuple_list, Merge},
41-
AsSliceMut,
41+
AsSliceMut, TargetArgs,
4242
};
4343
use libafl_targets::cmps::AFLppCmpLogMap;
4444
use nix::sys::signal::Signal;

fuzzers/forkserver/fuzzbench_forkserver_cmplog/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use libafl_bolts::{
3737
rands::StdRand,
3838
shmem::{ShMem, ShMemProvider, UnixShMemProvider},
3939
tuples::{tuple_list, Handled, Merge},
40-
AsSliceMut,
40+
AsSliceMut, TargetArgs,
4141
};
4242
use libafl_targets::{
4343
cmps::{observers::AFLppCmpLogObserver, stages::AFLppCmplogTracingStage},

fuzzers/forkserver/fuzzbench_forkserver_sand/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use libafl_bolts::{
3838
rands::StdRand,
3939
shmem::{ShMem, ShMemProvider, UnixShMemProvider},
4040
tuples::{tuple_list, Handled, Merge},
41-
AsSliceMut,
41+
AsSliceMut, TargetArgs,
4242
};
4343
use libafl_targets::cmps::AFLppCmpLogMap;
4444
use nix::sys::signal::Signal;

fuzzers/forkserver/libafl-fuzz/src/fuzzer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use libafl_bolts::{
5151
rands::StdRand,
5252
shmem::{ShMem, ShMemProvider, UnixShMemProvider},
5353
tuples::{tuple_list, Handled, Merge},
54-
AsSliceMut,
54+
AsSliceMut, TargetArgs,
5555
};
5656
#[cfg(feature = "nyx")]
5757
use libafl_nyx::{executor::NyxExecutor, helper::NyxHelper, settings::NyxSettings};

fuzzers/structure_aware/forkserver_simple_nautilus/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use libafl_bolts::{
2929
rands::StdRand,
3030
shmem::{ShMem, ShMemProvider, UnixShMemProvider},
3131
tuples::{tuple_list, Handled},
32-
AsSliceMut, Truncate,
32+
AsSliceMut, TargetArgs, Truncate,
3333
};
3434
use nix::sys::signal::Signal;
3535

libafl/src/executors/command.rs

Lines changed: 38 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ use std::{
2323
#[cfg(all(feature = "intel_pt", target_os = "linux"))]
2424
use libafl_bolts::core_affinity::CoreId;
2525
use libafl_bolts::{
26-
AsSlice,
27-
fs::{InputFile, get_unique_std_input_file},
26+
AsSlice, InputLocation, TargetArgs,
2827
tuples::{Handle, MatchName, RefIndexable},
2928
};
3029
#[cfg(all(feature = "intel_pt", target_os = "linux"))]
@@ -59,27 +58,6 @@ use crate::{
5958
std::borrow::ToOwned,
6059
};
6160

62-
/// How to deliver input to an external program
63-
/// `StdIn`: The target reads from stdin
64-
/// `File`: The target reads from the specified [`InputFile`]
65-
#[derive(Debug, Clone, PartialEq, Eq, Default)]
66-
pub enum InputLocation {
67-
/// Mutate a commandline argument to deliver an input
68-
Arg {
69-
/// The offset of the argument to mutate
70-
argnum: usize,
71-
},
72-
/// Deliver input via `StdIn`
73-
#[default]
74-
StdIn,
75-
/// Deliver the input via the specified [`InputFile`]
76-
/// You can use specify [`InputFile::create(INPUTFILE_STD)`] to use a default filename.
77-
File {
78-
/// The file to write input to. The target should read input from this location.
79-
out_file: InputFile,
80-
},
81-
}
82-
8361
/// A simple Configurator that takes the most common parameters
8462
/// Writes the input either to stdio or to a file
8563
/// Use [`CommandExecutor::builder()`] to use this configurator.
@@ -530,6 +508,40 @@ pub struct CommandExecutorBuilder {
530508
timeout: Duration,
531509
}
532510

511+
impl TargetArgs for CommandExecutorBuilder {
512+
fn arguments_ref(&self) -> &Vec<OsString> {
513+
&self.args
514+
}
515+
516+
fn arguments_mut(&mut self) -> &mut Vec<OsString> {
517+
&mut self.args
518+
}
519+
520+
fn envs_ref(&self) -> &Vec<(OsString, OsString)> {
521+
&self.envs
522+
}
523+
524+
fn envs_mut(&mut self) -> &mut Vec<(OsString, OsString)> {
525+
&mut self.envs
526+
}
527+
528+
fn program_ref(&self) -> &Option<OsString> {
529+
&self.program
530+
}
531+
532+
fn program_mut(&mut self) -> &mut Option<OsString> {
533+
&mut self.program
534+
}
535+
536+
fn input_location_ref(&self) -> &InputLocation {
537+
&self.input_location
538+
}
539+
540+
fn input_location_mut(&mut self) -> &mut InputLocation {
541+
&mut self.input_location
542+
}
543+
}
544+
533545
impl Default for CommandExecutorBuilder {
534546
fn default() -> Self {
535547
Self::new()
@@ -553,40 +565,6 @@ impl CommandExecutorBuilder {
553565
}
554566
}
555567

556-
/// Set the binary to execute
557-
/// This option is required.
558-
pub fn program<O>(&mut self, program: O) -> &mut Self
559-
where
560-
O: AsRef<OsStr>,
561-
{
562-
self.program = Some(program.as_ref().to_owned());
563-
self
564-
}
565-
566-
/// Set the input mode and location.
567-
/// This option is mandatory, if not set, the `build` method will error.
568-
fn input(&mut self, input: InputLocation) -> &mut Self {
569-
// This is a fatal error in the user code, no point in returning Err.
570-
assert_eq!(
571-
self.input_location,
572-
InputLocation::StdIn,
573-
"input location already set to non-stdin, cannot set it again"
574-
);
575-
self.input_location = input;
576-
self
577-
}
578-
579-
/// Sets the input mode to [`InputLocation::Arg`] and uses the current arg offset as `argnum`.
580-
/// During execution, at input will be provided _as argument_ at this position.
581-
/// Use [`Self::arg_input_file_std`] if you want to provide the input as a file instead.
582-
pub fn arg_input_arg(&mut self) -> &mut Self {
583-
let argnum = self.args.len();
584-
self.input(InputLocation::Arg { argnum });
585-
// Placeholder arg that gets replaced with the input name later.
586-
self.arg("PLACEHOLDER");
587-
self
588-
}
589-
590568
/// Sets the stdout observer
591569
pub fn stdout_observer(&mut self, stdout: Handle<StdOutObserver>) -> &mut Self {
592570
self.stdout = Some(stdout);
@@ -599,68 +577,6 @@ impl CommandExecutorBuilder {
599577
self
600578
}
601579

602-
/// Sets the input mode to [`InputLocation::File`]
603-
/// and adds the filename as arg to at the current position.
604-
/// Uses a default filename.
605-
/// Use [`Self::arg_input_file`] to specify a custom filename.
606-
pub fn arg_input_file_std(&mut self) -> &mut Self {
607-
self.arg_input_file(get_unique_std_input_file());
608-
self
609-
}
610-
611-
/// Sets the input mode to [`InputLocation::File`]
612-
/// and adds the filename as arg to at the current position.
613-
pub fn arg_input_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
614-
self.arg(path.as_ref());
615-
let out_file_std = InputFile::create(path.as_ref()).unwrap();
616-
self.input(InputLocation::File {
617-
out_file: out_file_std,
618-
});
619-
self
620-
}
621-
622-
/// Adds an argument to the program's commandline.
623-
pub fn arg<O: AsRef<OsStr>>(&mut self, arg: O) -> &mut CommandExecutorBuilder {
624-
self.args.push(arg.as_ref().to_owned());
625-
self
626-
}
627-
628-
/// Adds a range of arguments to the program's commandline.
629-
pub fn args<IT, O>(&mut self, args: IT) -> &mut CommandExecutorBuilder
630-
where
631-
IT: IntoIterator<Item = O>,
632-
O: AsRef<OsStr>,
633-
{
634-
for arg in args {
635-
self.arg(arg.as_ref());
636-
}
637-
self
638-
}
639-
640-
/// Adds a range of environment variables to the executed command.
641-
pub fn envs<IT, K, V>(&mut self, vars: IT) -> &mut CommandExecutorBuilder
642-
where
643-
IT: IntoIterator<Item = (K, V)>,
644-
K: AsRef<OsStr>,
645-
V: AsRef<OsStr>,
646-
{
647-
for (ref key, ref val) in vars {
648-
self.env(key.as_ref(), val.as_ref());
649-
}
650-
self
651-
}
652-
653-
/// Adds an environment variable to the executed command.
654-
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut CommandExecutorBuilder
655-
where
656-
K: AsRef<OsStr>,
657-
V: AsRef<OsStr>,
658-
{
659-
self.envs
660-
.push((key.as_ref().to_owned(), val.as_ref().to_owned()));
661-
self
662-
}
663-
664580
/// Sets the working directory for the child process.
665581
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut CommandExecutorBuilder {
666582
self.cwd = Some(dir.as_ref().to_owned());
@@ -865,6 +781,8 @@ fn waitpid_filtered(pid: Pid, options: Option<WaitPidFlag>) -> Result<WaitStatus
865781

866782
#[cfg(test)]
867783
mod tests {
784+
use libafl_bolts::TargetArgs;
785+
868786
use crate::{
869787
events::SimpleEventManager,
870788
executors::{
@@ -885,8 +803,7 @@ mod tests {
885803
log::info!("{status}");
886804
}));
887805

888-
let mut executor = CommandExecutor::builder();
889-
executor
806+
let executor = CommandExecutor::builder()
890807
.program("ls")
891808
.input(InputLocation::Arg { argnum: 0 });
892809
let executor = executor.build(());

0 commit comments

Comments
 (0)