From 922f60c140785071041a819e15704bdfe07fb593 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 5 Sep 2023 19:02:16 -0700 Subject: [PATCH] QtTest: block almost all Unix signals in the WatchDog thread MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. Change-Id: I512648fd617741199e67fffd17822cdcdf30926c Reviewed-by: Edward Welbourne Reviewed-by: Tor Arne Vestbø (cherry picked from commit da0571d87890b587154a6d9124ac29dc30b151dd) Reviewed-by: Qt Cherry-pick Bot --- src/testlib/qtestcase.cpp | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index fa0042f59f8..b04648eea8f 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 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");