Skip to content

Instantly share code, notes, and snippets.

@jroelofs
Created March 25, 2020 15:52
Show Gist options
  • Save jroelofs/588c49d2d3c8510abaa9a1e29a1aaba4 to your computer and use it in GitHub Desktop.
Save jroelofs/588c49d2d3c8510abaa9a1e29a1aaba4 to your computer and use it in GitHub Desktop.
__has_feature(fuzzer)
diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst
index 4c7af39e93e..85eca5034fb 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -2823,8 +2823,8 @@ Extensions for Dynamic Analysis
Use ``__has_feature(address_sanitizer)`` to check if the code is being built
with :doc:`AddressSanitizer`.
-Use ``__has_feature(thread_sanitizer)`` to check if the code is being built
-with :doc:`ThreadSanitizer`.
+Use ``__has_feature(fuzzer)`` to check if the code is being built with
+:doc:`SanitizerCoverage` or fuzzing enabled.
Use ``__has_feature(memory_sanitizer)`` to check if the code is being built
with :doc:`MemorySanitizer`.
@@ -2832,6 +2832,9 @@ with :doc:`MemorySanitizer`.
Use ``__has_feature(safe_stack)`` to check if the code is being built
with :doc:`SafeStack`.
+Use ``__has_feature(thread_sanitizer)`` to check if the code is being built
+with :doc:`ThreadSanitizer`.
+
Extensions for selectively disabling optimization
=================================================
diff --git a/clang/include/clang/Basic/Features.def b/clang/include/clang/Basic/Features.def
index 20e1b141a3e..5a8d3f8a928 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -88,6 +88,8 @@ FEATURE(memory_sanitizer,
LangOpts.Sanitize.hasOneOf(SanitizerKind::Memory |
SanitizerKind::KernelMemory))
FEATURE(thread_sanitizer, LangOpts.Sanitize.has(SanitizerKind::Thread))
+FEATURE(fuzzer, LangOpts.Sanitize.hasOneOf(SanitizerKind::Fuzzer |
+ SanitizerKind::FuzzerNoLink))
FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
// Objective-C features
diff --git a/clang/test/Lexer/has_feature_fuzzer.cpp b/clang/test/Lexer/has_feature_fuzzer.cpp
new file mode 100644
index 00000000000..114b482de58
--- /dev/null
+++ b/clang/test/Lexer/has_feature_fuzzer.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -E -fsanitize=fuzzer %s -o - | FileCheck --check-prefix=CHECK-FUZZER %s
+// RUN: %clang_cc1 -E -fsanitize=fuzzer-no-link %s -o - | FileCheck --check-prefix=CHECK-FUZZER %s
+// RUN: %clang_cc1 -E %s -o - | FileCheck --check-prefix=CHECK-NO-FUZZER %s
+
+#if __has_feature(fuzzer)
+int FuzzerEnabled();
+#else
+int FuzzerDisabled();
+#endif
+
+// CHECK-FUZZER: FuzzerEnabled
+// CHECK-NO-FUZZER: FuzzerDisabled
diff --git a/llvm/docs/LibFuzzer.rst b/llvm/docs/LibFuzzer.rst
index 0bf6f6bc6ff..da12c385578 100644
--- a/llvm/docs/LibFuzzer.rst
+++ b/llvm/docs/LibFuzzer.rst
@@ -540,13 +540,13 @@ Sometimes the code under test is not fuzzing-friendly. Examples:
E.g. png checks CRC for every chunk.
In many cases it makes sense to build a special fuzzing-friendly build
-with certain fuzzing-unfriendly features disabled. We propose to use a common build macro
-for all such cases for consistency: ``FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION``.
+with certain fuzzing-unfriendly features disabled. We propose to use a common feature test:
+``__has_feature(fuzzer)``.
.. code-block:: c++
void MyInitPRNG() {
- #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+ #if __has_feature(fuzzer)
// In fuzzing mode the behavior of the code should be deterministic.
srand(0);
#else
@@ -554,7 +554,8 @@ for all such cases for consistency: ``FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION``
#endif
}
-
+Alternatively, a common build-system provided macro could be used:
+``FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION``.
AFL compatibility
-----------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment