| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "MmapFaultHandler.h" |
| 8 |
|
| 9 |
#if defined(XP_UNIX) && !defined(XP_DARWIN) && !defined(__wasi__) |
| 10 |
|
| 11 |
# include "mozilla/Assertions.h" |
| 12 |
# include "mozilla/Atomics.h" |
| 13 |
# include "mozilla/ThreadLocal.h" |
| 14 |
# include <signal.h> |
| 15 |
# include <cstring> |
| 16 |
|
| 17 |
static MOZ_THREAD_LOCAL(MmapAccessScope*) sMmapAccessScope; |
| 18 |
|
| 19 |
static struct sigaction sPrevSIGBUSHandler; |
| 20 |
|
| 21 |
static void MmapSIGBUSHandler(int signum, siginfo_t* info, void* context) { |
| 22 |
MOZ_RELEASE_ASSERT(signum == SIGBUS); |
| 23 |
|
| 24 |
MmapAccessScope* mas = sMmapAccessScope.get(); |
| 25 |
|
| 26 |
if (mas && mas->IsInsideBuffer(info->si_addr)) { |
| 27 |
// The address is inside the buffer, handle the failure. |
| 28 |
siglongjmp(mas->mJmpBuf, signum); |
| 29 |
} |
| 30 |
|
| 31 |
// This signal is not caused by accessing region protected by MmapAccessScope. |
| 32 |
// Forward the signal to the next handler. |
| 33 |
if (sPrevSIGBUSHandler.sa_flags & SA_SIGINFO) { |
| 34 |
sPrevSIGBUSHandler.sa_sigaction(signum, info, context); |
| 35 |
} else if (sPrevSIGBUSHandler.sa_handler == SIG_DFL || |
| 36 |
sPrevSIGBUSHandler.sa_handler == SIG_IGN) { |
| 37 |
// There is no next handler. Uninstalling our handler and returning will |
| 38 |
// cause a crash. |
| 39 |
sigaction(signum, &sPrevSIGBUSHandler, nullptr); |
| 40 |
} else { |
| 41 |
sPrevSIGBUSHandler.sa_handler(signum); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
mozilla::Atomic<bool> gSIGBUSHandlerInstalled(false); |
| 46 |
mozilla::Atomic<bool> gSIGBUSHandlerInstalling(false); |
| 47 |
|
| 48 |
void InstallMmapFaultHandler() { |
| 49 |
// This function is called from MmapAccessScope's constructor because there is |
| 50 |
// no single point where we could install the handler during startup. This |
| 51 |
// means that it's called quite often, so to minimize using of the mutex we |
| 52 |
// first check the atomic variable outside the lock. |
| 53 |
if (gSIGBUSHandlerInstalled) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
if (gSIGBUSHandlerInstalling.compareExchange(false, true)) { |
| 58 |
sMmapAccessScope.infallibleInit(); |
| 59 |
|
| 60 |
struct sigaction busHandler; |
| 61 |
busHandler.sa_flags = SA_SIGINFO | SA_NODEFER | SA_ONSTACK; |
| 62 |
busHandler.sa_sigaction = MmapSIGBUSHandler; |
| 63 |
sigemptyset(&busHandler.sa_mask); |
| 64 |
if (sigaction(SIGBUS, &busHandler, &sPrevSIGBUSHandler)) { |
| 65 |
MOZ_CRASH("Unable to install SIGBUS handler"); |
| 66 |
} |
| 67 |
|
| 68 |
MOZ_ASSERT(!gSIGBUSHandlerInstalled); |
| 69 |
gSIGBUSHandlerInstalled = true; |
| 70 |
} else { |
| 71 |
// Just spin lock here. It should not take a substantial amount |
| 72 |
// of time, so a mutex would likely be a spin lock anyway, and |
| 73 |
// this avoids the need to new up a static mutex from within |
| 74 |
// mozglue/misc, which complicates things with |
| 75 |
// check_vanilla_allocations.py |
| 76 |
while (!gSIGBUSHandlerInstalled) { |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
MmapAccessScope::MmapAccessScope(void* aBuf, uint32_t aBufLen, |
| 82 |
const char* aFilename) { |
| 83 |
// Install signal handler if it wasn't installed yet. |
| 84 |
InstallMmapFaultHandler(); |
| 85 |
|
| 86 |
// We'll handle the signal only if the crashing address is inside this buffer. |
| 87 |
mBuf = aBuf; |
| 88 |
mBufLen = aBufLen; |
| 89 |
mFilename = aFilename; |
| 90 |
|
| 91 |
SetThreadLocalScope(); |
| 92 |
} |
| 93 |
|
| 94 |
MmapAccessScope::~MmapAccessScope() { |
| 95 |
MOZ_RELEASE_ASSERT(sMmapAccessScope.get() == this); |
| 96 |
sMmapAccessScope.set(mPreviousScope); |
| 97 |
} |
| 98 |
|
| 99 |
void MmapAccessScope::SetThreadLocalScope() { |
| 100 |
// mJmpBuf is set outside of this classs for reasons mentioned in the header |
| 101 |
// file, but we need to initialize the member here too to make Coverity happy. |
| 102 |
memset(mJmpBuf, 0, sizeof(sigjmp_buf)); |
| 103 |
|
| 104 |
// If MmapAccessScopes are nested, save the previous one and restore it in |
| 105 |
// the destructor. |
| 106 |
mPreviousScope = sMmapAccessScope.get(); |
| 107 |
|
| 108 |
// MmapAccessScope is now set up (except mJmpBuf for reasons mentioned in the |
| 109 |
// header file). Store the pointer in a thread-local variable sMmapAccessScope |
| 110 |
// so we can use it in the handler if the signal is triggered. |
| 111 |
sMmapAccessScope.set(this); |
| 112 |
} |
| 113 |
|
| 114 |
bool MmapAccessScope::IsInsideBuffer(void* aPtr) { |
| 115 |
return aPtr >= mBuf && aPtr < (void*)((char*)mBuf + mBufLen); |
| 116 |
} |
| 117 |
|
| 118 |
void MmapAccessScope::CrashWithInfo(void* aPtr) { |
| 119 |
// All we have is the buffer and the crashing address. |
| 120 |
MOZ_CRASH_UNSAFE_PRINTF( |
| 121 |
"SIGBUS received when accessing mmaped file [buffer=%p, " |
| 122 |
"buflen=%u, address=%p, filename=%s]", |
| 123 |
mBuf, mBufLen, aPtr, mFilename); |
| 124 |
} |
| 125 |
|
| 126 |
#endif |