Reviewers: Vyacheslav Egorov, Chris Evans,

Description:
Add guard pages in front of platform allocations

BUG=1555

Please review this at http://codereview.chromium.org/7352007/

SVN Base: http://v8.googlecode.com/svn/trunk/

Affected files:
  M     src/platform-linux.cc
  M     src/platform-macos.cc
  M     src/platform-win32.cc


Index: src/platform-linux.cc
===================================================================
--- src/platform-linux.cc       (revision 8619)
+++ src/platform-linux.cc       (working copy)
@@ -370,13 +370,16 @@
   // 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);
+  void* mbase = mmap(NULL, msize + Page::kPageSize, prot,
+                     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
   if (mbase == MAP_FAILED) {
     LOG(i::Isolate::Current(),
         StringEvent("OS::Allocate", "mmap failed"));
     return NULL;
   }
   *allocated = msize;
+  mprotect(mbase, Page::kPageSize, PROT_NONE);
+  mbase += Page::kPageSize;
   UpdateAllocatedSpaceLimits(mbase, msize);
   return mbase;
 }
@@ -384,7 +387,7 @@

 void OS::Free(void* address, const size_t size) {
   // TODO(1240712): munmap has a return value which is ignored here.
-  int result = munmap(address, size);
+  int result = munmap(address - Page::kPageSize, size + Page::kPageSize);
   USE(result);
   ASSERT(result == 0);
 }
Index: src/platform-macos.cc
===================================================================
--- src/platform-macos.cc       (revision 8619)
+++ src/platform-macos.cc       (working copy)
@@ -148,7 +148,7 @@
                    bool is_executable) {
   const size_t msize = RoundUp(requested, getpagesize());
   int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
-  void* mbase = mmap(NULL, msize, prot,
+  void* mbase = mmap(NULL, msize + Page::kPageSize, prot,
                      MAP_PRIVATE | MAP_ANON,
                      kMmapFd, kMmapFdOffset);
   if (mbase == MAP_FAILED) {
@@ -156,6 +156,8 @@
     return NULL;
   }
   *allocated = msize;
+  mprotect(mbase, Page::kPageSize, PROT_NONE);
+  mbase += Page::kPageSize;
   UpdateAllocatedSpaceLimits(mbase, msize);
   return mbase;
 }
@@ -163,7 +165,7 @@

 void OS::Free(void* address, const size_t size) {
   // TODO(1240712): munmap has a return value which is ignored here.
-  int result = munmap(address, size);
+  int result = munmap(address - Page::kPageSize, size + Page::kPageSize);
   USE(result);
   ASSERT(result == 0);
 }
Index: src/platform-win32.cc
===================================================================
--- src/platform-win32.cc       (revision 8619)
+++ src/platform-win32.cc       (working copy)
@@ -913,11 +913,14 @@
   }

   LPVOID mbase = VirtualAlloc(reinterpret_cast<void *>(address),
-                              msize,
+                              msize + Page::kPageSize,
                               MEM_COMMIT | MEM_RESERVE,
                               prot);
   if (mbase == NULL && address != 0)
-    mbase = VirtualAlloc(NULL, msize, MEM_COMMIT | MEM_RESERVE, prot);
+    mbase = VirtualAlloc(NULL,
+                         msize + Page::kPageSize,
+                         MEM_COMMIT | MEM_RESERVE,
+                         prot);

   if (mbase == NULL) {
     LOG(ISOLATE, StringEvent("OS::Allocate", "VirtualAlloc failed"));
@@ -925,8 +928,12 @@
   }

ASSERT(IsAligned(reinterpret_cast<size_t>(mbase), OS::AllocateAlignment()));
+  *allocated = msize;

-  *allocated = msize;
+  DWORD old_protect;
+ VirtualProtect(mbase, Page::kPageSize, PAGE_READONLY | PAGE_GUARD, &old_protect);
+  mbase = static_cast<BYTE*>(mbase) + Page::kPageSize;
+
   UpdateAllocatedSpaceLimits(mbase, static_cast<int>(msize));
   return mbase;
 }
@@ -934,7 +941,7 @@

 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);
+ VirtualFree(static_cast<BYTE*>(address) - Page::kPageSize, 0, MEM_RELEASE);
   USE(size);
 }



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

Reply via email to