[compiler-rt] r344104 - [libFuzzer] Generalize the code for getting the previous offset for different architectures

George Karpenkov via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 9 17:57:44 PDT 2018


Author: george.karpenkov
Date: Tue Oct  9 17:57:44 2018
New Revision: 344104

URL: http://llvm.org/viewvc/llvm-project?rev=344104&view=rev
Log:
[libFuzzer] Generalize the code for getting the previous offset for different architectures

Without this change, tests in coverage.test and dump_coverage.test are
failing on non-x86_64 platforms.
The diff is copied from sanitizer_common library, an alternative would
be to link it together with libFuzzer.

Differential Revision: https://reviews.llvm.org/D53040

Modified:
    compiler-rt/trunk/lib/fuzzer/FuzzerTracePC.cpp
    compiler-rt/trunk/test/fuzzer/coverage.test
    compiler-rt/trunk/test/fuzzer/dump_coverage.test
    compiler-rt/trunk/test/fuzzer/handle-unstable.test

Modified: compiler-rt/trunk/lib/fuzzer/FuzzerTracePC.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/fuzzer/FuzzerTracePC.cpp?rev=344104&r1=344103&r2=344104&view=diff
==============================================================================
--- compiler-rt/trunk/lib/fuzzer/FuzzerTracePC.cpp (original)
+++ compiler-rt/trunk/lib/fuzzer/FuzzerTracePC.cpp Tue Oct  9 17:57:44 2018
@@ -194,11 +194,42 @@ void TracePC::HandleCallerCallee(uintptr
   ValueProfileMap.AddValueModPrime(Idx);
 }
 
+/// \return the address of the previous instruction.
+/// Note: the logic is copied from `sanitizer_common/sanitizer_stacktrace.h`
+inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
+#if defined(__arm__)
+  // T32 (Thumb) branch instructions might be 16 or 32 bit long,
+  // so we return (pc-2) in that case in order to be safe.
+  // For A32 mode we return (pc-4) because all instructions are 32 bit long.
+  return (PC - 3) & (~1);
+#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__aarch64__)
+  // PCs are always 4 byte aligned.
+  return PC - 4;
+#elif defined(__sparc__) || defined(__mips__)
+  return PC - 8;
+#else
+  return PC - 1;
+#endif
+}
+
+/// \return the address of the next instruction.
+/// Note: the logic is copied from `sanitizer_common/sanitizer_stacktrace.cc`
+inline ALWAYS_INLINE uintptr_t GetNextInstructionPc(uintptr_t PC) {
+#if defined(__mips__)
+  return PC + 8;
+#elif defined(__powerpc__) || defined(__sparc__) || defined(__arm__) || \
+    defined(__aarch64__)
+  return PC + 4;
+#else
+  return PC + 1;
+#endif
+}
+
 void TracePC::UpdateObservedPCs() {
   Vector<uintptr_t> CoveredFuncs;
   auto ObservePC = [&](uintptr_t PC) {
     if (ObservedPCs.insert(PC).second && DoPrintNewPCs) {
-      PrintPC("\tNEW_PC: %p %F %L", "\tNEW_PC: %p", PC + 1);
+      PrintPC("\tNEW_PC: %p %F %L", "\tNEW_PC: %p", GetNextInstructionPc(PC));
       Printf("\n");
     }
   };
@@ -233,22 +264,11 @@ void TracePC::UpdateObservedPCs() {
   for (size_t i = 0, N = Min(CoveredFuncs.size(), NumPrintNewFuncs); i < N;
        i++) {
     Printf("\tNEW_FUNC[%zd/%zd]: ", i + 1, CoveredFuncs.size());
-    PrintPC("%p %F %L", "%p", CoveredFuncs[i] + 1);
+    PrintPC("%p %F %L", "%p", GetNextInstructionPc(CoveredFuncs[i]));
     Printf("\n");
   }
 }
 
-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;
-}
-
-inline ALWAYS_INLINE uintptr_t GetNextInstructionPc(uintptr_t PC) {
-  // TODO: this implementation is x86 only.
-  // see sanitizer_common GetPreviousInstructionPc for full implementation.
-  return PC + 1;
-}
 
 static std::string GetModuleName(uintptr_t PC) {
   char ModulePathRaw[4096] = "";  // What's PATH_MAX in portable C++?

Modified: compiler-rt/trunk/test/fuzzer/coverage.test
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/fuzzer/coverage.test?rev=344104&r1=344103&r2=344104&view=diff
==============================================================================
--- compiler-rt/trunk/test/fuzzer/coverage.test (original)
+++ compiler-rt/trunk/test/fuzzer/coverage.test Tue Oct  9 17:57:44 2018
@@ -1,5 +1,5 @@
 # FIXME: Disabled on Windows because -fPIC cannot be used to compile for Windows.
-UNSUPPORTED: aarch64, windows
+UNSUPPORTED: windows
 RUN: %cpp_compiler -mllvm -use-unknown-locations=Disable  %S/NullDerefTest.cpp -o %t-NullDerefTest
 RUN: %cpp_compiler -mllvm -use-unknown-locations=Disable %S/DSO1.cpp -fPIC %ld_flags_rpath_so1 -shared -o %dynamiclib1
 RUN: %cpp_compiler -mllvm -use-unknown-locations=Disable %S/DSO2.cpp -fPIC %ld_flags_rpath_so2 -shared -o %dynamiclib2

Modified: compiler-rt/trunk/test/fuzzer/dump_coverage.test
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/fuzzer/dump_coverage.test?rev=344104&r1=344103&r2=344104&view=diff
==============================================================================
--- compiler-rt/trunk/test/fuzzer/dump_coverage.test (original)
+++ compiler-rt/trunk/test/fuzzer/dump_coverage.test Tue Oct  9 17:57:44 2018
@@ -8,7 +8,7 @@ RUN: %cpp_compiler -fsanitize-coverage=0
 
 RUN: rm -rf %t_workdir && mkdir -p %t_workdir
 RUN: env ASAN_OPTIONS=coverage_dir='"%t_workdir"' not %run %t-NullDerefTest -dump_coverage=1 2>&1 | FileCheck %s
-RUN: sancov -covered-functions %t-NullDerefTest* %t_workdir/*.sancov | FileCheck %s --check-prefix=SANCOV
+RUN: sancov -covered-functions %t-NullDerefTest %t_workdir/*.sancov | FileCheck %s --check-prefix=SANCOV
 RUN: env ASAN_OPTIONS=coverage_dir='"%t_workdir"' %run %t-DSOTest -dump_coverage=1 -runs=0 2>&1 | FileCheck -allow-deprecated-dag-overlap %s --check-prefix=DSO
 RUN: env ASAN_OPTIONS=coverage_dir='"%t_workdir"' not %run %t-NullDerefTest -dump_coverage=0 2>&1 | FileCheck %s --check-prefix=NOCOV
 

Modified: compiler-rt/trunk/test/fuzzer/handle-unstable.test
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/fuzzer/handle-unstable.test?rev=344104&r1=344103&r2=344104&view=diff
==============================================================================
--- compiler-rt/trunk/test/fuzzer/handle-unstable.test (original)
+++ compiler-rt/trunk/test/fuzzer/handle-unstable.test Tue Oct  9 17:57:44 2018
@@ -1,6 +1,6 @@
 # Tests -handle_unstable
 # FIXME: Disabled on Windows until symbolization works properly.
-UNSUPPORTED: aarch64, windows
+UNSUPPORTED: windows
 
 RUN: %cpp_compiler %S/PrintUnstableStatsTest.cpp -o %t-HandleUnstableTest
 




More information about the llvm-commits mailing list