Add CMake rules for libFuzzer targets (#4445)

Adds CMake rules for the libFuzzer targets, with a view to running
them on OSS-Fuzz.

Fixes #4448.
This commit is contained in:
Alastair Donaldson 2021-08-05 09:11:21 +01:00 committed by GitHub
parent 0065c5672d
commit 3ab6fb9c03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 0 deletions

View File

@ -84,6 +84,8 @@ endif(SPIRV_BUILD_COMPRESSION)
option(SPIRV_BUILD_FUZZER "Build spirv-fuzz" OFF)
option(SPIRV_BUILD_LIBFUZZER_TARGETS "Build libFuzzer targets" OFF)
option(SPIRV_WERROR "Enable error on warning" ON)
if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") AND (NOT CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")))
set(COMPILER_IS_LIKE_GNU TRUE)

View File

@ -79,6 +79,10 @@ if [ $TOOL = "cmake" ]; then
SKIP_TESTS="True"
fi
if [ $COMPILER = "clang" ]; then
ADDITIONAL_CMAKE_FLAGS="$ADDITIONAL_CMAKE_FLAGS -DSPIRV_BUILD_LIBFUZZER_TARGETS=ON"
fi
clean_dir "$ROOT_DIR/build"
cd "$ROOT_DIR/build"

View File

@ -192,3 +192,4 @@ add_subdirectory(fuzz)
add_subdirectory(tools)
add_subdirectory(util)
add_subdirectory(val)
add_subdirectory(fuzzers)

View File

@ -0,0 +1,44 @@
# Copyright (c) 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
function(add_spvtools_libfuzzer_target)
set(one_value_args TARGET)
set(multi_value_args SRCS LIBS)
cmake_parse_arguments(
ARG "" "${one_value_args}" "${multi_value_args}" ${ARGN})
add_executable(${ARG_TARGET} ${ARG_SRCS})
spvtools_default_compile_options(${ARG_TARGET})
target_link_libraries(${ARG_TARGET} PRIVATE ${ARG_LIBS})
target_include_directories(${ARG_TARGET} PRIVATE
${spirv-tools_SOURCE_DIR}
${spirv-tools_BINARY_DIR}
)
set_property(TARGET ${ARG_TARGET} PROPERTY FOLDER "SPIRV-Tools libFuzzer targets")
target_compile_options(${ARG_TARGET} PRIVATE "-fsanitize=fuzzer")
target_link_options(${ARG_TARGET} PRIVATE "-fsanitize=fuzzer")
endfunction()
if (${SPIRV_BUILD_LIBFUZZER_TARGETS})
if(NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
message(FATAL_ERROR "The libFuzzer targets are only supported with the Clang compiler. Compiler '${CMAKE_CXX_COMPILER_ID}' is not supported!")
endif()
add_spvtools_libfuzzer_target(TARGET spvtools_as_fuzzer SRCS spvtools_as_fuzzer.cpp LIBS ${SPIRV_TOOLS_FULL_VISIBILITY})
add_spvtools_libfuzzer_target(TARGET spvtools_binary_parser_fuzzer SRCS spvtools_binary_parser_fuzzer.cpp LIBS ${SPIRV_TOOLS_FULL_VISIBILITY})
add_spvtools_libfuzzer_target(TARGET spvtools_dis_fuzzer SRCS spvtools_dis_fuzzer.cpp LIBS ${SPIRV_TOOLS_FULL_VISIBILITY})
add_spvtools_libfuzzer_target(TARGET spvtools_opt_legalization_fuzzer SRCS spvtools_opt_legalization_fuzzer.cpp LIBS SPIRV-Tools-opt ${SPIRV_TOOLS_FULL_VISIBILITY})
add_spvtools_libfuzzer_target(TARGET spvtools_opt_performance_fuzzer SRCS spvtools_opt_performance_fuzzer.cpp LIBS SPIRV-Tools-opt ${SPIRV_TOOLS_FULL_VISIBILITY})
add_spvtools_libfuzzer_target(TARGET spvtools_opt_size_fuzzer SRCS spvtools_opt_size_fuzzer.cpp LIBS SPIRV-Tools-opt ${SPIRV_TOOLS_FULL_VISIBILITY})
add_spvtools_libfuzzer_target(TARGET spvtools_val_fuzzer SRCS spvtools_val_fuzzer.cpp LIBS ${SPIRV_TOOLS_FULL_VISIBILITY})
endif()