[llvm] r293117 - [libFuzzer] don't call GetPreviousInstructionPc on the hot path -- only when dumping the PCs

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 25 16:22:09 PST 2017


Author: kcc
Date: Wed Jan 25 18:22:08 2017
New Revision: 293117

URL: http://llvm.org/viewvc/llvm-project?rev=293117&view=rev
Log:
[libFuzzer] don't call GetPreviousInstructionPc on the hot path -- only when dumping the PCs

Modified:
    llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp

Modified: llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp?rev=293117&r1=293116&r2=293117&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerTracePC.cpp Wed Jan 25 18:22:08 2017
@@ -188,9 +188,19 @@ void TracePC::PrintCoverage() {
   }
 }
 
+inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
+  // TODO: this implementation is x86 only.
+  // see sanitizer_common GetPreviousInstructionPc for full implementation.
+  return PC - 1;
+}
+
 void TracePC::DumpCoverage() {
-  if (EF->__sanitizer_dump_coverage)
-    EF->__sanitizer_dump_coverage(PCs, GetNumPCs());
+  if (EF->__sanitizer_dump_coverage) {
+    std::vector<uintptr_t> PCsCopy(GetNumPCs());
+    for (size_t i = 0; i < GetNumPCs(); i++)
+      PCsCopy[i] = PCs[i] ? GetPreviousInstructionPc(PCs[i]) : 0;
+    EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size());
+  }
 }
 
 // Value profile.
@@ -244,18 +254,12 @@ void TracePC::HandleCmp(uintptr_t PC, T
   HandleValueProfile(Idx);
 }
 
-inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(void* pc) {
-  // TODO: this implementation is x86 only.
-  // see sanitizer_common GetPreviousInstructionPc for full implementation.
-  return reinterpret_cast<uintptr_t>(pc) - 1;
-}
-
 } // namespace fuzzer
 
 extern "C" {
 ATTRIBUTE_INTERFACE
 void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
-  uintptr_t PC = fuzzer::GetPreviousInstructionPc(__builtin_return_address(0));
+  uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
   fuzzer::TPC.HandleTrace(Guard, PC);
 }
 
@@ -266,31 +270,31 @@ void __sanitizer_cov_trace_pc_guard_init
 
 ATTRIBUTE_INTERFACE
 void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
-  uintptr_t PC = fuzzer::GetPreviousInstructionPc(__builtin_return_address(0));
+  uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
   fuzzer::TPC.HandleCallerCallee(PC, Callee);
 }
 
 ATTRIBUTE_INTERFACE
 void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
-  uintptr_t PC = fuzzer::GetPreviousInstructionPc(__builtin_return_address(0));
+  uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
   fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
 }
 
 ATTRIBUTE_INTERFACE
 void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
-  uintptr_t PC = fuzzer::GetPreviousInstructionPc(__builtin_return_address(0));
+  uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
   fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
 }
 
 ATTRIBUTE_INTERFACE
 void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
-  uintptr_t PC = fuzzer::GetPreviousInstructionPc(__builtin_return_address(0));
+  uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
   fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
 }
 
 ATTRIBUTE_INTERFACE
 void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
-  uintptr_t PC = fuzzer::GetPreviousInstructionPc(__builtin_return_address(0));
+  uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
   fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
 }
 
@@ -302,7 +306,7 @@ void __sanitizer_cov_trace_switch(uint64
   // Skip the most common and the most boring case.
   if (Vals[N - 1]  < 256 && Val < 256)
     return;
-  uintptr_t PC = fuzzer::GetPreviousInstructionPc(__builtin_return_address(0));
+  uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
   size_t i;
   uint64_t Token = 0;
   for (i = 0; i < N; i++) {
@@ -321,19 +325,19 @@ void __sanitizer_cov_trace_switch(uint64
 
 ATTRIBUTE_INTERFACE
 void __sanitizer_cov_trace_div4(uint32_t Val) {
-  uintptr_t PC = fuzzer::GetPreviousInstructionPc(__builtin_return_address(0));
+  uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
   fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0);
 }
 
 ATTRIBUTE_INTERFACE
 void __sanitizer_cov_trace_div8(uint64_t Val) {
-  uintptr_t PC = fuzzer::GetPreviousInstructionPc(__builtin_return_address(0));
+  uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
   fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
 }
 
 ATTRIBUTE_INTERFACE
 void __sanitizer_cov_trace_gep(uintptr_t Idx) {
-  uintptr_t PC = fuzzer::GetPreviousInstructionPc(__builtin_return_address(0));
+  uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
   fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0);
 }
 }  // extern "C"




More information about the llvm-commits mailing list