test(windows-shim): silence MSVC Debug CRT invalid-parameter handler

Several Windows shim unit tests intentionally exercise bad-fd /
bad-handle paths (fcntl(0, 0xBAD), flock(-1, ...), _open() on a
closed fd, etc.). On MSVC's Debug CRT those calls trigger the
invalid-parameter handler, which by default raises Watson and
terminates the process before gtest can record the EXPECT_*
result; the test binary then exits with a non-zero status without
ever reaching the [  PASSED  ] line.

Add a static-init silencer (test_main_silence.cpp) that installs a
no-op _set_invalid_parameter_handler and disables the
_CrtSetReportMode dialogs for assertions, errors, and warnings.
The hook runs before main() and is linked into every shim unit
test target via the EXTRA_SRCS argument to px4_add_unit_gtest()
in the test CMakeLists.txt.

Same CMakeLists.txt change adds an explicit link to ws2_32 for
the headers-only test target: the inline fcntl() in the shim
routes O_NONBLOCK changes through ioctlsocket(), so the symbol
must be resolvable at link time even when the test does not call
fcntl() itself.

Signed-off-by: Nuno Marques <n.marques21@hotmail.com>
This commit is contained in:
Nuno Marques
2026-05-07 18:06:24 -07:00
parent ce12bbaefd
commit 3bb7a88607
2 changed files with 86 additions and 4 deletions
@@ -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
)
@@ -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 <stdlib.h>
#include <crtdbg.h>
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