[compiler-rt] r370094 - Add GWP-ASan fuzz target to compiler-rt/tools.

Mitch Phillips via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 27 11:28:07 PDT 2019


Author: hctim
Date: Tue Aug 27 11:28:07 2019
New Revision: 370094

URL: http://llvm.org/viewvc/llvm-project?rev=370094&view=rev
Log:
Add GWP-ASan fuzz target to compiler-rt/tools.

Summary:
@eugenis to approve addition of //compiler-rt/tools.
@pree-jackie please confirm that this WFY.

D66494 introduced the GWP-ASan stack_trace_compressor_fuzzer. Building fuzz
targets in compiler-rt is a new affair, and has some challenges:
- If the host compiler doesn't have compiler-rt, the -fsanitize=fuzzer may not
  be able to link against `libclang_rt.fuzzer*`.
- Things in compiler-rt generally aren't built when you want to build with
  sanitizers using `-DLLVM_USE_SANITIZER`. This tricky to work around, so
  we create the new tools directory so that we can build fuzz targets with
  sanitizers. This has the added bonus of fixing the problem above as well, as
  we can now just guard the fuzz target build to only be done with
  `-DLLVM_USE_SANITIZE_COVERAGE=On`.

Reviewers: eugenis, pree-jackie

Reviewed By: eugenis, pree-jackie

Subscribers: dberris, mgorny, #sanitizers, llvm-commits, eugenis, pree-jackie, lebedev.ri, vitalybuka, morehouse

Tags: #sanitizers, #llvm

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

Added:
    compiler-rt/trunk/tools/
    compiler-rt/trunk/tools/CMakeLists.txt
    compiler-rt/trunk/tools/gwp_asan/
    compiler-rt/trunk/tools/gwp_asan/CMakeLists.txt
    compiler-rt/trunk/tools/gwp_asan/stack_trace_compressor_fuzzer.cpp
Removed:
    compiler-rt/trunk/lib/gwp_asan/stack_trace_compressor_fuzzer.cpp
Modified:
    compiler-rt/trunk/CMakeLists.txt
    compiler-rt/trunk/lib/gwp_asan/CMakeLists.txt

Modified: compiler-rt/trunk/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/CMakeLists.txt?rev=370094&r1=370093&r2=370094&view=diff
==============================================================================
--- compiler-rt/trunk/CMakeLists.txt (original)
+++ compiler-rt/trunk/CMakeLists.txt Tue Aug 27 11:28:07 2019
@@ -513,3 +513,5 @@ if(COMPILER_RT_INCLUDE_TESTS)
     endif()
   endif()
 endif()
+
+add_subdirectory(tools)

Modified: compiler-rt/trunk/lib/gwp_asan/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/gwp_asan/CMakeLists.txt?rev=370094&r1=370093&r2=370094&view=diff
==============================================================================
--- compiler-rt/trunk/lib/gwp_asan/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/gwp_asan/CMakeLists.txt Tue Aug 27 11:28:07 2019
@@ -98,29 +98,6 @@ if (COMPILER_RT_HAS_GWP_ASAN)
       SOURCES optional/backtrace_sanitizer_common.cpp
       ADDITIONAL_HEADERS ${GWP_ASAN_BACKTRACE_HEADERS}
       CFLAGS ${GWP_ASAN_CFLAGS} ${SANITIZER_COMMON_CFLAGS})
