diff --git a/platforms/posix/src/px4/windows/tests/CMakeLists.txt b/platforms/posix/src/px4/windows/tests/CMakeLists.txt index 4a1a0b41f7..fd0033eeed 100644 --- a/platforms/posix/src/px4/windows/tests/CMakeLists.txt +++ b/platforms/posix/src/px4/windows/tests/CMakeLists.txt @@ -43,11 +43,24 @@ if(NOT BUILD_TESTING) return() endif() +# Shared static-init hook that silences the MSVC Debug CRT invalid +# parameter handler so tests that exercise bad-fd / bad-handle paths do +# not abort with a debug-time assertion dialog. Linked into every +# Windows shim test target as an EXTRA_SRC. +set(_px4_windows_shim_silencer + ${CMAKE_CURRENT_SOURCE_DIR}/test_main_silence.cpp +) + # Headers-only tests (libgen, dirent, sched, syslog, mman, time, unistd, # getopt, fcntl). The translation unit pulls the shim headers via the -# existing windows_shim include path; no extra link libraries are -# required beyond gtest itself. -px4_add_unit_gtest(SRC test_windows_shim_headers.cpp) +# existing windows_shim include path; the inline fcntl() routes +# O_NONBLOCK changes through ioctlsocket() so we need ws2_32 to satisfy +# the symbol even on the headers-only target. +px4_add_unit_gtest( + SRC test_windows_shim_headers.cpp + EXTRA_SRCS ${_px4_windows_shim_silencer} + LINKLIBS ws2_32 +) # Source-defined shim tests (errno_map, env, sysconf, mman, flock, dlfcn, # ids, if_query, resolver, socket loopback). Pulls in the standalone @@ -69,7 +82,7 @@ set(_px4_windows_shim_test_srcs px4_add_unit_gtest( SRC test_windows_shim_runtime.cpp - EXTRA_SRCS ${_px4_windows_shim_test_srcs} + EXTRA_SRCS ${_px4_windows_shim_test_srcs} ${_px4_windows_shim_silencer} INCLUDES ${PX4_SOURCE_DIR}/platforms/posix/include/windows_shim ${_px4_windows_shim_root}/include @@ -79,6 +92,7 @@ px4_add_unit_gtest( # poll() inline implementation - header-only test. px4_add_unit_gtest( SRC test_windows_shim_poll.cpp + EXTRA_SRCS ${_px4_windows_shim_silencer} INCLUDES ${PX4_SOURCE_DIR}/platforms/posix/include/windows_shim LINKLIBS ws2_32 @@ -88,6 +102,7 @@ px4_add_unit_gtest( # POSIX names route to MSVC CRT primitives via inline shim wrappers. px4_add_unit_gtest( SRC test_windows_shim_io.cpp + EXTRA_SRCS ${_px4_windows_shim_silencer} INCLUDES ${PX4_SOURCE_DIR}/platforms/posix/include/windows_shim ) diff --git a/platforms/posix/src/px4/windows/tests/test_main_silence.cpp b/platforms/posix/src/px4/windows/tests/test_main_silence.cpp new file mode 100644 index 0000000000..3d134e299f --- /dev/null +++ b/platforms/posix/src/px4/windows/tests/test_main_silence.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** + * + * Copyright (C) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file test_main_silence.cpp + * + * Suppress the MSVC Debug CRT invalid-parameter handler so unit tests that + * intentionally exercise bad-fd / bad-handle paths (e.g. fcntl(0, 0xBAD), + * flock(-1, ...), _open on a closed fd) do not abort with a debug-time + * assertion dialog. Without this hook the Debug CRT raises Watson and + * terminates the process before gtest can record the EXPECT_* result. + * + * Linked into every shim unit test target via + * platforms/posix/src/px4/windows/tests/CMakeLists.txt; takes effect at + * static-init time before main() runs gtest. + */ + +#ifdef _WIN32 +#include +#include + +namespace +{ +static void silent_invalid_param(const wchar_t *, const wchar_t *, const wchar_t *, + unsigned, uintptr_t) {} +struct InstallSilencer { + InstallSilencer() + { + _set_invalid_parameter_handler(silent_invalid_param); + _CrtSetReportMode(_CRT_ASSERT, 0); + _CrtSetReportMode(_CRT_ERROR, 0); + _CrtSetReportMode(_CRT_WARN, 0); + } +}; +static InstallSilencer kInstallSilencer; +} // namespace +#endif