@@ -23,8 +23,7 @@ use std::{
23
23
#[ cfg( all( feature = "intel_pt" , target_os = "linux" ) ) ]
24
24
use libafl_bolts:: core_affinity:: CoreId ;
25
25
use libafl_bolts:: {
26
- AsSlice ,
27
- fs:: { InputFile , get_unique_std_input_file} ,
26
+ AsSlice , InputLocation , TargetArgs ,
28
27
tuples:: { Handle , MatchName , RefIndexable } ,
29
28
} ;
30
29
#[ cfg( all( feature = "intel_pt" , target_os = "linux" ) ) ]
@@ -59,27 +58,6 @@ use crate::{
59
58
std:: borrow:: ToOwned ,
60
59
} ;
61
60
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
-
83
61
/// A simple Configurator that takes the most common parameters
84
62
/// Writes the input either to stdio or to a file
85
63
/// Use [`CommandExecutor::builder()`] to use this configurator.
@@ -530,6 +508,40 @@ pub struct CommandExecutorBuilder {
530
508
timeout : Duration ,
531
509
}
532
510
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
+
533
545
impl Default for CommandExecutorBuilder {
534
546
fn default ( ) -> Self {
535
547
Self :: new ( )
@@ -553,40 +565,6 @@ impl CommandExecutorBuilder {
553
565
}
554
566
}
555
567
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
-
590
568
/// Sets the stdout observer
591
569
pub fn stdout_observer ( & mut self , stdout : Handle < StdOutObserver > ) -> & mut Self {
592
570
self . stdout = Some ( stdout) ;
@@ -599,68 +577,6 @@ impl CommandExecutorBuilder {
599
577
self
600
578
}
601
579
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
-
664
580
/// Sets the working directory for the child process.
665
581
pub fn current_dir < P : AsRef < Path > > ( & mut self , dir : P ) -> & mut CommandExecutorBuilder {
666
582
self . cwd = Some ( dir. as_ref ( ) . to_owned ( ) ) ;
@@ -865,6 +781,8 @@ fn waitpid_filtered(pid: Pid, options: Option<WaitPidFlag>) -> Result<WaitStatus
865
781
866
782
#[ cfg( test) ]
867
783
mod tests {
784
+ use libafl_bolts:: TargetArgs ;
785
+
868
786
use crate :: {
869
787
events:: SimpleEventManager ,
870
788
executors:: {
@@ -885,8 +803,7 @@ mod tests {
885
803
log:: info!( "{status}" ) ;
886
804
} ) ) ;
887
805
888
- let mut executor = CommandExecutor :: builder ( ) ;
889
- executor
806
+ let executor = CommandExecutor :: builder ( )
890
807
. program ( "ls" )
891
808
. input ( InputLocation :: Arg { argnum : 0 } ) ;
892
809
let executor = executor. build ( ( ) ) ;
0 commit comments