What changed, and why it matters
This commit refactors the RISC-V RandomX mining code in XMRig. It removes a dynamic analysis that tried to insert scratchpad prefetch instructions at an optimal point in each generated program, and instead always places the prefetch at a fixed, predictable location in the assembly template. The change is described by the author as an improvement/cleanup of RISC-V code. There is no indication in the commit or supplied references that this fixes a security vulnerability.
No security action required; treat as a normal code-quality/performance commit for the RISC-V RandomX JIT path.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch modifies the RISC-V vector JIT compiler for RandomX. In the C++ generator it deletes the backward scan that computed scratchpad_prefetch_pos based on which registers are read (readReg0/readReg1) and on ISWAP_R/IMUL_RCP dependencies. It also removes the runtime memcpy that injected the prefetch code snippet at that computed position. In the static assembly file, the scratchpad prefetch code is moved from a standalone tail region into the main loop right after the dataset MX xor loads and before the mx/ma swap, making it unconditional and removing the _end label. The light-mode path is updated to jump to this new location. This is a performance/maintenance change, not a security patch.
Changed components
src/crypto/randomx/jit_compiler_rv64_vector.cppsrc/crypto/randomx/jit_compiler_rv64_vector_static.Ssrc/crypto/randomx/jit_compiler_rv64_vector_static.hInspect captured patch +20 / −74
diff --git a/src/crypto/randomx/jit_compiler_rv64_vector.cpp b/src/crypto/randomx/jit_compiler_rv64_vector.cpp
index 536d9a9..5725779 100644
--- a/src/crypto/randomx/jit_compiler_rv64_vector.cpp
+++ b/src/crypto/randomx/jit_compiler_rv64_vector.cpp
@@ -360,44 +360,6 @@ void* generateProgramVectorRV64(uint8_t* buf, Program& prog, ProgramConfiguratio
uint8_t* last_modified[RegistersCount] = { p, p, p, p, p, p, p, p };
- uint8_t readReg01[RegistersCount] = {};
-
- readReg01[pcfg.readReg0] = 1;
- readReg01[pcfg.readReg1] = 1;
-
- uint32_t scratchpad_prefetch_pos = 0;
-
- for (int32_t i = static_cast<int32_t>(prog.getSize()) - 1; i >= 0; --i) {
- Instruction instr = prog(i);
-
- const InstructionType inst_type = static_cast<InstructionType>(inst_map[instr.opcode]);
-
- if (inst_type == InstructionType::CBRANCH) {
- scratchpad_prefetch_pos = i;
- break;
- }
-
- if (inst_type < InstructionType::FSWAP_R) {
- const uint32_t src = instr.src % RegistersCount;
- const uint32_t dst = instr.dst % RegistersCount;
-
- if ((inst_type == InstructionType::ISWAP_R) && (src != dst) && (readReg01[src] || readReg01[dst])) {
- scratchpad_prefetch_pos = i;
- break;
- }
-
- if ((inst_type == InstructionType::IMUL_RCP) && readReg01[dst] && !isZeroOrPowerOf2(instr.getImm32())) {
- scratchpad_prefetch_pos = i;
- break;
- }
-
- if (readReg01[dst]) {
- scratchpad_prefetch_pos = i;
- break;
- }
- }
- }
-
for (uint32_t i = 0, n = prog.getSize(); i < n; ++i) {
Instruction instr = prog(i);
@@ -854,16 +816,6 @@ void* generateProgramVectorRV64(uint8_t* buf, Program& prog, ProgramConfiguratio
default:
UNREACHABLE;
}
-
- // Prefetch scratchpad lines for the next main loop iteration
- // scratchpad_prefetch_pos is a conservative estimate of the earliest place in the code where we can do it
- if (i == scratchpad_prefetch_pos) {
- uint8_t* e = (uint8_t*)(buf + DIST(randomx_riscv64_vector_code_begin, randomx_riscv64_vector_program_scratchpad_prefetch_end));
- const size_t n = e - ((uint8_t*)spaddr_xor2);
-
- memcpy(p, spaddr_xor2, n);
- p += n;
- }
}
const uint8_t* e;
diff --git a/src/crypto/randomx/jit_compiler_rv64_vector_static.S b/src/crypto/randomx/jit_compiler_rv64_vector_static.S
index 2640972..f8c0a78 100644
--- a/src/crypto/randomx/jit_compiler_rv64_vector_static.S
+++ b/src/crypto/randomx/jit_compiler_rv64_vector_static.S
@@ -75,7 +75,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.global DECL(randomx_riscv64_vector_program_main_loop_instructions_end_light_mode)
.global DECL(randomx_riscv64_vector_program_main_loop_mx_xor_light_mode)
.global DECL(randomx_riscv64_vector_program_scratchpad_prefetch)
-.global DECL(randomx_riscv64_vector_program_scratchpad_prefetch_end)
.global DECL(randomx_riscv64_vector_program_end)
@@ -614,8 +613,7 @@ DECL(randomx_riscv64_vector_program_main_loop_mx_xor):
and x5, x5, x19 // x5 = (readReg2 ^ readReg3) & dataset mask
xor x14, x14, x5 // mx ^= (readReg2 ^ readReg3) & dataset mask
- and x5, x14, x19 // x5 = mx & dataset mask
- add x5, x5, x11 // x5 = &dataset[mx & dataset mask]
+ add x5, x14, x11 // x5 = &dataset[mx & dataset mask]
#ifdef __riscv_zicbop
prefetch.r (x5)
@@ -643,7 +641,24 @@ DECL(randomx_riscv64_vector_program_main_loop_mx_xor):
ld x6, 56(x5)
xor x27, x27, x6
-randomx_riscv64_vector_program_main_loop_swap_mx_ma:
+DECL(randomx_riscv64_vector_program_scratchpad_prefetch):
+ xor x5, x20, x22 // spAddr0-spAddr1 = readReg0 ^ readReg1 (JIT compiler will substitute the actual registers)
+ srli x6, x5, 32 // x6 = spAddr1
+
+ and x5, x5, x9 // x5 = spAddr0 & 64-byte aligned L3 mask
+ and x6, x6, x9 // x6 = spAddr1 & 64-byte aligned L3 mask
+
+ c.add x5, x12 // x5 = &scratchpad[spAddr0 & 64-byte aligned L3 mask]
+ c.add x6, x12 // x6 = &scratchpad[spAddr1 & 64-byte aligned L3 mask]
+
+#ifdef __riscv_zicbop
+ prefetch.r (x5)
+ prefetch.r (x6)
+#else
+ ld x5, (x5)
+ ld x6, (x6)
+#endif
+
// swap mx <-> ma
#ifdef __riscv_zbb
rori x14, x14, 32
@@ -847,27 +862,7 @@ DECL(randomx_riscv64_vector_program_main_loop_mx_xor_light_mode):
addi sp, sp, 192
- j randomx_riscv64_vector_program_main_loop_swap_mx_ma
-
-DECL(randomx_riscv64_vector_program_scratchpad_prefetch):
- xor x5, x20, x22 // spAddr0-spAddr1 = readReg0 ^ readReg1 (JIT compiler will substitute the actual registers)
- srli x6, x5, 32 // x6 = spAddr1
-
- and x5, x5, x9 // x5 = spAddr0 & 64-byte aligned L3 mask
- and x6, x6, x9 // x6 = spAddr1 & 64-byte aligned L3 mask
-
- c.add x5, x12 // x5 = &scratchpad[spAddr0 & 64-byte aligned L3 mask]
- c.add x6, x12 // x6 = &scratchpad[spAddr1 & 64-byte aligned L3 mask]
-
-#ifdef __riscv_zicbop
- prefetch.r (x5)
- prefetch.r (x6)
-#else
- ld x5, (x5)
- ld x6, (x6)
-#endif
-
-DECL(randomx_riscv64_vector_program_scratchpad_prefetch_end):
+ j randomx_riscv64_vector_program_scratchpad_prefetch
DECL(randomx_riscv64_vector_program_end):
diff --git a/src/crypto/randomx/jit_compiler_rv64_vector_static.h b/src/crypto/randomx/jit_compiler_rv64_vector_static.h
index a616f27..0624cc8 100644
--- a/src/crypto/randomx/jit_compiler_rv64_vector_static.h
+++ b/src/crypto/randomx/jit_compiler_rv64_vector_static.h
@@ -66,7 +66,6 @@ void randomx_riscv64_vector_program_main_loop_instructions_end_light_mode();
void randomx_riscv64_vector_program_main_loop_mx_xor_light_mode();
void randomx_riscv64_vector_program_end();
void randomx_riscv64_vector_program_scratchpad_prefetch();
-void randomx_riscv64_vector_program_scratchpad_prefetch_end();
void randomx_riscv64_vector_code_end();
Why this scored 12/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.