[compiler-rt] 881faf4 - Enable extra coverage counters on Windows

Matt Morehouse via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 5 11:40:35 PDT 2021


Author: Matt Morehouse
Date: 2021-08-05T11:40:15-07:00
New Revision: 881faf41909b47376595e8d7bb9c9a109182d20b

URL: https://github.com/llvm/llvm-project/commit/881faf41909b47376595e8d7bb9c9a109182d20b
DIFF: https://github.com/llvm/llvm-project/commit/881faf41909b47376595e8d7bb9c9a109182d20b.diff

LOG: Enable extra coverage counters on Windows

- Enable extra coverage counters on Windows.
- Update extra_counters.test to run on Windows also.
- Update TableLookupTest.cpp to include the required pragma/declspec for the extra coverage counters.

Patch By: MichaelSquires

Reviewed By: morehouse

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

Added: 
    compiler-rt/lib/fuzzer/FuzzerExtraCountersDarwin.cpp
    compiler-rt/lib/fuzzer/FuzzerExtraCountersWindows.cpp

Modified: 
    compiler-rt/lib/fuzzer/CMakeLists.txt
    compiler-rt/lib/fuzzer/FuzzerExtraCounters.cpp
    compiler-rt/test/fuzzer/TableLookupTest.cpp
    compiler-rt/test/fuzzer/extra-counters.test

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/fuzzer/CMakeLists.txt b/compiler-rt/lib/fuzzer/CMakeLists.txt
index 3201ed279a621..1da8228fa75db 100644
--- a/compiler-rt/lib/fuzzer/CMakeLists.txt
+++ b/compiler-rt/lib/fuzzer/CMakeLists.txt
@@ -6,6 +6,8 @@ set(LIBFUZZER_SOURCES
   FuzzerExtFunctionsWeak.cpp
   FuzzerExtFunctionsWindows.cpp
   FuzzerExtraCounters.cpp
+  FuzzerExtraCountersDarwin.cpp
+  FuzzerExtraCountersWindows.cpp
   FuzzerFork.cpp
   FuzzerIO.cpp
   FuzzerIOPosix.cpp

diff  --git a/compiler-rt/lib/fuzzer/FuzzerExtraCounters.cpp b/compiler-rt/lib/fuzzer/FuzzerExtraCounters.cpp
index 04f569a1a879a..54ecbf7c62f12 100644
--- a/compiler-rt/lib/fuzzer/FuzzerExtraCounters.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerExtraCounters.cpp
@@ -31,12 +31,4 @@ void ClearExtraCounters() {  // hand-written memset, don't asan-ify.
 
 }  // namespace fuzzer
 
-#else
-// TODO: implement for other platforms.
-namespace fuzzer {
-uint8_t *ExtraCountersBegin() { return nullptr; }
-uint8_t *ExtraCountersEnd() { return nullptr; }
-void ClearExtraCounters() {}
-}  // namespace fuzzer
-
 #endif

diff  --git a/compiler-rt/lib/fuzzer/FuzzerExtraCountersDarwin.cpp b/compiler-rt/lib/fuzzer/FuzzerExtraCountersDarwin.cpp
new file mode 100644
index 0000000000000..61f288ce1c810
--- /dev/null
+++ b/compiler-rt/lib/fuzzer/FuzzerExtraCountersDarwin.cpp
@@ -0,0 +1,21 @@
+//===- FuzzerExtraCountersDarwin.cpp - Extra coverage counters for Darwin -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// Extra coverage counters defined by user code for Darwin.
+//===----------------------------------------------------------------------===//
+
+#include "FuzzerPlatform.h"
+
+#if LIBFUZZER_APPLE
+
+namespace fuzzer {
+uint8_t *ExtraCountersBegin() { return nullptr; }
+uint8_t *ExtraCountersEnd() { return nullptr; }
+void ClearExtraCounters() {}
+} // namespace fuzzer
+
+#endif

diff  --git a/compiler-rt/lib/fuzzer/FuzzerExtraCountersWindows.cpp b/compiler-rt/lib/fuzzer/FuzzerExtraCountersWindows.cpp
new file mode 100644
index 0000000000000..19101848408ba
--- /dev/null
+++ b/compiler-rt/lib/fuzzer/FuzzerExtraCountersWindows.cpp
@@ -0,0 +1,80 @@
+//===- FuzzerExtraCountersWindows.cpp - Extra coverage counters for Win32 -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// Extra coverage counters defined by user code for Windows.
+//===----------------------------------------------------------------------===//
+
+#include "FuzzerPlatform.h"
+#include <cstdint>
+
+#if LIBFUZZER_WINDOWS
+#include <Windows.h>
+
+namespace fuzzer {
+
+//
+// The __start___libfuzzer_extra_counters variable is align 16, size 16 to
+// ensure the padding between it and the next variable in this section (either
+// __libfuzzer_extra_counters or __stop___libfuzzer_extra_counters) will be
+// located at (__start___libfuzzer_extra_counters +
+// sizeof(__start___libfuzzer_extra_counters)). Otherwise, the calculation of
+// (stop - (start + sizeof(start))) might be skewed.
+//
+// The section name, __libfuzzer_extra_countaaa ends with "aaa", so it sorts
+// before __libfuzzer_extra_counters alphabetically. We want the start symbol to
+// be placed in the section just before the user supplied counters (if present).
+//
+#pragma section(".data$__libfuzzer_extra_countaaa")
+ATTRIBUTE_ALIGNED(16)
+__declspec(allocate(".data$__libfuzzer_extra_countaaa")) uint8_t
+    __start___libfuzzer_extra_counters[16] = {0};
+
+//
+// Example of what the user-supplied counters should look like. First, the
+// pragma to create the section name. It will fall alphabetically between
+// ".data$__libfuzzer_extra_countaaa" and ".data$__libfuzzer_extra_countzzz".
+// Next, the declspec to allocate the variable inside the specified section.
+// Finally, some array, struct, whatever that is used to track the counter data.
+// The size of this variable is computed at runtime by finding the 
diff erence of
+// __stop___libfuzzer_extra_counters and __start___libfuzzer_extra_counters +
+// sizeof(__start___libfuzzer_extra_counters).
+//
+
+//
+//     #pragma section(".data$__libfuzzer_extra_counters")
+//     __declspec(allocate(".data$__libfuzzer_extra_counters"))
+//         uint8_t any_name_variable[64 * 1024];
+//
+
+//
+// Here, the section name, __libfuzzer_extra_countzzz ends with "zzz", so it
+// sorts after __libfuzzer_extra_counters alphabetically. We want the stop
+// symbol to be placed in the section just after the user supplied counters (if
+// present). Align to 1 so there isn't any padding placed between this and the
+// previous variable.
+//
+#pragma section(".data$__libfuzzer_extra_countzzz")
+ATTRIBUTE_ALIGNED(1)
+__declspec(allocate(".data$__libfuzzer_extra_countzzz")) uint8_t
+    __stop___libfuzzer_extra_counters = 0;
+
+uint8_t *ExtraCountersBegin() {
+  return __start___libfuzzer_extra_counters +
+         sizeof(__start___libfuzzer_extra_counters);
+}
+
+uint8_t *ExtraCountersEnd() { return &__stop___libfuzzer_extra_counters; }
+
+ATTRIBUTE_NO_SANITIZE_ALL
+void ClearExtraCounters() {
+  uint8_t *Beg = ExtraCountersBegin();
+  SecureZeroMemory(Beg, ExtraCountersEnd() - Beg);
+}
+
+} // namespace fuzzer
+
+#endif

diff  --git a/compiler-rt/test/fuzzer/TableLookupTest.cpp b/compiler-rt/test/fuzzer/TableLookupTest.cpp
index 2b4052303324a..247963cca698e 100644
--- a/compiler-rt/test/fuzzer/TableLookupTest.cpp
+++ b/compiler-rt/test/fuzzer/TableLookupTest.cpp
@@ -25,7 +25,11 @@ const size_t N = 1 << 12;
 #ifdef __linux__
 __attribute__((section("__libfuzzer_extra_counters")))
 #endif
-static uint8_t Counters[N];
+#ifdef _WIN32
+#  pragma section(".data$__libfuzzer_extra_counters")
+__declspec(allocate(".data$__libfuzzer_extra_counters"))
+#endif
+    static uint8_t Counters[N];
 
 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
   static std::set<uint16_t> SeenIdx;

diff  --git a/compiler-rt/test/fuzzer/extra-counters.test b/compiler-rt/test/fuzzer/extra-counters.test
index a93f775dcfc5f..0cc69e47d43bb 100644
--- a/compiler-rt/test/fuzzer/extra-counters.test
+++ b/compiler-rt/test/fuzzer/extra-counters.test
@@ -1,4 +1,4 @@
-REQUIRES: linux
+UNSUPPORTED: darwin
 
 RUN: %cpp_compiler %S/TableLookupTest.cpp -o %t-TableLookupTest
 RUN: not %run %t-TableLookupTest -print_final_stats=1 2>&1 | FileCheck %s


        


More information about the llvm-commits mailing list