mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-07-25 13:24:46 +00:00
[sanitizer/coverage] Add AFL-style coverage counters (search heuristic for fuzzing).
Introduce -mllvm -sanitizer-coverage-8bit-counters=1 which adds imprecise thread-unfriendly 8-bit coverage counters. The run-time library maps these 8-bit counters to 8-bit bitsets in the same way AFL (http://lcamtuf.coredump.cx/afl/technical_details.txt) does: counter values are divided into 8 ranges and based on the counter value one of the bits in the bitset is set. The AFL ranges are used here: 1, 2, 3, 4-7, 8-15, 16-31, 32-127, 128+. These counters provide a search heuristic for single-threaded coverage-guided fuzzers, we do not expect them to be useful for other purposes. Depending on the value of -fsanitize-coverage=[123] flag, these counters will be added to the function entry blocks (=1), every basic block (=2), or every edge (=3). Use these counters as an optional search heuristic in the Fuzzer library. Add a test where this heuristic is critical. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@231166 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -158,6 +158,7 @@ int FuzzerDriver(int argc, char **argv, UserCallback Callback) {
|
||||
Options.DoCrossOver = Flags.cross_over;
|
||||
Options.MutateDepth = Flags.mutate_depth;
|
||||
Options.ExitOnFirst = Flags.exit_on_first;
|
||||
Options.UseCounters = Flags.use_counters;
|
||||
Options.UseFullCoverageSet = Flags.use_full_coverage_set;
|
||||
Options.UseCoveragePairs = Flags.use_coverage_pairs;
|
||||
Options.PreferSmallDuringInitialShuffle =
|
||||
|
@@ -32,6 +32,7 @@ FUZZER_FLAG(int, help, 0, "Print help.")
|
||||
FUZZER_FLAG(
|
||||
int, save_minimized_corpus, 0,
|
||||
"If 1, the minimized corpus is saved into the first input directory")
|
||||
FUZZER_FLAG(int, use_counters, 0, "Use coverage counters")
|
||||
FUZZER_FLAG(int, use_full_coverage_set, 0,
|
||||
"Experimental: Maximize the number of different full"
|
||||
" coverage sets as opposed to maximizing the total coverage."
|
||||
|
@@ -48,6 +48,7 @@ class Fuzzer {
|
||||
bool DoCrossOver = true;
|
||||
int MutateDepth = 5;
|
||||
bool ExitOnFirst = false;
|
||||
bool UseCounters = false;
|
||||
bool UseFullCoverageSet = false;
|
||||
bool UseCoveragePairs = false;
|
||||
int PreferSmallDuringInitialShuffle = -1;
|
||||
@@ -95,6 +96,15 @@ class Fuzzer {
|
||||
std::vector<Unit> Corpus;
|
||||
std::unordered_set<uintptr_t> FullCoverageSets;
|
||||
std::unordered_set<uint64_t> CoveragePairs;
|
||||
|
||||
// For UseCounters
|
||||
std::vector<uint8_t> CounterBitmap;
|
||||
size_t TotalBits() { // Slow. Call it only for printing stats.
|
||||
size_t Res = 0;
|
||||
for (auto x : CounterBitmap) Res += __builtin_popcount(x);
|
||||
return Res;
|
||||
}
|
||||
|
||||
UserCallback Callback;
|
||||
FuzzingOptions Options;
|
||||
system_clock::time_point ProcessStartTime = system_clock::now();
|
||||
|
@@ -138,17 +138,28 @@ size_t Fuzzer::RunOneMaximizeFullCoverageSet(const Unit &U) {
|
||||
}
|
||||
|
||||
size_t Fuzzer::RunOneMaximizeTotalCoverage(const Unit &U) {
|
||||
size_t NumCounters = __sanitizer_get_number_of_counters();
|
||||
if (Options.UseCounters) {
|
||||
CounterBitmap.resize(NumCounters);
|
||||
__sanitizer_update_counter_bitset_and_clear_counters(0);
|
||||
}
|
||||
size_t OldCoverage = __sanitizer_get_total_unique_coverage();
|
||||
Callback(U.data(), U.size());
|
||||
size_t NewCoverage = __sanitizer_get_total_unique_coverage();
|
||||
size_t NumNewBits = 0;
|
||||
if (Options.UseCounters)
|
||||
NumNewBits = __sanitizer_update_counter_bitset_and_clear_counters(
|
||||
CounterBitmap.data());
|
||||
|
||||
if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) && Options.Verbosity) {
|
||||
size_t Seconds = secondsSinceProcessStartUp();
|
||||
std::cerr
|
||||
<< "#" << TotalNumberOfRuns
|
||||
<< "\tcov: " << NewCoverage
|
||||
<< "\tbits: " << TotalBits()
|
||||
<< "\texec/s: " << (Seconds ? TotalNumberOfRuns / Seconds : 0) << "\n";
|
||||
}
|
||||
if (NewCoverage > OldCoverage)
|
||||
if (NewCoverage > OldCoverage || NumNewBits)
|
||||
return NewCoverage;
|
||||
return 0;
|
||||
}
|
||||
@@ -189,6 +200,7 @@ size_t Fuzzer::MutateAndTestOne(Unit *U) {
|
||||
if (Options.Verbosity) {
|
||||
std::cerr << "#" << TotalNumberOfRuns
|
||||
<< "\tNEW: " << NewCoverage
|
||||
<< " B: " << TotalBits()
|
||||
<< " L: " << U->size()
|
||||
<< " S: " << Corpus.size()
|
||||
<< " I: " << i
|
||||
|
@@ -5,6 +5,7 @@
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O0 -fsanitize-coverage=4")
|
||||
|
||||
set(Tests
|
||||
CounterTest
|
||||
FourIndependentBranchesTest
|
||||
FullCoverageSetTest
|
||||
InfiniteTest
|
||||
|
14
lib/Fuzzer/test/CounterTest.cpp
Normal file
14
lib/Fuzzer/test/CounterTest.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
// Test for a fuzzer: must find the case where a particular basic block is
|
||||
// executed many times.
|
||||
#include <iostream>
|
||||
|
||||
extern "C" void TestOneInput(const uint8_t *Data, size_t Size) {
|
||||
int Num = 0;
|
||||
for (size_t i = 0; i < Size; i++)
|
||||
if (Data[i] == 'A' + i)
|
||||
Num++;
|
||||
if (Num >= 4) {
|
||||
std::cerr << "BINGO!\n";
|
||||
exit(1);
|
||||
}
|
||||
}
|
@@ -17,3 +17,6 @@ FullCoverageSetTest: BINGO
|
||||
|
||||
RUN: not ./LLVMFuzzer-FourIndependentBranchesTest -timeout=15 -seed=1 -use_coverage_pairs=1 2>&1 | FileCheck %s --check-prefix=FourIndependentBranchesTest
|
||||
FourIndependentBranchesTest: BINGO
|
||||
|
||||
RUN: not ./LLVMFuzzer-CounterTest -use_counters=1 -max_len=6 -seed=1 -timeout=15 2>&1 | FileCheck %s --check-prefix=CounterTest
|
||||
CounterTest: BINGO
|
||||
|
Reference in New Issue
Block a user