-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.rs
1269 lines (1129 loc) · 44.5 KB
/
execute.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Compiles the target executable with LLVM instrumentation embedded.
//!
//! Available sanitizers:
//! - linux: `address`, `cfi`, `leak`, `memory`, `safe-stack`, `thread`, `undefined`
//! - macos: `address`, `thread`, `undefined`
//! - win10: `address`, `undefined`
//!
//! Enable sanitizers by setting `CFLAGS` in the environment, e.g.
//! ```bash
//! export CFLAGS="-fsanitize=memory,undefined"
//! ```
//!
//! Examples of options that can be passed to the compiler
//! ```bash
//! export CFLAGS="-O3 -mshstk -mllvm -polly -std=c17 -g -fcolor-diagnostics -fuse-ld=lld -L/opt/lib -D_FORTIFY_SOURCE=3 -fstack-protector-all -fcf-protection=full -flto -fvisibility=hidden"
//! ```
//!
//! Further reading:
//! <https://clang.llvm.org/docs/ClangCommandLineReference.html>
//! <https://developers.redhat.com/articles/2022/06/02/use-compiler-flags-stack-protection-gcc-and-clang>
const CFLAGS_DEFAULTS: &str = "-O0 -g -fcolor-diagnostics -fuse-ld=lld";
// also see:
// <https://lldb.llvm.org/use/tutorial.html#starting-or-attaching-to-your-program>
use std::cmp::max;
use std::collections::{BTreeSet, HashMap};
use std::ffi::OsString;
use std::fs::remove_file;
use std::io::{stdout, BufRead, BufReader, BufWriter, Read, Write};
#[cfg(target_os = "macos")]
use std::os::unix::ffi::OsStringExt;
#[cfg(target_os = "linux")]
use std::os::unix::ffi::OsStringExt;
//#[cfg(target_os = "linux")]
//use std::os::unix::process::ExitStatusExt;
#[cfg(target_os = "windows")]
use std::os::windows::{ffi::OsStringExt, process::ExitStatusExt};
use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};
use std::sync::mpsc::channel;
use std::sync::Arc;
use std::thread::{available_parallelism, current, Builder, JoinHandle};
use std::time::Instant;
use futures::future::FutureExt;
use futures::pin_mut;
use futures::select;
use rayon::ThreadPoolBuilder;
use crate::config::Config;
use crate::corpus::{BytesInput, CorpusOps, CorpusType, GraphInput, InputType};
use crate::grammar_tree::GraphTree;
use crate::mutator::{byte_index, Mutation};
/// number of mutations that will be queued for fuzzing before checking results
const FUZZING_QUEUE_SIZE: usize = 256;
#[cfg(not(debug_assertions))]
#[cfg(target_os = "linux")]
pub const SANITIZERS: &[&str] = &[
"address",
"cfi",
"memory",
"safe-stack",
"thread",
"undefined",
];
#[cfg(not(debug_assertions))]
#[cfg(target_os = "macos")]
//pub const SANITIZERS: &[&str] = &["address", "thread", "undefined"];
pub const SANITIZERS: &[&str] = &["cfi", "undefined"];
#[cfg(not(debug_assertions))]
#[cfg(target_os = "windows")]
pub const SANITIZERS: &[&str] = &["address", "undefined"];
#[cfg(debug_assertions)]
pub const SANITIZERS: &[&str] = &[""];
#[derive(Clone)]
pub struct Exec {
pub cfg: Arc<Config>,
_private: (),
}
#[derive(Debug)]
pub enum ExecResult<Output> {
Ok(Output),
Err(Output),
NonTerminatingErr(u32), // u32 val is the PID of the unterminated process
CoverageError(),
}
pub enum CoverageResult {
Ok(BTreeSet<u128>),
Err(),
}
/// create a unique number from two numbers.
/// this is used to assign a unique deterministic branch number when given a
/// line number and a block number.
/// https://en.wikipedia.org/wiki/Pairing_function
fn cantor_pair(a: u64, b: u64) -> u64 {
((a + b) >> 1) * (a + b + 1) + b
}
fn cantor_pair_u128(a: u128, b: u128) -> u128 {
((a + b) >> 1) * (a + b + 1) + b
}
fn compiled_executable_path(cfg: &Config, sanitizer: &str) -> PathBuf {
let signature = &cfg
.target_path
.iter()
.map(|t| t.file_stem().unwrap().to_str().unwrap())
.collect::<Vec<&str>>()
.join("-");
let exe_name: String = if sanitizer.is_empty() {
format!("ecfuzz_target.{}.out", signature)
} else {
format!("ecfuzz_target.{}-sanitized.{}.out", sanitizer, signature)
};
cfg.output_dir.as_path().join(exe_name)
}
/// convert raw profile data to an indexed file format
fn index_target_report(
cfg: &Arc<Config>,
raw_profile_filepath: &Path,
profile_filepath: &Path,
) -> Result<(), ()> {
let prof_merge_args = &[
"merge".to_string(),
"-sparse".to_string(),
//"--instr".to_string(),
raw_profile_filepath.display().to_string(),
"--output".to_string(),
profile_filepath.display().to_string(),
];
let prof_merge_result = Command::new(&cfg.llvm_profdata_path)
.args(prof_merge_args)
.output()
.expect("executing llvm-profdata");
if !prof_merge_result.status.success() {
eprintln!(
"Could not merge profile data. {} Args:\n{} {}\n",
String::from_utf8_lossy(&prof_merge_result.stderr),
&cfg.llvm_profdata_path.display(),
&prof_merge_args.join(" ")
);
return Err(());
}
Ok(())
}
/// count the number of code branches in the coverage file
pub fn count_branch_total(
cfg: &Arc<Config>,
sanitizer_idx: usize,
base_input: &[u8],
base_args: &[u8],
) -> Result<u64, Box<dyn std::error::Error>> {
let profraw = cfg.output_dir.join(format!(
"{}.profraw",
current().name().expect("getting thread name")
));
let profdata = cfg.output_dir.join(format!(
"{}.profdata",
current().name().expect("getting thread name")
));
let output = exec_target(cfg, 0, &profraw, base_input, base_args.to_owned());
if let ExecResult::Err(o) = output {
if o.status.code().is_none() {
panic!(
"could not get status code from target!\nINPUT {}\nARGS {}\nstderr: {:#?}",
String::from_utf8_lossy(base_input),
String::from_utf8_lossy(base_args),
String::from_utf8_lossy(&o.stderr),
);
}
}
index_target_report(cfg, &profraw, &profdata).unwrap();
remove_file(profraw).expect("removing raw profile data");
let prof_report = Command::new(&cfg.llvm_cov_path)
.args([
"export",
"--ignore-filename-regex=libfuzz-driver.cpp|fuzz.cpp|StandaloneFuzzTargetMain.c",
"--check-binary-ids",
"--num-threads=1",
"--skip-expansions",
"--skip-functions",
"--format=lcov",
"--instr-profile",
&profdata.display().to_string(),
"--object",
&compiled_executable_path(cfg, SANITIZERS[sanitizer_idx])
.display()
.to_string(),
])
.stdout(Stdio::piped())
.output()
.expect("executing llvm-cov");
if prof_report.stdout.is_empty() {
panic!(
"empty profdata: {:#?}\n{}",
profdata,
String::from_utf8_lossy(&prof_report.stderr)
);
}
remove_file(profdata).expect("removing coverage profile data");
let mut branches: BTreeSet<u128> = BTreeSet::new();
let mut file_index = 0;
let lines = prof_report.stdout.split(|byte| byte == &b'\n');
for line in lines {
if line.len() >= 2 && &line[0..2] == b"SF" {
file_index += 1;
continue;
}
if !(line.len() >= 4 && &line[0..4] == b"BRDA") {
continue;
}
let linenumber_block_expr_count = &line
.splitn(2, |l| l == &b':')
.last()
.unwrap()
.split(|l| l == &b',')
.collect::<Vec<_>>();
#[cfg(debug_assertions)]
assert!(linenumber_block_expr_count.len() == 4);
let line_num: u64 = String::from_utf8(linenumber_block_expr_count[0].to_vec())
.unwrap()
.parse::<u64>()
.unwrap();
let block: u64 = String::from_utf8(linenumber_block_expr_count[1].to_vec())
.unwrap()
.parse::<u64>()
.unwrap();
let branch0 = cantor_pair(line_num, block);
let branch = cantor_pair_u128(branch0.into(), file_index);
branches.insert(branch);
}
assert!(!branches.is_empty());
Ok(branches.len() as u64)
}
/// read coverage report file and create a BTreeSet from branches covered in the coverage file
pub fn check_report_coverage(
cfg: &Arc<Config>,
profile_filepath: &Path,
sanitizer_idx: usize,
) -> CoverageResult {
let mut prof_report = Command::new(&cfg.llvm_cov_path)
.args([
"export",
"--ignore-filename-regex=libfuzz-driver.cpp|fuzz.cpp",
"--check-binary-ids",
"--num-threads=1",
"--skip-expansions",
"--skip-functions",
"--format=lcov",
"--instr-profile",
&profile_filepath.display().to_string(),
"--object",
&compiled_executable_path(cfg, SANITIZERS[sanitizer_idx])
.display()
.to_string(),
])
.stdout(Stdio::piped())
.spawn()
.expect("executing llvm-cov");
let mut cov: BTreeSet<u128> = BTreeSet::new();
let mut file_index = 0;
let linereader = BufReader::new(prof_report.stdout.as_mut().unwrap());
for line_result in linereader.split(b'\n') {
let line = line_result.unwrap();
if line.len() >= 2 && &line[0..2] == b"SF" {
//println!("SOURCE FILE {}", String::from_utf8(line.to_vec()).unwrap());
file_index += 1;
continue;
}
if !(line.len() >= 4
&& &line[0..4] == b"BRDA"
&& match line.split(|l| l == &b',').last().unwrap() {
b"-" => false,
b"0" => false,
_count => true,
})
{
continue;
}
let linenumber_block_expr_count = &line
.splitn(2, |l| l == &b':')
.last()
.unwrap()
.split(|l| l == &b',')
.collect::<Vec<_>>();
#[cfg(debug_assertions)]
assert!(linenumber_block_expr_count.len() == 4);
let line_num: u64 = String::from_utf8(linenumber_block_expr_count[0].to_vec())
.unwrap()
.parse::<u64>()
.unwrap();
let block: u64 = String::from_utf8(linenumber_block_expr_count[1].to_vec())
.unwrap()
.parse::<u64>()
.unwrap();
let branch0 = cantor_pair(line_num, block);
let branch = cantor_pair_u128(branch0.into(), file_index);
cov.insert(branch);
}
prof_report.wait().unwrap();
#[cfg(debug_assertions)]
assert!(prof_report
.stdout
.unwrap()
.bytes()
.collect::<Vec<Result<u8, std::io::Error>>>()
.is_empty());
if cov.is_empty() {
return CoverageResult::Err();
}
CoverageResult::Ok(cov)
}
// move trial to Exec context ??
/// execute the target program with a new test input.
/// records profile data to the output directory.
pub fn trial(
cfg: &Arc<Config>,
args: &[u8],
stdin: &[u8],
hash_num: usize,
) -> (ExecResult<Output>, BTreeSet<u128>) {
let profraw = cfg.output_dir.join(format!(
"{}.profraw",
current().name().expect("getting thread name")
));
let profdata = cfg.output_dir.join(format!(
"{}.profdata",
current().name().expect("getting thread name")
));
let sanitizer_idx: usize = hash_num % SANITIZERS.len();
let output = exec_target(cfg, sanitizer_idx, &profraw, stdin, args.to_vec());
#[cfg(debug_assertions)]
assert!(profraw.exists()); // ensure profile data was generated
if index_target_report(cfg, &profraw, &profdata).is_err() {
eprintln!("Corrupted report file!");
return (ExecResult::CoverageError(), BTreeSet::new());
};
remove_file(&profraw).expect("removing raw profile data");
let cov = match check_report_coverage(cfg, &profdata, sanitizer_idx) {
CoverageResult::Ok(cov) => cov,
CoverageResult::Err() => BTreeSet::new(),
};
remove_file(&profdata).expect("removing coverage profile data");
(output, cov)
}
/// If the fuzz execution result for a given mutation yielded new coverage,
/// add it to the cov_corpus.
/// If the mutation yielded a crash with new coverage, add it to the crash_corpus.
/// Results will be logged to the output directory.
fn handle_trial_result(
cfg: &Arc<Config>,
result: &ExecResult<Output>,
corpus_entry: InputType,
cov_corpus: &mut CorpusType,
crash_corpus: &mut CorpusType,
i: &usize,
) {
let total_coverage = match cov_corpus {
CorpusType::Bytes(ref c) => &c.total_coverage,
CorpusType::Graph(ref c) => &c.total_coverage,
};
let crash_coverage = match crash_corpus {
CorpusType::Bytes(ref c) => &c.total_coverage,
CorpusType::Graph(ref c) => &c.total_coverage,
};
let input_coverage = match corpus_entry {
InputType::Bytes(ref c) => &c.coverage,
InputType::Graph(ref c) => &c.coverage,
};
match result {
// if the report contains new coverage, add to corpus as BytesInput
ExecResult::Ok(_output) | ExecResult::Err(_output)
if !total_coverage.is_superset(&input_coverage) =>
{
log_new_coverage(&corpus_entry, i, cfg.plaintext);
cov_corpus.add_and_distill_corpus(corpus_entry);
cov_corpus
.save(&cfg.output_dir, None)
.expect("saving corpus");
eprintln!("TODO: Implement graph corpus saving");
}
// if the input resulted in a crash covering new code branches,
// add it to the crash log
ExecResult::Err(output) if !crash_coverage.is_superset(&input_coverage) => {
log_crash_new(&corpus_entry, i, &output.stderr, cfg.plaintext);
crash_corpus.add_and_distill_corpus(corpus_entry);
crash_corpus
.save(&cfg.output_dir, None)
.expect("saving crashes");
eprintln!("TODO: Implement graph crash saving");
}
// warn if exited before logging coverage
ExecResult::Ok(_o) | ExecResult::Err(_o) if input_coverage.is_empty() => {
eprintln!(
"\nError: could not read coverage from input!\nResult type: {:?}",
result
);
}
ExecResult::NonTerminatingErr(pid) => {
panic!("\nRECEIVED KILL SIGNAL FROM WORKER (PID={})...", pid);
//remove_file(fname).expect("removing input mutation file after abort");
//profile_target.wait().unwrap();
/*
let pkill_result = Command::new("kill")
.arg(pid.to_string())
.output()
.unwrap()
.status
.code()
.unwrap();
*/
//eprintln!("PKILL RESULT {}", pkill.unwrap());
//eprintln!("DONE KILL PID={}...", pid);
//assert!(pkill_result == 0);
//eprintln!("\nNonTerminatingError: hopefully child process was killed already");
}
ExecResult::CoverageError() => {
eprintln!("unhandled CoverageError");
}
ExecResult::Ok(_o) => {
cov_corpus.check_matching_and_sort(corpus_entry);
}
ExecResult::Err(_o) => {
crash_corpus.check_matching_and_sort(corpus_entry);
}
}
}
pub fn prepare_mutation_bytes(cov_corpus: &CorpusType, engine: &mut Mutation) -> InputType {
match cov_corpus {
CorpusType::Bytes(c) => {
let idx = engine.hashfunc() % c.inputs.len();
engine.data = c.inputs[idx].data.clone();
engine.mutate();
InputType::Bytes(BytesInput {
data: engine.data.clone(),
args: Vec::new(), /* args will be populated by exec_target from cfg.run_args */
coverage: c.inputs[idx].coverage.clone(),
})
}
_ => panic!(),
}
}
pub fn prepare_mutation_graph(
cov_corpus: &mut CorpusType,
engine: &mut Mutation,
arg_grammar: &GraphTree,
grammar: &GraphTree,
) -> InputType {
let arg_permutation = arg_grammar.grammar_permutation(engine);
let stdin_permutation = grammar.grammar_permutation(engine);
match cov_corpus {
CorpusType::Graph(c) => {
if !c.inputs.is_empty() {
let idx = engine.hashfunc() % c.inputs.len();
let (p1, p2) =
grammar.swap_nodes(stdin_permutation, c.inputs[idx].encoding.clone(), engine);
InputType::Graph(GraphInput {
encoding: p1,
args: arg_permutation.clone(),
coverage: BTreeSet::new(),
//coverage: c.inputs[idx].coverage.clone(),
})
} else {
InputType::Graph(GraphInput {
encoding: stdin_permutation.clone(),
args: arg_permutation.clone(),
coverage: BTreeSet::new(),
})
}
}
_ => panic!(),
}
}
impl Exec {
/// compile and instrument the target
pub fn new(cfg: &Config) -> Self {
let exec = Exec {
cfg: cfg.clone().into(),
_private: (),
};
Exec::setup(&exec.cfg);
exec
}
/// compile and instrument target binaries (one for each sanitizer)
fn setup(cfg: &Config) {
// ensure cc is clang >= v14.0.0
let check_cc_ver = Command::new(&cfg.cc_path)
.arg("--version")
.output()
.expect("checking clang install");
let cc_ver = String::from_utf8_lossy(&check_cc_ver.stdout);
println!("{}", cc_ver);
let cc_ver_major = cc_ver.splitn(2, "version ").collect::<Vec<&str>>()[1]
.splitn(2, '.')
.collect::<Vec<&str>>()[0];
if !cc_ver_major.parse::<u64>().expect("parsing clang version") == 14 {
panic!("Requires CC version 14 or higher. Found {}", cc_ver);
}
let cflag_var = std::env::var("CFLAGS").unwrap_or(CFLAGS_DEFAULTS.to_string());
println!("CFLAGS={:?}", cflag_var);
let cflags: Vec<String> = cflag_var.split(' ').map(|s| s.to_string()).collect();
let ldflag_var = std::env::var("LDFLAGS").unwrap_or("".to_string());
println!("LDFLAGS={:?}", ldflag_var);
let ldflags: Vec<String> = ldflag_var.split(' ').map(|s| s.to_string()).collect();
std::fs::create_dir_all(&cfg.output_dir).expect("creating output dir");
println!("compiling...");
// check if target binary needs to be recompiled:
// get most recent modification timestamp from target source
let latest_modified: &std::time::SystemTime = &cfg
.target_path
.iter()
.map(|f| {
std::fs::metadata(f)
.expect("getting target source file metadata")
.modified()
.expect("reading modification time for target source file")
})
.reduce(max)
.expect("getting latest binary timestamp");
let mut handles: Vec<JoinHandle<_>> = Vec::new();
// use a variety of available sanitizers when possible
for sanitizer in SANITIZERS {
let cflags = cflags.clone();
let ldflags = ldflags.clone();
let cfg = cfg.clone();
let latest_modified = *latest_modified;
let compile_thread = Builder::new()
.name(format!(
"ecfuzz-compile-{}-",
if sanitizer == &"" {
"unsanitized"
} else {
sanitizer
}
))
.spawn(move || {
let sanitizer_arg = format!("-fsanitize={}", sanitizer);
let compiled_path = compiled_executable_path(&cfg, sanitizer);
if compiled_path.is_file()
&& std::fs::metadata(&compiled_path)
.unwrap()
.modified()
.unwrap()
> latest_modified
{
println!(
"target binary {} newer than target source, skipping compilation...",
compiled_path.display()
);
return;
}
let mut setup_args: Vec<String> = [
"-o",
&compiled_path.display().to_string(),
&sanitizer_arg,
"-g",
"-fsanitize-recover=all",
//"-D_FORTIFY_SOURCE=1",
"-fprofile-instr-generate",
"-fcoverage-mapping",
"-fno-optimize-sibling-calls",
"-fno-omit-frame-pointer",
#[cfg(target_arch = "aarch64")]
"-arch",
#[cfg(target_arch = "aarch64")]
"arm64",
]
.iter()
.map(|s| s.to_string())
.collect();
if setup_args.contains(&"-fsanitize=cfi".to_string()) {
setup_args.push("-flto=full".to_string());
setup_args.push("-fvisibility=hidden".to_string());
}
if cfg.plaintext {
setup_args.push("-fno-color-diagnostics".to_string());
} else {
setup_args.push("-fcolor-diagnostics".to_string());
setup_args.push("-fdiagnostics-color=always".to_string());
}
for flag in &cflags {
setup_args.push(flag.to_string());
}
for target in &cfg.target_path {
setup_args.push(target.display().to_string());
}
for flag in &ldflags {
setup_args.push(flag.to_string());
}
for link in &cfg.link_args {
setup_args.push(link.to_string());
}
if !cfg.include.is_empty() {
#[cfg(not(target_os = "macos"))]
setup_args.push("-Wl,--whole-archive".to_string());
#[cfg(target_os = "macos")]
setup_args.push("-all_load".to_string());
}
for inc in &cfg.include {
let mut include_string = inc.display().to_string();
include_string.insert_str(0, "-I");
setup_args.push(include_string);
}
if !cfg.include.is_empty() {
#[cfg(not(target_os = "macos"))]
setup_args.push("-Wl,--no-whole-archive".to_string());
}
println!(
"{} {}",
&cfg.cc_path.display().to_string(),
setup_args.join(" ")
);
let setup_result = Command::new(&cfg.cc_path)
.args(setup_args)
.output()
.expect("compiling instrumented target");
if !setup_result.stderr.is_empty()
&& !byte_index(b"error: ".as_ref(), &setup_result.stderr).is_empty()
{
panic!(
"compile failed:\n{}\n{}",
String::from_utf8(setup_result.stdout).unwrap(),
String::from_utf8(setup_result.stderr).unwrap(),
);
} else if !setup_result.stderr.is_empty() {
eprintln!(
"compiled with warnings:\n{}\n{}",
String::from_utf8(setup_result.stdout).unwrap(),
String::from_utf8(setup_result.stderr).unwrap(),
);
}
})
.expect("compiling target thread worker");
handles.push(compile_thread);
}
for handle in handles {
handle.join().expect("compiling target");
}
println!("done compiling");
}
/// main loop:
/// send input to target, read the coverage resulting from the input, and
/// update the corpus with inputs yielding new coverage
pub fn exec_loop(
&self,
cov_corpus: &mut CorpusType,
crash_corpus: &mut CorpusType,
engine: &mut Mutation,
args_graph: Option<Box<GraphTree>>,
stdin_graph: Option<Box<GraphTree>>,
) {
// worker thread pool
let (sender, receiver) = channel::<(usize, InputType, ExecResult<Output>)>();
let num_cpus: usize = available_parallelism().expect("checking CPU count").into();
let pool = ThreadPoolBuilder::new()
.thread_name(|f| format!("ecfuzz-worker-{}", f))
.num_threads(num_cpus)
.build()
.unwrap();
let base_args: Vec<u8> = self
.cfg
.run_args
.iter()
.map(|s| s.as_bytes().to_vec())
.map(|mut v| {
v.push(b' ');
v
})
.collect::<Vec<Vec<u8>>>()
.concat();
let branch_count = count_branch_total(&self.cfg, 0, &[], &base_args)
.expect("checking number of code branches in target executable");
assert!(num_cpus <= FUZZING_QUEUE_SIZE / 4);
assert!(self.cfg.iterations >= FUZZING_QUEUE_SIZE);
// store finished fuzzing jobs here in the order they finish
// this allows retrieval of jobs in a deterministic order
let mut finished_map: HashMap<usize, (InputType, ExecResult<Output>)> =
HashMap::with_capacity(FUZZING_QUEUE_SIZE);
//let mut input_history: Vec<Vec<u8>> = Vec::with_capacity(FUZZING_QUEUE_SIZE);
//let mut input_arg_history: Vec<Vec<u8>> = Vec::with_capacity(FUZZING_QUEUE_SIZE);
let mut input_history: Vec<InputType> = Vec::with_capacity(FUZZING_QUEUE_SIZE);
let timer_start = Instant::now();
let mut checkpoint = Instant::now();
let iter_count = self.cfg.iterations;
// set some color codes in the output
let colorcode_red = if !self.cfg.plaintext { "\x1b[31m" } else { "" };
let colorcode_normal = if !self.cfg.plaintext { "\x1b[0m" } else { "" };
for i in 0..iter_count + FUZZING_QUEUE_SIZE {
if i < iter_count - (FUZZING_QUEUE_SIZE) {
let cfg_clone = self.cfg.clone();
let mutation_trial = match cov_corpus {
CorpusType::Bytes(..) => prepare_mutation_bytes(cov_corpus, engine),
CorpusType::Graph(..) => prepare_mutation_graph(
cov_corpus,
engine,
args_graph.as_ref().unwrap(),
stdin_graph.as_ref().unwrap(),
),
};
input_history.push(mutation_trial.clone());
let (args, stdin) = match mutation_trial {
InputType::Bytes(ref m) => (m.args.clone(), m.data.clone()),
InputType::Graph(ref m) => (
args_graph.as_ref().unwrap().decode(&m.args),
stdin_graph.as_ref().unwrap().decode(&m.encoding),
),
};
let prev_cov = match mutation_trial {
InputType::Bytes(ref i) => i.coverage.clone(),
InputType::Graph(ref i) => i.coverage.clone(),
};
let hash_num = engine.hashfunc();
let sender = sender.clone();
pool.spawn_fifo(move || {
let (result, result_cov) = trial(&cfg_clone, &args, &stdin, hash_num);
let mut mutation = mutation_trial.clone();
if !result_cov.is_empty() {
match mutation {
InputType::Graph(ref mut i) => {
i.coverage = result_cov;
}
InputType::Bytes(ref mut i) => {
i.coverage = result_cov;
}
};
} else {
match mutation {
InputType::Graph(ref mut i) => {
i.coverage = prev_cov;
}
InputType::Bytes(ref mut i) => {
i.coverage = prev_cov;
}
};
}
sender
.send((i, mutation, result))
.expect("sending results from worker");
});
}
// start some jobs in the queue before retrieving any results
if i <= FUZZING_QUEUE_SIZE {
continue;
}
// fuzz jobs may be completed by parallel workers out of order
// add finished results to a HashMap, and retrieve the latest
// result from the map at an offset greater than the number of workers
if i <= self.cfg.iterations {
let (n, corpus_entry_unordered, result_unordered) = receiver
.recv()
.expect("receiving results from parallel worker");
finished_map.insert(n, (corpus_entry_unordered, result_unordered));
}
#[cfg(debug_assertions)]
assert!(finished_map.len() <= FUZZING_QUEUE_SIZE);
// allow some completed fuzz jobs to gather in the finished queue
if i < FUZZING_QUEUE_SIZE * 2 {
continue;
}
// get completed fuzz jobs starting at the earliest index
//.expect("retrieving execution result from finished map");
let (corpus_entry, result): (InputType, ExecResult<Output>) = finished_map
.remove(&(i - FUZZING_QUEUE_SIZE * 2))
.unwrap_or_else(|| {
let failed_input = input_history.remove(0);
let outpath1 = self.cfg.output_dir.join(PathBuf::from("FATAL.crash"));
let mut out1 = std::fs::File::create(&outpath1).unwrap();
let outpath2 = self.cfg.output_dir.join(PathBuf::from("FATAL.args"));
let mut out2 = std::fs::File::create(outpath2).unwrap();
match failed_input {
InputType::Bytes(ref i) => {
out1.write_all(&i.data).unwrap();
out2.write_all(&i.args).unwrap();
}
InputType::Graph(ref i) => {
out1.write_all(&stdin_graph.as_ref().unwrap().decode(&i.encoding))
.unwrap();
out2.write_all(&args_graph.as_ref().unwrap().decode(&i.args))
.unwrap();
}
}
let wait_timer = Instant::now();
while wait_timer.elapsed().as_millis() <= 2000 {
let retry_get_result = finished_map.remove(&(i - FUZZING_QUEUE_SIZE * 2));
if let Some((corpus_entry, result)) = retry_get_result {
return (corpus_entry, result);
}
}
eprintln!(
"\n{}Warning: killed non-terminating process! {} exec: {}\ndumping stdin to {}",
colorcode_red,
colorcode_normal,
i,
outpath1.display(),
);
/*
if std::env::var("ECFUZZ_ATTACH_DEBUGGER").is_ok() {
eprintln!("ATTACHING LLDB TO PID {}", pid.trim());
let mut attach_debug = Command::new ("/opt/bin/lldb")
.arg("-p")
.arg(pid.trim())
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.unwrap();
attach_debug.wait().unwrap();
} else {
eprintln!("Killing nonterminating process {}!\nTo attach a debugger instead, set ECFUZZ_ATTACH_DEBUGGER=1", pid.trim());
let pkill = Command::new("kill").arg(pid.trim()).output().unwrap().status.code();
pkill.unwrap();
}
*/
(failed_input.clone(), ExecResult::NonTerminatingErr(0))
});
// if the expected data isn't at the front of the queue, consider it
// to be abandoned by unresponsive process
let output_matches_expected = match (&corpus_entry, &input_history[0]) {
(InputType::Bytes(i), InputType::Bytes(h)) => i.data == h.data && i.args == h.args,
(InputType::Graph(i), InputType::Graph(h)) => {
i.encoding == h.encoding && i.args == h.args
}
_ => panic!(),
};
if !output_matches_expected {
if !(matches!(result, ExecResult::NonTerminatingErr(..))) {
match result {
ExecResult::Err(e) => {
panic!("Err(e) {}", String::from_utf8_lossy(&e.stderr))
}
ExecResult::Ok(e) => panic!("Ok(output) {}", e.status),
ExecResult::CoverageError() => panic!("coverage error",),
ExecResult::NonTerminatingErr(..) => panic!("impossible match case"),
}
};
} else {
input_history.remove(0);
};
// remove tracking for abandoned items
let abandoned_keys: Vec<usize> = finished_map
.keys()
.filter(|k| *k < &(i - (FUZZING_QUEUE_SIZE * 2)))
.copied()
.collect();
for k in abandoned_keys {
finished_map.remove(&k);
}
handle_trial_result(
&self.cfg,
&result,
corpus_entry,
cov_corpus,
crash_corpus,
&i,
);
// print some status info
if !self.cfg.plaintext && checkpoint.elapsed() > std::time::Duration::from_millis(125) {
log_status_msg(
&self.cfg,
cov_corpus,
crash_corpus,
branch_count,
i,
&timer_start,
);
checkpoint = Instant::now();
}
}
log_status_msg(
&self.cfg,
cov_corpus,
crash_corpus,
branch_count,
self.cfg.iterations,
&timer_start,
);
assert!(finished_map.is_empty());
assert!(receiver.try_recv().is_err());
}
}
/// log coverage increases to stdout
fn log_new_coverage(new: &InputType, i: &usize, plaintext: bool) {
let line_replacement = if !plaintext { "\r" } else { "" };
let colorcode_green = if !plaintext { "\x1b[32m" } else { "" };
let colorcode_normal = if !plaintext { "\x1b[0m" } else { "" };
println!(
"{}{}New coverage!{:>6}exec: {:<6}{:>57}{:?}\n",
line_replacement, colorcode_green, colorcode_normal, i, "", new
);
}
/// log new crashes to stderr
fn log_crash_new(new: &InputType, i: &usize, stderr: &[u8], plaintext: bool) {
let line_replacement = if !plaintext { "\r" } else { "\n" };
let colorcode_red = if !plaintext { "\x1b[31m" } else { "" };
let colorcode_normal = if !plaintext { "\x1b[0m" } else { "" };
eprintln!(