diff --git a/src/crypto/randomx/dataset.cpp b/src/crypto/randomx/dataset.cpp index 9215cd59a..4acf366ad 100644 --- a/src/crypto/randomx/dataset.cpp +++ b/src/crypto/randomx/dataset.cpp @@ -61,10 +61,11 @@ namespace randomx { template<class Allocator> void deallocCache(randomx_cache* cache) { - if (cache->memory != nullptr) + if (cache->memory != nullptr) { Allocator::freeMemory(cache->memory, RANDOMX_CACHE_MAX_SIZE); - if (cache->jit != nullptr) - delete cache->jit; + } + + delete cache->jit; } template void deallocCache<DefaultAllocator>(randomx_cache* cache); diff --git a/src/crypto/randomx/jit_compiler_a64.cpp b/src/crypto/randomx/jit_compiler_a64.cpp index 97f781094..d61e20b5c 100644 --- a/src/crypto/randomx/jit_compiler_a64.cpp +++ b/src/crypto/randomx/jit_compiler_a64.cpp @@ -114,6 +114,10 @@ JitCompilerA64::~JitCompilerA64() void JitCompilerA64::generateProgram(Program& program, ProgramConfiguration& config, uint32_t) { +# ifdef XMRIG_SECURE_JIT + enableWriting(); +# endif + uint32_t codePos = MainLoopBegin + 4; // and w16, w10, ScratchpadL3Mask64 @@ -334,8 +338,12 @@ void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[N]) template void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[RANDOMX_CACHE_MAX_ACCESSES]); -DatasetInitFunc* JitCompilerA64::getDatasetInitFunc() +DatasetInitFunc* JitCompilerA64::getDatasetInitFunc() const { +# ifdef XMRIG_SECURE_JIT + enableExecution(); +# endif + return (DatasetInitFunc*)(code + (((uint8_t*)randomx_init_dataset_aarch64) - ((uint8_t*)randomx_program_aarch64))); } @@ -344,21 +352,16 @@ size_t JitCompilerA64::getCodeSize() return CodeSize; } -void JitCompilerA64::enableWriting() +void JitCompilerA64::enableWriting() const { xmrig::VirtualMemory::protectRW(code, CodeSize + CalcDatasetItemSize()); } -void JitCompilerA64::enableExecution() +void JitCompilerA64::enableExecution() const { 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; diff --git a/src/crypto/randomx/jit_compiler_a64.hpp b/src/crypto/randomx/jit_compiler_a64.hpp index 3b47ea3a4..3f5b9a980 100644 --- a/src/crypto/randomx/jit_compiler_a64.hpp +++ b/src/crypto/randomx/jit_compiler_a64.hpp @@ -59,16 +59,24 @@ namespace randomx { void generateDatasetInitCode() {} - ProgramFunc* getProgramFunc() { return reinterpret_cast<ProgramFunc*>(code); } - DatasetInitFunc* getDatasetInitFunc(); + inline ProgramFunc *getProgramFunc() const { +# ifdef XMRIG_SECURE_JIT + enableExecution(); +# endif + + return reinterpret_cast<ProgramFunc*>(code); + } + + DatasetInitFunc* getDatasetInitFunc() const; uint8_t* getCode() { return code; } size_t getCodeSize(); - void enableWriting(); - void enableExecution(); - void enableAll(); + void enableWriting() const; + void enableExecution() const; static InstructionGeneratorA64 engine[256]; + + private: uint32_t reg_changed_offset[8]; uint8_t* code; uint32_t literalPos; @@ -95,6 +103,7 @@ namespace randomx { template<uint32_t tmp_reg_fp> void emitMemLoadFP(uint32_t src, Instruction& instr, uint8_t* code, uint32_t& codePos); + public: void h_IADD_RS(Instruction&, uint32_t&); void h_IADD_M(Instruction&, uint32_t&); void h_ISUB_R(Instruction&, uint32_t&); diff --git a/src/crypto/randomx/jit_compiler_fallback.hpp b/src/crypto/randomx/jit_compiler_fallback.hpp index 0eb4058ff..b86411976 100644 --- a/src/crypto/randomx/jit_compiler_fallback.hpp +++ b/src/crypto/randomx/jit_compiler_fallback.hpp @@ -74,6 +74,5 @@ namespace randomx { } void enableWriting() {} void enableExecution() {} - void enableAll() {} }; } diff --git a/src/crypto/randomx/jit_compiler_x86.cpp b/src/crypto/randomx/jit_compiler_x86.cpp index 5552c66b6..d0a97bbde 100644 --- a/src/crypto/randomx/jit_compiler_x86.cpp +++ b/src/crypto/randomx/jit_compiler_x86.cpp @@ -174,16 +174,12 @@ namespace randomx { return codePos < prologueSize ? 0 : codePos - prologueSize; } - void JitCompilerX86::enableAll() { - xmrig::VirtualMemory::protectRWX(code, CodeSize); + void JitCompilerX86::enableWriting() const { + xmrig::VirtualMemory::protectRW(allocatedCode, allocatedSize); } - void JitCompilerX86::enableWriting() { - xmrig::VirtualMemory::protectRW(code, CodeSize); - } - - void JitCompilerX86::enableExecution() { - xmrig::VirtualMemory::protectRX(code, CodeSize); + void JitCompilerX86::enableExecution() const { + xmrig::VirtualMemory::protectRX(allocatedCode, allocatedSize); } static inline void cpuid(uint32_t level, int32_t output[4]) @@ -216,7 +212,8 @@ namespace randomx { cpuid(0x80000001, info); hasXOP = ((info[2] & (1 << 11)) != 0); - allocatedCode = (uint8_t*)allocExecutableMemory(CodeSize * 2, hugePagesJIT && hugePagesEnable); + allocatedSize = CodeSize * 2; + allocatedCode = static_cast<uint8_t*>(allocExecutableMemory(allocatedSize, hugePagesJIT && hugePagesEnable)); // Shift code base address to improve caching - all threads will use different L2/L3 cache sets code = allocatedCode + (codeOffset.fetch_add(codeOffsetIncrement) % CodeSize); @@ -240,7 +237,7 @@ namespace randomx { JitCompilerX86::~JitCompilerX86() { codeOffset.fetch_sub(codeOffsetIncrement); - freePagedMemory(allocatedCode, CodeSize); + freePagedMemory(allocatedCode, allocatedSize); } void JitCompilerX86::prepare() { @@ -253,6 +250,10 @@ namespace randomx { void JitCompilerX86::generateProgram(Program& prog, ProgramConfiguration& pcfg, uint32_t flags) { PROFILE_SCOPE(RandomX_JIT_compile); +# ifdef XMRIG_SECURE_JIT + enableWriting(); +# endif + vm_flags = flags; generateProgramPrologue(prog, pcfg); diff --git a/src/crypto/randomx/jit_compiler_x86.hpp b/src/crypto/randomx/jit_compiler_x86.hpp index 730779239..32ca97b5d 100644 --- a/src/crypto/randomx/jit_compiler_x86.hpp +++ b/src/crypto/randomx/jit_compiler_x86.hpp @@ -57,22 +57,33 @@ namespace randomx { template<size_t N> void generateSuperscalarHash(SuperscalarProgram (&programs)[N]); void generateDatasetInitCode(); - ProgramFunc* getProgramFunc() { - return (ProgramFunc*)code; + + inline ProgramFunc *getProgramFunc() const { +# ifdef XMRIG_SECURE_JIT + enableExecution(); +# endif + + return reinterpret_cast<ProgramFunc*>(code); } - DatasetInitFunc* getDatasetInitFunc() { + + inline DatasetInitFunc *getDatasetInitFunc() const { +# ifdef XMRIG_SECURE_JIT + enableExecution(); +# endif + return (DatasetInitFunc*)code; } + uint8_t* getCode() { return code; } size_t getCodeSize(); - void enableWriting(); - void enableExecution(); - void enableAll(); + void enableWriting() const; + void enableExecution() const; alignas(64) static InstructionGeneratorX86 engine[256]; + private: int registerUsage[RegistersCount] = {}; uint8_t* code = nullptr; uint32_t codePos = 0; @@ -87,7 +98,8 @@ namespace randomx { bool hasAVX; bool hasXOP; - uint8_t* allocatedCode; + uint8_t* allocatedCode = nullptr; + size_t allocatedSize = 0; void generateProgramPrologue(Program&, ProgramConfiguration&); void generateProgramEpilogue(Program&, ProgramConfiguration&); @@ -124,6 +136,7 @@ namespace randomx { codePos += count; } + public: void h_IADD_RS(const Instruction&); void h_IADD_M(const Instruction&); void h_ISUB_R(const Instruction&); diff --git a/src/crypto/randomx/vm_compiled.cpp b/src/crypto/randomx/vm_compiled.cpp index c158324d1..842bfe651 100644 --- a/src/crypto/randomx/vm_compiled.cpp +++ b/src/crypto/randomx/vm_compiled.cpp @@ -49,17 +49,7 @@ 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(); } @@ -68,9 +58,9 @@ namespace randomx { void CompiledVm<softAes>::execute() { PROFILE_SCOPE(RandomX_JIT_execute); -#ifdef XMRIG_ARM +# ifdef XMRIG_ARM memcpy(reg.f, config.eMask, sizeof(config.eMask)); -#endif +# endif compiler.getProgramFunc()(reg, mem, scratchpad, RandomX_CurrentConfig.ProgramIterations); } diff --git a/src/crypto/randomx/vm_compiled_light.cpp b/src/crypto/randomx/vm_compiled_light.cpp index 2adb8c72b..e7fdd7e91 100644 --- a/src/crypto/randomx/vm_compiled_light.cpp +++ b/src/crypto/randomx/vm_compiled_light.cpp @@ -44,10 +44,6 @@ namespace randomx { # endif compiler.generateSuperscalarHash(cache->programs); - -# ifdef XMRIG_SECURE_JIT - compiler.enableExecution(); -# endif } template<int softAes> @@ -61,10 +57,6 @@ namespace randomx { compiler.generateProgramLight(program, config, datasetOffset); -# ifdef XMRIG_SECURE_JIT - compiler.enableExecution(); -# endif - CompiledVm<softAes>::execute(); }