From 39c569ae44b0567ad4856fc422290c62d889c7cd Mon Sep 17 00:00:00 2001 From: tevador Date: Thu, 27 Dec 2018 21:42:38 +0100 Subject: [PATCH] Fixed a potential crash in JitCompilerX86 --- src/JitCompilerX86.cpp | 15 ++++++++++++--- src/common.hpp | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/JitCompilerX86.cpp b/src/JitCompilerX86.cpp index f5740fb..6f83e40 100644 --- a/src/JitCompilerX86.cpp +++ b/src/JitCompilerX86.cpp @@ -81,6 +81,11 @@ namespace RandomX { */ + constexpr uint8_t ic3 = (InstructionCount >> 24); + constexpr uint8_t ic2 = (InstructionCount >> 16); + constexpr uint8_t ic1 = (InstructionCount >> 8); + constexpr uint8_t ic0 = (InstructionCount >> 0); + const uint8_t prologue[] = { 0x53, //push rbx 0x55, //push rbp @@ -108,7 +113,7 @@ namespace RandomX { 0x48, 0x8b, 0xcf, //mov rcx, rdi #endif 0x48, 0x8b, 0xec, //mov rbp,rsp - 0x48, 0xc7, 0xc7, 0x00, 0x00, 0x10, 0x00, //mov rdi,0x100000 + 0x48, 0xc7, 0xc7, ic0, ic1, ic2, ic3, //mov rdi, "InstructionCount" 0x4c, 0x8b, 0x01, //mov r8,QWORD PTR[rcx] 0x4c, 0x8b, 0x49, 0x08, //mov r9,QWORD PTR[rcx+0x8] 0x4c, 0x8b, 0x51, 0x10, //mov r10,QWORD PTR[rcx+0x10] @@ -209,11 +214,15 @@ namespace RandomX { throw std::runtime_error("mmap failed"); #endif memcpy(code, prologue, sizeof(prologue)); - if (startOffsetAligned - sizeof(prologue) > 4) { - codePos = sizeof(prologue); + codePos = sizeof(prologue); + if (startOffsetAligned - codePos > 4) { emitByte(0xeb); emitByte(startOffsetAligned - (codePos + 1)); } + else { + while (codePos < startOffsetAligned) + emitByte(0x90); //nop + } memcpy(code + readDatasetSubOffset, readDatasetSub, sizeof(readDatasetSub)); memcpy(code + epilogueOffset, epilogue, sizeof(epilogue)); } diff --git a/src/common.hpp b/src/common.hpp index 32743dd..761d9f5 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -62,7 +62,7 @@ namespace RandomX { }; constexpr int ProgramLength = 512; - constexpr int InstructionCount = 1024 * 1024; + constexpr uint32_t InstructionCount = 1024 * 1024; constexpr uint32_t ScratchpadSize = 256 * 1024; constexpr uint32_t ScratchpadLength = ScratchpadSize / sizeof(convertible_t); constexpr uint32_t ScratchpadL1 = ScratchpadSize / 16 / sizeof(convertible_t);