Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Unified Diff: src/platform-win32.cc

Issue 9372083: Randomize allocation addresses on windows. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/platform.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
}
« no previous file with comments | « src/platform.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698