| Index: src/platform-win32.cc
|
| diff --git a/src/platform-win32.cc b/src/platform-win32.cc
|
| index 8f188a057f1a8a7ca2595280fe4121258ba5fd3b..ea4f7ea11f4177bdbee4f5de2dca570e50985741 100644
|
| --- a/src/platform-win32.cc
|
| +++ b/src/platform-win32.cc
|
| @@ -69,6 +69,11 @@ int strncasecmp(const char* s1, const char* s2, int n) {
|
| #define _TRUNCATE 0
|
| #define STRUNCATE 80
|
|
|
| +inline void MemoryBarrier() {
|
| + int barrier = 0;
|
| + __asm__ __volatile__("xchgl %%eax,%0 ":"=r" (barrier));
|
| +}
|
| +
|
| #endif // __MINGW64_VERSION_MAJOR
|
|
|
|
|
| @@ -123,6 +128,11 @@ int strncpy_s(char* dest, size_t dest_size, const char* source, size_t count) {
|
| namespace v8 {
|
| namespace internal {
|
|
|
| +intptr_t OS::MaxVirtualMemory() {
|
| + return 0;
|
| +}
|
| +
|
| +
|
| double ceiling(double x) {
|
| return ceil(x);
|
| }
|
| @@ -733,6 +743,127 @@ void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) {
|
| #undef STRUNCATE
|
|
|
|
|
| +// Get the system's page size used by VirtualAlloc() or the next power
|
| +// of two. The reason for always returning a power of two is that the
|
| +// rounding up in OS::Allocate expects that.
|
| +static size_t GetPageSize() {
|
| + static size_t page_size = 0;
|
| + if (page_size == 0) {
|
| + SYSTEM_INFO info;
|
| + GetSystemInfo(&info);
|
| + page_size = RoundUpToPowerOf2(info.dwPageSize);
|
| + }
|
| + return page_size;
|
| +}
|
| +
|
| +
|
| +// The allocation alignment is the guaranteed alignment for
|
| +// VirtualAlloc'ed blocks of memory.
|
| +size_t OS::AllocateAlignment() {
|
| + static size_t allocate_alignment = 0;
|
| + if (allocate_alignment == 0) {
|
| + SYSTEM_INFO info;
|
| + GetSystemInfo(&info);
|
| + allocate_alignment = info.dwAllocationGranularity;
|
| + }
|
| + return allocate_alignment;
|
| +}
|
| +
|
| +
|
| +void* OS::GetRandomMmapAddr() {
|
| + Isolate* isolate = Isolate::UncheckedCurrent();
|
| + // Note that the current isolate isn't set up in a call path via
|
| + // CpuFeatures::Probe. We don't care about randomization in this case because
|
| + // the code page is immediately freed.
|
| + if (isolate != NULL) {
|
| + // 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 intptr_t kAllocationRandomAddressMin = 0x0000000080000000;
|
| + static const intptr_t kAllocationRandomAddressMax = 0x000003FFFFFF0000;
|
| +#else
|
| + static const intptr_t kAllocationRandomAddressMin = 0x04000000;
|
| + static const intptr_t kAllocationRandomAddressMax = 0x3FFF0000;
|
| +#endif
|
| + uintptr_t address =
|
| + (isolate->random_number_generator()->NextInt() << kPageSizeBits) |
|
| + kAllocationRandomAddressMin;
|
| + address &= kAllocationRandomAddressMax;
|
| + return reinterpret_cast<void *>(address);
|
| + }
|
| + return NULL;
|
| +}
|
| +
|
| +
|
| +static void* RandomizedVirtualAlloc(size_t size, int action, int protection) {
|
| + LPVOID base = NULL;
|
| +
|
| + if (protection == PAGE_EXECUTE_READWRITE || protection == PAGE_NOACCESS) {
|
| + // For exectutable pages try and randomize the allocation address
|
| + for (size_t attempts = 0; base == NULL && attempts < 3; ++attempts) {
|
| + base = VirtualAlloc(OS::GetRandomMmapAddr(), size, action, protection);
|
| + }
|
| + }
|
| +
|
| + // After three attempts give up and let the OS find an address to use.
|
| + if (base == NULL) base = VirtualAlloc(NULL, size, action, protection);
|
| +
|
| + return base;
|
| +}
|
| +
|
| +
|
| +void* OS::Allocate(const size_t requested,
|
| + size_t* allocated,
|
| + bool is_executable) {
|
| + // VirtualAlloc rounds allocated size to page size automatically.
|
| + size_t msize = RoundUp(requested, static_cast<int>(GetPageSize()));
|
| +
|
| + // Windows XP SP2 allows Data Excution Prevention (DEP).
|
| + int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
|
| +
|
| + LPVOID mbase = RandomizedVirtualAlloc(msize,
|
| + MEM_COMMIT | MEM_RESERVE,
|
| + prot);
|
| +
|
| + if (mbase == NULL) {
|
| + LOG(Isolate::Current(), StringEvent("OS::Allocate", "VirtualAlloc failed"));
|
| + return NULL;
|
| + }
|
| +
|
| + ASSERT(IsAligned(reinterpret_cast<size_t>(mbase), OS::AllocateAlignment()));
|
| +
|
| + *allocated = msize;
|
| + return mbase;
|
| +}
|
| +
|
| +
|
| +void OS::Free(void* address, const size_t size) {
|
| + // TODO(1240712): VirtualFree has a return value which is ignored here.
|
| + VirtualFree(address, 0, MEM_RELEASE);
|
| + USE(size);
|
| +}
|
| +
|
| +
|
| +intptr_t OS::CommitPageSize() {
|
| + return 4096;
|
| +}
|
| +
|
| +
|
| +void OS::ProtectCode(void* address, const size_t size) {
|
| + DWORD old_protect;
|
| + VirtualProtect(address, size, PAGE_EXECUTE_READ, &old_protect);
|
| +}
|
| +
|
| +
|
| +void OS::Guard(void* address, const size_t size) {
|
| + DWORD oldprotect;
|
| + VirtualProtect(address, size, PAGE_NOACCESS, &oldprotect);
|
| +}
|
| +
|
| +
|
| void OS::Sleep(int milliseconds) {
|
| ::Sleep(milliseconds);
|
| }
|
| @@ -1237,6 +1368,111 @@ int OS::ActivationFrameAlignment() {
|
| }
|
|
|
|
|
| +VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
|
| +
|
| +
|
| +VirtualMemory::VirtualMemory(size_t size)
|
| + : address_(ReserveRegion(size)), size_(size) { }
|
| +
|
| +
|
| +VirtualMemory::VirtualMemory(size_t size, size_t alignment)
|
| + : address_(NULL), size_(0) {
|
| + ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
|
| + size_t request_size = RoundUp(size + alignment,
|
| + static_cast<intptr_t>(OS::AllocateAlignment()));
|
| + void* address = ReserveRegion(request_size);
|
| + if (address == NULL) return;
|
| + Address base = RoundUp(static_cast<Address>(address), alignment);
|
| + // Try reducing the size by freeing and then reallocating a specific area.
|
| + bool result = ReleaseRegion(address, request_size);
|
| + USE(result);
|
| + ASSERT(result);
|
| + address = VirtualAlloc(base, size, MEM_RESERVE, PAGE_NOACCESS);
|
| + if (address != NULL) {
|
| + request_size = size;
|
| + ASSERT(base == static_cast<Address>(address));
|
| + } else {
|
| + // Resizing failed, just go with a bigger area.
|
| + address = ReserveRegion(request_size);
|
| + if (address == NULL) return;
|
| + }
|
| + address_ = address;
|
| + size_ = request_size;
|
| +}
|
| +
|
| +
|
| +VirtualMemory::~VirtualMemory() {
|
| + if (IsReserved()) {
|
| + bool result = ReleaseRegion(address(), size());
|
| + ASSERT(result);
|
| + USE(result);
|
| + }
|
| +}
|
| +
|
| +
|
| +bool VirtualMemory::IsReserved() {
|
| + return address_ != NULL;
|
| +}
|
| +
|
| +
|
| +void VirtualMemory::Reset() {
|
| + address_ = NULL;
|
| + size_ = 0;
|
| +}
|
| +
|
| +
|
| +bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
|
| + return CommitRegion(address, size, is_executable);
|
| +}
|
| +
|
| +
|
| +bool VirtualMemory::Uncommit(void* address, size_t size) {
|
| + ASSERT(IsReserved());
|
| + return UncommitRegion(address, size);
|
| +}
|
| +
|
| +
|
| +bool VirtualMemory::Guard(void* address) {
|
| + if (NULL == VirtualAlloc(address,
|
| + OS::CommitPageSize(),
|
| + MEM_COMMIT,
|
| + PAGE_NOACCESS)) {
|
| + return false;
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +
|
| +void* VirtualMemory::ReserveRegion(size_t size) {
|
| + return RandomizedVirtualAlloc(size, MEM_RESERVE, PAGE_NOACCESS);
|
| +}
|
| +
|
| +
|
| +bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
|
| + int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
|
| + if (NULL == VirtualAlloc(base, size, MEM_COMMIT, prot)) {
|
| + return false;
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +
|
| +bool VirtualMemory::UncommitRegion(void* base, size_t size) {
|
| + return VirtualFree(base, size, MEM_DECOMMIT) != 0;
|
| +}
|
| +
|
| +
|
| +bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
|
| + return VirtualFree(base, 0, MEM_RELEASE) != 0;
|
| +}
|
| +
|
| +
|
| +bool VirtualMemory::HasLazyCommits() {
|
| + // TODO(alph): implement for the platform.
|
| + return false;
|
| +}
|
| +
|
| +
|
| // ----------------------------------------------------------------------------
|
| // Win32 thread support.
|
|
|
|
|