[llvm] r262102 - [libFuzzer] speedup path coverage handling

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 26 17:50:17 PST 2016


Author: kcc
Date: Fri Feb 26 19:50:16 2016
New Revision: 262102

URL: http://llvm.org/viewvc/llvm-project?rev=262102&view=rev
Log:
[libFuzzer] speedup path coverage handling

Modified:
    llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp
    llvm/trunk/lib/Fuzzer/test/fuzzer-trace-pc.test

Modified: llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp?rev=262102&r1=262101&r2=262102&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp Fri Feb 26 19:50:16 2016
@@ -10,50 +10,50 @@
 // This module implements __sanitizer_cov_trace_pc, a callback required
 // for -fsanitize-coverage=trace-pc instrumentation.
 //
-// Experimental and not yet tuned for performance.
 //===----------------------------------------------------------------------===//
 
 #include "FuzzerInternal.h"
 
 namespace fuzzer {
-static const size_t kMapSize = 65371; // Prime.
-static uint8_t CurMap[kMapSize];
-static uint8_t CombinedMap[kMapSize];
+static const size_t kMapSizeInBits        = 65371; // Prime.
+static const size_t kMapSizeInBitsAligned = 65536;  // 2^16
+static const size_t kBitsInWord =(sizeof(uintptr_t) * 8);
+static const size_t kMapSizeInWords = kMapSizeInBitsAligned / kBitsInWord;
+static uintptr_t CurrentMap[kMapSizeInWords] __attribute__((aligned(512)));
+static uintptr_t CombinedMap[kMapSizeInWords] __attribute__((aligned(512)));
 static size_t CombinedMapSize;
 static thread_local uintptr_t Prev;
 
 void PcMapResetCurrent() {
   if (Prev) {
     Prev = 0;
-    memset(CurMap, 0, sizeof(CurMap));
+    memset(CurrentMap, 0, sizeof(CurrentMap));
   }
 }
 
-// TODO: speed this up.
 void PcMapMergeCurrentToCombined() {
   if (!Prev) return;
   uintptr_t Res = 0;
-  for (size_t i = 0; i < kMapSize; i++) {
-    uint8_t p = (CombinedMap[i] |= CurMap[i]);
-    CurMap[i] = 0;
-    Res += p != 0;
-  }
+  for (size_t i = 0; i < kMapSizeInWords; i++)
+    Res += __builtin_popcountl(CombinedMap[i] |= CurrentMap[i]);
   CombinedMapSize = Res;
 }
 
 size_t PcMapCombinedSize() { return CombinedMapSize; }
 
-static void HandlePC(uintptr_t PC) {
+static void HandlePC(uint32_t PC) {
   // We take 12 bits of PC and mix it with the previous PCs.
-  uintptr_t Idx = (Prev << 5) ^ (PC & 4095);
-  CurMap[Idx % kMapSize] = 1;
-  Prev = Idx;
+  uintptr_t Next = (Prev << 5) ^ (PC & 4095);
+  uintptr_t Idx = Next % kMapSizeInBits;
+  uintptr_t WordIdx = Idx / kBitsInWord;
+  uintptr_t BitIdx  = Idx % kBitsInWord;
+  CurrentMap[WordIdx] |= 1UL << BitIdx;
+  Prev = Next;
 }
 
 } // namespace fuzzer
 
 extern "C" void __sanitizer_cov_trace_pc() {
-  fuzzer::HandlePC(reinterpret_cast<uintptr_t>(__builtin_return_address(0)));
+  fuzzer::HandlePC(static_cast<uint32_t>(
+      reinterpret_cast<uintptr_t>(__builtin_return_address(0))));
 }
-//uintptr_t __sanitizer_get_total_unique_coverage() { return 0; }
-//uintptr_t __sanitizer_get_number_of_counters() { return 0; }

Modified: llvm/trunk/lib/Fuzzer/test/fuzzer-trace-pc.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/test/fuzzer-trace-pc.test?rev=262102&r1=262101&r2=262102&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/test/fuzzer-trace-pc.test (original)
+++ llvm/trunk/lib/Fuzzer/test/fuzzer-trace-pc.test Fri Feb 26 19:50:16 2016
@@ -1,2 +1,3 @@
 CHECK: BINGO
 RUN: not LLVMFuzzer-FourIndependentBranchesTest-TracePC      -seed=1 -runs=1000000 2>&1 | FileCheck %s
+RUN: not LLVMFuzzer-FullCoverageSetTest-TracePC              -seed=1 -runs=1000000 2>&1 | FileCheck %s




More information about the llvm-commits mailing list