| 1 |
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
# SPDX-License-Identifier: Apache-2.0. |
| 3 |
|
| 4 |
include(CTest) |
| 5 |
include(AwsSanitizers) |
| 6 |
|
| 7 |
option(ENABLE_FUZZ_TESTS "Build and run fuzz tests" OFF) |
| 8 |
set(FUZZ_TESTS_MAX_TIME 60 CACHE STRING "Max time to run each fuzz test") |
| 9 |
|
| 10 |
# Adds fuzz tests to ctest |
| 11 |
# Options: |
| 12 |
# fuzz_files: The list of fuzz test files |
| 13 |
# other_files: Other files to link into each fuzz test |
| 14 |
# corpus_dir: directory where corpus files can be found |
| 15 |
function(aws_add_fuzz_tests fuzz_files other_files corpus_dir) |
| 16 |
if(ENABLE_FUZZ_TESTS) |
| 17 |
if(NOT ENABLE_SANITIZERS) |
| 18 |
message(FATAL_ERROR "ENABLE_FUZZ_TESTS is set but ENABLE_SANITIZERS is set to OFF") |
| 19 |
endif() |
| 20 |
|
| 21 |
aws_check_sanitizer(fuzzer) |
| 22 |
if (NOT HAS_SANITIZER_fuzzer) |
| 23 |
message(FATAL_ERROR "ENABLE_FUZZ_TESTS is set but the current compiler (${CMAKE_CXX_COMPILER_ID}) doesn't support -fsanitize=fuzzer") |
| 24 |
endif() |
| 25 |
|
| 26 |
foreach(test_file ${fuzz_files}) |
| 27 |
get_filename_component(TEST_FILE_NAME ${test_file} NAME_WE) |
| 28 |
|
| 29 |
set(FUZZ_BINARY_NAME ${PROJECT_NAME}-fuzz-${TEST_FILE_NAME}) |
| 30 |
add_executable(${FUZZ_BINARY_NAME} ${test_file} ${other_files}) |
| 31 |
target_link_libraries(${FUZZ_BINARY_NAME} PRIVATE ${PROJECT_NAME}) |
| 32 |
aws_set_common_properties(${FUZZ_BINARY_NAME}) |
| 33 |
aws_add_sanitizers(${FUZZ_BINARY_NAME} SANITIZERS "fuzzer") |
| 34 |
target_compile_definitions(${FUZZ_BINARY_NAME} PRIVATE AWS_UNSTABLE_TESTING_API=1) |
| 35 |
target_include_directories(${FUZZ_BINARY_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}) |
| 36 |
|
| 37 |
if (corpus_dir) |
| 38 |
file(TO_NATIVE_PATH "${corpus_dir}/${TEST_FILE_NAME}" TEST_CORPUS_DIR) |
| 39 |
endif() |
| 40 |
|
| 41 |
if (TEST_CORPUS_DIR AND (EXISTS "${TEST_CORPUS_DIR}")) |
| 42 |
add_test(NAME fuzz_${TEST_FILE_NAME} COMMAND ${FUZZ_BINARY_NAME} -timeout=1 -max_total_time=${FUZZ_TESTS_MAX_TIME} "${TEST_CORPUS_DIR}") |
| 43 |
else() |
| 44 |
add_test(NAME fuzz_${TEST_FILE_NAME} COMMAND ${FUZZ_BINARY_NAME} -timeout=1 -max_total_time=${FUZZ_TESTS_MAX_TIME}) |
| 45 |
endif() |
| 46 |
endforeach() |
| 47 |
endif() |
| 48 |
endfunction() |