From caae7c64f0c920b633b78fb03413fdb67d459f3e Mon Sep 17 00:00:00 2001
From: SChernykh <15806605+SChernykh@users.noreply.github.com>
Date: Sun, 14 Apr 2024 09:13:00 +0200
Subject: [PATCH] RandomX: correct memcpy size for JIT initialization

No buffer overflow, better fix for `_FORTIFY_SOURCE`
---
 src/crypto/randomx/jit_compiler_a64.cpp | 2 +-
 src/crypto/randomx/jit_compiler_a64.hpp | 2 +-
 src/crypto/randomx/jit_compiler_x86.cpp | 2 +-
 src/crypto/randomx/jit_compiler_x86.hpp | 2 +-
 src/crypto/randomx/randomx.cpp          | 3 +--
 5 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/crypto/randomx/jit_compiler_a64.cpp b/src/crypto/randomx/jit_compiler_a64.cpp
index 4bfe157ea..05dac9f78 100644
--- a/src/crypto/randomx/jit_compiler_a64.cpp
+++ b/src/crypto/randomx/jit_compiler_a64.cpp
@@ -1078,6 +1078,6 @@ void JitCompilerA64::h_NOP(Instruction& instr, uint32_t& codePos)
 {
 }
 
-InstructionGeneratorA64 JitCompilerA64::engine[257] = {};
+InstructionGeneratorA64 JitCompilerA64::engine[256] = {};
 
 }
diff --git a/src/crypto/randomx/jit_compiler_a64.hpp b/src/crypto/randomx/jit_compiler_a64.hpp
index 15c90af80..32ff5166e 100644
--- a/src/crypto/randomx/jit_compiler_a64.hpp
+++ b/src/crypto/randomx/jit_compiler_a64.hpp
@@ -74,7 +74,7 @@ namespace randomx {
 		void enableWriting() const;
 		void enableExecution() const;
 
-		static InstructionGeneratorA64 engine[257];
+		static InstructionGeneratorA64 engine[256];
 
 	private:
 		const bool hugePages;
diff --git a/src/crypto/randomx/jit_compiler_x86.cpp b/src/crypto/randomx/jit_compiler_x86.cpp
index 78ab8b58f..7f9fb3b68 100644
--- a/src/crypto/randomx/jit_compiler_x86.cpp
+++ b/src/crypto/randomx/jit_compiler_x86.cpp
@@ -1443,6 +1443,6 @@ namespace randomx {
 		emitByte(0x90, code, codePos);
 	}
 
-	alignas(64) InstructionGeneratorX86 JitCompilerX86::engine[257] = {};
+	alignas(64) InstructionGeneratorX86 JitCompilerX86::engine[256] = {};
 
 }
diff --git a/src/crypto/randomx/jit_compiler_x86.hpp b/src/crypto/randomx/jit_compiler_x86.hpp
index 11106b2f3..152619226 100644
--- a/src/crypto/randomx/jit_compiler_x86.hpp
+++ b/src/crypto/randomx/jit_compiler_x86.hpp
@@ -81,7 +81,7 @@ namespace randomx {
 		void enableWriting() const;
 		void enableExecution() const;
 
-		alignas(64) static InstructionGeneratorX86 engine[257];
+		alignas(64) static InstructionGeneratorX86 engine[256];
 
 	private:
 		int registerUsage[RegistersCount] = {};
diff --git a/src/crypto/randomx/randomx.cpp b/src/crypto/randomx/randomx.cpp
index 0485d4ec2..9047293b0 100644
--- a/src/crypto/randomx/randomx.cpp
+++ b/src/crypto/randomx/randomx.cpp
@@ -260,8 +260,7 @@ typedef void(randomx::JitCompilerX86::* InstructionGeneratorX86_2)(const randomx
 
 #define JIT_HANDLE(x, prev) do { \
 		const InstructionGeneratorX86_2 p = &randomx::JitCompilerX86::h_##x; \
-		static_assert(sizeof(p) == sizeof(randomx::JitCompilerX86::engine[k]), "Pointer size mismatch"); \
-		memcpy(randomx::JitCompilerX86::engine + k, &p, sizeof(p)); \
+		memcpy(randomx::JitCompilerX86::engine + k, &p, sizeof(randomx::JitCompilerX86::engine[k])); \
 	} while (0)
 
 #elif (XMRIG_ARM == 8)