Source code
Revision control
Copy as Markdown
Other Tools
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
// PHC is a probabilistic heap checker. A tiny fraction of randomly chosen heap
// allocations are subject to some expensive checking via the use of OS page
// access protection. A failed check triggers a crash, whereupon useful
// information about the failure is put into the crash report. The cost and
// coverage for each user is minimal, but spread over the entire user base the
// coverage becomes significant.
//
// The idea comes from Chromium, where it is called GWP-ASAN. (Firefox uses PHC
// as the name because GWP-ASAN is long, awkward, and doesn't have any
// particular meaning.)
//
// In the current implementation up to 64 allocations per process can become
// PHC allocations. These allocations must be page-sized or smaller. Each PHC
// allocation gets its own page, and when the allocation is freed its page is
// marked inaccessible until the page is reused for another allocation. This
// means that a use-after-free defect (which includes double-frees) will be
// caught if the use occurs before the page is reused for another allocation.
// The crash report will contain stack traces for the allocation site, the free
// site, and the use-after-free site, which is often enough to diagnose the
// defect.
//
// Also, each PHC allocation is followed by a guard page. The PHC allocation is
// positioned so that its end abuts the guard page (or as close as possible,
// given alignment constraints). This means that a bounds violation at the end
// of the allocation (overflow) will be caught. The crash report will contain
// stack traces for the allocation site and the bounds violation use site,
// which is often enough to diagnose the defect.
//
// (A bounds violation at the start of the allocation (underflow) will not be
// caught, unless it is sufficiently large to hit the preceding allocation's
// guard page, which is not that likely. It would be possible to look more
// assiduously for underflow by randomly placing some allocations at the end of
// the page and some at the start of the page, and GWP-ASAN does this. PHC does
// not, however, because overflow is likely to be much more common than
// underflow in practice.)
//
// We use a simple heuristic to categorize a guard page access as overflow or
// underflow: if the address falls in the lower half of the guard page, we
// assume it is overflow, otherwise we assume it is underflow. More
// sophisticated heuristics are possible, but this one is very simple, and it is
// likely that most overflows/underflows in practice are very close to the page
// boundary.
//
// The design space for the randomization strategy is large. The current
// implementation has a large random delay before it starts operating, and a
// small random delay between each PHC allocation attempt. Each freed PHC
// allocation is quarantined for a medium random delay before being reused, in
// order to increase the chance of catching UAFs.
//
// The basic cost of PHC's operation is as follows.
//
// - The physical memory cost is 64 pages plus some metadata (including stack
// traces) for each page. This amounts to 256 KiB per process on
// architectures with 4 KiB pages and 1024 KiB on macOS/AArch64 which uses
// 16 KiB pages.
//
// - The virtual memory cost is the physical memory cost plus the guard pages:
// another 64 pages. This amounts to another 256 KiB per process on
// architectures with 4 KiB pages and 1024 KiB on macOS/AArch64 which uses
// 16 KiB pages. PHC is currently only enabled on 64-bit platforms so the
// impact of the virtual memory usage is negligible.
//
// - Every allocation requires a size check and a decrement-and-check of an
// atomic counter. When the counter reaches zero a PHC allocation can occur,
// which involves marking a page as accessible and getting a stack trace for
// the allocation site. Otherwise, mozjemalloc performs the allocation.
//
// - Every deallocation requires a range check on the pointer to see if it
// involves a PHC allocation. (The choice to only do PHC allocations that are
// a page or smaller enables this range check, because the 64 pages are
// contiguous. Allowing larger allocations would make this more complicated,
// and we definitely don't want something as slow as a hash table lookup on
// every deallocation.) PHC deallocations involve marking a page as
// inaccessible and getting a stack trace for the deallocation site.
//
// Note that calls to realloc(), free(), and malloc_usable_size() will
// immediately crash if the given pointer falls within a page allocation's
// page, but does not point to the start of the allocation itself.
//
// void* p = malloc(64);
// free(p + 1); // p+1 doesn't point to the allocation start; crash
//
// Such crashes will not have the PHC fields in the crash report.
//
// PHC-specific tests can be run with the following commands:
// - gtests: `./mach gtest '*PHC*'`
// - xpcshell-tests: `./mach test toolkit/crashreporter/test/unit`
// - This runs some non-PHC tests as well.
#include "PHC.h"
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#ifdef XP_WIN
# include <process.h>
#else
# include <sys/mman.h>
# include <sys/types.h>
# include <pthread.h>
# include <unistd.h>
#endif
#include "mozjemalloc.h"
#include "mozjemalloc.h"
#include "FdPrintf.h"
#include "Mutex.h"
#include "mozilla/Assertions.h"
#include "mozilla/Atomics.h"
#include "mozilla/Attributes.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/Maybe.h"
#include "mozilla/StackWalk.h"
#include "mozilla/ThreadLocal.h"
#include "mozilla/XorShift128PlusRNG.h"
using namespace mozilla;
//---------------------------------------------------------------------------
// Utilities
//---------------------------------------------------------------------------
#ifdef ANDROID
// Android doesn't have pthread_atfork defined in pthread.h.
extern "C" MOZ_EXPORT int pthread_atfork(void (*)(void), void (*)(void),
void (*)(void));
#endif
#ifndef DISALLOW_COPY_AND_ASSIGN
# define DISALLOW_COPY_AND_ASSIGN(T) \
T(const T&); \
void operator=(const T&)
#endif
// This class provides infallible operations for the small number of heap
// allocations that PHC does for itself. It would be nice if we could use the
// InfallibleAllocPolicy from mozalloc, but PHC cannot use mozalloc.
class InfallibleAllocPolicy {
public:
static void AbortOnFailure(const void* aP) {
if (!aP) {
MOZ_CRASH("PHC failed to allocate");
}
}
template <class T>
static T* new_() {
void* p = MozJemalloc::malloc(sizeof(T));
AbortOnFailure(p);
return new (p) T;
}
};
//---------------------------------------------------------------------------
// Stack traces
//---------------------------------------------------------------------------
// This code is similar to the equivalent code within DMD.
class StackTrace : public phc::StackTrace {
public:
StackTrace() = default;
void Clear() { mLength = 0; }
void Fill();
private:
static void StackWalkCallback(uint32_t aFrameNumber, void* aPc, void* aSp,
void* aClosure) {
StackTrace* st = (StackTrace*)aClosure;
MOZ_ASSERT(st->mLength < kMaxFrames);
st->mPcs[st->mLength] = aPc;
st->mLength++;
MOZ_ASSERT(st->mLength == aFrameNumber);
}
};
// WARNING WARNING WARNING: this function must only be called when PHC::mMutex
// is *not* locked, otherwise we might get deadlocks.
//
// How? On Windows, MozStackWalk() can lock a mutex, M, from the shared library
// loader. Another thread might call malloc() while holding M locked (when
// loading a shared library) and try to lock PHC::mMutex, causing a deadlock.
// So PHC::mMutex can't be locked during the call to MozStackWalk(). (For
// on all platforms.)
//
// In DMD, to avoid this problem we temporarily unlock the equivalent mutex for
// the MozStackWalk() call. But that's grotty, and things are a bit different
// here, so we just require that stack traces be obtained before locking
// PHC::mMutex.
//
// Unfortunately, there is no reliable way at compile-time or run-time to ensure
// this pre-condition. Hence this large comment.
//
void StackTrace::Fill() {
mLength = 0;
// These ifdefs should be kept in sync with the conditions in
// phc_implies_frame_pointers in build/moz.configure/memory.configure
#if defined(XP_WIN) && defined(_M_IX86)
// This avoids MozStackWalk(), which causes unusably slow startup on Win32
//
// This code is cribbed from the Gecko Profiler, which also uses
// FramePointerStackWalk() on Win32: Registers::SyncPopulate() for the
// frame pointer, and GetStackTop() for the stack end.
CONTEXT context;
RtlCaptureContext(&context);
void** fp = reinterpret_cast<void**>(context.Ebp);
PNT_TIB pTib = reinterpret_cast<PNT_TIB>(NtCurrentTeb());
void* stackEnd = static_cast<void*>(pTib->StackBase);
FramePointerStackWalk(StackWalkCallback, kMaxFrames, this, fp, stackEnd);
#elif defined(XP_DARWIN)
// This avoids MozStackWalk(), which has become unusably slow on Mac due to
// changes in libunwind.
//
// This code is cribbed from the Gecko Profiler, which also uses
// FramePointerStackWalk() on Mac: Registers::SyncPopulate() for the frame
// pointer, and GetStackTop() for the stack end.
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wframe-address"
void** fp = reinterpret_cast<void**>(__builtin_frame_address(1));
# pragma GCC diagnostic pop
void* stackEnd = pthread_get_stackaddr_np(pthread_self());
FramePointerStackWalk(StackWalkCallback, kMaxFrames, this, fp, stackEnd);
#else
MozStackWalk(StackWalkCallback, nullptr, kMaxFrames, this);
#endif
}
//---------------------------------------------------------------------------
// Logging
//---------------------------------------------------------------------------
// Change this to 1 to enable some PHC logging. Useful for debugging.
#define PHC_LOGGING 0
#if PHC_LOGGING
static size_t GetPid() { return size_t(getpid()); }
static size_t GetTid() {
# if defined(XP_WIN)
return size_t(GetCurrentThreadId());
# else
return size_t(pthread_self());
# endif
}
# if defined(XP_WIN)
# define LOG_STDERR \
reinterpret_cast<intptr_t>(GetStdHandle(STD_ERROR_HANDLE))
# else
# define LOG_STDERR 2
# endif
# define LOG(fmt, ...) \
FdPrintf(LOG_STDERR, "PHC[%zu,%zu,~%zu] " fmt, GetPid(), GetTid(), \
size_t(PHC::Now()), ##__VA_ARGS__)
#else
# define LOG(fmt, ...)
#endif // PHC_LOGGING
//---------------------------------------------------------------------------
// Global state
//---------------------------------------------------------------------------
// Throughout this entire file time is measured as the number of sub-page
// allocations performed (by PHC and mozjemalloc combined). `Time` is 64-bit
// because we could have more than 2**32 allocations in a long-running session.
// `Delay` is 32-bit because the delays used within PHC are always much smaller
// than 2**32. Delay must be unsigned so that IsPowerOfTwo() can work on some
// Delay values.
using Time = uint64_t; // A moment in time.
using Delay = uint32_t; // A time duration.
static constexpr Delay DELAY_MAX = UINT32_MAX / 2;
// PHC only runs if the page size is 4 KiB; anything more is uncommon and would
// use too much memory. So we hardwire this size for all platforms but macOS
// on ARM processors. For the latter we make an exception because the minimum
// page size supported is 16KiB so there's no way to go below that.
static const size_t kPageSize =
#if defined(XP_DARWIN) && defined(__aarch64__)
16384
#else
4096
#endif
;
// We align the PHC area to a multiple of the jemalloc and JS GC chunk size
// (both use 1MB aligned chunks) so that their address computations don't lead
// from non-PHC memory into PHC memory causing misleading PHC stacks to be
// attached to a crash report.
static const size_t kPhcAlign = 1024 * 1024;
static_assert(IsPowerOfTwo(kPhcAlign));
static_assert((kPhcAlign % kPageSize) == 0);
// There are two kinds of page.
// - Allocation pages, from which allocations are made.
// - Guard pages, which are never touched by PHC.
//
// These page kinds are interleaved; each allocation page has a guard page on
// either side.
#ifdef EARLY_BETA_OR_EARLIER
static const size_t kNumAllocPages = kPageSize == 4096 ? 4096 : 1024;
#else
// This will use between 82KiB and 1.1MiB per process (depending on how many
// objects are currently allocated). We will tune this in the future.
static const size_t kNumAllocPages = kPageSize == 4096 ? 256 : 64;
#endif
static const size_t kNumAllPages = kNumAllocPages * 2 + 1;
// The total size of the allocation pages and guard pages.
static const size_t kAllPagesSize = kNumAllPages * kPageSize;
// jemalloc adds a guard page to the end of our allocation, see the comment in
// AllocAllPages() for more information.
static const size_t kAllPagesJemallocSize = kAllPagesSize - kPageSize;
// The amount to decrement from the shared allocation delay each time a thread's
// local allocation delay reaches zero.
static const Delay kDelayDecrementAmount = 256;
// When PHC is disabled on the current thread wait this many allocations before
// accessing sAllocDelay once more.
static const Delay kDelayBackoffAmount = 64;
// When PHC is disabled globally reset the shared delay by this many allocations
// to keep code running on the fast path.
static const Delay kDelayResetWhenDisabled = 64 * 1024;
// The default state for PHC. Either Enabled or OnlyFree.
#define DEFAULT_STATE mozilla::phc::OnlyFree
// The maximum time.
static const Time kMaxTime = ~(Time(0));
// Truncate aRnd to the range (1 .. aAvgDelay*2). If aRnd is random, this
// results in an average value of aAvgDelay + 0.5, which is close enough to
// aAvgDelay. aAvgDelay must be a power-of-two for speed.
constexpr Delay Rnd64ToDelay(Delay aAvgDelay, uint64_t aRnd) {
MOZ_ASSERT(IsPowerOfTwo(aAvgDelay), "must be a power of two");
return (aRnd & (uint64_t(aAvgDelay) * 2 - 1)) + 1;
}
static Delay CheckProbability(int64_t aProb) {
// Limit delays calculated from prefs to 0x80000000, this is the largest
// power-of-two that fits in a Delay since it is a uint32_t.
// The minimum is 2 that way not every allocation goes straight to PHC.
return RoundUpPow2(std::clamp(aProb, int64_t(2), int64_t(0x80000000)));
}
// Maps a pointer to a PHC-specific structure:
// - Nothing
// - A guard page (it is unspecified which one)
// - An allocation page (with an index < kNumAllocPages)
//
// The standard way of handling a PtrKind is to check IsNothing(), and if that
// fails, to check IsGuardPage(), and if that fails, to call AllocPage().
class PtrKind {
private:
enum class Tag : uint8_t {
Nothing,
GuardPage,
AllocPage,
};
Tag mTag;
uintptr_t mIndex; // Only used if mTag == Tag::AllocPage.
public:
// Detect what a pointer points to. This constructor must be fast because it
// is called for every call to free(), realloc(), malloc_usable_size(), and
// jemalloc_ptr_info().
PtrKind(const void* aPtr, const uint8_t* aPagesStart,
const uint8_t* aPagesLimit) {
if (!(aPagesStart <= aPtr && aPtr < aPagesLimit)) {
mTag = Tag::Nothing;
} else {
uintptr_t offset = static_cast<const uint8_t*>(aPtr) - aPagesStart;
uintptr_t allPageIndex = offset / kPageSize;
MOZ_ASSERT(allPageIndex < kNumAllPages);
if (allPageIndex & 1) {
// Odd-indexed pages are allocation pages.
uintptr_t allocPageIndex = allPageIndex / 2;
MOZ_ASSERT(allocPageIndex < kNumAllocPages);
mTag = Tag::AllocPage;
mIndex = allocPageIndex;
} else {
// Even-numbered pages are guard pages.
mTag = Tag::GuardPage;
}
}
}
bool IsNothing() const { return mTag == Tag::Nothing; }
bool IsGuardPage() const { return mTag == Tag::GuardPage; }
// This should only be called after IsNothing() and IsGuardPage() have been
// checked and failed.
uintptr_t AllocPageIndex() const {
MOZ_RELEASE_ASSERT(mTag == Tag::AllocPage);
return mIndex;
}
};
// On MacOS, the first __thread/thread_local access calls malloc, which leads
// to an infinite loop. So we use pthread-based TLS instead, which somehow
// doesn't have this problem.
#if !defined(XP_DARWIN)
# define PHC_THREAD_LOCAL(T) MOZ_THREAD_LOCAL(T)
#else
# define PHC_THREAD_LOCAL(T) \
detail::ThreadLocal<T, detail::ThreadLocalKeyStorage>
#endif
// The virtual address space reserved by PHC. It is shared, immutable global
// state. Initialized by phc_init() and never changed after that. phc_init()
// runs early enough that no synchronization is needed.
class PHCRegion {
private:
// The bounds of the allocated pages.
uint8_t* const mPagesStart;
uint8_t* const mPagesLimit;
// Allocates the allocation pages and the guard pages, contiguously.
uint8_t* AllocAllPages() {
// The memory allocated here is never freed, because it would happen at
// process termination when it would be of little use.
// We can rely on jemalloc's behaviour that when it allocates memory aligned
// with its own chunk size it will over-allocate and guarantee that the
// memory after the end of our allocation, but before the next chunk, is
// decommitted and inaccessible. Elsewhere in PHC we assume that we own
// that page (so that memory errors in it get caught by PHC) but here we
// use kAllPagesJemallocSize which subtracts jemalloc's guard page.
void* pages = MozJemalloc::memalign(kPhcAlign, kAllPagesJemallocSize);
if (!pages) {
MOZ_CRASH();
}
// Make the pages inaccessible.
#ifdef XP_WIN
if (!VirtualFree(pages, kAllPagesJemallocSize, MEM_DECOMMIT)) {
MOZ_CRASH("VirtualFree failed");
}
#else
if (mmap(pages, kAllPagesJemallocSize, PROT_NONE,
MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == MAP_FAILED) {
MOZ_CRASH("mmap failed");
}
#endif
return static_cast<uint8_t*>(pages);
}
public:
PHCRegion();
class PtrKind PtrKind(const void* aPtr) {
class PtrKind pk(aPtr, mPagesStart, mPagesLimit);
return pk;
}
bool IsInFirstGuardPage(const void* aPtr) {
return mPagesStart <= aPtr && aPtr < mPagesStart + kPageSize;
}
// Get the address of the allocation page referred to via an index. Used when
// marking the page as accessible/inaccessible.
uint8_t* AllocPagePtr(uintptr_t aIndex) {
MOZ_ASSERT(aIndex < kNumAllocPages);
// Multiply by two and add one to account for allocation pages *and* guard
// pages.
return mPagesStart + (2 * aIndex + 1) * kPageSize;
}
};
// This type is used as a proof-of-lock token, to make it clear which functions
// require mMutex to be locked.
using PHCLock = const MutexAutoLock&;
// Shared, mutable global state. Many fields are protected by sMutex; functions
// that access those feilds should take a PHCLock as proof that mMutex is held.
// Other fields are TLS or Atomic and don't need the lock.
class PHC {
enum class AllocPageState {
NeverAllocated = 0,
InUse = 1,
Freed = 2,
};
// Metadata for each allocation page.
class AllocPageInfo {
public:
AllocPageInfo()
: mState(AllocPageState::NeverAllocated),
mBaseAddr(nullptr),
mReuseTime(0) {}
// The current allocation page state.
AllocPageState mState;
// The arena that the allocation is nominally from. This isn't meaningful
// within PHC, which has no arenas. But it is necessary for reallocation of
// page allocations as normal allocations, such as in this code:
//
// p = moz_arena_malloc(arenaId, 4096);
// realloc(p, 8192);
//
// The realloc is more than one page, and thus too large for PHC to handle.
// Therefore, if PHC handles the first allocation, it must ask mozjemalloc
// to allocate the 8192 bytes in the correct arena, and to do that, it must
// call MozJemalloc::moz_arena_malloc with the correct arenaId under the
// covers. Therefore it must record that arenaId.
//
// This field is also needed for jemalloc_ptr_info() to work, because it
// also returns the arena ID (but only in debug builds).
//
// - NeverAllocated: must be 0.
// - InUse | Freed: can be any valid arena ID value.
Maybe<arena_id_t> mArenaId;
// The starting address of the allocation. Will not be the same as the page
// address unless the allocation is a full page.
// - NeverAllocated: must be 0.
// - InUse | Freed: must be within the allocation page.
uint8_t* mBaseAddr;
// Usable size is computed as the number of bytes between the pointer and
// the end of the allocation page. This might be bigger than the requested
// size, especially if an outsized alignment is requested.
size_t UsableSize() const {
return mState == AllocPageState::NeverAllocated
? 0
: kPageSize - (reinterpret_cast<uintptr_t>(mBaseAddr) &
(kPageSize - 1));
}
// The internal fragmentation for this allocation.
size_t FragmentationBytes() const {
MOZ_ASSERT(kPageSize >= UsableSize());
return mState == AllocPageState::InUse ? kPageSize - UsableSize() : 0;
}
// The allocation stack.
// - NeverAllocated: Nothing.
// - InUse | Freed: Some.
Maybe<StackTrace> mAllocStack;
// The free stack.
// - NeverAllocated | InUse: Nothing.
// - Freed: Some.
Maybe<StackTrace> mFreeStack;
// The time at which the page is available for reuse, as measured against
// mNow. When the page is in use this value will be kMaxTime.
// - NeverAllocated: must be 0.
// - InUse: must be kMaxTime.
// - Freed: must be > 0 and < kMaxTime.
Time mReuseTime;
// The next index for a free list of pages.`
Maybe<uintptr_t> mNextPage;
};
public:
// The RNG seeds here are poor, but non-reentrant since this can be called
// from malloc(). SetState() will reset the RNG later.
PHC() : mRNG(RandomSeed<1>(), RandomSeed<2>()) {
mMutex.Init();
if (!tlsIsDisabled.init()) {
MOZ_CRASH();
}
if (!tlsAllocDelay.init()) {
MOZ_CRASH();
}
if (!tlsLastDelay.init()) {
MOZ_CRASH();
}
// This constructor is part of PHC's very early initialisation,
// see phc_init(), and if PHC is default-on it'll start marking allocations
// and we must setup the delay. However once XPCOM starts it'll call
// SetState() which will re-initialise the RNG and allocation delay.
MutexAutoLock lock(mMutex);
ForceSetNewAllocDelay(Rnd64ToDelay(mAvgFirstAllocDelay, Random64(lock)));
for (uintptr_t i = 0; i < kNumAllocPages; i++) {
AppendPageToFreeList(lock, i);
}
}
uint64_t Random64(PHCLock) { return mRNG.next(); }
bool IsPageInUse(PHCLock, uintptr_t aIndex) {
return mAllocPages[aIndex].mState == AllocPageState::InUse;
}
// Is the page free? And if so, has enough time passed that we can use it?
bool IsPageAllocatable(PHCLock, uintptr_t aIndex, Time aNow) {
const AllocPageInfo& page = mAllocPages[aIndex];
return page.mState != AllocPageState::InUse && aNow >= page.mReuseTime;
}
// Get the address of the allocation page referred to via an index. Used
// when checking pointers against page boundaries.
uint8_t* AllocPageBaseAddr(PHCLock, uintptr_t aIndex) {
return mAllocPages[aIndex].mBaseAddr;
}
Maybe<arena_id_t> PageArena(PHCLock aLock, uintptr_t aIndex) {
const AllocPageInfo& page = mAllocPages[aIndex];
AssertAllocPageInUse(aLock, page);
return page.mArenaId;
}
size_t PageUsableSize(PHCLock aLock, uintptr_t aIndex) {
const AllocPageInfo& page = mAllocPages[aIndex];
AssertAllocPageInUse(aLock, page);
return page.UsableSize();
}
// The total fragmentation in PHC
size_t FragmentationBytes() const {
size_t sum = 0;
for (const auto& page : mAllocPages) {
sum += page.FragmentationBytes();
}
return sum;
}
void SetPageInUse(PHCLock aLock, uintptr_t aIndex,
const Maybe<arena_id_t>& aArenaId, uint8_t* aBaseAddr,
const StackTrace& aAllocStack) {
AllocPageInfo& page = mAllocPages[aIndex];
AssertAllocPageNotInUse(aLock, page);
page.mState = AllocPageState::InUse;
page.mArenaId = aArenaId;
page.mBaseAddr = aBaseAddr;
page.mAllocStack = Some(aAllocStack);
page.mFreeStack = Nothing();
page.mReuseTime = kMaxTime;
MOZ_ASSERT(!page.mNextPage);
}
#if PHC_LOGGING
Time GetFreeTime(uintptr_t aIndex) const { return mFreeTime[aIndex]; }
#endif
void ResizePageInUse(PHCLock aLock, uintptr_t aIndex,
const Maybe<arena_id_t>& aArenaId, uint8_t* aNewBaseAddr,
const StackTrace& aAllocStack) {
AllocPageInfo& page = mAllocPages[aIndex];
AssertAllocPageInUse(aLock, page);
// page.mState is not changed.
if (aArenaId.isSome()) {
// Crash if the arenas don't match.
MOZ_RELEASE_ASSERT(page.mArenaId == aArenaId);
}
page.mBaseAddr = aNewBaseAddr;
// We could just keep the original alloc stack, but the realloc stack is
// more recent and therefore seems more useful.
page.mAllocStack = Some(aAllocStack);
// page.mFreeStack is not changed.
// page.mReuseTime is not changed.
// page.mNextPage is not changed.
};
void SetPageFreed(PHCLock aLock, uintptr_t aIndex,
const Maybe<arena_id_t>& aArenaId,
const StackTrace& aFreeStack, Delay aReuseDelay) {
AllocPageInfo& page = mAllocPages[aIndex];
AssertAllocPageInUse(aLock, page);
page.mState = AllocPageState::Freed;
// page.mArenaId is left unchanged, for jemalloc_ptr_info() calls that
// occur after freeing (e.g. in the PtrInfo test in TestJemalloc.cpp).
if (aArenaId.isSome()) {
// Crash if the arenas don't match.
MOZ_RELEASE_ASSERT(page.mArenaId == aArenaId);
}
// page.musableSize is left unchanged, for reporting on UAF, and for
// jemalloc_ptr_info() calls that occur after freeing (e.g. in the PtrInfo
// test in TestJemalloc.cpp).
// page.mAllocStack is left unchanged, for reporting on UAF.
page.mFreeStack = Some(aFreeStack);
Time now = Now();
#if PHC_LOGGING
mFreeTime[aIndex] = now;
#endif
page.mReuseTime = now + aReuseDelay;
MOZ_ASSERT(!page.mNextPage);
AppendPageToFreeList(aLock, aIndex);
}
static void CrashOnGuardPage(void* aPtr) {
// An operation on a guard page? This is a bounds violation. Deliberately
// touch the page in question to cause a crash that triggers the usual PHC
// machinery.
LOG("CrashOnGuardPage(%p), bounds violation\n", aPtr);
*static_cast<uint8_t*>(aPtr) = 0;
MOZ_CRASH("unreachable");
}
void EnsureValidAndInUse(PHCLock, void* aPtr, uintptr_t aIndex)
MOZ_REQUIRES(mMutex) {
const AllocPageInfo& page = mAllocPages[aIndex];
// The pointer must point to the start of the allocation.
MOZ_RELEASE_ASSERT(page.mBaseAddr == aPtr);
if (page.mState == AllocPageState::Freed) {
LOG("EnsureValidAndInUse(%p), use-after-free\n", aPtr);
// An operation on a freed page? This is a particular kind of
// use-after-free. Deliberately touch the page in question, in order to
// cause a crash that triggers the usual PHC machinery. But unlock mMutex
// first, because that self-same PHC machinery needs to re-lock it, and
// the crash causes non-local control flow so mMutex won't be unlocked
// the normal way in the caller.
mMutex.Unlock();
*static_cast<uint8_t*>(aPtr) = 0;
MOZ_CRASH("unreachable");
}
}
// This expects GMUt::mMutex to be locked but can't check it with a parameter
// since we try-lock it.
void FillAddrInfo(uintptr_t aIndex, const void* aBaseAddr, bool isGuardPage,
phc::AddrInfo& aOut) {
const AllocPageInfo& page = mAllocPages[aIndex];
if (isGuardPage) {
aOut.mKind = phc::AddrInfo::Kind::GuardPage;
} else {
switch (page.mState) {
case AllocPageState::NeverAllocated:
aOut.mKind = phc::AddrInfo::Kind::NeverAllocatedPage;
break;
case AllocPageState::InUse:
aOut.mKind = phc::AddrInfo::Kind::InUsePage;
break;
case AllocPageState::Freed:
aOut.mKind = phc::AddrInfo::Kind::FreedPage;
break;
default:
MOZ_CRASH();
}
}
aOut.mBaseAddr = page.mBaseAddr;
aOut.mUsableSize = page.UsableSize();
aOut.mAllocStack = page.mAllocStack;
aOut.mFreeStack = page.mFreeStack;
}
void FillJemallocPtrInfo(PHCLock, const void* aPtr, uintptr_t aIndex,
jemalloc_ptr_info_t* aInfo) {
const AllocPageInfo& page = mAllocPages[aIndex];
switch (page.mState) {
case AllocPageState::NeverAllocated:
break;
case AllocPageState::InUse: {
// Only return TagLiveAlloc if the pointer is within the bounds of the
// allocation's usable size.
uint8_t* base = page.mBaseAddr;
uint8_t* limit = base + page.UsableSize();
if (base <= aPtr && aPtr < limit) {
*aInfo = {TagLiveAlloc, page.mBaseAddr, page.UsableSize(),
page.mArenaId.valueOr(0)};
return;
}
break;
}
case AllocPageState::Freed: {
// Only return TagFreedAlloc if the pointer is within the bounds of the
// former allocation's usable size.
uint8_t* base = page.mBaseAddr;
uint8_t* limit = base + page.UsableSize();
if (base <= aPtr && aPtr < limit) {
*aInfo = {TagFreedAlloc, page.mBaseAddr, page.UsableSize(),
page.mArenaId.valueOr(0)};
return;
}
break;
}
default:
MOZ_CRASH();
}
// Pointers into guard pages will end up here, as will pointers into
// allocation pages that aren't within the allocation's bounds.
*aInfo = {TagUnknown, nullptr, 0, 0};
}
#ifndef XP_WIN
static void prefork() MOZ_NO_THREAD_SAFETY_ANALYSIS {
PHC::sPHC->mMutex.Lock();
}
static void postfork_parent() MOZ_NO_THREAD_SAFETY_ANALYSIS {
PHC::sPHC->mMutex.Unlock();
}
static void postfork_child() { PHC::sPHC->mMutex.Init(); }
#endif
#if PHC_LOGGING
void IncPageAllocHits(PHCLock) { mPageAllocHits++; }
void IncPageAllocMisses(PHCLock) { mPageAllocMisses++; }
#else
void IncPageAllocHits(PHCLock) {}
void IncPageAllocMisses(PHCLock) {}
#endif
phc::PHCStats GetPageStats(PHCLock) {
phc::PHCStats stats;
for (const auto& page : mAllocPages) {
stats.mSlotsAllocated += page.mState == AllocPageState::InUse ? 1 : 0;
stats.mSlotsFreed += page.mState == AllocPageState::Freed ? 1 : 0;
}
stats.mSlotsUnused =
kNumAllocPages - stats.mSlotsAllocated - stats.mSlotsFreed;
return stats;
}
#if PHC_LOGGING
size_t PageAllocHits(PHCLock) { return mPageAllocHits; }
size_t PageAllocAttempts(PHCLock) {
return mPageAllocHits + mPageAllocMisses;
}
// This is an integer because FdPrintf only supports integer printing.
size_t PageAllocHitRate(PHCLock) {
return mPageAllocHits * 100 / (mPageAllocHits + mPageAllocMisses);
}
#endif
// Should we make new PHC allocations?
bool ShouldMakeNewAllocations() const {
return mPhcState == mozilla::phc::Enabled;
}
using PHCState = mozilla::phc::PHCState;
void SetState(PHCState aState) {
if (mPhcState != PHCState::Enabled && aState == PHCState::Enabled) {
MutexAutoLock lock(mMutex);
// Reset the RNG at this point with a better seed.
ResetRNG(lock);
ForceSetNewAllocDelay(Rnd64ToDelay(mAvgFirstAllocDelay, Random64(lock)));
}
mPhcState = aState;
}
void ResetRNG(MutexAutoLock&) {
mRNG = non_crypto::XorShift128PlusRNG(RandomSeed<0>(), RandomSeed<1>());
}
void SetProbabilities(int64_t aAvgDelayFirst, int64_t aAvgDelayNormal,
int64_t aAvgDelayPageReuse) {
MutexAutoLock lock(mMutex);
mAvgFirstAllocDelay = CheckProbability(aAvgDelayFirst);
mAvgAllocDelay = CheckProbability(aAvgDelayNormal);
mAvgPageReuseDelay = CheckProbability(aAvgDelayPageReuse);
}
static void DisableOnCurrentThread() {
MOZ_ASSERT(!tlsIsDisabled.get());
tlsIsDisabled.set(true);
}
void EnableOnCurrentThread() {
MOZ_ASSERT(tlsIsDisabled.get());
tlsIsDisabled.set(false);
}
static bool IsDisabledOnCurrentThread() { return tlsIsDisabled.get(); }
static Time Now() {
if (!sPHC) {
return 0;
}
return sPHC->mNow;
}
void AdvanceNow(uint32_t delay = 0) {
mNow += tlsLastDelay.get() - delay;
tlsLastDelay.set(delay);
}
// Decrements the delay and returns true if it's time to make a new PHC
// allocation.
static bool DecrementDelay() {
const Delay alloc_delay = tlsAllocDelay.get();
if (MOZ_LIKELY(alloc_delay > 0)) {
tlsAllocDelay.set(alloc_delay - 1);
return false;
}
// The local delay has expired, check the shared delay. This path is also
// executed on a new thread's first allocation, the result is the same: all
// the thread's TLS fields will be initialised.
// This accesses sPHC but we want to ensure it's still a static member
// function so that sPHC isn't dereferenced until after the hot path above.
MOZ_ASSERT(sPHC);
sPHC->AdvanceNow();
// Use an atomic fetch-and-subtract. This uses unsigned underflow semantics
// to avoid doing a full compare-and-swap.
Delay new_delay = (sAllocDelay -= kDelayDecrementAmount);
Delay old_delay = new_delay + kDelayDecrementAmount;
if (MOZ_LIKELY(new_delay < DELAY_MAX)) {
// Normal case, we decremented the shared delay but it's not yet
// underflowed.
tlsAllocDelay.set(kDelayDecrementAmount);
tlsLastDelay.set(kDelayDecrementAmount);
LOG("Update sAllocDelay <- %zu, tlsAllocDelay <- %zu\n",
size_t(new_delay), size_t(kDelayDecrementAmount));
return false;
}
if (old_delay < new_delay) {
// The shared delay only just underflowed, so unless we hit exactly zero
// we should set our local counter and continue.
LOG("Update sAllocDelay <- %zu, tlsAllocDelay <- %zu\n",
size_t(new_delay), size_t(old_delay));
if (old_delay == 0) {
// We don't need to set tlsAllocDelay because it's already zero, we know
// because the condition at the beginning of this function failed.
return true;
}
tlsAllocDelay.set(old_delay);
tlsLastDelay.set(old_delay);
return false;
}
// The delay underflowed on another thread or a previous failed allocation
// by this thread. Return true and attempt the next allocation, if the
// other thread wins we'll check for that before committing.
LOG("Update sAllocDelay <- %zu, tlsAllocDelay <- %zu\n", size_t(new_delay),
size_t(alloc_delay));
return true;
}
static void ResetLocalAllocDelay(Delay aDelay = 0) {
// We could take some delay from the shared delay but we'd need a
// compare-and-swap because this is called on paths that don't make
// allocations. Or we can set the local delay to zero and let it get
// initialised on the next allocation.
tlsAllocDelay.set(aDelay);
tlsLastDelay.set(aDelay);
}
static void ForceSetNewAllocDelay(Delay aNewAllocDelay) {
LOG("Setting sAllocDelay <- %zu\n", size_t(aNewAllocDelay));
sAllocDelay = aNewAllocDelay;
ResetLocalAllocDelay();
}
// Set a new allocation delay and return true if the delay was less than zero
// (but it's unsigned so interpret it as signed) indicating that we won the
// race to make the next allocation.
static bool SetNewAllocDelay(Delay aNewAllocDelay) {
bool cas_retry;
do {
// We read the current delay on every iteration, we consider that the PHC
// allocation is still "up for grabs" if sAllocDelay < 0. This is safe
// even while other threads continuing to fetch-and-subtract sAllocDelay
// in DecrementDelay(), up to DELAY_MAX (2^31) calls to DecrementDelay().
Delay read_delay = sAllocDelay;
if (read_delay < DELAY_MAX) {
// Another thread already set a valid delay.
LOG("Observe delay %zu this thread lost the race\n",
size_t(read_delay));
ResetLocalAllocDelay();
return false;
} else {
LOG("Preparing for CAS, read sAllocDelay %zu\n", size_t(read_delay));
}
cas_retry = !sAllocDelay.compareExchange(read_delay, aNewAllocDelay);
if (cas_retry) {
LOG("Lost the CAS, sAllocDelay is now %zu\n", size_t(sAllocDelay));
cpu_pause();
// We raced against another thread and lost.
}
} while (cas_retry);
LOG("Won the CAS, set sAllocDelay = %zu\n", size_t(sAllocDelay));
ResetLocalAllocDelay();
return true;
}
static Delay LocalAllocDelay() { return tlsAllocDelay.get(); }
static Delay SharedAllocDelay() { return sAllocDelay; }
static Delay LastDelay() { return tlsLastDelay.get(); }
Maybe<uintptr_t> PopNextFreeIfAllocatable(const MutexAutoLock& lock,
Time now) {
if (!mFreePageListHead) {
return Nothing();
}
uintptr_t index = mFreePageListHead.value();
MOZ_RELEASE_ASSERT(index < kNumAllocPages);
AllocPageInfo& page = mAllocPages[index];
AssertAllocPageNotInUse(lock, page);
if (!IsPageAllocatable(lock, index, now)) {
return Nothing();
}
mFreePageListHead = page.mNextPage;
page.mNextPage = Nothing();
if (!mFreePageListHead) {
mFreePageListTail = Nothing();
}
return Some(index);
}
void UnpopNextFree(const MutexAutoLock& lock, uintptr_t index) {
AllocPageInfo& page = mAllocPages[index];
MOZ_ASSERT(!page.mNextPage);
page.mNextPage = mFreePageListHead;
mFreePageListHead = Some(index);
if (!mFreePageListTail) {
mFreePageListTail = Some(index);
}
}
void AppendPageToFreeList(const MutexAutoLock& lock, uintptr_t aIndex) {
MOZ_RELEASE_ASSERT(aIndex < kNumAllocPages);
AllocPageInfo& page = mAllocPages[aIndex];
MOZ_ASSERT(!page.mNextPage);
MOZ_ASSERT(mFreePageListHead != Some(aIndex) &&
mFreePageListTail != Some(aIndex));
if (!mFreePageListTail) {
// The list is empty this page will become the beginning and end.
MOZ_ASSERT(!mFreePageListHead);
mFreePageListHead = Some(aIndex);
} else {
MOZ_ASSERT(mFreePageListTail.value() < kNumAllocPages);
AllocPageInfo& tail_page = mAllocPages[mFreePageListTail.value()];
MOZ_ASSERT(!tail_page.mNextPage);
tail_page.mNextPage = Some(aIndex);
}
page.mNextPage = Nothing();
mFreePageListTail = Some(aIndex);
}
private:
template <int N>
uint64_t RandomSeed() {
// An older version of this code used RandomUint64() here, but on Mac that
// function uses arc4random(), which can allocate, which would cause
// re-entry, which would be bad. So we just use time(), a local variable
// address and a global variable address. These are mediocre sources of
// entropy, but good enough for PHC.
static_assert(N == 0 || N == 1 || N == 2, "must be 0, 1 or 2");
uint64_t seed;
if (N == 0) {
time_t t = time(nullptr);
seed = t ^ (t << 32);
} else if (N == 1) {
seed = uintptr_t(&seed) ^ (uintptr_t(&seed) << 32);
} else {
seed = uintptr_t(&sRegion) ^ (uintptr_t(&sRegion) << 32);
}
return seed;
}
void AssertAllocPageInUse(PHCLock, const AllocPageInfo& aPage) {
MOZ_ASSERT(aPage.mState == AllocPageState::InUse);
// There is nothing to assert about aPage.mArenaId.
MOZ_ASSERT(aPage.mBaseAddr);
MOZ_ASSERT(aPage.UsableSize() > 0);
MOZ_ASSERT(aPage.mAllocStack.isSome());
MOZ_ASSERT(aPage.mFreeStack.isNothing());
MOZ_ASSERT(aPage.mReuseTime == kMaxTime);
MOZ_ASSERT(!aPage.mNextPage);
}
void AssertAllocPageNotInUse(PHCLock, const AllocPageInfo& aPage) {
// We can assert a lot about `NeverAllocated` pages, but not much about
// `Freed` pages.
#ifdef DEBUG
bool isFresh = aPage.mState == AllocPageState::NeverAllocated;
MOZ_ASSERT(isFresh || aPage.mState == AllocPageState::Freed);
MOZ_ASSERT_IF(isFresh, aPage.mArenaId == Nothing());
MOZ_ASSERT(isFresh == (aPage.mBaseAddr == nullptr));