Skip to content

Commit

Permalink
OSS Fuzz CMake changes (firebase#1611)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Mina Farid authored Aug 1, 2018
1 parent 6739dea commit af0e5fc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
9 changes: 4 additions & 5 deletions Firestore/core/src/firebase/firestore/fuzzing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
32 changes: 19 additions & 13 deletions cmake/FindLibFuzzer.cmake → cmake/FindFuzzer.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 10 additions & 1 deletion cmake/external/libfuzzer.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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(
Expand Down
1 change: 1 addition & 0 deletions cmake/external_rules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
13 changes: 10 additions & 3 deletions cmake/fuzzing_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down

0 comments on commit af0e5fc

Please sign in to comment.