Provide a macro for code specific to fuzz testing.

There's a quasi-standard in the industry for supporting fuzz testing
through use of the macro "FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" to
conditionalize code for maximum fuzzing efficiency -- see
https://llvm.org/docs/LibFuzzer.html#fuzzer-friendly-build-mode .

Common uses for this macro would be to replace a nondeterministic
process (such as fetching a random number or determining the
time-of-day of execution) with a substitute deterministic process, or
to bypass the validation of checksums or digital signatures where they
would otherwise hamper fuzz-test coverage.

As the macro name suggests, it is never defined for regular
"non-fuzzing" builds, so whichever alterations are made under the
control of this macro should have no effect on regular usage.

This change simply adds the macro and wires it up in CMake so that it
is defined in config.h iff either of the ENABLE_FUZZER or OSS_FUZZ
CMake flags are set.
This commit is contained in:
Darius Davis 2024-12-02 22:51:02 +10:00 committed by Michael Mann
parent b4ebba3ec0
commit 019e13a850
2 changed files with 5 additions and 0 deletions

View File

@ -495,6 +495,7 @@ if(OSS_FUZZ)
endif()
# Must not depend on external dependencies so statically link all libs.
set(USE_STATIC ON)
set(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION ON)
endif()
if(USE_STATIC)
@ -1153,6 +1154,7 @@ if(ENABLE_FUZZER)
endif()
set(CMAKE_C_FLAGS "-fsanitize=fuzzer-no-link ${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "-fsanitize=fuzzer-no-link ${CMAKE_CXX_FLAGS}")
set(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION ON)
endif()
if(NOT MSVC)

View File

@ -40,6 +40,9 @@
/* Enable LeakSanitizer standalone */
#cmakedefine ENABLE_LSAN 1
/* Adapt build products for fuzzing (e.g. accepting incorrect checksums or prioritizing determinism). */
#cmakedefine FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 1
/* Define to 1 if you have the <arpa/inet.h> header file. */
#cmakedefine HAVE_ARPA_INET_H 1