1 ///===- ThreadCrashReporterTests.cpp - Thread local signal handling tests -===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "support/ThreadCrashReporter.h"
10 #include "support/Threading.h"
11 #include "llvm/Support/Signals.h"
12 #include "gtest/gtest.h"
21 static void infoSignalHandler() { ThreadCrashReporter::runCrashHandlers(); }
23 TEST(ThreadCrashReporterTest
, All
) {
25 // Simulate signals on Windows for unit testing purposes.
26 // The `crash.test` lit test checks the end-to-end integration.
27 auto SignalCurrentThread
= []() { infoSignalHandler(); };
29 llvm::sys::SetInfoSignalFunction(&infoSignalHandler
);
30 auto SignalCurrentThread
= []() { raise(SIGUSR1
); };
33 AsyncTaskRunner Runner
;
34 auto SignalAnotherThread
= [&]() {
35 Runner
.runAsync("signal another thread", SignalCurrentThread
);
41 ThreadCrashReporter
ScopedReporter([&Called
]() { Called
= true; });
42 // Check handler gets called when a signal gets delivered to the current
45 SignalCurrentThread();
48 // Check handler does not get called when another thread gets signalled.
50 SignalAnotherThread();
53 // Check handler does not get called when the reporter object goes out of
56 SignalCurrentThread();
59 std::string Order
= "";
61 ThreadCrashReporter
ScopedReporter([&Order
] { Order
.push_back('a'); });
63 ThreadCrashReporter
ScopedReporter([&Order
] { Order
.push_back('b'); });
64 SignalCurrentThread();
66 // Check that handlers are called in LIFO order.
67 EXPECT_EQ(Order
, "ba");
69 // Check that current handler is the only one after the nested scope is
71 SignalCurrentThread();
72 EXPECT_EQ(Order
, "baa");