Răsfoiți Sursa

OSS Fuzz CMake changes (#1611)

* Detect OSS Fuzz thorough `LIB_FUZZING_ENGINE` env var.
* Avoid building libFuzzer if not fuzzing.
* Load the fuzzing library either from the library provided by OSS Fuzz or from libFuzzer.a that was manually built from sources.
Mina Farid 7 ani în urmă
părinte
comite
af0e5fca4c

+ 4 - 5
Firestore/core/src/firebase/firestore/fuzzing/CMakeLists.txt

@@ -16,16 +16,15 @@ if(NOT FUZZING)
   return()
 endif()
 
-# TODO(minafarid): Currently we support local fuzzing only where we build
-# libFuzzer. Future plans include integrating into OSS Fuzz, where the fuzzing
-# library is already provided.
-find_package(LibFuzzer REQUIRED)
+# Finds the fuzzer library that is either provided by OSS Fuzz, if enabled, or
+# libFuzzer that is manually built from sources.
+find_package(Fuzzer REQUIRED)
 
 cc_binary(
   firebase_firestore_fuzzing_serializer
   SOURCES
     fuzz_test_serializer.cc
   DEPENDS
-    LibFuzzer
+    Fuzzer
     firebase_firestore_remote
 )

+ 19 - 13
cmake/FindLibFuzzer.cmake → cmake/FindFuzzer.cmake

@@ -12,26 +12,32 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+if(TARGET Fuzzer)
+  return()
+endif()
+
+# OSS Fuzz provides its own fuzzing library libFuzzingEngine.a in the path
+# defined by LIB_FUZZING_ENGINE environment variable. For local fuzzing, search
+# for the libFuzzer.a library that was manually built.
 find_library(
-  LIBFUZZER_LIBRARY
-  NAMES Fuzzer
+  FUZZER_LOCATION
+  NAMES FuzzingEngine Fuzzer
   HINTS
+    $ENV{LIB_FUZZING_ENGINE}
     ${FIREBASE_BINARY_DIR}/external/src/libfuzzer
 )
 
 include(FindPackageHandleStandardArgs)
 find_package_handle_standard_args(
-  LibFuzzer
+  FUZZER
   DEFAULT_MSG
-  LIBFUZZER_LIBRARY
+  FUZZER_LOCATION
 )
 
-if(LIBFUZZER_FOUND)
-  if (NOT TARGET LibFuzzer)
-    add_library(LibFuzzer STATIC IMPORTED)
-    set_target_properties(
-      LibFuzzer PROPERTIES
-      IMPORTED_LOCATION ${LIBFUZZER_LIBRARY}
-    )
-  endif()
-endif(LIBFUZZER_FOUND)
+if(FUZZER_FOUND)
+  add_library(Fuzzer STATIC IMPORTED)
+  set_target_properties(
+    Fuzzer PROPERTIES
+    IMPORTED_LOCATION ${FUZZER_LOCATION}
+  )
+endif(FUZZER_FOUND)

+ 10 - 1
cmake/external/libfuzzer.cmake

@@ -16,7 +16,7 @@
 # provided by libFuzzer to compile the sources and produce a library with the
 # name libFuzzer.a in the same directory as the sources because we have
 # BUILD_IN_SOURCES set to TRUE.
-#
+
 # This build method might not work on all systems. See the build.sh script of
 # libFuzzer here:
 # (https://github.com/llvm-mirror/compiler-rt/blob/master/lib/fuzzer/build.sh).
@@ -27,6 +27,15 @@ if(TARGET libfuzzer)
   return()
 endif()
 
+# Mark libfuzzer target as done if: (a) fuzzing is not enabled and libFuzzer is
+# not needed; (b) a fuzzing library is already provided in LIB_FUZZING_ENGINE
+# environment variable as in OSS Fuzz and there is no need to build it; and
+# (c) on Windows because fuzzing is not supported.
+if(NOT FUZZING OR DEFINED ENV{LIB_FUZZING_ENGINE} OR WIN32)
+  add_custom_target(libfuzzer)
+  return()
+endif()
+
 set(tag RELEASE_601)  # latest release@{2018-07-27}
 
 ExternalProject_Add(

+ 1 - 0
cmake/external_rules.cmake

@@ -19,6 +19,7 @@ function(download_external_sources)
       ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}"
       -DFIREBASE_DOWNLOAD_DIR=${FIREBASE_DOWNLOAD_DIR}
       -DCMAKE_INSTALL_PREFIX=${FIREBASE_INSTALL_DIR}
+      -DFUZZING=${FUZZING}
       ${PROJECT_SOURCE_DIR}/cmake/external
     WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/external
   )

+ 10 - 3
cmake/fuzzing_options.cmake

@@ -18,9 +18,16 @@ include(compiler_id)
 
 option(FUZZING "Build for Fuzz Testing (local fuzzing and OSS Fuzz)" OFF)
 
-# If fuzzing is enabled, multiple compile and linking flags must be set.
-# These flags are set according to the compiler kind.
-if(FUZZING)
+# Assume OSS Fuzz if LIB_FUZZING_ENGINE environment variable is set. OSS Fuzz
+# provides its required compiler-specific flags in CXXFLAGS, which are
+# automatically added to CMAKE_CXX_FLAGS. For local fuzzing, multiple compile
+# and linking flags must be set. These flags depend on the compiler version.
+if(FUZZING AND NOT DEFINED ENV{LIB_FUZZING_ENGINE})
+  if(WIN32)
+    # Currently, libFuzzer cannot be built on Windows.
+    message(FATAL_ERROR "Fuzzing is currently not supported on Windows.")
+  endif()
+
   # Address sanitizer must be enabled during fuzzing to detect memory errors.
   if(NOT WITH_ASAN)
     message(FATAL_ERROR "Fuzzing requires WITH_ASAN=ON to detect memory errors.")