This source file includes following definitions.
- systemAllocPages
- trimMapping
- ranvalInternal
- ranval
- getRandomPageBase
- allocPages
- freePages
- setSystemPagesInaccessible
- setSystemPagesAccessible
- decommitSystemPages
#include "config.h"
#include "wtf/PageAllocator.h"
#include "wtf/Assertions.h"
#include "wtf/ProcessID.h"
#include "wtf/SpinLock.h"
#include <limits.h>
#if OS(POSIX)
#include <sys/mman.h>
#ifndef MADV_FREE
#define MADV_FREE MADV_DONTNEED
#endif
#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
#endif
#elif OS(WIN)
#include <windows.h>
#else
#error Unknown OS
#endif
namespace WTF {
static void* systemAllocPages(void* addr, size_t len)
{
ASSERT(!(len & kPageAllocationGranularityOffsetMask));
ASSERT(!(reinterpret_cast<uintptr_t>(addr) & kPageAllocationGranularityOffsetMask));
void* ret;
#if OS(WIN)
ret = VirtualAlloc(addr, len, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (!ret)
ret = VirtualAlloc(0, len, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
#else
ret = mmap(addr, len, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (ret == MAP_FAILED)
ret = 0;
#endif
return ret;
}
static bool trimMapping(void* baseAddr, size_t baseLen, void* trimAddr, size_t trimLen)
{
#if OS(WIN)
return false;
#else
char* basePtr = static_cast<char*>(baseAddr);
char* trimPtr = static_cast<char*>(trimAddr);
ASSERT(trimPtr >= basePtr);
ASSERT(trimPtr + trimLen <= basePtr + baseLen);
size_t preLen = trimPtr - basePtr;
if (preLen) {
int ret = munmap(basePtr, preLen);
RELEASE_ASSERT(!ret);
}
size_t postLen = (basePtr + baseLen) - (trimPtr + trimLen);
if (postLen) {
int ret = munmap(trimPtr + trimLen, postLen);
RELEASE_ASSERT(!ret);
}
return true;
#endif
}
struct ranctx {
int lock;
bool initialized;
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
};
#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
uint32_t ranvalInternal(ranctx* x)
{
uint32_t e = x->a - rot(x->b, 27);
x->a = x->b ^ rot(x->c, 17);
x->b = x->c + x->d;
x->c = x->d + e;
x->d = e + x->a;
return x->d;
}
#undef rot
uint32_t ranval(ranctx* x)
{
spinLockLock(&x->lock);
if (UNLIKELY(!x->initialized)) {
x->initialized = true;
char c;
uint32_t seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&c));
seed ^= static_cast<uint32_t>(getCurrentProcessID());
x->a = 0xf1ea5eed;
x->b = x->c = x->d = seed;
for (int i = 0; i < 20; ++i) {
(void) ranvalInternal(x);
}
}
uint32_t ret = ranvalInternal(x);
spinLockUnlock(&x->lock);
return ret;
}
static struct ranctx s_ranctx;
static void* getRandomPageBase()
{
uintptr_t random;
random = static_cast<uintptr_t>(ranval(&s_ranctx));
#if CPU(X86_64)
random <<= 32UL;
random |= static_cast<uintptr_t>(ranval(&s_ranctx));
#if OS(WIN)
random &= 0x3ffffffffffUL;
random += 0x10000000000UL;
#else
random &= 0x3fffffffffffUL;
#endif
#else
random &= 0x3fffffff;
random += 0x20000000;
#endif
random &= kPageAllocationGranularityBaseMask;
return reinterpret_cast<void*>(random);
}
void* allocPages(void* addr, size_t len, size_t align)
{
ASSERT(len >= kPageAllocationGranularity);
ASSERT(!(len & kPageAllocationGranularityOffsetMask));
ASSERT(align >= kPageAllocationGranularity);
ASSERT(!(align & kPageAllocationGranularityOffsetMask));
ASSERT(!(reinterpret_cast<uintptr_t>(addr) & kPageAllocationGranularityOffsetMask));
size_t alignOffsetMask = align - 1;
size_t alignBaseMask = ~alignOffsetMask;
ASSERT(!(reinterpret_cast<uintptr_t>(addr) & alignOffsetMask));
if (!addr) {
addr = getRandomPageBase();
addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) & alignBaseMask);
}
void* ret = systemAllocPages(addr, len);
if (!ret || !(reinterpret_cast<uintptr_t>(ret) & alignOffsetMask))
return ret;
freePages(ret, len);
size_t tryLen = len + (align - kPageAllocationGranularity);
RELEASE_ASSERT(tryLen > len);
int count = 0;
while (count++ < 100) {
ret = systemAllocPages(addr, tryLen);
if (!ret)
return 0;
addr = reinterpret_cast<void*>((reinterpret_cast<uintptr_t>(ret) + alignOffsetMask) & alignBaseMask);
if (trimMapping(ret, tryLen, addr, len))
return addr;
freePages(ret, tryLen);
ret = systemAllocPages(addr, len);
if (ret == addr || !ret)
return ret;
freePages(ret, len);
addr = getRandomPageBase();
addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) & alignBaseMask);
}
IMMEDIATE_CRASH();
return 0;
}
void freePages(void* addr, size_t len)
{
ASSERT(!(reinterpret_cast<uintptr_t>(addr) & kPageAllocationGranularityOffsetMask));
ASSERT(!(len & kPageAllocationGranularityOffsetMask));
#if OS(POSIX)
int ret = munmap(addr, len);
RELEASE_ASSERT(!ret);
#else
BOOL ret = VirtualFree(addr, 0, MEM_RELEASE);
RELEASE_ASSERT(ret);
#endif
}
void setSystemPagesInaccessible(void* addr, size_t len)
{
ASSERT(!(len & kSystemPageOffsetMask));
#if OS(POSIX)
int ret = mprotect(addr, len, PROT_NONE);
RELEASE_ASSERT(!ret);
#else
BOOL ret = VirtualFree(addr, len, MEM_DECOMMIT);
RELEASE_ASSERT(ret);
#endif
}
void setSystemPagesAccessible(void* addr, size_t len)
{
ASSERT(!(len & kSystemPageOffsetMask));
#if OS(POSIX)
int ret = mprotect(addr, len, PROT_READ | PROT_WRITE);
RELEASE_ASSERT(!ret);
#else
void* ret = VirtualAlloc(addr, len, MEM_COMMIT, PAGE_READWRITE);
RELEASE_ASSERT(ret);
#endif
}
void decommitSystemPages(void* addr, size_t len)
{
ASSERT(!(len & kSystemPageOffsetMask));
#if OS(POSIX)
int ret = madvise(addr, len, MADV_FREE);
RELEASE_ASSERT(!ret);
#else
void* ret = VirtualAlloc(addr, len, MEM_RESET, PAGE_READWRITE);
RELEASE_ASSERT(ret);
#endif
}
}