-
-  # Build the stack trace compressor fuzzer. Note that clang versions 4.* did
-  # not have -fsanitize=fuzzer, and Clang versions 5.* didn't have
-  # -fsanitize=fuzzer-no-link. In general, the way we build fuzz targets in LLVM
-  # core is to link it against a dummy main when DLLVM_USE_SANITIZE_COVERAGE
-  # isn't specified. Instead, here we only build fuzz targets if clang version
-  # is >= 6.0.
-  if (COMPILER_RT_BUILD_LIBFUZZER AND
-      "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND
-      NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
-    add_executable(stack_trace_compressor_fuzzer
-        stack_trace_compressor_fuzzer.cpp
-        ${GWP_ASAN_SOURCES}
-        ${GWP_ASAN_HEADERS})
-    set_target_properties(
-        stack_trace_compressor_fuzzer PROPERTIES FOLDER "Fuzzers")
-    target_compile_options(
-        stack_trace_compressor_fuzzer PRIVATE -fsanitize=fuzzer-no-link)
-    set_target_properties(
-        stack_trace_compressor_fuzzer PROPERTIES LINK_FLAGS -fsanitize=fuzzer)
-    add_dependencies(stack_trace_compressor_fuzzer fuzzer)
-    add_dependencies(gwp_asan stack_trace_compressor_fuzzer)
-  endif()
 endif()
 
 if(COMPILER_RT_INCLUDE_TESTS)

Removed: compiler-rt/trunk/lib/gwp_asan/stack_trace_compressor_fuzzer.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/gwp_asan/stack_trace_compressor_fuzzer.cpp?rev=370093&view=auto
==============================================================================
--- compiler-rt/trunk/lib/gwp_asan/stack_trace_compressor_fuzzer.cpp (original)
+++ compiler-rt/trunk/lib/gwp_asan/stack_trace_compressor_fuzzer.cpp (removed)
@@ -1,49 +0,0 @@
-#include <cstddef>
-#include <cstdint>
-#include <cstdio>
-#include <cstdlib>
-#include <vector>
-
-#include "gwp_asan/stack_trace_compressor.h"
-
-constexpr size_t kBytesForLargestVarInt = (sizeof(uintptr_t) * 8) / 7 + 1;
-
-extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
-  size_t BufferSize = kBytesForLargestVarInt * Size / sizeof(uintptr_t);
-  std::vector<uint8_t> Buffer(BufferSize);
-  std::vector<uint8_t> Buffer2(BufferSize);
-
-  // Unpack the fuzz bytes.
-  gwp_asan::compression::unpack(Data, Size,
-                                reinterpret_cast<uintptr_t *>(Buffer2.data()),
-                                BufferSize / sizeof(uintptr_t));
-
-  // Pack the fuzz bytes.
-  size_t BytesWritten = gwp_asan::compression::pack(
-      reinterpret_cast<const uintptr_t *>(Data), Size / sizeof(uintptr_t),
-      Buffer.data(), BufferSize);
-
-  // Unpack the compressed buffer.
-  size_t DecodedElements = gwp_asan::compression::unpack(
-      Buffer.data(), BytesWritten,
-      reinterpret_cast<uintptr_t *>(Buffer2.data()),
-      BufferSize / sizeof(uintptr_t));
-
-  // Ensure that every element was encoded and decoded properly.
-  if (DecodedElements != Size / sizeof(uintptr_t))
-    abort();
-
-  // Ensure that the compression and uncompression resulted in the same trace.
-  const uintptr_t *FuzzPtrs = reinterpret_cast<const uintptr_t *>(Data);
-  const uintptr_t *DecodedPtrs =
-      reinterpret_cast<const uintptr_t *>(Buffer2.data());
-  for (size_t i = 0; i < Size / sizeof(uintptr_t); ++i) {
-    if (FuzzPtrs[i] != DecodedPtrs[i]) {
-      fprintf(stderr, "FuzzPtrs[%zu] != DecodedPtrs[%zu] (0x%zx vs. 0x%zx)", i,
-              i, FuzzPtrs[i], DecodedPtrs[i]);
-      abort();
-    }
-  }
-
-  return 0;
-}

Added: compiler-rt/trunk/tools/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/tools/CMakeLists.txt?rev=370094&view=auto
==============================================================================
--- compiler-rt/trunk/tools/CMakeLists.txt (added)
+++ compiler-rt/trunk/tools/CMakeLists.txt Tue Aug 27 11:28:07 2019
@@ -0,0 +1 @@
+add_subdirectory(gwp_asan)

