summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <[email protected]>2025-01-17 16:38:30 -0800
committerThiago Macieira <[email protected]>2025-01-19 19:24:28 -0800
commitb119fee87d5579dc1c4bfc2f508b0e3a75d69fa3 (patch)
tree2676788f69a0af43823a63f8b9bda11db11be471
parentbec49144f809ef8c5c61099ce46241c16b20b180 (diff)
De-duplicate the disabling of crash dialogs in our unit tests
The code was duplicated in multiple places. Pick-to: 6.5 6.8 6.9 Change-Id: If2ab30b7afbf6d2f99c9fffd999218802b734d5e Reviewed-by: Ahmad Samir <[email protected]>
-rw-r--r--tests/auto/corelib/io/qprocess/crasher.h26
-rw-r--r--tests/auto/corelib/io/qprocess/testProcessCrash/CMakeLists.txt1
-rw-r--r--tests/auto/corelib/io/qprocess/testProcessCrash/main.cpp2
-rw-r--r--tests/auto/corelib/io/qprocess/tst_qprocess.cpp20
-rw-r--r--tests/auto/network/socket/qtcpserver/crashingServer/CMakeLists.txt7
-rw-r--r--tests/auto/network/socket/qtcpserver/crashingServer/main.cpp15
-rw-r--r--tests/auto/testlib/selftests/crashes/CMakeLists.txt1
-rw-r--r--tests/auto/testlib/selftests/crashes/tst_crashes.cpp10
-rw-r--r--tests/auto/testlib/selftests/silent_fatal/CMakeLists.txt1
-rw-r--r--tests/auto/testlib/selftests/silent_fatal/tst_silent_fatal.cpp23
-rw-r--r--tests/shared/disablecoredumps.cpp41
11 files changed, 64 insertions, 83 deletions
diff --git a/tests/auto/corelib/io/qprocess/crasher.h b/tests/auto/corelib/io/qprocess/crasher.h
index f1ca82a86b7..6b27e4f6790 100644
--- a/tests/auto/corelib/io/qprocess/crasher.h
+++ b/tests/auto/corelib/io/qprocess/crasher.h
@@ -5,37 +5,11 @@
#if defined(_MSC_VER)
# include <intrin.h>
#endif
-#if __has_include(<signal.h>)
-# include <signal.h>
-#endif
-#if __has_include(<sys/resource.h>)
-# include <sys/resource.h>
-#endif
-
#ifndef __has_builtin
# define __has_builtin(x) 0
#endif
namespace tst_QProcessCrash {
-struct NoCoreDumps
-{
-#if defined(RLIMIT_CORE)
- struct rlimit rlim;
- NoCoreDumps()
- {
- if (getrlimit(RLIMIT_CORE, &rlim) == 0 && rlim.rlim_cur != 0) {
- struct rlimit newrlim = rlim;
- newrlim.rlim_cur = 0;
- setrlimit(RLIMIT_CORE, &newrlim);
- }
- }
- ~NoCoreDumps()
- {
- setrlimit(RLIMIT_CORE, &rlim);
- }
-#endif // RLIMIT_CORE
-};
-
void crashFallback(volatile int *ptr = nullptr)
{
*ptr = 0;
diff --git a/tests/auto/corelib/io/qprocess/testProcessCrash/CMakeLists.txt b/tests/auto/corelib/io/qprocess/testProcessCrash/CMakeLists.txt
index 2f167899eb8..02c04c6ad16 100644
--- a/tests/auto/corelib/io/qprocess/testProcessCrash/CMakeLists.txt
+++ b/tests/auto/corelib/io/qprocess/testProcessCrash/CMakeLists.txt
@@ -10,4 +10,5 @@ qt_internal_add_executable(testProcessCrash
CORE_LIBRARY None
SOURCES
main.cpp
+ ../../../../../shared/disablecoredumps.cpp
)
diff --git a/tests/auto/corelib/io/qprocess/testProcessCrash/main.cpp b/tests/auto/corelib/io/qprocess/testProcessCrash/main.cpp
index a5f1eef88a9..384ac6f17d8 100644
--- a/tests/auto/corelib/io/qprocess/testProcessCrash/main.cpp
+++ b/tests/auto/corelib/io/qprocess/testProcessCrash/main.cpp
@@ -7,8 +7,6 @@ using namespace tst_QProcessCrash;
int main()
{
- [[maybe_unused]] // NoCoreDumps may be an empty struct, not a RAII class
- NoCoreDumps disableCoreDumps;
crash();
return 0;
}
diff --git a/tests/auto/corelib/io/qprocess/tst_qprocess.cpp b/tests/auto/corelib/io/qprocess/tst_qprocess.cpp
index 704696e2c24..355e75abdc8 100644
--- a/tests/auto/corelib/io/qprocess/tst_qprocess.cpp
+++ b/tests/auto/corelib/io/qprocess/tst_qprocess.cpp
@@ -22,6 +22,7 @@
#include <qplatformdefs.h>
#ifdef Q_OS_UNIX
# include <private/qcore_unix_p.h>
+# include <sys/resource.h>
# include <sys/wait.h>
#endif
@@ -1491,8 +1492,25 @@ void tst_QProcess::createProcessArgumentsModifier()
static constexpr int sigs[] = { SIGABRT, SIGILL, SIGSEGV };
struct DisableCrashLogger
{
+#if defined(RLIMIT_CORE)
// disable core dumps too
- tst_QProcessCrash::NoCoreDumps disableCoreDumps {};
+ struct NoCoreDumps {
+ struct rlimit rlim;
+ NoCoreDumps()
+ {
+ if (getrlimit(RLIMIT_CORE, &rlim) == 0 && rlim.rlim_cur != 0) {
+ struct rlimit newrlim = rlim;
+ newrlim.rlim_cur = 0;
+ setrlimit(RLIMIT_CORE, &newrlim);
+ }
+ }
+ ~NoCoreDumps()
+ {
+ setrlimit(RLIMIT_CORE, &rlim);
+ }
+ } disableCoreDumps;
+#endif // RLIMIT_CORE
+
std::array<struct sigaction, std::size(sigs)> oldhandlers;
DisableCrashLogger()
{
diff --git a/tests/auto/network/socket/qtcpserver/crashingServer/CMakeLists.txt b/tests/auto/network/socket/qtcpserver/crashingServer/CMakeLists.txt
index bb1feb02379..348075f8d3f 100644
--- a/tests/auto/network/socket/qtcpserver/crashingServer/CMakeLists.txt
+++ b/tests/auto/network/socket/qtcpserver/crashingServer/CMakeLists.txt
@@ -1,17 +1,12 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
-#####################################################################
-## crashingServer Binary:
-#####################################################################
-
qt_internal_add_executable(crashingServer
OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/"
SOURCES
main.cpp
+ ../../../../../shared/disablecoredumps.cpp
LIBRARIES
Qt::Network
)
-## Scopes:
-#####################################################################
diff --git a/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp b/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp
index 63bd7b1e078..ed7f557c18e 100644
--- a/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp
+++ b/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp
@@ -7,27 +7,12 @@
#include <QtCore/qcoreapplication.h>
-#if defined(Q_OS_WIN) && defined(Q_CC_MSVC)
-# include <crtdbg.h>
-#endif
#ifdef Q_OS_UNIX
-# include <sys/resource.h>
# include <unistd.h>
#endif
int main(int argc, char *argv[])
{
-#if defined(Q_OS_WIN) && defined(Q_CC_MSVC)
- // Windows: Suppress crash notification dialog.
- _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
-#elif defined(RLIMIT_CORE)
- // Unix: set our core dump limit to zero to request no dialogs.
- if (struct rlimit rlim; getrlimit(RLIMIT_CORE, &rlim) == 0) {
- rlim.rlim_cur = 0;
- setrlimit(RLIMIT_CORE, &rlim);
- }
-#endif
-
QCoreApplication app(argc, argv);
if (argc < 1) {
fprintf(stderr, "Need a port number\n");
diff --git a/tests/auto/testlib/selftests/crashes/CMakeLists.txt b/tests/auto/testlib/selftests/crashes/CMakeLists.txt
index c11bab90d0b..324b3cbedcd 100644
--- a/tests/auto/testlib/selftests/crashes/CMakeLists.txt
+++ b/tests/auto/testlib/selftests/crashes/CMakeLists.txt
@@ -11,6 +11,7 @@ qt_internal_add_executable(crashes
OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
SOURCES
tst_crashes.cpp
+ ../../../../shared/disablecoredumps.cpp
LIBRARIES
Qt::Test
)
diff --git a/tests/auto/testlib/selftests/crashes/tst_crashes.cpp b/tests/auto/testlib/selftests/crashes/tst_crashes.cpp
index 4ba8a426e6b..d3dc2922cb0 100644
--- a/tests/auto/testlib/selftests/crashes/tst_crashes.cpp
+++ b/tests/auto/testlib/selftests/crashes/tst_crashes.cpp
@@ -21,16 +21,6 @@ private slots:
void tst_Crashes::crash()
{
-#if defined(Q_OS_WIN)
- //we avoid the error dialogbox to appear on windows
- SetErrorMode( SEM_NOGPFAULTERRORBOX | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
-#elif defined(RLIMIT_CORE)
- // Unix: set our core dump limit to zero to request no dialogs.
- if (struct rlimit rlim; getrlimit(RLIMIT_CORE, &rlim) == 0) {
- rlim.rlim_cur = 0;
- setrlimit(RLIMIT_CORE, &rlim);
- }
-#endif
/*
We deliberately dereference an invalid but non-zero address;
it should be non-zero because a few platforms may have special crash behavior
diff --git a/tests/auto/testlib/selftests/silent_fatal/CMakeLists.txt b/tests/auto/testlib/selftests/silent_fatal/CMakeLists.txt
index e4b59e399ff..2cdc714ed0b 100644
--- a/tests/auto/testlib/selftests/silent_fatal/CMakeLists.txt
+++ b/tests/auto/testlib/selftests/silent_fatal/CMakeLists.txt
@@ -7,6 +7,7 @@ qt_internal_add_executable(silent_fatal
OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
SOURCES
tst_silent_fatal.cpp
+ ../../../../shared/disablecoredumps.cpp
LIBRARIES
Qt::TestPrivate
)
diff --git a/tests/auto/testlib/selftests/silent_fatal/tst_silent_fatal.cpp b/tests/auto/testlib/selftests/silent_fatal/tst_silent_fatal.cpp
index a7b4324992d..57aa1f702cc 100644
--- a/tests/auto/testlib/selftests/silent_fatal/tst_silent_fatal.cpp
+++ b/tests/auto/testlib/selftests/silent_fatal/tst_silent_fatal.cpp
@@ -5,29 +5,6 @@
#include <QTest>
#include <private/qtestlog_p.h>
-#if defined(Q_OS_WIN) && defined(Q_CC_MSVC)
-# include <crtdbg.h>
-#endif
-#ifdef Q_OS_UNIX
-# include <sys/resource.h>
-# include <unistd.h>
-#endif
-
-void disableCoreDumps()
-{
-#if defined(Q_OS_WIN) && defined(Q_CC_MSVC)
- // Windows: Suppress crash notification dialog.
- _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
-#elif defined(RLIMIT_CORE)
- // Unix: set our core dump limit to zero to request no dialogs.
- if (struct rlimit rlim; getrlimit(RLIMIT_CORE, &rlim) == 0) {
- rlim.rlim_cur = 0;
- setrlimit(RLIMIT_CORE, &rlim);
- }
-#endif
-}
-Q_CONSTRUCTOR_FUNCTION(disableCoreDumps)
-
class tst_SilentFatal : public QObject
{
Q_OBJECT
diff --git a/tests/shared/disablecoredumps.cpp b/tests/shared/disablecoredumps.cpp
new file mode 100644
index 00000000000..326c51b3afc
--- /dev/null
+++ b/tests/shared/disablecoredumps.cpp
@@ -0,0 +1,41 @@
+// Copyright (C) 2025 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+// This file is added to some test helpers that don't link to Qt, so don't
+// use Qt #includes here.
+
+#ifdef _WIN32
+# include <windows.h>
+#else
+# include <unistd.h>
+#endif
+#if __has_include(<sys/resource.h>)
+# include <sys/resource.h>
+#endif
+#ifdef _MSC_VER
+# include <crtdbg.h>
+#endif
+
+static void disableCoreDumps()
+{
+#ifdef _WIN32
+ // Windows: suppress the OS error dialog box.
+ SetErrorMode(SEM_NOGPFAULTERRORBOX | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
+# ifdef _MSC_VER
+ // MSVC: Suppress runtime's crash notification dialog.
+ _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
+# endif
+#elif defined(RLIMIT_CORE)
+ // Unix: set our core dump limit to zero to request no dialogs.
+ if (struct rlimit rlim; getrlimit(RLIMIT_CORE, &rlim) == 0) {
+ rlim.rlim_cur = 0;
+ setrlimit(RLIMIT_CORE, &rlim);
+ }
+#endif
+}
+
+struct DisableCoreDumps
+{
+ DisableCoreDumps() { disableCoreDumps(); }
+};
+[[maybe_unused]] DisableCoreDumps disableCoreDumpsConstructorFunction;