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.
This commit is contained in:
Julian Oes 2026-02-19 05:43:44 +13:00
parent 64ddfebfc6
commit 5dbf62cd11
No known key found for this signature in database
GPG Key ID: F0ED380FEA56DE41
6 changed files with 79 additions and 35 deletions

View File

@ -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}")

View File

@ -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

View File

@ -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()

View File

@ -32,7 +32,9 @@
****************************************************************************/
#include <gtest/gtest.h>
#ifdef HAVE_FUZZTEST
#include <fuzztest/init_fuzztest.h>
#endif
#include <uORB/Subscription.hpp>
@ -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();

View File

@ -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()

View File

@ -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)