Added generic secure JIT support for RandomX.

This commit is contained in:
XMRig 2020-12-11 23:17:54 +07:00
parent f9c0933f05
commit ec62ded279
No known key found for this signature in database
GPG key ID: 446A53638BE94409
19 changed files with 227 additions and 110 deletions

View file

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 2.8.12)
project(xmrig) project(xmrig)
option(WITH_HWLOC "Enable hwloc support" ON) option(WITH_HWLOC "Enable hwloc support" ON)
@ -25,6 +25,7 @@ option(WITH_INTERLEAVE_DEBUG_LOG "Enable debug log for threads interleave" OFF)
option(WITH_PROFILING "Enable profiling for developers" OFF) option(WITH_PROFILING "Enable profiling for developers" OFF)
option(WITH_SSE4_1 "Enable SSE 4.1 for Blake2" ON) option(WITH_SSE4_1 "Enable SSE 4.1 for Blake2" ON)
option(WITH_BENCHMARK "Enable builtin RandomX benchmark and stress test" ON) option(WITH_BENCHMARK "Enable builtin RandomX benchmark and stress test" ON)
option(WITH_SECURE_JIT "Enable secure access to JIT memory" OFF)
option(BUILD_STATIC "Build static binary" OFF) option(BUILD_STATIC "Build static binary" OFF)
option(ARM_TARGET "Force use specific ARM target 8 or 7" 0) option(ARM_TARGET "Force use specific ARM target 8 or 7" 0)

View file

@ -32,6 +32,10 @@ elseif(XMRIG_OS_APPLE)
else() else()
add_definitions(/DXMRIG_OS_MACOS) add_definitions(/DXMRIG_OS_MACOS)
endif() endif()
if (XMRIG_ARM)
set(WITH_SECURE_JIT ON)
endif()
elseif(XMRIG_OS_UNIX) elseif(XMRIG_OS_UNIX)
add_definitions(/DXMRIG_OS_UNIX) add_definitions(/DXMRIG_OS_UNIX)
@ -43,3 +47,7 @@ elseif(XMRIG_OS_UNIX)
add_definitions(/DXMRIG_OS_FREEBSD) add_definitions(/DXMRIG_OS_FREEBSD)
endif() endif()
endif() endif()
if (WITH_SECURE_JIT)
add_definitions(/DXMRIG_SECURE_JIT)
endif()

View file

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 2.8.12)
project(argon2 C) project(argon2 C)
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)

View file

@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 2.8) cmake_minimum_required (VERSION 2.8.12)
project (ethash C) project (ethash C)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Os") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Os")

View file

@ -1,7 +1,7 @@
include (src/backend/cpu/cpu.cmake) include(src/backend/cpu/cpu.cmake)
include (src/backend/opencl/opencl.cmake) include(src/backend/opencl/opencl.cmake)
include (src/backend/cuda/cuda.cmake) include(src/backend/cuda/cuda.cmake)
include (src/backend/common/common.cmake) include(src/backend/common/common.cmake)
set(HEADERS_BACKEND set(HEADERS_BACKEND

View file

@ -36,12 +36,14 @@
#include "3rdparty/rapidjson/document.h" #include "3rdparty/rapidjson/document.h"
#ifdef XMRIG_OS_UNIX #if defined(XMRIG_OS_UNIX)
namespace xmrig { namespace xmrig {
extern String cpu_name_arm(); extern String cpu_name_arm();
} // namespace xmrig } // namespace xmrig
#elif defined(XMRIG_OS_MACOS)
# include <sys/sysctl.h>
#endif #endif
@ -62,13 +64,16 @@ xmrig::BasicCpuInfo::BasicCpuInfo() :
# endif # endif
# endif # endif
# ifdef XMRIG_OS_UNIX # if defined(XMRIG_OS_UNIX)
auto name = cpu_name_arm(); auto name = cpu_name_arm();
if (!name.isNull()) { if (!name.isNull()) {
strncpy(m_brand, name, sizeof(m_brand) - 1); strncpy(m_brand, name, sizeof(m_brand) - 1);
} }
m_flags.set(FLAG_PDPE1GB, std::ifstream("/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages").good()); m_flags.set(FLAG_PDPE1GB, std::ifstream("/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages").good());
# elif defined(XMRIG_OS_MACOS)
size_t buflen = sizeof(m_brand);
sysctlbyname("machdep.cpu.brand_string", &m_brand, &buflen, nullptr, 0);
# endif # endif
} }

View file

@ -219,7 +219,7 @@ static void patchAsmVariants()
patchCode(cn_double_double_mainloop_sandybridge_asm, cnv2_double_mainloop_sandybridge_asm, ITER); patchCode(cn_double_double_mainloop_sandybridge_asm, cnv2_double_mainloop_sandybridge_asm, ITER);
} }
VirtualMemory::protectExecutableMemory(base, allocation_size); VirtualMemory::protectRX(base, allocation_size);
VirtualMemory::flushInstructionCache(base, allocation_size); VirtualMemory::flushInstructionCache(base, allocation_size);
} }
} // namespace xmrig } // namespace xmrig

