Commit d607f1e7 authored by Bill Budge's avatar Bill Budge Committed by Commit Bot

[Memory] Move GetRandomMmapAddr from base::OS platform to v8::internal.

- Moves GetRandomMmapAddr from platform to v8::internal allocation
  primitives, in preparation for delegating this to the embedder.
- Adds hint parameters to OS functions that used to use this function.

Bug: chromium:756050
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng
Change-Id: Iad72e6eac9c08a3e22c2cd2b2905623b8e514ae0
Reviewed-on: https://chromium-review.googlesource.com/677777Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Commit-Queue: Bill Budge <bbudge@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48124}
parent c9b08c65
...@@ -6,8 +6,11 @@ ...@@ -6,8 +6,11 @@
#include <stdlib.h> // For free, malloc. #include <stdlib.h> // For free, malloc.
#include "src/base/bits.h" #include "src/base/bits.h"
#include "src/base/lazy-instance.h"
#include "src/base/logging.h" #include "src/base/logging.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
#include "src/base/utils/random-number-generator.h"
#include "src/flags.h"
#include "src/utils.h" #include "src/utils.h"
#include "src/v8.h" #include "src/v8.h"
...@@ -203,5 +206,104 @@ bool AlignedAllocVirtualMemory(size_t size, size_t alignment, void* hint, ...@@ -203,5 +206,104 @@ bool AlignedAllocVirtualMemory(size_t size, size_t alignment, void* hint,
return result->IsReserved(); return result->IsReserved();
} }
namespace {
struct RNGInitializer {
static void Construct(void* mem) {
auto rng = new (mem) base::RandomNumberGenerator();
int64_t random_seed = FLAG_random_seed;
if (random_seed) {
rng->SetSeed(random_seed);
}
}
};
} // namespace
static base::LazyInstance<base::RandomNumberGenerator, RNGInitializer>::type
random_number_generator = LAZY_INSTANCE_INITIALIZER;
void* GetRandomMmapAddr() {
#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
defined(THREAD_SANITIZER)
// Dynamic tools do not support custom mmap addresses.
return NULL;
#endif
uintptr_t raw_addr;
random_number_generator.Pointer()->NextBytes(&raw_addr, sizeof(raw_addr));
#if V8_OS_POSIX
#if V8_TARGET_ARCH_X64
// Currently available CPUs have 48 bits of virtual addressing. Truncate
// the hint address to 46 bits to give the kernel a fighting chance of
// fulfilling our placement request.
raw_addr &= V8_UINT64_C(0x3ffffffff000);
#elif V8_TARGET_ARCH_PPC64
#if V8_OS_AIX
// AIX: 64 bits of virtual addressing, but we limit address range to:
// a) minimize Segment Lookaside Buffer (SLB) misses and
raw_addr &= V8_UINT64_C(0x3ffff000);
// Use extra address space to isolate the mmap regions.
raw_addr += V8_UINT64_C(0x400000000000);
#elif V8_TARGET_BIG_ENDIAN
// Big-endian Linux: 44 bits of virtual addressing.
raw_addr &= V8_UINT64_C(0x03fffffff000);
#else
// Little-endian Linux: 48 bits of virtual addressing.
raw_addr &= V8_UINT64_C(0x3ffffffff000);
#endif
#elif V8_TARGET_ARCH_S390X
// Linux on Z uses bits 22-32 for Region Indexing, which translates to 42 bits
// of virtual addressing. Truncate to 40 bits to allow kernel chance to
// fulfill request.
raw_addr &= V8_UINT64_C(0xfffffff000);
#elif V8_TARGET_ARCH_S390
// 31 bits of virtual addressing. Truncate to 29 bits to allow kernel chance
// to fulfill request.
raw_addr &= 0x1ffff000;
#else
raw_addr &= 0x3ffff000;
#ifdef __sun
// For our Solaris/illumos mmap hint, we pick a random address in the bottom
// half of the top half of the address space (that is, the third quarter).
// Because we do not MAP_FIXED, this will be treated only as a hint -- the
// system will not fail to mmap() because something else happens to already
// be mapped at our random address. We deliberately set the hint high enough
// to get well above the system's break (that is, the heap); Solaris and
// illumos will try the hint and if that fails allocate as if there were
// no hint at all. The high hint prevents the break from getting hemmed in
// at low values, ceding half of the address space to the system heap.
raw_addr += 0x80000000;
#elif V8_OS_AIX
// The range 0x30000000 - 0xD0000000 is available on AIX;
// choose the upper range.
raw_addr += 0x90000000;
#else
// The range 0x20000000 - 0x60000000 is relatively unpopulated across a
// variety of ASLR modes (PAE kernel, NX compat mode, etc) and on macos
// 10.6 and 10.7.
raw_addr += 0x20000000;
#endif
#endif
#else // V8_OS_WIN
// The address range used to randomize RWX allocations in OS::Allocate
// Try not to map pages into the default range that windows loads DLLs
// Use a multiple of 64k to prevent committing unused memory.
// Note: This does not guarantee RWX regions will be within the
// range kAllocationRandomAddressMin to kAllocationRandomAddressMax
#ifdef V8_HOST_ARCH_64_BIT
static const uintptr_t kAllocationRandomAddressMin = 0x0000000080000000;
static const uintptr_t kAllocationRandomAddressMax = 0x000003FFFFFF0000;
#else
static const uintptr_t kAllocationRandomAddressMin = 0x04000000;
static const uintptr_t kAllocationRandomAddressMax = 0x3FFF0000;
#endif
raw_addr <<= kPageSizeBits;
raw_addr += kAllocationRandomAddressMin;
raw_addr &= kAllocationRandomAddressMax;
#endif // V8_OS_WIN
return reinterpret_cast<void*>(raw_addr);
}
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
...@@ -159,6 +159,9 @@ bool AllocVirtualMemory(size_t size, void* hint, VirtualMemory* result); ...@@ -159,6 +159,9 @@ bool AllocVirtualMemory(size_t size, void* hint, VirtualMemory* result);
bool AlignedAllocVirtualMemory(size_t size, size_t alignment, void* hint, bool AlignedAllocVirtualMemory(size_t size, size_t alignment, void* hint,
VirtualMemory* result); VirtualMemory* result);
// Generate a random address to be used for hinting mmap().
void* GetRandomMmapAddr();
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
......
...@@ -485,7 +485,7 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { ...@@ -485,7 +485,7 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
virtual void Free(void* data, size_t) { free(data); } virtual void Free(void* data, size_t) { free(data); }
virtual void* Reserve(size_t length) { virtual void* Reserve(size_t length) {
return base::OS::ReserveRegion(length, base::OS::GetRandomMmapAddr()); return base::OS::ReserveRegion(length, i::GetRandomMmapAddr());
} }
virtual void Free(void* data, size_t length, virtual void Free(void* data, size_t length,
......
...@@ -205,7 +205,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { ...@@ -205,7 +205,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
return result; return result;
} }
void OS::SignalCodeMovingGC() {} void OS::SignalCodeMovingGC(void* hint) {}
} // namespace base } // namespace base
} // namespace v8 } // namespace v8
...@@ -220,8 +220,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { ...@@ -220,8 +220,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
return result; return result;
} }
void OS::SignalCodeMovingGC(void* hint) {
void OS::SignalCodeMovingGC() {
// Nothing to do on Cygwin. // Nothing to do on Cygwin.
} }
......
...@@ -183,7 +183,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { ...@@ -183,7 +183,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
return result; return result;
} }
void OS::SignalCodeMovingGC() {} void OS::SignalCodeMovingGC(void* hint) {}
} // namespace base } // namespace base
} // namespace v8 } // namespace v8
...@@ -140,7 +140,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { ...@@ -140,7 +140,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
return std::vector<SharedLibraryAddress>(); return std::vector<SharedLibraryAddress>();
} }
void OS::SignalCodeMovingGC() { void OS::SignalCodeMovingGC(void* hint) {
CHECK(false); // TODO(scottmg): Port, https://crbug.com/731217. CHECK(false); // TODO(scottmg): Port, https://crbug.com/731217.
} }
......
...@@ -272,7 +272,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { ...@@ -272,7 +272,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
return result; return result;
} }
void OS::SignalCodeMovingGC() { void OS::SignalCodeMovingGC(void* hint) {
// Support for ll_prof.py. // Support for ll_prof.py.
// //
// The Linux profiler built into the kernel logs all mmap's with // The Linux profiler built into the kernel logs all mmap's with
...@@ -287,8 +287,8 @@ void OS::SignalCodeMovingGC() { ...@@ -287,8 +287,8 @@ void OS::SignalCodeMovingGC() {
OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile()); OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
OS::Abort(); OS::Abort();
} }
void* addr = mmap(OS::GetRandomMmapAddr(), size, PROT_READ | PROT_EXEC, void* addr =
MAP_PRIVATE, fileno(f), 0); mmap(hint, size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fileno(f), 0);
DCHECK_NE(MAP_FAILED, addr); DCHECK_NE(MAP_FAILED, addr);
OS::Free(addr, size); OS::Free(addr, size);
fclose(f); fclose(f);
......
...@@ -175,7 +175,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { ...@@ -175,7 +175,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
return result; return result;
} }
void OS::SignalCodeMovingGC() {} void OS::SignalCodeMovingGC(void* hint) {}
TimezoneCache* OS::CreateTimezoneCache() { TimezoneCache* OS::CreateTimezoneCache() {
return new PosixDefaultTimezoneCache(); return new PosixDefaultTimezoneCache();
......
...@@ -198,8 +198,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { ...@@ -198,8 +198,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
return result; return result;
} }
void OS::SignalCodeMovingGC(void* hint) {
void OS::SignalCodeMovingGC() {
// Support for ll_prof.py. // Support for ll_prof.py.
// //
// The Linux profiler built into the kernel logs all mmap's with // The Linux profiler built into the kernel logs all mmap's with
...@@ -214,9 +213,9 @@ void OS::SignalCodeMovingGC() { ...@@ -214,9 +213,9 @@ void OS::SignalCodeMovingGC() {
OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile()); OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
OS::Abort(); OS::Abort();
} }
void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE, void* addr =
fileno(f), 0); mmap(hint, size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fileno(f), 0);
DCHECK(addr != MAP_FAILED); DCHECK_NE(MAP_FAILED, addr);
OS::Free(addr, size); OS::Free(addr, size);
fclose(f); fclose(f);
} }
......
...@@ -150,15 +150,7 @@ void OS::Unprotect(void* address, const size_t size) { ...@@ -150,15 +150,7 @@ void OS::Unprotect(void* address, const size_t size) {
#endif #endif
} }
static LazyInstance<RandomNumberGenerator>::type void OS::Initialize(bool hard_abort, const char* const gc_fake_mmap) {
platform_random_number_generator = LAZY_INSTANCE_INITIALIZER;
void OS::Initialize(int64_t random_seed, bool hard_abort,
const char* const gc_fake_mmap) {
if (random_seed) {
platform_random_number_generator.Pointer()->SetSeed(random_seed);
}
g_hard_abort = hard_abort; g_hard_abort = hard_abort;
g_gc_fake_mmap = gc_fake_mmap; g_gc_fake_mmap = gc_fake_mmap;
} }
...@@ -169,72 +161,6 @@ const char* OS::GetGCFakeMMapFile() { ...@@ -169,72 +161,6 @@ const char* OS::GetGCFakeMMapFile() {
} }
void* OS::GetRandomMmapAddr() {
#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
defined(THREAD_SANITIZER)
// Dynamic tools do not support custom mmap addresses.
return NULL;
#endif
uintptr_t raw_addr;
platform_random_number_generator.Pointer()->NextBytes(&raw_addr,
sizeof(raw_addr));
#if V8_TARGET_ARCH_X64
// Currently available CPUs have 48 bits of virtual addressing. Truncate
// the hint address to 46 bits to give the kernel a fighting chance of
// fulfilling our placement request.
raw_addr &= V8_UINT64_C(0x3ffffffff000);
#elif V8_TARGET_ARCH_PPC64
#if V8_OS_AIX
// AIX: 64 bits of virtual addressing, but we limit address range to:
// a) minimize Segment Lookaside Buffer (SLB) misses and
raw_addr &= V8_UINT64_C(0x3ffff000);
// Use extra address space to isolate the mmap regions.
raw_addr += V8_UINT64_C(0x400000000000);
#elif V8_TARGET_BIG_ENDIAN
// Big-endian Linux: 44 bits of virtual addressing.
raw_addr &= V8_UINT64_C(0x03fffffff000);
#else
// Little-endian Linux: 48 bits of virtual addressing.
raw_addr &= V8_UINT64_C(0x3ffffffff000);
#endif
#elif V8_TARGET_ARCH_S390X
// Linux on Z uses bits 22-32 for Region Indexing, which translates to 42 bits
// of virtual addressing. Truncate to 40 bits to allow kernel chance to
// fulfill request.
raw_addr &= V8_UINT64_C(0xfffffff000);
#elif V8_TARGET_ARCH_S390
// 31 bits of virtual addressing. Truncate to 29 bits to allow kernel chance
// to fulfill request.
raw_addr &= 0x1ffff000;
#else
raw_addr &= 0x3ffff000;
# ifdef __sun
// For our Solaris/illumos mmap hint, we pick a random address in the bottom
// half of the top half of the address space (that is, the third quarter).
// Because we do not MAP_FIXED, this will be treated only as a hint -- the
// system will not fail to mmap() because something else happens to already
// be mapped at our random address. We deliberately set the hint high enough
// to get well above the system's break (that is, the heap); Solaris and
// illumos will try the hint and if that fails allocate as if there were
// no hint at all. The high hint prevents the break from getting hemmed in
// at low values, ceding half of the address space to the system heap.
raw_addr += 0x80000000;
#elif V8_OS_AIX
// The range 0x30000000 - 0xD0000000 is available on AIX;
// choose the upper range.
raw_addr += 0x90000000;
# else
// The range 0x20000000 - 0x60000000 is relatively unpopulated across a
// variety of ASLR modes (PAE kernel, NX compat mode, etc) and on macos
// 10.6 and 10.7.
raw_addr += 0x20000000;
# endif
#endif
return reinterpret_cast<void*>(raw_addr);
}
size_t OS::AllocateAlignment() { size_t OS::AllocateAlignment() {
return static_cast<size_t>(sysconf(_SC_PAGESIZE)); return static_cast<size_t>(sysconf(_SC_PAGESIZE));
} }
...@@ -294,14 +220,13 @@ class PosixMemoryMappedFile final : public OS::MemoryMappedFile { ...@@ -294,14 +220,13 @@ class PosixMemoryMappedFile final : public OS::MemoryMappedFile {
// static // static
OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name, void* hint) {
if (FILE* file = fopen(name, "r+")) { if (FILE* file = fopen(name, "r+")) {
if (fseek(file, 0, SEEK_END) == 0) { if (fseek(file, 0, SEEK_END) == 0) {
long size = ftell(file); // NOLINT(runtime/int) long size = ftell(file); // NOLINT(runtime/int)
if (size >= 0) { if (size >= 0) {
void* const memory = void* const memory = mmap(hint, size, PROT_READ | PROT_WRITE,
mmap(OS::GetRandomMmapAddr(), size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
MAP_SHARED, fileno(file), 0);
if (memory != MAP_FAILED) { if (memory != MAP_FAILED) {
return new PosixMemoryMappedFile(file, memory, size); return new PosixMemoryMappedFile(file, memory, size);
} }
...@@ -314,13 +239,13 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { ...@@ -314,13 +239,13 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
// static // static
OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, void* hint,
size_t size, void* initial) { size_t size, void* initial) {
if (FILE* file = fopen(name, "w+")) { if (FILE* file = fopen(name, "w+")) {
size_t result = fwrite(initial, 1, size, file); size_t result = fwrite(initial, 1, size, file);
if (result == size && !ferror(file)) { if (result == size && !ferror(file)) {
void* memory = mmap(OS::GetRandomMmapAddr(), result, void* memory = mmap(hint, result, PROT_READ | PROT_WRITE, MAP_SHARED,
PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); fileno(file), 0);
if (memory != MAP_FAILED) { if (memory != MAP_FAILED) {
return new PosixMemoryMappedFile(file, memory, result); return new PosixMemoryMappedFile(file, memory, result);
} }
......
...@@ -242,7 +242,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { ...@@ -242,7 +242,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
return result; return result;
} }
void OS::SignalCodeMovingGC() {} void OS::SignalCodeMovingGC(void* hint) {}
} // namespace base } // namespace base
} // namespace v8 } // namespace v8
...@@ -163,7 +163,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { ...@@ -163,7 +163,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
return std::vector<SharedLibraryAddress>(); return std::vector<SharedLibraryAddress>();
} }
void OS::SignalCodeMovingGC() {} void OS::SignalCodeMovingGC(void* hint) {}
} // namespace base } // namespace base
} // namespace v8 } // namespace v8
...@@ -20,12 +20,10 @@ ...@@ -20,12 +20,10 @@
#include "src/base/win32-headers.h" #include "src/base/win32-headers.h"
#include "src/base/bits.h" #include "src/base/bits.h"
#include "src/base/lazy-instance.h"
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
#include "src/base/platform/time.h" #include "src/base/platform/time.h"
#include "src/base/timezone-cache.h" #include "src/base/timezone-cache.h"
#include "src/base/utils/random-number-generator.h"
// Extra functions for MinGW. Most of these are the _s functions which are in // Extra functions for MinGW. Most of these are the _s functions which are in
// the Microsoft Visual Studio C++ CRT. // the Microsoft Visual Studio C++ CRT.
...@@ -701,42 +699,10 @@ size_t OS::AllocateAlignment() { ...@@ -701,42 +699,10 @@ size_t OS::AllocateAlignment() {
return allocate_alignment; return allocate_alignment;
} }
void OS::Initialize(bool hard_abort, const char* const gc_fake_mmap) {
static LazyInstance<RandomNumberGenerator>::type
platform_random_number_generator = LAZY_INSTANCE_INITIALIZER;
void OS::Initialize(int64_t random_seed, bool hard_abort,
const char* const gc_fake_mmap) {
if (random_seed) {
platform_random_number_generator.Pointer()->SetSeed(random_seed);
}
g_hard_abort = hard_abort; g_hard_abort = hard_abort;
} }
void* OS::GetRandomMmapAddr() {
// The address range used to randomize RWX allocations in OS::Allocate
// Try not to map pages into the default range that windows loads DLLs
// Use a multiple of 64k to prevent committing unused memory.
// Note: This does not guarantee RWX regions will be within the
// range kAllocationRandomAddressMin to kAllocationRandomAddressMax
#ifdef V8_HOST_ARCH_64_BIT
static const uintptr_t kAllocationRandomAddressMin = 0x0000000080000000;
static const uintptr_t kAllocationRandomAddressMax = 0x000003FFFFFF0000;
#else
static const uintptr_t kAllocationRandomAddressMin = 0x04000000;
static const uintptr_t kAllocationRandomAddressMax = 0x3FFF0000;
#endif
uintptr_t address;
platform_random_number_generator.Pointer()->NextBytes(&address,
sizeof(address));
address <<= kPageSizeBits;
address += kAllocationRandomAddressMin;
address &= kAllocationRandomAddressMax;
return reinterpret_cast<void *>(address);
}
namespace { namespace {
static void* RandomizedVirtualAlloc(size_t size, int action, int protection, static void* RandomizedVirtualAlloc(size_t size, int action, int protection,
...@@ -952,7 +918,7 @@ class Win32MemoryMappedFile final : public OS::MemoryMappedFile { ...@@ -952,7 +918,7 @@ class Win32MemoryMappedFile final : public OS::MemoryMappedFile {
// static // static
OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name, void* hint) {
// Open a physical file // Open a physical file
HANDLE file = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, HANDLE file = CreateFileA(name, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
...@@ -960,7 +926,7 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { ...@@ -960,7 +926,7 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
DWORD size = GetFileSize(file, NULL); DWORD size = GetFileSize(file, NULL);
// Create a file mapping for the physical file // Create a file mapping for the physical file. Ignore hint on Windows.
HANDLE file_mapping = HANDLE file_mapping =
CreateFileMapping(file, NULL, PAGE_READWRITE, 0, size, NULL); CreateFileMapping(file, NULL, PAGE_READWRITE, 0, size, NULL);
if (file_mapping == NULL) return NULL; if (file_mapping == NULL) return NULL;
...@@ -972,14 +938,14 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { ...@@ -972,14 +938,14 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
// static // static
OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, void* hint,
size_t size, void* initial) { size_t size, void* initial) {
// Open a physical file // Open a physical file
HANDLE file = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, HANDLE file = CreateFileA(name, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, 0, NULL); OPEN_ALWAYS, 0, NULL);
if (file == NULL) return NULL; if (file == NULL) return NULL;
// Create a file mapping for the physical file // Create a file mapping for the physical file. Ignore hint on Windows.
HANDLE file_mapping = CreateFileMapping(file, NULL, PAGE_READWRITE, 0, HANDLE file_mapping = CreateFileMapping(file, NULL, PAGE_READWRITE, 0,
static_cast<DWORD>(size), NULL); static_cast<DWORD>(size), NULL);
if (file_mapping == NULL) return NULL; if (file_mapping == NULL) return NULL;
...@@ -1248,20 +1214,13 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { ...@@ -1248,20 +1214,13 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
return LoadSymbols(process_handle); return LoadSymbols(process_handle);
} }
void OS::SignalCodeMovingGC() {
}
#else // __MINGW32__ #else // __MINGW32__
std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
return std::vector<OS::SharedLibraryAddress>(); return std::vector<OS::SharedLibraryAddress>();
} }
void OS::SignalCodeMovingGC() { }
#endif // __MINGW32__ #endif // __MINGW32__
void OS::SignalCodeMovingGC(void* hint) {}
int OS::ActivationFrameAlignment() { int OS::ActivationFrameAlignment() {
#ifdef _WIN64 #ifdef _WIN64
......
...@@ -107,12 +107,9 @@ class TimezoneCache; ...@@ -107,12 +107,9 @@ class TimezoneCache;
class V8_BASE_EXPORT OS { class V8_BASE_EXPORT OS {
public: public:
// Initialize the OS class. // Initialize the OS class.
// - random_seed: Used for the GetRandomMmapAddress() if non-zero.
// - hard_abort: If true, OS::Abort() will crash instead of aborting. // - hard_abort: If true, OS::Abort() will crash instead of aborting.
// - gc_fake_mmap: Name of the file for fake gc mmap used in ll_prof. // - gc_fake_mmap: Name of the file for fake gc mmap used in ll_prof.
static void Initialize(int64_t random_seed, static void Initialize(bool hard_abort, const char* const gc_fake_mmap);
bool hard_abort,
const char* const gc_fake_mmap);
// Returns the accumulated user time for thread. This routine // Returns the accumulated user time for thread. This routine
// can be used for profiling. The implementation should // can be used for profiling. The implementation should
...@@ -191,9 +188,6 @@ class V8_BASE_EXPORT OS { ...@@ -191,9 +188,6 @@ class V8_BASE_EXPORT OS {
// Make a region of memory readable and writable. // Make a region of memory readable and writable.
static void Unprotect(void* address, const size_t size); static void Unprotect(void* address, const size_t size);
// Generate a random address to be used for hinting mmap().
static void* GetRandomMmapAddr();
// Get the Alignment guaranteed by Allocate(). // Get the Alignment guaranteed by Allocate().
static size_t AllocateAlignment(); static size_t AllocateAlignment();
...@@ -237,8 +231,8 @@ class V8_BASE_EXPORT OS { ...@@ -237,8 +231,8 @@ class V8_BASE_EXPORT OS {
virtual void* memory() const = 0; virtual void* memory() const = 0;
virtual size_t size() const = 0; virtual size_t size() const = 0;
static MemoryMappedFile* open(const char* name); static MemoryMappedFile* open(const char* name, void* hint);
static MemoryMappedFile* create(const char* name, size_t size, static MemoryMappedFile* create(const char* name, void* hint, size_t size,
void* initial); void* initial);
}; };
...@@ -277,7 +271,7 @@ class V8_BASE_EXPORT OS { ...@@ -277,7 +271,7 @@ class V8_BASE_EXPORT OS {
// process that a code moving garbage collection starts. Can do // process that a code moving garbage collection starts. Can do
// nothing, in which case the code objects must not move (e.g., by // nothing, in which case the code objects must not move (e.g., by
// using --never-compact) if accurate profiling is desired. // using --never-compact) if accurate profiling is desired.
static void SignalCodeMovingGC(); static void SignalCodeMovingGC(void* hint);
// Support runtime detection of whether the hard float option of the // Support runtime detection of whether the hard float option of the
// EABI is used. // EABI is used.
......
...@@ -1545,7 +1545,7 @@ Counter* CounterCollection::GetNextCounter() { ...@@ -1545,7 +1545,7 @@ Counter* CounterCollection::GetNextCounter() {
void Shell::MapCounters(v8::Isolate* isolate, const char* name) { void Shell::MapCounters(v8::Isolate* isolate, const char* name) {
counters_file_ = base::OS::MemoryMappedFile::create( counters_file_ = base::OS::MemoryMappedFile::create(
name, sizeof(CounterCollection), &local_counters_); name, nullptr, sizeof(CounterCollection), &local_counters_);
void* memory = (counters_file_ == NULL) ? void* memory = (counters_file_ == NULL) ?
NULL : counters_file_->memory(); NULL : counters_file_->memory();
if (memory == NULL) { if (memory == NULL) {
......
...@@ -5392,7 +5392,7 @@ bool Heap::SetUp() { ...@@ -5392,7 +5392,7 @@ bool Heap::SetUp() {
} }
mmap_region_base_ = mmap_region_base_ =
reinterpret_cast<uintptr_t>(base::OS::GetRandomMmapAddr()) & reinterpret_cast<uintptr_t>(v8::internal::GetRandomMmapAddr()) &
~kMmapRegionMask; ~kMmapRegionMask;
// Set up memory allocator. // Set up memory allocator.
......
...@@ -1521,7 +1521,7 @@ class Heap { ...@@ -1521,7 +1521,7 @@ class Heap {
void ReportCodeStatistics(const char* title); void ReportCodeStatistics(const char* title);
#endif #endif
void* GetRandomMmapAddr() { void* GetRandomMmapAddr() {
void* result = base::OS::GetRandomMmapAddr(); void* result = v8::internal::GetRandomMmapAddr();
#if V8_TARGET_ARCH_X64 #if V8_TARGET_ARCH_X64
#if V8_OS_MACOSX #if V8_OS_MACOSX
// The Darwin kernel [as of macOS 10.12.5] does not clean up page // The Darwin kernel [as of macOS 10.12.5] does not clean up page
...@@ -1532,7 +1532,7 @@ class Heap { ...@@ -1532,7 +1532,7 @@ class Heap {
// killed. Confine the hint to a 32-bit section of the virtual address // killed. Confine the hint to a 32-bit section of the virtual address
// space. See crbug.com/700928. // space. See crbug.com/700928.
uintptr_t offset = uintptr_t offset =
reinterpret_cast<uintptr_t>(base::OS::GetRandomMmapAddr()) & reinterpret_cast<uintptr_t>(v8::internal::GetRandomMmapAddr()) &
kMmapRegionMask; kMmapRegionMask;
result = reinterpret_cast<void*>(mmap_region_base_ + offset); result = reinterpret_cast<void*>(mmap_region_base_ + offset);
#endif // V8_OS_MACOSX #endif // V8_OS_MACOSX
......
...@@ -122,7 +122,7 @@ bool CodeRange::SetUp(size_t requested) { ...@@ -122,7 +122,7 @@ bool CodeRange::SetUp(size_t requested) {
requested, requested,
Max(kCodeRangeAreaAlignment, Max(kCodeRangeAreaAlignment,
static_cast<size_t>(base::OS::AllocateAlignment())), static_cast<size_t>(base::OS::AllocateAlignment())),
base::OS::GetRandomMmapAddr(), &reservation)) { v8::internal::GetRandomMmapAddr(), &reservation)) {
return false; return false;
} }
......
...@@ -1281,7 +1281,7 @@ void Logger::CodeDisableOptEvent(AbstractCode* code, ...@@ -1281,7 +1281,7 @@ void Logger::CodeDisableOptEvent(AbstractCode* code,
void Logger::CodeMovingGCEvent() { void Logger::CodeMovingGCEvent() {
if (!is_logging_code_events()) return; if (!is_logging_code_events()) return;
if (!log_->IsEnabled() || !FLAG_ll_prof) return; if (!log_->IsEnabled() || !FLAG_ll_prof) return;
base::OS::SignalCodeMovingGC(); base::OS::SignalCodeMovingGC(GetRandomMmapAddr());
} }
void Logger::RegExpCodeCreateEvent(AbstractCode* code, String* source) { void Logger::RegExpCodeCreateEvent(AbstractCode* code, String* source) {
......
...@@ -66,7 +66,7 @@ void V8::InitializeOncePerProcessImpl() { ...@@ -66,7 +66,7 @@ void V8::InitializeOncePerProcessImpl() {
FLAG_max_semi_space_size = 1; FLAG_max_semi_space_size = 1;
} }
base::OS::Initialize(FLAG_random_seed, FLAG_hard_abort, FLAG_gc_fake_mmap); base::OS::Initialize(FLAG_hard_abort, FLAG_gc_fake_mmap);
Isolate::InitializeOncePerProcess(); Isolate::InitializeOncePerProcess();
......
...@@ -54,7 +54,7 @@ size_t GetHugeMemoryAmount() { ...@@ -54,7 +54,7 @@ size_t GetHugeMemoryAmount() {
static size_t huge_memory = 0; static size_t huge_memory = 0;
if (!huge_memory) { if (!huge_memory) {
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
huge_memory |= bit_cast<size_t>(v8::base::OS::GetRandomMmapAddr()); huge_memory |= bit_cast<size_t>(v8::internal::GetRandomMmapAddr());
} }
// Make it larger than the available address space. // Make it larger than the available address space.
huge_memory *= 2; huge_memory *= 2;
......
...@@ -37,7 +37,7 @@ namespace internal { ...@@ -37,7 +37,7 @@ namespace internal {
TEST(OSReserveMemory) { TEST(OSReserveMemory) {
size_t mem_size = 0; size_t mem_size = 0;
void* mem_addr = OS::ReserveAlignedRegion(1 * MB, OS::AllocateAlignment(), void* mem_addr = OS::ReserveAlignedRegion(1 * MB, OS::AllocateAlignment(),
OS::GetRandomMmapAddr(), &mem_size); GetRandomMmapAddr(), &mem_size);
CHECK_NE(0, mem_size); CHECK_NE(0, mem_size);
CHECK_NOT_NULL(mem_addr); CHECK_NOT_NULL(mem_addr);
size_t block_size = 4 * KB; size_t block_size = 4 * KB;
......
...@@ -37,7 +37,7 @@ namespace internal { ...@@ -37,7 +37,7 @@ namespace internal {
TEST(OSReserveMemory) { TEST(OSReserveMemory) {
size_t mem_size = 0; size_t mem_size = 0;
void* mem_addr = OS::ReserveAlignedRegion(1 * MB, OS::AllocateAlignment(), void* mem_addr = OS::ReserveAlignedRegion(1 * MB, OS::AllocateAlignment(),
OS::GetRandomMmapAddr(), &mem_size); GetRandomMmapAddr(), &mem_size);
CHECK_NE(0, mem_size); CHECK_NE(0, mem_size);
CHECK_NOT_NULL(mem_addr); CHECK_NOT_NULL(mem_addr);
size_t block_size = 4 * KB; size_t block_size = 4 * KB;
......
...@@ -99,7 +99,7 @@ TEST_F(HeapTest, ASLR) { ...@@ -99,7 +99,7 @@ TEST_F(HeapTest, ASLR) {
} }
if (hints.size() == 1) { if (hints.size() == 1) {
EXPECT_TRUE((*hints.begin()) == nullptr); EXPECT_TRUE((*hints.begin()) == nullptr);
EXPECT_TRUE(base::OS::GetRandomMmapAddr() == nullptr); EXPECT_TRUE(v8::internal::GetRandomMmapAddr() == nullptr);
} else { } else {
// It is unlikely that 1000 random samples will collide to less then 500 // It is unlikely that 1000 random samples will collide to less then 500
// values. // values.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment