1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 02:52:53 +02:00

[Support] On Unix, let the CrashRecoveryContext return the signal code

Before this patch, the CrashRecoveryContext was returning -2 upon a signal, like ExecuteAndWait does. This didn't match the behavior on Windows, where the the exception code was returned.

We now return the signal's code, which optionally allows for re-throwing the signal later. Doing so requires all custom handlers to be removed first, through llvm::sys::unregisterHandlers() which we made a public API.

This is part of https://reviews.llvm.org/D70378
This commit is contained in:
Alexandre Ganea 2020-09-24 08:14:45 -04:00
parent 072701ad39
commit e7d01f8d51
5 changed files with 52 additions and 5 deletions

View File

@ -117,6 +117,8 @@ namespace sys {
/// Context is a system-specific failure context: it is the signal type on
/// Unix; the ExceptionContext on Windows.
void CleanupOnSignal(uintptr_t Context);
void unregisterHandlers();
} // End sys namespace
} // End llvm namespace

View File

@ -375,9 +375,10 @@ static void CrashRecoverySignalHandler(int Signal) {
sigaddset(&SigMask, Signal);
sigprocmask(SIG_UNBLOCK, &SigMask, nullptr);
// As per convention, -2 indicates a crash or timeout as opposed to failure to
// execute (see llvm/include/llvm/Support/Program.h)
int RetCode = -2;
// Return the same error code as if the program crashed, as mentioned in the
// section "Exit Status for Commands":
// https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html
int RetCode = 128 + Signal;
// Don't consider a broken pipe as a crash (see clang/lib/Driver/Driver.cpp)
if (Signal == SIGPIPE)

View File

@ -331,7 +331,7 @@ static void RegisterHandlers() { // Not signal-safe.
registerHandler(S, SignalKind::IsInfo);
}
static void UnregisterHandlers() {
void sys::unregisterHandlers() {
// Restore all of the signal handlers to how they were before we showed up.
for (unsigned i = 0, e = NumRegisteredSignals.load(); i != e; ++i) {
sigaction(RegisteredSignalInfo[i].SigNo,
@ -367,7 +367,7 @@ static RETSIGTYPE SignalHandler(int Sig) {
// crashes when we return and the signal reissues. This also ensures that if
// we crash in our signal handler that the program will terminate immediately
// instead of recursing in the signal handler.
UnregisterHandlers();
sys::unregisterHandlers();
// Unmask all potentially blocked kill signals.
sigset_t SigMask;

View File

@ -869,3 +869,5 @@ static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
#pragma GCC diagnostic warning "-Wformat"
#pragma GCC diagnostic warning "-Wformat-extra-args"
#endif
void sys::unregisterHandlers() {}

View File

@ -7,10 +7,12 @@
//===----------------------------------------------------------------------===//
#include "llvm/ADT/Triple.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/CrashRecoveryContext.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
@ -131,3 +133,43 @@ TEST(CrashRecoveryTest, Abort) {
EXPECT_FALSE(CrashRecoveryContext().RunSafely(A));
}
#endif
// Specifically ensure that programs that signal() or abort() through the
// CrashRecoveryContext can re-throw again their signal, so that `not --crash`
// succeeds.
#ifdef LLVM_ON_UNIX
// See llvm/utils/unittest/UnitTestMain/TestMain.cpp
extern const char *TestMainArgv0;
// Just a reachable symbol to ease resolving of the executable's path.
static cl::opt<std::string> CrashTestStringArg1("crash-test-string-arg1");
TEST(CrashRecoveryTest, UnixCRCReturnCode) {
using namespace llvm::sys;
if (getenv("LLVM_CRC_UNIXCRCRETURNCODE")) {
llvm::CrashRecoveryContext::Enable();
CrashRecoveryContext CRC;
EXPECT_FALSE(CRC.RunSafely(abort));
EXPECT_EQ(CRC.RetCode, 128 + SIGABRT);
// re-throw signal
llvm::sys::unregisterHandlers();
raise(CRC.RetCode - 128);
llvm_unreachable("Should have exited already!");
}
std::string Executable =
sys::fs::getMainExecutable(TestMainArgv0, &CrashTestStringArg1);
StringRef argv[] = {
Executable, "--gtest_filter=CrashRecoveryTest.UnixCRCReturnCode"};
// Add LLVM_CRC_UNIXCRCRETURNCODE to the environment of the child process.
std::vector<StringRef> EnvTable;
EnvTable.push_back("LLVM_CRC_UNIXCRCRETURNCODE=1");
std::string Error;
bool ExecutionFailed;
int RetCode = ExecuteAndWait(Executable, argv, makeArrayRef(EnvTable), {}, 0, 0, &Error,
&ExecutionFailed);
ASSERT_EQ(-2, RetCode);
}
#endif