From 9f24cd0eef9f448a02f33e9dcfc9fb9455b00150 Mon Sep 17 00:00:00 2001 From: "vegorov@chromium.org" Date: Tue, 2 Aug 2011 13:34:52 +0000 Subject: [PATCH] Mark optimized modulo and memcpy code pages -w after writing them. BUG=91245 Review URL: http://codereview.chromium.org/7538028 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8780 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/ia32/codegen-ia32.cc | 1 + src/platform-posix.cc | 6 ++++++ src/platform-win32.cc | 6 ++++++ src/platform.h | 3 +++ src/x64/codegen-x64.cc | 1 + 5 files changed, 17 insertions(+) diff --git a/src/ia32/codegen-ia32.cc b/src/ia32/codegen-ia32.cc index 572c36c..3a657bd 100644 --- a/src/ia32/codegen-ia32.cc +++ b/src/ia32/codegen-ia32.cc @@ -255,6 +255,7 @@ OS::MemCopyFunction CreateMemCopyFunction() { ASSERT(desc.reloc_size == 0); CPU::FlushICache(buffer, actual_size); + OS::ProtectCode(buffer, actual_size); return FUNCTION_CAST(buffer); } diff --git a/src/platform-posix.cc b/src/platform-posix.cc index deb4b75..1ea53c8 100644 --- a/src/platform-posix.cc +++ b/src/platform-posix.cc @@ -70,6 +70,12 @@ intptr_t OS::MaxVirtualMemory() { } +// Get rid of writable permission on code allocations. +void OS::ProtectCode(void* address, const size_t size) { + mprotect(address, size, PROT_READ | PROT_EXEC); +} + + // Create guard pages. void OS::Guard(void* address, const size_t size) { mprotect(address, size, PROT_NONE); diff --git a/src/platform-win32.cc b/src/platform-win32.cc index b23e25e..e5df5ff 100644 --- a/src/platform-win32.cc +++ b/src/platform-win32.cc @@ -957,6 +957,12 @@ void OS::Free(void* address, const size_t size) { } +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_READONLY | PAGE_GUARD, &oldprotect); diff --git a/src/platform.h b/src/platform.h index 4d7f9cf..6b2348c 100644 --- a/src/platform.h +++ b/src/platform.h @@ -207,6 +207,9 @@ class OS { bool is_executable); static void Free(void* address, const size_t size); + // Mark code segments non-writable. + static void ProtectCode(void* address, const size_t size); + // Assign memory as a guard page so that access will cause an exception. static void Guard(void* address, const size_t size); diff --git a/src/x64/codegen-x64.cc b/src/x64/codegen-x64.cc index f8f2d6e..507bbd4 100644 --- a/src/x64/codegen-x64.cc +++ b/src/x64/codegen-x64.cc @@ -132,6 +132,7 @@ ModuloFunction CreateModuloFunction() { CodeDesc desc; masm.GetCode(&desc); + OS::ProtectCode(buffer, actual_size); // Call the function from C++ through this pointer. return FUNCTION_CAST(buffer); } -- 2.7.4