Skip to content

Commit

Permalink
De-duplicate the disabling of crash dialogs in our unit tests
Browse files Browse the repository at this point in the history
The code was duplicated in multiple places.

Pick-to: 6.5 6.8 6.9
Change-Id: If2ab30b7afbf6d2f99c9fffd999218802b734d5e
Reviewed-by: Ahmad Samir <[email protected]>
  • Loading branch information
thiagomacieira committed Jan 20, 2025
1 parent bec4914 commit b119fee
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 83 deletions.
26 changes: 0 additions & 26 deletions tests/auto/corelib/io/qprocess/crasher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ qt_internal_add_executable(testProcessCrash
CORE_LIBRARY None
SOURCES
main.cpp
../../../../../shared/disablecoredumps.cpp
)
2 changes: 0 additions & 2 deletions tests/auto/corelib/io/qprocess/testProcessCrash/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
20 changes: 19 additions & 1 deletion tests/auto/corelib/io/qprocess/tst_qprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
#####################################################################
15 changes: 0 additions & 15 deletions tests/auto/network/socket/qtcpserver/crashingServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions tests/auto/testlib/selftests/crashes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
10 changes: 0 additions & 10 deletions tests/auto/testlib/selftests/crashes/tst_crashes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/auto/testlib/selftests/silent_fatal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
23 changes: 0 additions & 23 deletions tests/auto/testlib/selftests/silent_fatal/tst_silent_fatal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions tests/shared/disablecoredumps.cpp
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit b119fee

Please sign in to comment.