1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 04:32:44 +01:00

[LLD][COFF] When using LLD-as-a-library, always prevent re-entrance on failures

This is a follow-up for D70378 (Cover usage of LLD as a library).

While debugging an intermittent failure on a bot, I recalled this scenario which
causes the issue:

1.When executing lld/test/ELF/invalid/symtab-sh-info.s L45, we reach
  lld:🧝:Obj-File::ObjFile() which goes straight into its base ELFFileBase(),
  then ELFFileBase::init().
2.At that point fatal() is thrown in lld/ELF/InputFiles.cpp L381, leaving a
  half-initialized ObjFile instance.
3.We then end up in lld::exitLld() and since we are running with LLD_IN_TEST, we
  hapily restore the control flow to CrashRecoveryContext::RunSafely() then back
  in lld::safeLldMain().
4.Before this patch, we called errorHandler().reset() just after, and this
  attempted to reset the associated SpecificAlloc<ObjFile<ELF64LE>>. That tried
  to free the half-initialized ObjFile instance, and more precisely its
  ObjFile::dwarf member.

Sometimes that worked, sometimes it failed and was catched by the
CrashRecoveryContext. This scenario was the reason we called
errorHandler().reset() through a CrashRecoveryContext.

But in some rare cases, the above repro somehow corrupted the heap, creating a
stack overflow. When the CrashRecoveryContext's filter (that is,
__except (ExceptionFilter(GetExceptionInformation()))) tried to handle the
exception, it crashed again since the stack was exhausted -- and that took the
whole application down. That is the issue seen on the bot. Locally it happens
about 1 times out of 15.

Now this situation can happen anywhere in LLD. Since catching stack overflows is
not a reliable scenario ATM when using CrashRecoveryContext, we're now
preventing further re-entrance when such failures occur, by signaling
lld::SafeReturn::canRunAgain=false. When running with LLD_IN_TEST=2 (or above),
only one iteration will be executed, instead of two.

Differential Revision: https://reviews.llvm.org/D88348
This commit is contained in:
Alexandre Ganea 2020-11-12 08:14:20 -05:00
parent 8fbbc2d925
commit 733e59acce
4 changed files with 34 additions and 3 deletions

View File

@ -102,6 +102,10 @@ public:
LLVM_ATTRIBUTE_NORETURN
void HandleExit(int RetCode);
/// Throw again a signal or an exception, after it was catched once by a
/// CrashRecoveryContext.
static bool throwIfCrash(int RetCode);
/// In case of a crash, this is the crash identifier.
int RetCode = 0;

View File

@ -213,8 +213,9 @@ public:
/// Equivalent to ::exit(), except when running inside a CrashRecoveryContext.
/// In that case, the control flow will resume after RunSafely(), like for a
/// crash, rather than exiting the current process.
/// Use \arg NoCleanup for calling _exit() instead of exit().
LLVM_ATTRIBUTE_NORETURN
static void Exit(int RetCode);
static void Exit(int RetCode, bool NoCleanup = false);
};
}

View File

@ -442,6 +442,26 @@ void CrashRecoveryContext::HandleExit(int RetCode) {
llvm_unreachable("Most likely setjmp wasn't called!");
}
bool CrashRecoveryContext::throwIfCrash(int RetCode) {
#if defined(_WIN32)
// On Windows, the high bits are reserved for kernel return codes. Values
// starting with 0x80000000 are reserved for "warnings"; values of 0xC0000000
// and up are for "errors". In practice, both are interpreted as a
// non-continuable signal.
unsigned Code = ((unsigned)RetCode & 0xF0000000) >> 28;
if (Code != 0xC && Code != 8)
return false;
::RaiseException(RetCode, 0, 0, NULL);
#else
// On Unix, signals are represented by return codes of 128 or higher.
if (RetCode <= 128)
return false;
llvm::sys::unregisterHandlers();
raise(RetCode - 128);
#endif
return true;
}
// FIXME: Portability.
static void setThreadBackgroundPriority() {
#ifdef __APPLE__

View File

@ -20,6 +20,8 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include <stddef.h> // for _Exit
using namespace llvm;
using namespace sys;
@ -91,10 +93,14 @@ static bool coreFilesPrevented = !LLVM_ENABLE_CRASH_DUMPS;
bool Process::AreCoreFilesPrevented() { return coreFilesPrevented; }
LLVM_ATTRIBUTE_NORETURN
void Process::Exit(int RetCode) {
void Process::Exit(int RetCode, bool NoCleanup) {
if (CrashRecoveryContext *CRC = CrashRecoveryContext::GetCurrent())
CRC->HandleExit(RetCode);
::exit(RetCode);
if (NoCleanup)
_Exit(RetCode);
else
::exit(RetCode);
}
// Include the platform-specific parts of this class.