diff --git a/src/jit_compiler_a64.cpp b/src/jit_compiler_a64.cpp index 2de66d0c..eea89789 100644 --- a/src/jit_compiler_a64.cpp +++ b/src/jit_compiler_a64.cpp @@ -110,17 +110,20 @@ JitCompilerA64::~JitCompilerA64() void JitCompilerA64::enableWriting() { - setPagesRW(code, CodeSize + CalcDatasetItemSize); + if (setPagesRW(code, CodeSize + CalcDatasetItemSize)) + throw std::runtime_error("setPagesRW"); } void JitCompilerA64::enableExecution() { - setPagesRX(code, CodeSize + CalcDatasetItemSize); + if (setPagesRX(code, CodeSize + CalcDatasetItemSize)) + throw std::runtime_error("setPagesRX"); } void JitCompilerA64::enableAll() { - setPagesRWX(code, CodeSize + CalcDatasetItemSize); + if (setPagesRWX(code, CodeSize + CalcDatasetItemSize)) + throw std::runtime_error("setPagesRWX"); } void JitCompilerA64::generateProgram(Program& program, ProgramConfiguration& config) diff --git a/src/jit_compiler_rv64.cpp b/src/jit_compiler_rv64.cpp index 91f42f09..87a36f15 100644 --- a/src/jit_compiler_rv64.cpp +++ b/src/jit_compiler_rv64.cpp @@ -612,15 +612,18 @@ namespace randomx { } void JitCompilerRV64::enableAll() { - setPagesRWX(entryDataInit, ExecutableSize); + if (setPagesRWX(entryDataInit, ExecutableSize)) + throw std::runtime_error("setPagesRWX"); } void JitCompilerRV64::enableWriting() { - setPagesRW(entryDataInit, ExecutableSize); + if (setPagesRW(entryDataInit, ExecutableSize)) + throw std::runtime_error("setPagesRW"); } void JitCompilerRV64::enableExecution() { - setPagesRX(entryDataInit, ExecutableSize); + if (setPagesRX(entryDataInit, ExecutableSize)) + throw std::runtime_error("setPagesRX"); } void JitCompilerRV64::generateProgram(Program& prog, ProgramConfiguration& pcfg) { diff --git a/src/jit_compiler_x86.cpp b/src/jit_compiler_x86.cpp index 785ce5f5..8894de24 100644 --- a/src/jit_compiler_x86.cpp +++ b/src/jit_compiler_x86.cpp @@ -236,15 +236,18 @@ namespace randomx { } void JitCompilerX86::enableAll() { - setPagesRWX(code, CodeSize); + if (setPagesRWX(code, CodeSize)) + throw std::runtime_error("setPagesRWX"); } void JitCompilerX86::enableWriting() { - setPagesRW(code, CodeSize); + if (setPagesRW(code, CodeSize)) + throw std::runtime_error("setPagesRW"); } void JitCompilerX86::enableExecution() { - setPagesRX(code, CodeSize); + if (setPagesRX(code, CodeSize)) + throw std::runtime_error("setPagesRX"); } void JitCompilerX86::generateProgram(Program& prog, ProgramConfiguration& pcfg) { diff --git a/src/randomx.cpp b/src/randomx.cpp index 4eac9e00..6a0e134d 100644 --- a/src/randomx.cpp +++ b/src/randomx.cpp @@ -88,6 +88,7 @@ extern "C" { case RANDOMX_FLAG_JIT: cache->dealloc = &randomx::deallocCache; cache->jit = new randomx::JitCompiler(); + cache->jit->enableExecution(); cache->initialize = &randomx::initCacheCompile; cache->datasetInit = cache->jit->getDatasetInitFunc(); cache->memory = (uint8_t*)randomx::DefaultAllocator::allocMemory(randomx::CacheSize); @@ -104,6 +105,7 @@ extern "C" { case RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES: cache->dealloc = &randomx::deallocCache; cache->jit = new randomx::JitCompiler(); + cache->jit->enableExecution(); cache->initialize = &randomx::initCacheCompile; cache->datasetInit = cache->jit->getDatasetInitFunc(); cache->memory = (uint8_t*)randomx::LargePageAllocator::allocMemory(randomx::CacheSize); diff --git a/src/virtual_memory.c b/src/virtual_memory.c index d2cdcda0..d872c0fb 100644 --- a/src/virtual_memory.c +++ b/src/virtual_memory.c @@ -169,38 +169,40 @@ static inline int pageProtect(void* ptr, size_t bytes, int rules, char **errfunc return 0; } -void setPagesRW(void* ptr, size_t bytes) { +int setPagesRW(void* ptr, size_t bytes) { char *errfunc; #if defined(USE_PTHREAD_JIT_WP) && defined(MAC_OS_VERSION_11_0) \ && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_11_0 if (__builtin_available(macOS 11.0, *)) { pthread_jit_write_protect_np(0); + return 0; } else { - pageProtect(ptr, bytes, PAGE_READWRITE, &errfunc); + return pageProtect(ptr, bytes, PAGE_READWRITE, &errfunc); } #else - pageProtect(ptr, bytes, PAGE_READWRITE, &errfunc); + return pageProtect(ptr, bytes, PAGE_READWRITE, &errfunc); #endif } -void setPagesRX(void* ptr, size_t bytes) { +int setPagesRX(void* ptr, size_t bytes) { char *errfunc; #if defined(USE_PTHREAD_JIT_WP) && defined(MAC_OS_VERSION_11_0) \ && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_11_0 if (__builtin_available(macOS 11.0, *)) { pthread_jit_write_protect_np(1); __builtin___clear_cache((char*)ptr, ((char*)ptr) + bytes); + return 0; } else { - pageProtect(ptr, bytes, PAGE_EXECUTE_READ, &errfunc); + return pageProtect(ptr, bytes, PAGE_EXECUTE_READ, &errfunc); } #else - pageProtect(ptr, bytes, PAGE_EXECUTE_READ, &errfunc); + return pageProtect(ptr, bytes, PAGE_EXECUTE_READ, &errfunc); #endif } -void setPagesRWX(void* ptr, size_t bytes) { +int setPagesRWX(void* ptr, size_t bytes) { char *errfunc; - pageProtect(ptr, bytes, PAGE_EXECUTE_READWRITE, &errfunc); + return pageProtect(ptr, bytes, PAGE_EXECUTE_READWRITE, &errfunc); } void* allocLargePagesMemory(size_t bytes) { diff --git a/src/virtual_memory.h b/src/virtual_memory.h index 5e8e31d5..6eba4c5b 100644 --- a/src/virtual_memory.h +++ b/src/virtual_memory.h @@ -37,9 +37,9 @@ extern "C" { #define alignSize(pos, align) (((pos - 1) / align + 1) * align) void* allocMemoryPages(size_t); -void setPagesRW(void*, size_t); -void setPagesRX(void*, size_t); -void setPagesRWX(void*, size_t); +int setPagesRW(void*, size_t); +int setPagesRX(void*, size_t); +int setPagesRWX(void*, size_t); void* allocLargePagesMemory(size_t); void freePagedMemory(void*, size_t); diff --git a/src/vm_compiled.cpp b/src/vm_compiled.cpp index 060abec3..7fcd28fa 100644 --- a/src/vm_compiled.cpp +++ b/src/vm_compiled.cpp @@ -36,7 +36,10 @@ namespace randomx { template CompiledVm::CompiledVm() { - if (!secureJit) { + if (secureJit) { + compiler.enableExecution(); + } + else { compiler.enableAll(); //make JIT buffer both writable and executable } }