diff options
Diffstat (limited to 'lib/Fuzzer/FuzzerTracePC.cpp')
-rw-r--r-- | lib/Fuzzer/FuzzerTracePC.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/Fuzzer/FuzzerTracePC.cpp b/lib/Fuzzer/FuzzerTracePC.cpp new file mode 100644 index 0000000000000..46c43d0c17f08 --- /dev/null +++ b/lib/Fuzzer/FuzzerTracePC.cpp @@ -0,0 +1,71 @@ +//===- FuzzerTracePC.cpp - PC tracing--------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// Trace PCs. +// This module implements __sanitizer_cov_trace_pc, a callback required +// for -fsanitize-coverage=trace-pc instrumentation. +// +//===----------------------------------------------------------------------===// + +#include "FuzzerInternal.h" + +namespace fuzzer { + +void PcCoverageMap::Reset() { memset(Map, 0, sizeof(Map)); } + +void PcCoverageMap::Update(uintptr_t Addr) { + uintptr_t Idx = Addr % kMapSizeInBits; + uintptr_t WordIdx = Idx / kBitsInWord; + uintptr_t BitIdx = Idx % kBitsInWord; + Map[WordIdx] |= 1UL << BitIdx; +} + +size_t PcCoverageMap::MergeFrom(const PcCoverageMap &Other) { + uintptr_t Res = 0; + for (size_t i = 0; i < kMapSizeInWords; i++) + Res += __builtin_popcountl(Map[i] |= Other.Map[i]); + return Res; +} + +static PcCoverageMap CurrentMap; +static thread_local uintptr_t Prev; + +void PcMapResetCurrent() { + if (Prev) { + Prev = 0; + CurrentMap.Reset(); + } +} + +size_t PcMapMergeInto(PcCoverageMap *Map) { + if (!Prev) + return 0; + return Map->MergeFrom(CurrentMap); +} + +static void HandlePC(uint32_t PC) { + // We take 12 bits of PC and mix it with the previous PCs. + uintptr_t Next = (Prev << 5) ^ (PC & 4095); + CurrentMap.Update(Next); + Prev = Next; +} + +} // namespace fuzzer + +extern "C" { +void __sanitizer_cov_trace_pc() { + fuzzer::HandlePC(static_cast<uint32_t>( + reinterpret_cast<uintptr_t>(__builtin_return_address(0)))); +} + +void __sanitizer_cov_trace_pc_indir(int *) { + // Stub to allow linking with code built with + // -fsanitize=indirect-calls,trace-pc. + // This isn't used currently. +} +} |