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,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.
@ -77,16 +79,16 @@ namespace randomx {
context.pwdlen = (uint32_t)keySize;
context.salt = CONST_CAST(uint8_t *)RandomX_CurrentConfig.ArgonSalt;
context.saltlen = (uint32_t)strlen(RandomX_CurrentConfig.ArgonSalt);
context.secret = NULL;
context.secret = nullptr;
context.secretlen = 0;
context.ad = NULL;
context.ad = nullptr;
context.adlen = 0;
context.t_cost = RandomX_CurrentConfig.ArgonIterations;
context.m_cost = RandomX_CurrentConfig.ArgonMemory;
context.lanes = RandomX_CurrentConfig.ArgonLanes;
context.threads = 1;
context.allocate_cbk = NULL;
context.free_cbk = NULL;
context.allocate_cbk = nullptr;
context.free_cbk = nullptr;
context.flags = ARGON2_DEFAULT_FLAGS;
context.version = ARGON2_VERSION_NUMBER;
@ -100,8 +102,17 @@ namespace randomx {
void initCacheCompile(randomx_cache* cache, const void* key, size_t keySize) {
initCache(cache, key, keySize);
# ifdef XMRIG_SECURE_JIT
cache->jit->enableWriting();
# endif
cache->jit->generateSuperscalarHash(cache->programs);
cache->jit->generateDatasetInitCode();
# ifdef XMRIG_SECURE_JIT
cache->jit->enableExecution();
# endif
}
constexpr uint64_t superscalarMul0 = 6364136223846793005ULL;

View file

@ -1,6 +1,7 @@
/*
Copyright (c) 2018-2019, tevador <tevador@gmail.com>
Copyright (c) 2019, SChernykh <https://github.com/SChernykh>
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.
@ -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/superscalar.hpp"
#include "crypto/common/VirtualMemory.h"
#include "crypto/randomx/program.hpp"
#include "crypto/randomx/reciprocal.h"
#include "crypto/randomx/superscalar.hpp"
#include "crypto/randomx/virtual_memory.hpp"
static bool hugePagesJIT = false;
@ -357,6 +359,21 @@ size_t JitCompilerA64::getCodeSize()
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)
{
uint32_t k = codePos;

View file

@ -1,6 +1,7 @@
/*
Copyright (c) 2018-2019, tevador <tevador@gmail.com>
Copyright (c) 2019, SChernykh <https://github.com/SChernykh>
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.
@ -63,6 +64,10 @@ namespace randomx {
uint8_t* getCode() { return code; }
size_t getCodeSize();
void enableWriting();
void enableExecution();
void enableAll();
static InstructionGeneratorA64 engine[256];
uint32_t reg_changed_offset[8];
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.
@ -70,5 +72,8 @@ namespace randomx {
size_t getCodeSize() {
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.
@ -30,8 +32,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cstring>
#include <climits>
#include <atomic>
#include "crypto/randomx/jit_compiler_x86.hpp"
#include "backend/cpu/Cpu.h"
#include "crypto/common/VirtualMemory.h"
#include "crypto/randomx/jit_compiler_x86_static.hpp"
#include "crypto/randomx/program.hpp"
#include "crypto/randomx/reciprocal.h"
@ -170,16 +174,28 @@ namespace randomx {
return codePos < prologueSize ? 0 : codePos - prologueSize;
}
static inline void cpuid(uint32_t level, int32_t output[4])
{
memset(output, 0, sizeof(int32_t) * 4);
void JitCompilerX86::enableAll() {
xmrig::VirtualMemory::protectRWX(code, CodeSize);
}
# ifdef _MSC_VER
__cpuid(output, static_cast<int>(level));
# else
__cpuid_count(level, 0, output[0], output[1], output[2], output[3]);
# endif
}
void JitCompilerX86::enableWriting() {
xmrig::VirtualMemory::protectRW(code, CodeSize);
}
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
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.
@ -65,6 +67,9 @@ namespace randomx {
return code;
}
size_t getCodeSize();
void enableWriting();
void enableExecution();
void enableAll();
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.
@ -47,7 +49,17 @@ namespace randomx {
compiler.prepare();
VmBase<softAes>::generateProgram(seed);
randomx_vm::initialize();
# ifdef XMRIG_SECURE_JIT
compiler.enableWriting();
# endif
compiler.generateProgram(program, config, randomx_vm::getFlags());
# ifdef XMRIG_SECURE_JIT
compiler.enableExecution();
# endif
mem.memory = datasetPtr->memory + datasetOffset;
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.
@ -36,14 +38,33 @@ namespace randomx {
void CompiledLightVm<softAes>::setCache(randomx_cache* cache) {
cachePtr = cache;
mem.memory = cache->memory;
# ifdef XMRIG_SECURE_JIT
compiler.enableWriting();
# endif
compiler.generateSuperscalarHash(cache->programs);
# ifdef XMRIG_SECURE_JIT
compiler.enableExecution();
# endif
}
template<int softAes>
void CompiledLightVm<softAes>::run(void* seed) {
VmBase<softAes>::generateProgram(seed);
randomx_vm::initialize();
# ifdef XMRIG_SECURE_JIT
compiler.enableWriting();
# endif
compiler.generateProgramLight(program, config, datasetOffset);
# ifdef XMRIG_SECURE_JIT
compiler.enableExecution();
# endif
CompiledVm<softAes>::execute();
}