diff --git a/src/ia32/codegen-ia32.cc b/src/ia32/codegen-ia32.cc index 572c36c881..3a657bd541 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 deb4b75618..1ea53c83ea 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 b23e25ec97..e5df5ff3bf 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 4d7f9cf775..6b2348c890 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 f8f2d6e687..507bbd44c3 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); }