From f89f6a8abf1daf010ee5e5af0cd31056a432668b Mon Sep 17 00:00:00 2001 From: SChernykh Date: Mon, 14 Dec 2020 18:22:58 +0100 Subject: [PATCH 1/2] Fix: secure JIT and huge pages are incompatible on Windows --- src/crypto/randomx/jit_compiler_x86.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/crypto/randomx/jit_compiler_x86.cpp b/src/crypto/randomx/jit_compiler_x86.cpp index d0a97bbde..8be8ba448 100644 --- a/src/crypto/randomx/jit_compiler_x86.cpp +++ b/src/crypto/randomx/jit_compiler_x86.cpp @@ -175,11 +175,11 @@ namespace randomx { } void JitCompilerX86::enableWriting() const { - xmrig::VirtualMemory::protectRW(allocatedCode, allocatedSize); + xmrig::VirtualMemory::protectRW(code, CodeSize); } void JitCompilerX86::enableExecution() const { - xmrig::VirtualMemory::protectRX(allocatedCode, allocatedSize); + xmrig::VirtualMemory::protectRX(code, CodeSize); } static inline void cpuid(uint32_t level, int32_t output[4]) @@ -213,7 +213,13 @@ namespace randomx { hasXOP = ((info[2] & (1 << 11)) != 0); allocatedSize = CodeSize * 2; - allocatedCode = static_cast(allocExecutableMemory(allocatedSize, hugePagesJIT && hugePagesEnable)); + allocatedCode = static_cast(allocExecutableMemory(allocatedSize, +# ifdef XMRIG_SECURE_JIT + false +# else + hugePagesJIT && hugePagesEnable +# endif + )); // Shift code base address to improve caching - all threads will use different L2/L3 cache sets code = allocatedCode + (codeOffset.fetch_add(codeOffsetIncrement) % CodeSize); From 414588d701306a6e67c0f1c1235f9cd081c13857 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Mon, 14 Dec 2020 18:32:25 +0100 Subject: [PATCH 2/2] Fix alignment for Linux --- src/crypto/randomx/jit_compiler_x86.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/crypto/randomx/jit_compiler_x86.cpp b/src/crypto/randomx/jit_compiler_x86.cpp index 8be8ba448..ee3e1b458 100644 --- a/src/crypto/randomx/jit_compiler_x86.cpp +++ b/src/crypto/randomx/jit_compiler_x86.cpp @@ -170,16 +170,26 @@ namespace randomx { {0x0F, 0x1F, 0x44, 0x00, 0x00, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E}, }; + static inline uint8_t* alignToPage(uint8_t* p, size_t pageSize) { + size_t k = (size_t) p; + k -= k % pageSize; + return (uint8_t*) k; + } + size_t JitCompilerX86::getCodeSize() { return codePos < prologueSize ? 0 : codePos - prologueSize; } void JitCompilerX86::enableWriting() const { - xmrig::VirtualMemory::protectRW(code, CodeSize); + uint8_t* p1 = alignToPage(code, 4096); + uint8_t* p2 = code + CodeSize; + xmrig::VirtualMemory::protectRW(p1, p2 - p1); } void JitCompilerX86::enableExecution() const { - xmrig::VirtualMemory::protectRX(code, CodeSize); + uint8_t* p1 = alignToPage(code, 4096); + uint8_t* p2 = code + CodeSize; + xmrig::VirtualMemory::protectRX(p1, p2 - p1); } static inline void cpuid(uint32_t level, int32_t output[4])