Added: compiler-rt/trunk/tools/gwp_asan/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/tools/gwp_asan/CMakeLists.txt?rev=370094&view=auto
==============================================================================
--- compiler-rt/trunk/tools/gwp_asan/CMakeLists.txt (added)
+++ compiler-rt/trunk/tools/gwp_asan/CMakeLists.txt Tue Aug 27 11:28:07 2019
@@ -0,0 +1,20 @@
+# Build the stack trace compressor fuzzer. This will require Clang >= 6.0.0, as
+# -fsanitize=fuzzer-no-link was not a valid command line flag prior to this.
+if (LLVM_USE_SANITIZE_COVERAGE)
+  add_executable(stack_trace_compressor_fuzzer
+      ../../lib/gwp_asan/stack_trace_compressor.cpp
+      ../../lib/gwp_asan/stack_trace_compressor.h
+      stack_trace_compressor_fuzzer.cpp)
+  set_target_properties(
+      stack_trace_compressor_fuzzer PROPERTIES FOLDER "Fuzzers")
+  target_compile_options(
+      stack_trace_compressor_fuzzer PRIVATE -fsanitize=fuzzer-no-link)
+  set_target_properties(
+      stack_trace_compressor_fuzzer PROPERTIES LINK_FLAGS -fsanitize=fuzzer)
+  target_include_directories(
+      stack_trace_compressor_fuzzer PRIVATE ../../lib/)
+
+  if (TARGET gwp_asan)
+    add_dependencies(gwp_asan stack_trace_compressor_fuzzer)
+  endif()
+endif()

Added: compiler-rt/trunk/tools/gwp_asan/stack_trace_compressor_fuzzer.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/tools/gwp_asan/stack_trace_compressor_fuzzer.cpp?rev=370094&view=auto
==============================================================================
--- compiler-rt/trunk/tools/gwp_asan/stack_trace_compressor_fuzzer.cpp (added)
+++ compiler-rt/trunk/tools/gwp_asan/stack_trace_compressor_fuzzer.cpp Tue Aug 27 11:28:07 2019
@@ -0,0 +1,49 @@
+#include <cstddef>
+#include <cstdint>
+#include <cstdio>
+#include <cstdlib>
+#include <vector>
+
+#include "gwp_asan/stack_trace_compressor.h"
+
+constexpr size_t kBytesForLargestVarInt = (sizeof(uintptr_t) * 8) / 7 + 1;
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
+  size_t BufferSize = kBytesForLargestVarInt * Size / sizeof(uintptr_t);
+  std::vector<uint8_t> Buffer(BufferSize);
+  std::vector<uint8_t> Buffer2(BufferSize);
+
+  // Unpack the fuzz bytes.
+  gwp_asan::compression::unpack(Data, Size,
+                                reinterpret_cast<uintptr_t *>(Buffer2.data()),
+                                BufferSize / sizeof(uintptr_t));
+
+  // Pack the fuzz bytes.
+  size_t BytesWritten = gwp_asan::compression::pack(
+      reinterpret_cast<const uintptr_t *>(Data), Size / sizeof(uintptr_t),
+      Buffer.data(), BufferSize);
+
+  // Unpack the compressed buffer.
+  size_t DecodedElements = gwp_asan::compression::unpack(
+      Buffer.data(), BytesWritten,
+      reinterpret_cast<uintptr_t *>(Buffer2.data()),
+      BufferSize / sizeof(uintptr_t));
+
+  // Ensure that every element was encoded and decoded properly.
+  if (DecodedElements != Size / sizeof(uintptr_t))
+    abort();
+
+  // Ensure that the compression and uncompression resulted in the same trace.
+  const uintptr_t *FuzzPtrs = reinterpret_cast<const uintptr_t *>(Data);
+  const uintptr_t *DecodedPtrs =
+      reinterpret_cast<const uintptr_t *>(Buffer2.data());
+  for (size_t i = 0; i < Size / sizeof(uintptr_t); ++i) {
+    if (FuzzPtrs[i] != DecodedPtrs[i]) {
+      fprintf(stderr, "FuzzPtrs[%zu] != DecodedPtrs[%zu] (0x%zx vs. 0x%zx)", i,
+              i, FuzzPtrs[i], DecodedPtrs[i]);
+      abort();
+    }
+  }
+
+  return 0;
+}




More information about the llvm-commits mailing list