De-duplicate the disabling of crash dialogs in our unit tests

The code was duplicated in multiple places.

Pick-to: 6.5
Change-Id: If2ab30b7afbf6d2f99c9fffd999218802b734d5e
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
(cherry picked from commit b119fee87d5579dc1c4bfc2f508b0e3a75d69fa3)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit f9bdb80ef1f0d56d9585df61f0e209fe0b718443)
This commit is contained in:
Thiago Macieira 2025-01-17 16:38:30 -08:00 committed by Qt Cherry-pick Bot
parent 5c9b69feff
commit f1b03014c4
11 changed files with 64 additions and 83 deletions

View File

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

View File

@ -10,4 +10,5 @@ qt_internal_add_executable(testProcessCrash
CORE_LIBRARY None
SOURCES
main.cpp
../../../../../shared/disablecoredumps.cpp
)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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