Index: src/platform-win32.cc |
=================================================================== |
--- src/platform-win32.cc (revision 10785) |
+++ src/platform-win32.cc (working copy) |
@@ -830,10 +830,7 @@ |
return allocate_alignment; |
} |
Vyacheslav Egorov (Chromium)
2012/02/22 12:22:36
one more new line
|
- |
-void* OS::Allocate(const size_t requested, |
- size_t* allocated, |
- bool is_executable) { |
+void* OS::GetRandomAddr() { |
// 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. |
@@ -846,10 +843,18 @@ |
static const intptr_t kAllocationRandomAddressMin = 0x04000000; |
static const intptr_t kAllocationRandomAddressMax = 0x3FFF0000; |
#endif |
+ uintptr_t address = (V8::RandomPrivate(Isolate::Current()) << kPageSizeBits) |
+ | kAllocationRandomAddressMin; |
+ address &= kAllocationRandomAddressMax; |
+ return reinterpret_cast<void *>(address); |
+} |
Vyacheslav Egorov (Chromium)
2012/02/22 12:22:36
one more new line
|
+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())); |
- intptr_t address = 0; |
+ void* address = 0; |
// Windows XP SP2 allows Data Excution Prevention (DEP). |
int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; |
@@ -857,12 +862,10 @@ |
// For exectutable pages try and randomize the allocation address |
if (prot == PAGE_EXECUTE_READWRITE && |
msize >= static_cast<size_t>(Page::kPageSize)) { |
- address = (V8::RandomPrivate(Isolate::Current()) << kPageSizeBits) |
- | kAllocationRandomAddressMin; |
- address &= kAllocationRandomAddressMax; |
+ address = OS::GetRandomAddr(); |
Vyacheslav Egorov (Chromium)
2012/02/22 12:22:36
I don't think GetRandomAddr has to be member of OS
|
} |
- LPVOID mbase = VirtualAlloc(reinterpret_cast<void *>(address), |
+ LPVOID mbase = VirtualAlloc(address, |
msize, |
MEM_COMMIT | MEM_RESERVE, |
prot); |
@@ -1471,7 +1474,18 @@ |
void* VirtualMemory::ReserveRegion(size_t size) { |
- return VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); |
+ void* address = 0; |
+ LPVOID mbase = NULL; |
+ |
+ for (size_t attempts = 0; mbase == NULL && attempts < 3; ++attempts) { |
+ address = OS::GetRandomAddr(); |
+ mbase = VirtualAlloc(address, size, MEM_RESERVE, PAGE_NOACCESS); |
+ } |
+ |
+ // After three attempts give up and let the OS find an address to use. |
+ if (mbase == NULL) |
+ mbase = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); |
Vyacheslav Egorov (Chromium)
2012/02/22 12:22:36
we do not omit {} for if-s that span several lines
|
+ return mbase; |
Vyacheslav Egorov (Chromium)
2012/02/22 12:22:36
I would abstract loop and last attempt into a func
|
} |