summaryrefslogtreecommitdiffstats
path: root/src/testlib/qtestcase.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2023-09-05 19:02:16 -0700
committerThiago Macieira <thiago.macieira@intel.com>2023-10-01 16:29:42 -0700
commitda0571d87890b587154a6d9124ac29dc30b151dd (patch)
tree378a92d8c5945588d0219efc2b8bf5511e0d3f08 /src/testlib/qtestcase.cpp
parent011d71664bcebcfdbaee0f7ed7c9422f9e4a5ceb (diff)
QtTest: block almost all Unix signals in the WatchDog thread
Signals delivered via kill(2) are delivered to any thread that is running, so let's make sure the WatchDog thread doesn't get them. This may be hiding bugs in the user's handler code, but in simple unit tests the user may not be expecting there to be multiple threads in the first place. Pick-to: 6.6 Change-Id: I512648fd617741199e67fffd17822cdcdf30926c Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'src/testlib/qtestcase.cpp')
-rw-r--r--src/testlib/qtestcase.cpp32
1 files changed, 28 insertions, 4 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index cc687fe369f..7ab5e2553c4 100644
--- a/src/testlib/qtestcase.cpp
+++ b/src/testlib/qtestcase.cpp
@@ -217,6 +217,8 @@ static std::string asyncSafeToString(int n)
#endif // defined(Q_OS_UNIX)
} // unnamed namespace
+[[maybe_unused]] static void blockUnixSignals();
+
static bool alreadyDebugging()
{
#if defined(Q_OS_LINUX)
@@ -1287,6 +1289,7 @@ public:
void run() override
{
+ blockUnixSignals();
auto locker = qt_unique_lock(mutex);
expecting.store(TestFunctionStart, std::memory_order_release);
waitCondition.notify_all();
@@ -1783,9 +1786,8 @@ bool reportResult(bool success, qxp::function_ref<const char *()> lhs,
} // namespace QTest
-namespace {
#if defined(Q_OS_WIN)
-
+namespace {
// Helper class for resolving symbol names by dynamically loading "dbghelp.dll".
class DebugSymbolResolver
{
@@ -1939,9 +1941,17 @@ private:
return EXCEPTION_EXECUTE_HANDLER;
}
};
+} // unnamed namespace
using FatalSignalHandler = WindowsFaultHandler;
+inline void blockUnixSignals()
+{
+ // Windows does have C signals, but doesn't use them for the purposes we're
+ // talking about here
+}
+
#elif defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
+namespace {
class FatalSignalHandler
{
public:
@@ -2192,12 +2202,26 @@ private:
static bool pauseOnCrash;
};
bool FatalSignalHandler::pauseOnCrash = false;
+} // unnamed namespace
+
+inline void blockUnixSignals()
+{
+ // Block most Unix signals so the WatchDog thread won't be called when
+ // external signals are delivered, thus avoiding interfering with the test
+ sigset_t set;
+ sigfillset(&set);
+
+ // we allow the crashing signals, in case we have bugs
+ for (int signo : FatalSignalHandler::fatalSignals)
+ sigdelset(&set, signo);
+
+ pthread_sigmask(SIG_BLOCK, &set, nullptr);
+}
#else // Q_OS_WASM or weird systems
class FatalSignalHandler {};
+inline void blockUnixSignals() {}
#endif // Q_OS_* choice
-} // unnamed namespace
-
static void initEnvironment()
{
qputenv("QT_QTESTLIB_RUNNING", "1");