From 5dbf62cd1171fcbc3ddfcd43d12fcb7de4225641 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Thu, 19 Feb 2026 05:43:44 +1300 Subject: [PATCH] build: disable fuzztest when building with TSAN fuzztest's coverage instrumentation is incompatible with Thread Sanitizer. Add px4_setup_gtest_without_fuzztest() macro to cmake/px4_add_gtest.cmake that fetches GTest standalone and stubs out fuzztest cmake functions. Guard all fuzztest-specific code on TARGET fuzztest::fuzztest so it compiles cleanly without fuzztest. --- cmake/px4_add_gtest.cmake | 63 ++++++++++++++----- platforms/posix/CMakeLists.txt | 2 +- .../px4/common/gtest_runner/CMakeLists.txt | 7 ++- .../gtest_runner/gtest_functional_main.cpp | 4 ++ src/drivers/gnss/septentrio/CMakeLists.txt | 24 +++---- test/CMakeLists.txt | 14 +++-- 6 files changed, 79 insertions(+), 35 deletions(-) diff --git a/cmake/px4_add_gtest.cmake b/cmake/px4_add_gtest.cmake index 7b4d1bc4c9..9f5db9e99c 100644 --- a/cmake/px4_add_gtest.cmake +++ b/cmake/px4_add_gtest.cmake @@ -31,6 +31,30 @@ # ############################################################################ +#============================================================================= +# +# px4_setup_gtest_without_fuzztest +# +# Fetches GTest standalone and stubs out fuzztest cmake functions. +# Used when fuzztest is not available (e.g. TSAN builds where fuzztest's +# coverage instrumentation is incompatible). +# +macro(px4_setup_gtest_without_fuzztest) + include(FetchContent) + FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG v1.16.0 + ) + FetchContent_MakeAvailable(googletest) + + function(link_fuzztest name) + target_link_libraries(${name} PRIVATE gtest gtest_main) + endfunction() + macro(fuzztest_setup_fuzzing_flags) + endmacro() +endmacro() + #============================================================================= # # px4_add_unit_gtest @@ -98,23 +122,28 @@ function(px4_add_functional_gtest) add_executable(${TESTNAME} EXCLUDE_FROM_ALL ${SRC} ${EXTRA_SRCS}) # link the libary to test and gtest - target_link_libraries(${TESTNAME} PRIVATE ${LINKLIBS} gtest_functional_main - px4_layer - px4_platform - uORB - systemlib - cdev - px4_work_queue - px4_daemon - work_queue - parameters - events - perf - tinybson - uorb_msgs - fuzztest::fuzztest # Do not use link_fuzztest() here because that - # also links to fuzztest_gtest_main - test_stubs) # put test_stubs last + set(_FUNCTIONAL_GTEST_LIBS ${LINKLIBS} gtest_functional_main + px4_layer + px4_platform + uORB + systemlib + cdev + px4_work_queue + px4_daemon + work_queue + parameters + events + perf + tinybson + uorb_msgs) + if(TARGET fuzztest::fuzztest) + list(APPEND _FUNCTIONAL_GTEST_LIBS fuzztest::fuzztest) # Do not use link_fuzztest() here because that + # also links to fuzztest_gtest_main + else() + list(APPEND _FUNCTIONAL_GTEST_LIBS gtest) + endif() + list(APPEND _FUNCTIONAL_GTEST_LIBS test_stubs) # put test_stubs last + target_link_libraries(${TESTNAME} PRIVATE ${_FUNCTIONAL_GTEST_LIBS}) target_compile_definitions(${TESTNAME} PRIVATE MODULE_NAME="${TESTNAME}") diff --git a/platforms/posix/CMakeLists.txt b/platforms/posix/CMakeLists.txt index 5b94634f57..a2de775743 100644 --- a/platforms/posix/CMakeLists.txt +++ b/platforms/posix/CMakeLists.txt @@ -28,7 +28,7 @@ add_executable(px4 apps.cpp ) -if (BUILD_TESTING) +if (BUILD_TESTING AND TARGET fuzztest::fuzztest) # Build mavlink fuzz tests. These run other modules and thus cannot be a functional/unit test add_executable(mavlink_fuzz_tests EXCLUDE_FROM_ALL src/px4/common/mavlink_fuzz_tests.cpp diff --git a/platforms/posix/src/px4/common/gtest_runner/CMakeLists.txt b/platforms/posix/src/px4/common/gtest_runner/CMakeLists.txt index 735769d32f..1374618185 100644 --- a/platforms/posix/src/px4/common/gtest_runner/CMakeLists.txt +++ b/platforms/posix/src/px4/common/gtest_runner/CMakeLists.txt @@ -36,4 +36,9 @@ set(SRCS ) px4_add_library(gtest_functional_main ${SRCS}) -target_link_libraries(gtest_functional_main PUBLIC gtest fuzztest::init_fuzztest) +if(TARGET fuzztest::init_fuzztest) + target_link_libraries(gtest_functional_main PUBLIC gtest fuzztest::init_fuzztest) + target_compile_definitions(gtest_functional_main PRIVATE HAVE_FUZZTEST) +else() + target_link_libraries(gtest_functional_main PUBLIC gtest) +endif() diff --git a/platforms/posix/src/px4/common/gtest_runner/gtest_functional_main.cpp b/platforms/posix/src/px4/common/gtest_runner/gtest_functional_main.cpp index bb32ca4212..111aab03e5 100644 --- a/platforms/posix/src/px4/common/gtest_runner/gtest_functional_main.cpp +++ b/platforms/posix/src/px4/common/gtest_runner/gtest_functional_main.cpp @@ -32,7 +32,9 @@ ****************************************************************************/ #include +#ifdef HAVE_FUZZTEST #include +#endif #include @@ -42,8 +44,10 @@ int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); +#ifdef HAVE_FUZZTEST fuzztest::ParseAbslFlags(argc, argv); fuzztest::InitFuzzTest(&argc, &argv); +#endif uORB::Manager::initialize(); param_init(); diff --git a/src/drivers/gnss/septentrio/CMakeLists.txt b/src/drivers/gnss/septentrio/CMakeLists.txt index 3b3cd47ecc..bf0c281b33 100644 --- a/src/drivers/gnss/septentrio/CMakeLists.txt +++ b/src/drivers/gnss/septentrio/CMakeLists.txt @@ -49,14 +49,16 @@ px4_add_module( module.yaml ) -px4_add_functional_gtest(SRC septentrio_fuzz_tests.cpp - LINKLIBS - driver__septentrio - COMPILE_FLAGS - # There warnings come from within fuzztest - -Wno-float-equal - -Wno-sign-compare - -Wno-shadow - -Wno-extra - -Wno-non-template-friend -) +if(TARGET fuzztest::fuzztest) + px4_add_functional_gtest(SRC septentrio_fuzz_tests.cpp + LINKLIBS + driver__septentrio + COMPILE_FLAGS + # There warnings come from within fuzztest + -Wno-float-equal + -Wno-sign-compare + -Wno-shadow + -Wno-extra + -Wno-non-template-friend + ) +endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d3815d44d7..5fe2ac189a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,8 +1,12 @@ -px4_add_git_submodule(TARGET git_fuzztest PATH "fuzztest") - -message(STATUS "Adding fuzztest") -# This will also add GTest -add_subdirectory(fuzztest EXCLUDE_FROM_ALL) +if(CMAKE_BUILD_TYPE STREQUAL "ThreadSanitizer") + message(STATUS "TSAN build: skipping fuzztest, fetching GTest only") + px4_setup_gtest_without_fuzztest() +else() + px4_add_git_submodule(TARGET git_fuzztest PATH "fuzztest") + message(STATUS "Adding fuzztest") + # This will also add GTest + add_subdirectory(fuzztest EXCLUDE_FROM_ALL) +endif() # Ensure there's no -R without any filter expression since that trips newer ctest versions if(TESTFILTER)