From f2dc1ef7390c4dd05a54958da1942399e26e3fde Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Fri, 15 Jan 2016 15:33:00 +0100 Subject: V4 JIT: Switch all platforms to W^X for the ExecutableAllocator. This also fixes makeWritable, which needs to work in order to add more c0d3 to used pages. Also finish the WinRT parts. Change-Id: Idaa4187b1ec256e65c881285a608f3a51fdbeebd Reviewed-by: Simon Hausmann --- src/3rdparty/masm/masm-defs.pri | 3 +- src/3rdparty/masm/stubs/ExecutableAllocator.h | 74 +++++++++++++++++++-------- 2 files changed, 53 insertions(+), 24 deletions(-) (limited to 'src/3rdparty') diff --git a/src/3rdparty/masm/masm-defs.pri b/src/3rdparty/masm/masm-defs.pri index f09a8329c9..c76eeec724 100644 --- a/src/3rdparty/masm/masm-defs.pri +++ b/src/3rdparty/masm/masm-defs.pri @@ -1,6 +1,5 @@ - - DEFINES += WTF_EXPORT_PRIVATE="" JS_EXPORT_PRIVATE="" +DEFINES += ENABLE_ASSEMBLER_WX_EXCLUSIVE=1 # Avoid symbol clashes with QtScript during static linking DEFINES += WTFReportAssertionFailure=qmlWTFReportAssertionFailure diff --git a/src/3rdparty/masm/stubs/ExecutableAllocator.h b/src/3rdparty/masm/stubs/ExecutableAllocator.h index 5f7d5678ab..578a944bf9 100644 --- a/src/3rdparty/masm/stubs/ExecutableAllocator.h +++ b/src/3rdparty/masm/stubs/ExecutableAllocator.h @@ -89,15 +89,34 @@ struct ExecutableAllocator { return adoptRef(new ExecutableMemoryHandle(realAllocator, size)); } - static void makeWritable(void* addr, int size) + static void makeWritable(void* addr, size_t size) { -#if ENABLE(ASSEMBLER_WX_EXCLUSIVE) - size_t pageSize = WTF::pageSize(); - size_t iaddr = reinterpret_cast(addr); - size_t roundAddr = iaddr & ~(pageSize - static_cast(1)); + quintptr pageSize = WTF::pageSize(); + quintptr iaddr = reinterpret_cast(addr); + quintptr roundAddr = iaddr & ~(pageSize - 1); + size = size + (iaddr - roundAddr); + addr = reinterpret_cast(roundAddr); +#if ENABLE(ASSEMBLER_WX_EXCLUSIVE) +# if OS(WINDOWS) + DWORD oldProtect; +# if !OS(WINRT) + VirtualProtect(addr, size, PAGE_READWRITE, &oldProtect); +# elif _MSC_VER >= 1900 + bool hr = VirtualProtectFromApp(addr, size, PAGE_READWRITE, &oldProtect); + if (!hr) { + Q_UNREACHABLE(); + } +# else + (void)oldProtect; +# endif +# else int mode = PROT_READ | PROT_WRITE; - mprotect(reinterpret_cast(roundAddr), size + (iaddr - roundAddr), mode); + if (mprotect(addr, size, mode) != 0) { + perror("mprotect failed in ExecutableAllocator::makeWritable"); + Q_UNREACHABLE(); + } +# endif #else // We assume we already have RWX (void)addr; // suppress unused parameter warning @@ -105,25 +124,36 @@ struct ExecutableAllocator { #endif } - static void makeExecutable(void* addr, int size) + static void makeExecutable(void* addr, size_t size) { - size_t pageSize = WTF::pageSize(); - size_t iaddr = reinterpret_cast(addr); - size_t roundAddr = iaddr & ~(pageSize - static_cast(1)); -#if OS(WINDOWS) -#if !OS(WINRT) + quintptr pageSize = WTF::pageSize(); + quintptr iaddr = reinterpret_cast(addr); + quintptr roundAddr = iaddr & ~(pageSize - 1); + size = size + (iaddr - roundAddr); + addr = reinterpret_cast(roundAddr); + +#if ENABLE(ASSEMBLER_WX_EXCLUSIVE) +# if OS(WINDOWS) DWORD oldProtect; - VirtualProtect(reinterpret_cast(roundAddr), size + (iaddr - roundAddr), PAGE_EXECUTE_READWRITE, &oldProtect); -#else - (void)size; // suppress unused parameter warning - (void)roundAddr; // suppress unused parameter warning -#endif -#else +# if !OS(WINRT) + VirtualProtect(addr, size, PAGE_EXECUTE_READ, &oldProtect); +# elif _MSC_VER >= 1900 + bool hr = VirtualProtectFromApp(addr, size, PAGE_EXECUTE_READ, &oldProtect); + if (!hr) { + Q_UNREACHABLE(); + } +# else + (void)oldProtect; +# endif +# else int mode = PROT_READ | PROT_EXEC; -#if !ENABLE(ASSEMBLER_WX_EXCLUSIVE) - mode |= PROT_WRITE; -#endif - mprotect(reinterpret_cast(roundAddr), size + (iaddr - roundAddr), mode); + if (mprotect(addr, size, mode) != 0) { + perror("mprotect failed in ExecutableAllocator::makeExecutable"); + Q_UNREACHABLE(); + } +# endif +#else +# error "Only W^X is supported" #endif } -- cgit v1.2.3