From 32d1a76a947551f78fca5f734ccc526a22f31570 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Fri, 20 Oct 2023 09:04:35 +0200 Subject: [PATCH] Fixed UB in ARM64 JIT compiler Fixed unaligned memory writes --- src/jit_compiler_a64.cpp | 3 ++- src/jit_compiler_a64.hpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/jit_compiler_a64.cpp b/src/jit_compiler_a64.cpp index 0c557662..e3fd9bc9 100644 --- a/src/jit_compiler_a64.cpp +++ b/src/jit_compiler_a64.cpp @@ -709,7 +709,8 @@ void JitCompilerA64::h_IMUL_RCP(Instruction& instr, uint32_t& codePos) const uint32_t literal_id = (ImulRcpLiteralsEnd - literalPos) / sizeof(uint64_t); literalPos -= sizeof(uint64_t); - *(uint64_t*)(code + literalPos) = (q << shift) + ((r << shift) / divisor); + const uint64_t literal = (q << shift) + ((r << shift) / divisor); + memcpy(code + literalPos, &literal, sizeof(literal)); if (literal_id < 12) { diff --git a/src/jit_compiler_a64.hpp b/src/jit_compiler_a64.hpp index a02824ff..f8484c08 100644 --- a/src/jit_compiler_a64.hpp +++ b/src/jit_compiler_a64.hpp @@ -81,7 +81,7 @@ namespace randomx { static void emit64(uint64_t val, uint8_t* code, uint32_t& codePos) { - *(uint64_t*)(code + codePos) = val; + memcpy(code + codePos, &val, sizeof(val)); codePos += sizeof(val); }