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
|
/*
* OptiMin, an optimal fuzzing corpus minimizer.
*
* Author: Adrian Herrera
*/
#include <cstdint>
#include <cstdlib>
#include <vector>
#include <llvm/ADT/DenseSet.h>
#include <llvm/ADT/DenseMap.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/ADT/StringExtras.h>
#include <llvm/ADT/StringMap.h>
#include <llvm/Support/Chrono.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/Error.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/Path.h>
#include <llvm/Support/Program.h>
#include <llvm/Support/WithColor.h>
#include "EvalMaxSAT.h"
using namespace llvm;
namespace {
// -------------------------------------------------------------------------- //
// Classes
// -------------------------------------------------------------------------- //
/// Ensure seed weights default to 1
class Weight {
public:
Weight() : Weight(1){};
Weight(uint32_t V) : Value(V){};
operator unsigned() const {
return Value;
}
private:
const unsigned Value;
};
// -------------------------------------------------------------------------- //
// Typedefs
// -------------------------------------------------------------------------- //
/// AFL tuple (edge) ID
using AFLTupleID = uint32_t;
/// Pair of tuple ID and hit count
using AFLTuple = std::pair<AFLTupleID, /* Frequency */ unsigned>;
/// Coverage for a given seed file
using AFLCoverageVector = std::vector<AFLTuple>;
/// Map seed file paths to its coverage vector
using AFLCoverageMap = StringMap<AFLCoverageVector>;
/// Map seed file paths to a weight
using WeightsMap = StringMap<Weight>;
/// A seed identifier in the MaxSAT solver
using SeedID = int;
/// Associates seed identifiers to seed files
using MaxSATSeeds =
SmallVector<std::pair<SeedID, /* Seed file */ std::string>, 0>;
/// Set of literal identifiers
using MaxSATSeedSet = DenseSet<SeedID>;
/// Maps tuple IDs to the literal identifiers that "cover" that tuple
using MaxSATCoverageMap = DenseMap<AFLTupleID, MaxSATSeedSet>;
// -------------------------------------------------------------------------- //
// Global variables
// -------------------------------------------------------------------------- //
// This is based on the human class count in `count_class_human[256]` in
// `afl-showmap.c`
static constexpr uint32_t MAX_EDGE_FREQ = 8;
// The maximum number of failures allowed when parsing a weights file
static constexpr unsigned MAX_WEIGHT_FAILURES = 5;
static sys::TimePoint<> StartTime, EndTime;
static std::chrono::seconds Duration;
static std::string ShowmapPath;
static bool TargetArgsHasAtAt = false;
static bool KeepTraces = false;
static bool SkipBinCheck = false;
static const auto ErrMsg = [] {
return WithColor(errs(), raw_ostream::RED, /*Bold=*/true) << "[-] ";
};
static const auto WarnMsg = [] {
return WithColor(errs(), raw_ostream::MAGENTA, /*Bold=*/true) << "[-] ";
};
static const auto SuccMsg = [] {
return WithColor(outs(), raw_ostream::GREEN, /*Bold=*/true) << "[+] ";
};
static const auto StatMsg = [] {
return WithColor(outs(), raw_ostream::BLUE, /*Bold=*/true) << "[*] ";
};
static cl::opt<std::string> InputDir("i", cl::desc("Input directory"),
cl::value_desc("dir"), cl::Required);
static cl::opt<std::string> OutputDir("o", cl::desc("Output directory"),
cl::value_desc("dir"), cl::Required);
static cl::opt<bool> EdgesOnly("f", cl::desc("Include edge hit counts"),
cl::init(true));
static cl::opt<std::string> WeightsFile("w", cl::desc("Weights file"),
cl::value_desc("csv"));
static cl::opt<std::string> TargetProg(cl::Positional,
cl::desc("<target program>"),
cl::Required);
static cl::list<std::string> TargetArgs(cl::ConsumeAfter,
cl::desc("[target args...]"));
static cl::opt<std::string> MemLimit(
"m", cl::desc("Memory limit for child process (default=none)"),
cl::value_desc("megs"), cl::init("none"));
static cl::opt<std::string> Timeout(
"t", cl::desc("Run time limit for child process (default=5000)"),
cl::value_desc("msec"), cl::init("5000"));
static cl::opt<bool> CrashMode(
"C", cl::desc("Keep crashing inputs, reject everything else"));
static cl::opt<bool> FridaMode(
"O", cl::desc("Use binary-only instrumentation (FRIDA mode)"));
static cl::opt<bool> QemuMode(
"Q", cl::desc("Use binary-only instrumentation (QEMU mode)"));
static cl::opt<bool> UnicornMode(
"U", cl::desc("Use unicorn-based instrumentation (unicorn mode)"));
} // anonymous namespace
// -------------------------------------------------------------------------- //
// Helper functions
// -------------------------------------------------------------------------- //
static void GetWeights(const MemoryBuffer &MB, WeightsMap &Weights) {
SmallVector<StringRef, 0> Lines;
MB.getBuffer().trim().split(Lines, '\n');
unsigned FailureCount = 0;
unsigned Weight = 0;
for (const auto &Line : Lines) {
const auto &[Seed, WeightStr] = Line.split(',');
if (to_integer(WeightStr, Weight, 10)) {
Weights.try_emplace(Seed, Weight);
} else {
if (FailureCount >= MAX_WEIGHT_FAILURES) {
ErrMsg() << "Too many failures. Aborting\n";
std::exit(1);
}
WarnMsg() << "Failed to read weight for '" << Seed << "'. Skipping...\n";
FailureCount++;
}
}
}
static std::error_code readCov(const StringRef Trace, AFLCoverageVector &Cov) {
const auto CovOrErr = MemoryBuffer::getFile(Trace);
if (const auto EC = CovOrErr.getError()) return EC;
SmallVector<StringRef, 0> Lines;
CovOrErr.get()->getBuffer().trim().split(Lines, '\n');
AFLTupleID Edge = 0;
unsigned Freq = 0;
for (const auto &Line : Lines) {
const auto &[EdgeStr, FreqStr] = Line.split(':');
to_integer(EdgeStr, Edge, 10);
to_integer(FreqStr, Freq, 10);
Cov.push_back({Edge, Freq});
}
return std::error_code();
}
static Error runShowmap(AFLCoverageMap &CovMap, const StringRef Input,
bool BinCheck = false) {
const bool InputIsFile = !sys::fs::is_directory(Input);
Optional<StringRef> Redirects[] = {None, None, None};
SmallString<32> TraceDir{OutputDir};
sys::path::append(TraceDir, ".traces");
SmallString<32> Output{TraceDir};
SmallString<32> StdinFile{TraceDir};
// ------------------------------------------------------------------------ //
// Prepare afl-showmap arguments
//
// If the given input is a file, then feed this directly into stdin.
// Otherwise, if it is a directory, specify this on the afl-showmap command
// line.
// ------------------------------------------------------------------------ //
SmallVector<StringRef, 12> ShowmapArgs{ShowmapPath, "-q",
"-m", MemLimit,
"-t", Timeout};
if (InputIsFile) {
StdinFile = Input;
sys::path::append(Output,
BinCheck ? ".run_test" : sys::path::filename(Input));
} else {
sys::path::append(StdinFile, ".cur_input");
ShowmapArgs.append({"-i", Input});
}
if (TargetArgsHasAtAt) {
ShowmapArgs.append({"-A", StdinFile});
Redirects[/* stdin */ 0] = "/dev/null";
} else if (InputIsFile) {
Redirects[/* stdin */ 0] = Input;
}
if (FridaMode) ShowmapArgs.push_back("-O");
if (QemuMode) ShowmapArgs.push_back("-Q");
if (UnicornMode) ShowmapArgs.push_back("-U");
ShowmapArgs.append({"-o", Output, "--", TargetProg});
ShowmapArgs.append(TargetArgs.begin(), TargetArgs.end());
// ------------------------------------------------------------------------ //
// Run afl-showmap
// ------------------------------------------------------------------------ //
const int RC = sys::ExecuteAndWait(ShowmapPath, ShowmapArgs,
/*env=*/None, Redirects);
if (RC && !CrashMode) {
ErrMsg() << "Exit code " << RC << " != 0 received from afl-showmap\n";
return createStringError(inconvertibleErrorCode(), "afl-showmap failed");
}
// ------------------------------------------------------------------------ //
// Parse afl-showmap output
// ------------------------------------------------------------------------ //
AFLCoverageVector Cov;
std::error_code EC;
sys::fs::file_status Status;
if (InputIsFile) {
// Read a single output coverage file
if ((EC = readCov(Output, Cov))) {
sys::fs::remove(Output);
return errorCodeToError(EC);
}
CovMap.try_emplace(sys::path::filename(Input), Cov);
if (!KeepTraces) sys::fs::remove(Output);
} else {
// Read a directory of output coverage files
for (sys::fs::recursive_directory_iterator Dir(TraceDir, EC), DirEnd;
Dir != DirEnd && !EC; Dir.increment(EC)) {
if (EC) return errorCodeToError(EC);
const auto &Path = Dir->path();
if ((EC = sys::fs::status(Path, Status))) return errorCodeToError(EC);
switch (Status.type()) {
case sys::fs::file_type::regular_file:
case sys::fs::file_type::symlink_file:
case sys::fs::file_type::type_unknown:
Cov.clear();
if ((EC = readCov(Path, Cov))) {
sys::fs::remove(Path);
return errorCodeToError(EC);
}
CovMap.try_emplace(sys::path::filename(Path), Cov);
default:
// Ignore
break;
}
}
if (!KeepTraces) sys::fs::remove_directories(TraceDir);
}
return Error::success();
}
static inline void StartTimer() {
StartTime = std::chrono::system_clock::now();
}
static inline void EndTimer() {
EndTime = std::chrono::system_clock::now();
Duration =
std::chrono::duration_cast<std::chrono::seconds>(EndTime - StartTime);
SuccMsg() << " Completed in " << Duration.count() << " s\n";
}
// -------------------------------------------------------------------------- //
// Main function
// -------------------------------------------------------------------------- //
int main(int argc, char *argv[]) {
WeightsMap Weights;
std::error_code EC;
// ------------------------------------------------------------------------ //
// Parse command-line options and environment variables
//
// Also check the target arguments, as this determines how we run afl-showmap.
// ------------------------------------------------------------------------ //
cl::ParseCommandLineOptions(argc, argv, "Optimal corpus minimizer");
KeepTraces = !!std::getenv("AFL_KEEP_TRACES");
SkipBinCheck = !!std::getenv("AFL_SKIP_BIN_CHECK");
const auto AFLPath = std::getenv("AFL_PATH");
if (CrashMode) ::setenv("AFL_CMIN_CRASHES_ONLY", "1", /*overwrite=*/true);
for (const auto &Arg : TargetArgs)
if (Arg == "@@") TargetArgsHasAtAt = true;
// ------------------------------------------------------------------------ //
// Find afl-showmap
// ------------------------------------------------------------------------ //
SmallVector<StringRef, 16> EnvPaths;
if (const char *PathEnv = std::getenv("PATH"))
SplitString(PathEnv, EnvPaths, ":");
if (AFLPath) EnvPaths.push_back(AFLPath);
const auto ShowmapOrErr = sys::findProgramByName("afl-showmap", EnvPaths);
if (ShowmapOrErr.getError()) {
ErrMsg() << "Failed to find afl-showmap. Check your PATH\n";
return 1;
}
ShowmapPath = *ShowmapOrErr;
// ------------------------------------------------------------------------ //
// Parse weights
//
// Weights are stored in CSV file mapping a seed file name to an integer
// greater than zero.
// ------------------------------------------------------------------------ //
if (WeightsFile != "") {
StatMsg() << "Reading weights from '" << WeightsFile << "'...\n";
StartTimer();
const auto WeightsOrErr = MemoryBuffer::getFile(WeightsFile);
if ((EC = WeightsOrErr.getError())) {
ErrMsg() << "Failed to read weights from '" << WeightsFile
<< "': " << EC.message() << '\n';
return 1;
}
GetWeights(*WeightsOrErr.get(), Weights);
EndTimer();
}
// ------------------------------------------------------------------------ //
// Traverse input directory
//
// Find the seed files inside this directory (and subdirectories).
// ------------------------------------------------------------------------ //
StatMsg() << "Locating seeds in '" << InputDir << "'...\n";
StartTimer();
bool IsDirResult;
if ((EC = sys::fs::is_directory(InputDir, IsDirResult))) {
ErrMsg() << "Invalid input directory '" << InputDir << "': " << EC.message()
<< '\n';
return 1;
}
sys::fs::file_status Status;
StringMap<std::string> SeedFiles;
for (sys::fs::recursive_directory_iterator Dir(InputDir, EC), DirEnd;
Dir != DirEnd && !EC; Dir.increment(EC)) {
if (EC) {
ErrMsg() << "Failed to traverse input directory '" << InputDir
<< "': " << EC.message() << '\n';
return 1;
}
const auto &Path = Dir->path();
if ((EC = sys::fs::status(Path, Status))) {
ErrMsg() << "Failed to access '" << Path << "': " << EC.message() << '\n';
return 1;
}
switch (Status.type()) {
case sys::fs::file_type::regular_file:
case sys::fs::file_type::symlink_file:
case sys::fs::file_type::type_unknown:
SeedFiles.try_emplace(sys::path::filename(Path),
sys::path::parent_path(Path));
default:
/* Ignore */
break;
}
}
EndTimer();
if (SeedFiles.empty()) {
ErrMsg() << "Failed to find any seed files in '" << InputDir << "'\n";
return 1;
}
// ------------------------------------------------------------------------ //
// Setup output directory
// ------------------------------------------------------------------------ //
SmallString<32> TraceDir{OutputDir};
sys::path::append(TraceDir, ".traces");
if ((EC = sys::fs::remove_directories(TraceDir))) {
ErrMsg() << "Failed to remove existing trace directory in '" << OutputDir
<< "': " << EC.message() << '\n';
return 1;
}
if ((EC = sys::fs::create_directories(TraceDir))) {
ErrMsg() << "Failed to create output directory '" << OutputDir
<< "': " << EC.message() << '\n';
return 1;
}
// ------------------------------------------------------------------------ //
// Test the target binary
// ------------------------------------------------------------------------ //
AFLCoverageMap CovMap;
if (!SkipBinCheck) {
const auto It = SeedFiles.begin();
SmallString<32> TestSeed{It->second};
sys::path::append(TestSeed, It->first());
StatMsg() << "Testing the target binary with '" << TestSeed << "`...\n";
StartTimer();
if (auto Err = runShowmap(CovMap, TestSeed, /*BinCheck=*/true)) {
ErrMsg() << "No instrumentation output detected \n";
return 1;
}
EndTimer();
SuccMsg() << "OK, " << CovMap.begin()->second.size()
<< " tuples recorded\n";
}
// ------------------------------------------------------------------------ //
// Generate seed coverage
//
// Iterate over the corpus directory, which should contain seed files. Execute
// these seeds in the target program to generate coverage information, and
// then store this coverage information in the appropriate data structures.
// ------------------------------------------------------------------------ //
StatMsg() << "Running afl-showmap on " << SeedFiles.size() << " seeds...\n";
StartTimer();
MaxSATSeeds SeedVars;
MaxSATCoverageMap SeedCoverage;
EvalMaxSAT Solver(/*nbMinimizeThread=*/0);
CovMap.clear();
if (auto Err = runShowmap(CovMap, InputDir)) {
ErrMsg() << "Failed to generate coverage: " << Err << '\n';
return 1;
}
for (const auto &SeedCov : CovMap) {
// Create a variable to represent the seed
const SeedID Var = Solver.newVar();
SeedVars.emplace_back(Var, SeedCov.first());
// Record the set of seeds that cover a particular edge
for (auto &[Edge, Freq] : SeedCov.second) {
if (EdgesOnly) {
// Ignore edge frequency
SeedCoverage[Edge].insert(Var);
} else {
// Executing edge `E` `N` times means that it was executed `N - 1` times
for (unsigned I = 0; I < Freq; ++I)
SeedCoverage[MAX_EDGE_FREQ * Edge + I].insert(Var);
}
}
}
EndTimer();
// ------------------------------------------------------------------------ //
// Set the hard and soft constraints in the solver
// ------------------------------------------------------------------------ //
StatMsg() << "Generating constraints...\n";
StartTimer();
size_t SeedCount = 0;
// Ensure that at least one seed is selected that covers a particular edge
// (hard constraint)
std::vector<SeedID> Clauses;
for (const auto &[_, Seeds] : SeedCoverage) {
if (Seeds.empty()) continue;
Clauses.clear();
for (const auto &Seed : Seeds)
Clauses.push_back(Seed);
Solver.addClause(Clauses);
}
// Select the minimum number of seeds that cover a particular set of edges
// (soft constraint)
for (const auto &[Var, Seed] : SeedVars)
Solver.addWeightedClause({-Var}, Weights[sys::path::filename(Seed)]);
EndTimer();
// ------------------------------------------------------------------------ //
// Generate a solution
// ------------------------------------------------------------------------ //
StatMsg() << "Solving...\n";
StartTimer();
const bool Solved = Solver.solve();
EndTimer();
// ------------------------------------------------------------------------ //
// Save the solution
//
// This will copy the selected seeds to the given output directory.
// ------------------------------------------------------------------------ //
SmallVector<StringRef, 64> Solution;
SmallString<32> InputSeed, OutputSeed;
if (Solved) {
for (const auto &[Var, Seed] : SeedVars)
if (Solver.getValue(Var) > 0) Solution.push_back(Seed);
} else {
ErrMsg() << "Failed to find an optimal solution for '" << InputDir << "'\n";
return 1;
}
StatMsg() << "Copying " << Solution.size() << " seeds to '" << OutputDir
<< "'...\n";
StartTimer();
SeedCount = 0;
for (const auto &Seed : Solution) {
InputSeed = SeedFiles[Seed];
sys::path::append(InputSeed, Seed);
OutputSeed = OutputDir;
sys::path::append(OutputSeed, Seed);
if ((EC = sys::fs::copy_file(InputSeed, OutputSeed))) {
ErrMsg() << "Failed to copy '" << Seed << "' to '" << OutputDir
<< "': " << EC.message() << '\n';
return 1;
}
}
EndTimer();
SuccMsg() << "Done!\n";
return 0;
}
|