1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 04:32:44 +01:00

[libFuzzer] experimental support for Clang's coverage (fprofile-instr-generate), Linux-only

llvm-svn: 310771
This commit is contained in:
Kostya Serebryany 2017-08-11 23:03:22 +00:00
parent 03422abc5b
commit a2bcec7132
6 changed files with 99 additions and 15 deletions

View File

@ -31,6 +31,7 @@ endif()
if (LIBFUZZER_ENABLE) if (LIBFUZZER_ENABLE)
add_library(LLVMFuzzerNoMainObjects OBJECT add_library(LLVMFuzzerNoMainObjects OBJECT
FuzzerClangCounters.cpp
FuzzerCrossOver.cpp FuzzerCrossOver.cpp
FuzzerDriver.cpp FuzzerDriver.cpp
FuzzerExtFunctionsDlsym.cpp FuzzerExtFunctionsDlsym.cpp

View File

@ -0,0 +1,49 @@
//===- FuzzerExtraCounters.cpp - Extra coverage counters ------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Coverage counters from Clang's SourceBasedCodeCoverage.
//===----------------------------------------------------------------------===//
// Support for SourceBasedCodeCoverage is experimental:
// * Works only for the main binary, not DSOs yet.
// * Works only on Linux.
// * Does not implement print_pcs/print_coverage yet.
// * Is not fully evaluated for performance and sensitivity.
// We expect large performance drop due to 64-bit counters,
// and *maybe* better sensitivity due to more fine-grained counters.
// Preliminary comparison on a single benchmark (RE2) shows
// a bit worse sensitivity though.
#include "FuzzerDefs.h"
#if LIBFUZZER_LINUX
__attribute__((weak)) extern uint64_t __start___llvm_prf_cnts;
__attribute__((weak)) extern uint64_t __stop___llvm_prf_cnts;
namespace fuzzer {
uint64_t *ClangCountersBegin() { return &__start___llvm_prf_cnts; }
uint64_t *ClangCountersEnd() { return &__stop___llvm_prf_cnts; }
} // namespace fuzzer
#else
// TODO: Implement on Mac (if the data shows it's worth it).
//__attribute__((visibility("hidden")))
//extern uint64_t CountersStart __asm("section$start$__DATA$__llvm_prf_cnts");
//__attribute__((visibility("hidden")))
//extern uint64_t CountersEnd __asm("section$end$__DATA$__llvm_prf_cnts");
namespace fuzzer {
uint64_t *ClangCountersBegin() { return nullptr; }
uint64_t *ClangCountersEnd() { return nullptr; }
} // namespace fuzzer
#endif
namespace fuzzer {
ATTRIBUTE_NO_SANITIZE_ALL
void ClearClangCounters() { // hand-written memset, don't asan-ify.
for (auto P = ClangCountersBegin(); P < ClangCountersEnd(); P++)
*P = 0;
}
}

View File

@ -123,6 +123,10 @@ uint8_t *ExtraCountersBegin();
uint8_t *ExtraCountersEnd(); uint8_t *ExtraCountersEnd();
void ClearExtraCounters(); void ClearExtraCounters();
uint64_t *ClangCountersBegin();
uint64_t *ClangCountersEnd();
void ClearClangCounters();
} // namespace fuzzer } // namespace fuzzer
#endif // LLVM_FUZZER_DEFS_H #endif // LLVM_FUZZER_DEFS_H

View File

@ -126,6 +126,8 @@ void TracePC::PrintModuleInfo() {
_Exit(1); _Exit(1);
} }
} }
if (size_t NumClangCounters = ClangCountersEnd() - ClangCountersBegin())
Printf("INFO: %zd Clang Coverage Counters\n", NumClangCounters);
} }
ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ALL
@ -137,13 +139,12 @@ void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
} }
void TracePC::UpdateObservedPCs() { void TracePC::UpdateObservedPCs() {
if (NumPCsInPCTables) {
auto Observe = [&](uintptr_t PC) { auto Observe = [&](uintptr_t PC) {
bool Inserted = ObservedPCs.insert(PC).second; bool Inserted = ObservedPCs.insert(PC).second;
if (Inserted && DoPrintNewPCs) if (Inserted && DoPrintNewPCs)
PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PC + 1); PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PC + 1);
}; };
if (NumPCsInPCTables) {
if (NumInline8bitCounters == NumPCsInPCTables) { if (NumInline8bitCounters == NumPCsInPCTables) {
for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
uint8_t *Beg = ModuleCounters[i].Start; uint8_t *Beg = ModuleCounters[i].Start;
@ -167,6 +168,13 @@ void TracePC::UpdateObservedPCs() {
} }
} }
} }
if (size_t NumClangCounters =
ClangCountersEnd() - ClangCountersBegin()) {
auto P = ClangCountersBegin();
for (size_t Idx = 0; Idx < NumClangCounters; Idx++)
if (P[Idx])
Observe((uintptr_t)Idx);
}
} }
inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) { inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {

View File

@ -91,6 +91,7 @@ class TracePC {
memset(Counters(), 0, GetNumPCs()); memset(Counters(), 0, GetNumPCs());
ClearExtraCounters(); ClearExtraCounters();
ClearInlineCounters(); ClearInlineCounters();
ClearClangCounters();
} }
void ClearInlineCounters(); void ClearInlineCounters();
@ -196,14 +197,9 @@ void ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End,
Handle8bitCounter(FirstFeature, P - Begin, V); Handle8bitCounter(FirstFeature, P - Begin, V);
} }
template <class Callback> // bool Callback(size_t Feature) // Given a non-zero Counters returns a number in [0,7].
ATTRIBUTE_NO_SANITIZE_ADDRESS template<class T>
__attribute__((noinline)) unsigned CounterToFeature(T Counter) {
void TracePC::CollectFeatures(Callback HandleFeature) const {
uint8_t *Counters = this->Counters();
size_t N = GetNumPCs();
auto Handle8bitCounter = [&](size_t FirstFeature,
size_t Idx, uint8_t Counter) {
assert(Counter); assert(Counter);
unsigned Bit = 0; unsigned Bit = 0;
/**/ if (Counter >= 128) Bit = 7; /**/ if (Counter >= 128) Bit = 7;
@ -213,7 +209,18 @@ void TracePC::CollectFeatures(Callback HandleFeature) const {
else if (Counter >= 4) Bit = 3; else if (Counter >= 4) Bit = 3;
else if (Counter >= 3) Bit = 2; else if (Counter >= 3) Bit = 2;
else if (Counter >= 2) Bit = 1; else if (Counter >= 2) Bit = 1;
HandleFeature(FirstFeature + Idx * 8 + Bit); return Bit;
}
template <class Callback> // bool Callback(size_t Feature)
ATTRIBUTE_NO_SANITIZE_ADDRESS
__attribute__((noinline))
void TracePC::CollectFeatures(Callback HandleFeature) const {
uint8_t *Counters = this->Counters();
size_t N = GetNumPCs();
auto Handle8bitCounter = [&](size_t FirstFeature,
size_t Idx, uint8_t Counter) {
HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Counter));
}; };
size_t FirstFeature = 0; size_t FirstFeature = 0;
@ -231,6 +238,14 @@ void TracePC::CollectFeatures(Callback HandleFeature) const {
} }
} }
if (size_t NumClangCounters = ClangCountersEnd() - ClangCountersBegin()) {
auto P = ClangCountersBegin();
for (size_t Idx = 0; Idx < NumClangCounters; Idx++)
if (auto Cnt = P[Idx])
HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Cnt));
FirstFeature += NumClangCounters;
}
ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(), FirstFeature, ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(), FirstFeature,
Handle8bitCounter); Handle8bitCounter);
FirstFeature += (ExtraCountersEnd() - ExtraCountersBegin()) * 8; FirstFeature += (ExtraCountersEnd() - ExtraCountersBegin()) * 8;

View File

@ -0,0 +1,7 @@
# Test libFuzzer + -fprofile-instr-generate
REQUIRES: linux
RUN: %cpp_compiler %S/SimpleTest.cpp -fsanitize-coverage=0 -fprofile-instr-generate -o %t-SimpleTest-fprofile-instr-generate
CHECK-NOT: INFO: Loaded 1 modules
CHECK: INFO: {{.*}} Clang Coverage Counters
CHECK: BINGO
RUN: not %t-SimpleTest-fprofile-instr-generate -runs=1000000 -seed=1 2>&1 | FileCheck %s