View file

@ -1,14 +1,7 @@
/* XMRig /* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com> * Copyright (c) 2018-2020 tevador <tevador@gmail.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 tevador <tevador@gmail.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by

View file

@ -1,14 +1,7 @@
/* XMRig /* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com> * Copyright (c) 2018-2020 tevador <tevador@gmail.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 tevador <tevador@gmail.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -60,6 +53,9 @@ public:
static bool isHugepagesAvailable(); static bool isHugepagesAvailable();
static bool isOneGbPagesAvailable(); static bool isOneGbPagesAvailable();
static bool protectRW(void *p, size_t size);
static bool protectRWX(void *p, size_t size);
static bool protectRX(void *p, size_t size);
static uint32_t bindToNUMANode(int64_t affinity); static uint32_t bindToNUMANode(int64_t affinity);
static void *allocateExecutableMemory(size_t size, bool hugePages); static void *allocateExecutableMemory(size_t size, bool hugePages);
static void *allocateLargePagesMemory(size_t size); static void *allocateLargePagesMemory(size_t size);
@ -68,8 +64,6 @@ public:
static void flushInstructionCache(void *p, size_t size); static void flushInstructionCache(void *p, size_t size);
static void freeLargePagesMemory(void *p, size_t size); static void freeLargePagesMemory(void *p, size_t size);
static void init(size_t poolSize, bool hugePages); static void init(size_t poolSize, bool hugePages);
static void protectExecutableMemory(void *p, size_t size);
static void unprotectExecutableMemory(void *p, size_t size);
static inline constexpr size_t align(size_t pos, size_t align = 2097152) { return ((pos - 1) / align + 1) * align; } static inline constexpr size_t align(size_t pos, size_t align = 2097152) { return ((pos - 1) / align + 1) * align; }

View file

@ -1,14 +1,7 @@
/* XMRig /* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com> * Copyright (c) 2018-2020 tevador <tevador@gmail.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2018-2019 tevador <tevador@gmail.com>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -34,8 +27,16 @@
#include "crypto/common/VirtualMemory.h" #include "crypto/common/VirtualMemory.h"
#if defined(__APPLE__) #ifdef __APPLE__
# include <TargetConditionals.h>
# include <mach/vm_statistics.h> # include <mach/vm_statistics.h>
# ifdef XMRIG_OS_MACOS
# define MEXTRA MAP_JIT
# else
# define MEXTRA 0
# endif
#else
# define MEXTRA 0
#endif #endif
@ -47,6 +48,13 @@
#endif #endif
#ifdef XMRIG_SECURE_JIT
# define SECURE_PROT_EXEC 0
#else
# define SECURE_PROT_EXEC PROT_EXEC
#endif
bool xmrig::VirtualMemory::isHugepagesAvailable() bool xmrig::VirtualMemory::isHugepagesAvailable()
{ {
return true; return true;
@ -63,19 +71,37 @@ bool xmrig::VirtualMemory::isOneGbPagesAvailable()
} }
bool xmrig::VirtualMemory::protectRW(void *p, size_t size)
{
return mprotect(p, size, PROT_READ | PROT_WRITE) == 0;
}
bool xmrig::VirtualMemory::protectRWX(void *p, size_t size)
{
return mprotect(p, size, PROT_READ | PROT_WRITE | PROT_EXEC) == 0;
}
bool xmrig::VirtualMemory::protectRX(void *p, size_t size)
{
return mprotect(p, size, PROT_READ | PROT_EXEC) == 0;
}
void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size, bool hugePages) void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size, bool hugePages)
{ {
# if defined(__APPLE__) # if defined(__APPLE__)
void *mem = mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANON, -1, 0); void *mem = mmap(0, size, PROT_READ | PROT_WRITE | SECURE_PROT_EXEC, MAP_PRIVATE | MAP_ANON | MEXTRA, -1, 0);
# elif defined(__FreeBSD__) # elif defined(__FreeBSD__)
void *mem = nullptr; void *mem = nullptr;
if (hugePages) { if (hugePages) {
mem = mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0); mem = mmap(0, size, PROT_READ | PROT_WRITE | SECURE_PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0);
} }
if (!mem) { if (!mem) {
mem = mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); mem = mmap(0, size, PROT_READ | PROT_WRITE | SECURE_PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
} }
# else # else
@ -91,11 +117,11 @@ void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size, bool hugePages
void *mem = nullptr; void *mem = nullptr;
if (hugePages) { if (hugePages) {
mem = mmap(0, align(size), PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE | flag_2mb, -1, 0); mem = mmap(0, align(size), PROT_READ | PROT_WRITE | SECURE_PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE | flag_2mb, -1, 0);
} }
if (!mem) { if (!mem) {
mem = mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); mem = mmap(0, size, PROT_READ | PROT_WRITE | SECURE_PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
} }
# endif # endif
@ -164,18 +190,6 @@ void xmrig::VirtualMemory::freeLargePagesMemory(void *p, size_t size)
} }
void xmrig::VirtualMemory::protectExecutableMemory(void *p, size_t size)
{
mprotect(p, size, PROT_READ | PROT_EXEC);
}
void xmrig::VirtualMemory::unprotectExecutableMemory(void *p, size_t size)
{
mprotect(p, size, PROT_WRITE | PROT_EXEC);
}
void xmrig::VirtualMemory::osInit(bool) void xmrig::VirtualMemory::osInit(bool)
{ {
} }

View file

@ -1,14 +1,7 @@
/* XMRig /* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com> * Copyright (c) 2018-2020 tevador <tevador@gmail.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2018-2019 tevador <tevador@gmail.com>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -36,6 +29,13 @@
#include "crypto/common/VirtualMemory.h" #include "crypto/common/VirtualMemory.h"
#ifdef XMRIG_SECURE_JIT
# define SECURE_PAGE_EXECUTE_READWRITE PAGE_READWRITE
#else
# define SECURE_PAGE_EXECUTE_READWRITE PAGE_EXECUTE_READWRITE
#endif
namespace xmrig { namespace xmrig {
@ -162,16 +162,40 @@ bool xmrig::VirtualMemory::isOneGbPagesAvailable()
} }
bool xmrig::VirtualMemory::protectRW(void *p, size_t size)
{
DWORD oldProtect;
return VirtualProtect(p, size, PAGE_READWRITE, &oldProtect) != 0;
}
bool xmrig::VirtualMemory::protectRWX(void *p, size_t size)
{
DWORD oldProtect;
return VirtualProtect(p, size, PAGE_EXECUTE_READWRITE, &oldProtect) != 0;
}
bool xmrig::VirtualMemory::protectRX(void *p, size_t size)
{
DWORD oldProtect;
return VirtualProtect(p, size, PAGE_EXECUTE_READ, &oldProtect) != 0;
}
void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size, bool hugePages) void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size, bool hugePages)
{ {
void* result = nullptr; void* result = nullptr;
if (hugePages) { if (hugePages) {
result = VirtualAlloc(nullptr, align(size), MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_EXECUTE_READWRITE); result = VirtualAlloc(nullptr, align(size), MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, SECURE_PAGE_EXECUTE_READWRITE);
} }
if (!result) { if (!result) {
result = VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); result = VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, SECURE_PAGE_EXECUTE_READWRITE);
} }
return result; return result;
@ -209,20 +233,6 @@ void xmrig::VirtualMemory::freeLargePagesMemory(void *p, size_t)
} }
void xmrig::VirtualMemory::protectExecutableMemory(void *p, size_t size)
{
DWORD oldProtect;
VirtualProtect(p, size, PAGE_EXECUTE_READ, &oldProtect);
}
void xmrig::VirtualMemory::unprotectExecutableMemory(void *p, size_t size)
{
DWORD oldProtect;
VirtualProtect(p, size, PAGE_EXECUTE_READWRITE, &oldProtect);
}
void xmrig::VirtualMemory::osInit(bool hugePages) void xmrig::VirtualMemory::osInit(bool hugePages)
{ {
if (hugePages) { if (hugePages) {

View file

@ -1,5 +1,7 @@
/* /*
Copyright (c) 2018-2019, tevador <tevador@gmail.com> Copyright (c) 2018-2020, tevador <tevador@gmail.com>
Copyright (c) 2019-2020, SChernykh <https://github.com/SChernykh>
Copyright (c) 2019-2020, XMRig <https://github.com/xmrig>, <support@xmrig.com>
All rights reserved. All rights reserved.
@ -77,16 +79,16 @@ namespace randomx {
context.pwdlen = (uint32_t)keySize; context.pwdlen = (uint32_t)keySize;
context.salt = CONST_CAST(uint8_t *)RandomX_CurrentConfig.ArgonSalt; context.salt = CONST_CAST(uint8_t *)RandomX_CurrentConfig.ArgonSalt;
context.saltlen = (uint32_t)strlen(RandomX_CurrentConfig.ArgonSalt); context.saltlen = (uint32_t)strlen(RandomX_CurrentConfig.ArgonSalt);
context.secret = NULL; context.secret = nullptr;
context.secretlen = 0; context.secretlen = 0;
context.ad = NULL; context.ad = nullptr;
context.adlen = 0; context.adlen = 0;
context.t_cost = RandomX_CurrentConfig.ArgonIterations; context.t_cost = RandomX_CurrentConfig.ArgonIterations;
context.m_cost = RandomX_CurrentConfig.ArgonMemory; context.m_cost = RandomX_CurrentConfig.ArgonMemory;
context.lanes = RandomX_CurrentConfig.ArgonLanes; context.lanes = RandomX_CurrentConfig.ArgonLanes;
context.threads = 1; context.threads = 1;
context.allocate_cbk = NULL; context.allocate_cbk = nullptr;
context.free_cbk = NULL; context.free_cbk = nullptr;
context.flags = ARGON2_DEFAULT_FLAGS; context.flags = ARGON2_DEFAULT_FLAGS;
context.version = ARGON2_VERSION_NUMBER; context.version = ARGON2_VERSION_NUMBER;
@ -100,8 +102,17 @@ namespace randomx {
void initCacheCompile(randomx_cache* cache, const void* key, size_t keySize) { void initCacheCompile(randomx_cache* cache, const void* key, size_t keySize) {
initCache(cache, key, keySize); initCache(cache, key, keySize);
# ifdef XMRIG_SECURE_JIT
cache->jit->enableWriting();
# endif
cache->jit->generateSuperscalarHash(cache->programs); cache->jit->generateSuperscalarHash(cache->programs);
cache->jit->generateDatasetInitCode(); cache->jit->generateDatasetInitCode();
# ifdef XMRIG_SECURE_JIT
cache->jit->enableExecution();
# endif
} }
constexpr uint64_t superscalarMul0 = 6364136223846793005ULL; constexpr uint64_t superscalarMul0 = 6364136223846793005ULL;

View file

@ -1,6 +1,7 @@
/* /*
Copyright (c) 2018-2019, tevador <tevador@gmail.com> Copyright (c) 2018-2020, tevador <tevador@gmail.com>
Copyright (c) 2019, SChernykh <https://github.com/SChernykh> Copyright (c) 2019-2020, SChernykh <https://github.com/SChernykh>
Copyright (c) 2019-2020, XMRig <https://github.com/xmrig>, <support@xmrig.com>
All rights reserved. All rights reserved.
@ -28,9 +29,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "crypto/randomx/jit_compiler_a64.hpp" #include "crypto/randomx/jit_compiler_a64.hpp"
#include "crypto/randomx/superscalar.hpp" #include "crypto/common/VirtualMemory.h"
#include "crypto/randomx/program.hpp" #include "crypto/randomx/program.hpp"
#include "crypto/randomx/reciprocal.h" #include "crypto/randomx/reciprocal.h"
#include "crypto/randomx/superscalar.hpp"
#include "crypto/randomx/virtual_memory.hpp" #include "crypto/randomx/virtual_memory.hpp"
static bool hugePagesJIT = false; static bool hugePagesJIT = false;
@ -357,6 +359,21 @@ size_t JitCompilerA64::getCodeSize()
return CodeSize; return CodeSize;
} }
void JitCompilerA64::enableWriting()
{
xmrig::VirtualMemory::protectRW(code, CodeSize + CalcDatasetItemSize());
}
void JitCompilerA64::enableExecution()
{
xmrig::VirtualMemory::protectRX(code, CodeSize + CalcDatasetItemSize());
}
void JitCompilerA64::enableAll()
{
xmrig::VirtualMemory::protectRWX(code, CodeSize + CalcDatasetItemSize());
}
void JitCompilerA64::emitMovImmediate(uint32_t dst, uint32_t imm, uint8_t* code, uint32_t& codePos) void JitCompilerA64::emitMovImmediate(uint32_t dst, uint32_t imm, uint8_t* code, uint32_t& codePos)
{ {
uint32_t k = codePos; uint32_t k = codePos;

View file

@ -1,6 +1,7 @@
/* /*
Copyright (c) 2018-2019, tevador <tevador@gmail.com> Copyright (c) 2018-2020, tevador <tevador@gmail.com>
Copyright (c) 2019, SChernykh <https://github.com/SChernykh> Copyright (c) 2019-2020, SChernykh <https://github.com/SChernykh>
Copyright (c) 2019-2020, XMRig <https://github.com/xmrig>, <support@xmrig.com>
All rights reserved. All rights reserved.
@ -63,6 +64,10 @@ namespace randomx {
uint8_t* getCode() { return code; } uint8_t* getCode() { return code; }
size_t getCodeSize(); size_t getCodeSize();
void enableWriting();
void enableExecution();
void enableAll();
static InstructionGeneratorA64 engine[256]; static InstructionGeneratorA64 engine[256];
uint32_t reg_changed_offset[8]; uint32_t reg_changed_offset[8];
uint8_t* code; uint8_t* code;

View file

@ -1,5 +1,7 @@
/* /*
Copyright (c) 2018-2019, tevador <tevador@gmail.com> Copyright (c) 2018-2020, tevador <tevador@gmail.com>
Copyright (c) 2019-2020, SChernykh <https://github.com/SChernykh>
Copyright (c) 2019-2020, XMRig <https://github.com/xmrig>, <support@xmrig.com>
All rights reserved. All rights reserved.
@ -70,5 +72,8 @@ namespace randomx {
size_t getCodeSize() { size_t getCodeSize() {
return 0; return 0;
} }
void enableWriting() {}
void enableExecution() {}
void enableAll() {}
}; };
} }

View file

@ -1,5 +1,7 @@
/* /*
Copyright (c) 2018-2019, tevador <tevador@gmail.com> Copyright (c) 2018-2020, tevador <tevador@gmail.com>
Copyright (c) 2019-2020, SChernykh <https://github.com/SChernykh>
Copyright (c) 2019-2020, XMRig <https://github.com/xmrig>, <support@xmrig.com>
All rights reserved. All rights reserved.
@ -30,8 +32,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cstring> #include <cstring>
#include <climits> #include <climits>
#include <atomic> #include <atomic>
#include "crypto/randomx/jit_compiler_x86.hpp" #include "crypto/randomx/jit_compiler_x86.hpp"
#include "backend/cpu/Cpu.h" #include "backend/cpu/Cpu.h"
#include "crypto/common/VirtualMemory.h"
#include "crypto/randomx/jit_compiler_x86_static.hpp" #include "crypto/randomx/jit_compiler_x86_static.hpp"
#include "crypto/randomx/program.hpp" #include "crypto/randomx/program.hpp"
#include "crypto/randomx/reciprocal.h" #include "crypto/randomx/reciprocal.h"
@ -170,16 +174,28 @@ namespace randomx {
return codePos < prologueSize ? 0 : codePos - prologueSize; return codePos < prologueSize ? 0 : codePos - prologueSize;
} }
static inline void cpuid(uint32_t level, int32_t output[4]) void JitCompilerX86::enableAll() {
{ xmrig::VirtualMemory::protectRWX(code, CodeSize);
memset(output, 0, sizeof(int32_t) * 4); }
# ifdef _MSC_VER void JitCompilerX86::enableWriting() {
__cpuid(output, static_cast<int>(level)); xmrig::VirtualMemory::protectRW(code, CodeSize);
# else }
__cpuid_count(level, 0, output[0], output[1], output[2], output[3]);
# endif void JitCompilerX86::enableExecution() {
} xmrig::VirtualMemory::protectRX(code, CodeSize);
}
static inline void cpuid(uint32_t level, int32_t output[4])
{
memset(output, 0, sizeof(int32_t) * 4);
# ifdef _MSC_VER
__cpuid(output, static_cast<int>(level));
# else
__cpuid_count(level, 0, output[0], output[1], output[2], output[3]);
# endif
}
# ifdef _MSC_VER # ifdef _MSC_VER
static FORCE_INLINE uint32_t rotl32(uint32_t a, int shift) { return _rotl(a, shift); } static FORCE_INLINE uint32_t rotl32(uint32_t a, int shift) { return _rotl(a, shift); }

View file

@ -1,5 +1,7 @@
/* /*
Copyright (c) 2018-2019, tevador <tevador@gmail.com> Copyright (c) 2018-2020, tevador <tevador@gmail.com>
Copyright (c) 2019-2020, SChernykh <https://github.com/SChernykh>
Copyright (c) 2019-2020, XMRig <https://github.com/xmrig>, <support@xmrig.com>
All rights reserved. All rights reserved.
@ -65,6 +67,9 @@ namespace randomx {
return code; return code;
} }
size_t getCodeSize(); size_t getCodeSize();
void enableWriting();
void enableExecution();
void enableAll();
alignas(64) static InstructionGeneratorX86 engine[256]; alignas(64) static InstructionGeneratorX86 engine[256];

View file

@ -1,5 +1,7 @@
/* /*
Copyright (c) 2018-2019, tevador <tevador@gmail.com> Copyright (c) 2018-2020, tevador <tevador@gmail.com>
Copyright (c) 2019-2020, SChernykh <https://github.com/SChernykh>
Copyright (c) 2019-2020, XMRig <https://github.com/xmrig>, <support@xmrig.com>
All rights reserved. All rights reserved.
@ -47,7 +49,17 @@ namespace randomx {
compiler.prepare(); compiler.prepare();
VmBase<softAes>::generateProgram(seed); VmBase<softAes>::generateProgram(seed);
randomx_vm::initialize(); randomx_vm::initialize();
# ifdef XMRIG_SECURE_JIT
compiler.enableWriting();
# endif
compiler.generateProgram(program, config, randomx_vm::getFlags()); compiler.generateProgram(program, config, randomx_vm::getFlags());
# ifdef XMRIG_SECURE_JIT
compiler.enableExecution();
# endif
mem.memory = datasetPtr->memory + datasetOffset; mem.memory = datasetPtr->memory + datasetOffset;
execute(); execute();
} }

View file

@ -1,5 +1,7 @@
/* /*
Copyright (c) 2018-2019, tevador <tevador@gmail.com> Copyright (c) 2018-2020, tevador <tevador@gmail.com>
Copyright (c) 2019-2020, SChernykh <https://github.com/SChernykh>
Copyright (c) 2019-2020, XMRig <https://github.com/xmrig>, <support@xmrig.com>
All rights reserved. All rights reserved.
@ -36,14 +38,33 @@ namespace randomx {
void CompiledLightVm<softAes>::setCache(randomx_cache* cache) { void CompiledLightVm<softAes>::setCache(randomx_cache* cache) {
cachePtr = cache; cachePtr = cache;
mem.memory = cache->memory; mem.memory = cache->memory;
# ifdef XMRIG_SECURE_JIT
compiler.enableWriting();
# endif
compiler.generateSuperscalarHash(cache->programs); compiler.generateSuperscalarHash(cache->programs);
# ifdef XMRIG_SECURE_JIT
compiler.enableExecution();
# endif
} }
template<int softAes> template<int softAes>
void CompiledLightVm<softAes>::run(void* seed) { void CompiledLightVm<softAes>::run(void* seed) {
VmBase<softAes>::generateProgram(seed); VmBase<softAes>::generateProgram(seed);
randomx_vm::initialize(); randomx_vm::initialize();
# ifdef XMRIG_SECURE_JIT
compiler.enableWriting();
# endif
compiler.generateProgramLight(program, config, datasetOffset); compiler.generateProgramLight(program, config, datasetOffset);
# ifdef XMRIG_SECURE_JIT
compiler.enableExecution();
# endif
CompiledVm<softAes>::execute(); CompiledVm<softAes>::execute();
} }