Revision: 5169
Author: [email protected]
Date: Mon Aug  2 08:27:25 2010
Log: VirtualAlloc on Windows 7 does not currently provide sufficient randomization to protect JIT code from being aligned in large regions at a predictable location.

This patch manually randomizes the allocation address for PAGE_EXECUTE_READWRITE regions between kAllocationRandomAddressMin and kAllocationRandomAddressMax.

BUG=none
TEST=allocate lots of javascript code and check for contiguous allocations

Patch by Paul Mehta <[email protected]>

Review URL: http://codereview.chromium.org/2832095

http://code.google.com/p/v8/source/detail?r=5169

Modified:
 /branches/bleeding_edge/src/platform-linux.cc
 /branches/bleeding_edge/src/platform-win32.cc

=======================================
--- /branches/bleeding_edge/src/platform-linux.cc       Mon May 31 03:09:07 2010
+++ /branches/bleeding_edge/src/platform-linux.cc       Mon Aug  2 08:27:25 2010
@@ -236,6 +236,7 @@
 void* OS::Allocate(const size_t requested,
                    size_t* allocated,
                    bool is_executable) {
+  // TODO(805): Port randomization of allocated executable memory to Linux.
   const size_t msize = RoundUp(requested, sysconf(_SC_PAGESIZE));
   int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
=======================================
--- /branches/bleeding_edge/src/platform-win32.cc       Fri May 21 22:27:19 2010
+++ /branches/bleeding_edge/src/platform-win32.cc       Mon Aug  2 08:27:25 2010
@@ -838,12 +838,38 @@
 void* OS::Allocate(const size_t requested,
                    size_t* allocated,
                    bool is_executable) {
+  // The address range used to randomize RWX allocations in OS::Allocate
+  // Try not to map pages into the default range that windows loads DLLs
+  // 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 = 0x000004FFFFFFFFFF;
+#else
+  static const intptr_t kAllocationRandomAddressMin = 0x04000000;
+  static const intptr_t kAllocationRandomAddressMax = 0x4FFFFFFF;
+#endif
+
   // VirtualAlloc rounds allocated size to page size automatically.
   size_t msize = RoundUp(requested, static_cast<int>(GetPageSize()));
+  intptr_t address = NULL;

   // Windows XP SP2 allows Data Excution Prevention (DEP).
   int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
-  LPVOID mbase = VirtualAlloc(NULL, msize, MEM_COMMIT | MEM_RESERVE, prot);
+
+  // For exectutable pages try and randomize the allocation address
+  if (prot == PAGE_EXECUTE_READWRITE && msize >= Page::kPageSize) {
+ address = (V8::Random() << kPageSizeBits) | kAllocationRandomAddressMin;
+      address &= kAllocationRandomAddressMax;
+  }
+
+  LPVOID mbase = VirtualAlloc(reinterpret_cast<void *>(address),
+                              msize,
+                              MEM_COMMIT | MEM_RESERVE,
+                              prot);
+  if (mbase == NULL && address != NULL)
+    mbase = VirtualAlloc(NULL, msize, MEM_COMMIT | MEM_RESERVE, prot);
+
   if (mbase == NULL) {
     LOG(StringEvent("OS::Allocate", "VirtualAlloc failed"));
     return NULL;

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to