What changed, and why it matters
This commit adds a build-time check for optional RISC-V CPU instruction extensions (Zba and Zbb) in the XMRig cryptocurrency miner. It does not change how the program runs on existing machines; it only lets the compiler use faster instructions when building specifically for the machine doing the compilation. There is no security bug or vulnerability visible in the change.
No security action required. Treat as a normal build-system enhancement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces CMake try_run tests that compile and execute tiny assembly snippets to detect RISC-V Zba (sh1add) and Zbb (ror) support. If detected, the -march flag is extended from rv64gc to rv64gc_zba and/or rv64gc_zbb. This is a build-system feature enabling optional ISA extensions for native builds; it does not alter runtime behavior, add network-facing code, or introduce unsafe memory operations.
Changed components
cmake/cpu.cmakecmake/flags.cmakesrc/crypto/randomx/tests/riscv64_zba.ssrc/crypto/randomx/tests/riscv64_zbb.sInspect captured patch +54 / −8
diff --git a/cmake/cpu.cmake b/cmake/cpu.cmake
index 84ef245..5701720 100644
--- a/cmake/cpu.cmake
+++ b/cmake/cpu.cmake
@@ -47,6 +47,38 @@ if (XMRIG_RISCV)
set(WITH_SSE4_1 OFF)
set(WITH_AVX2 OFF)
set(WITH_VAES OFF)
+
+ # default build uses the RV64GC baseline
+ set(RVARCH "rv64gc")
+
+ # for native builds, enable Zba and Zbb if supported by the CPU
+ if(ARCH STREQUAL "native")
+ enable_language(ASM)
+
+ try_run(RANDOMX_ZBA_RUN_FAIL
+ RANDOMX_ZBA_COMPILE_OK
+ ${CMAKE_CURRENT_BINARY_DIR}/
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/crypto/randomx/tests/riscv64_zba.s
+ COMPILE_DEFINITIONS "-march=rv64gc_zba")
+
+ if (RANDOMX_ZBA_COMPILE_OK AND NOT RANDOMX_ZBA_RUN_FAIL)
+ set(RVARCH "${RVARCH}_zba")
+ message(STATUS "RISC-V zba extension detected")
+ endif()
+
+ try_run(RANDOMX_ZBB_RUN_FAIL
+ RANDOMX_ZBB_COMPILE_OK
+ ${CMAKE_CURRENT_BINARY_DIR}/
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/crypto/randomx/tests/riscv64_zbb.s
+ COMPILE_DEFINITIONS "-march=rv64gc_zbb")
+
+ if (RANDOMX_ZBB_COMPILE_OK AND NOT RANDOMX_ZBB_RUN_FAIL)
+ set(RVARCH "${RVARCH}_zbb")
+ message(STATUS "RISC-V zbb extension detected")
+ endif()
+ endif()
+
+ message(STATUS "Using -march=${RVARCH}")
endif()
add_definitions(-DRAPIDJSON_WRITE_DEFAULT_FLAGS=6) # rapidjson::kWriteNanAndInfFlag | rapidjson::kWriteNanAndInfNullFlag
diff --git a/cmake/flags.cmake b/cmake/flags.cmake
index 2046e85..a29a1a6 100644
--- a/cmake/flags.cmake
+++ b/cmake/flags.cmake
@@ -31,10 +31,8 @@ if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv7-a -mfpu=neon -flax-vector-conversions")
add_definitions(-DHAVE_ROTR)
elseif (XMRIG_RISCV)
- # RISC-V baseline: rv64gc (RV64IMAFD + Zicsr + Zifencei)
- # Use rv64gc for broad compatibility, extensions will be detected at runtime
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=rv64gc")
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=rv64gc")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=${RVARCH}")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=${RVARCH}")
add_definitions(-DHAVE_ROTR)
else()
@@ -86,10 +84,8 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon -march=${CMAKE_SYSTEM_PROCESSOR}")
add_definitions(-DHAVE_ROTR)
elseif (XMRIG_RISCV)
- # RISC-V baseline: rv64gc (RV64IMAFD + Zicsr + Zifencei)
- # Use rv64gc for broad compatibility, extensions will be detected at runtime
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=rv64gc")
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=rv64gc")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=${RVARCH}")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=${RVARCH}")
add_definitions(-DHAVE_ROTR)
else()
diff --git a/src/crypto/randomx/tests/riscv64_zba.s b/src/crypto/randomx/tests/riscv64_zba.s
new file mode 100644
index 0000000..e1947e7
--- /dev/null
+++ b/src/crypto/randomx/tests/riscv64_zba.s
@@ -0,0 +1,9 @@
+/* RISC-V - test if the Zba extension is present */
+
+.text
+.global main
+
+main:
+ sh1add x6, x6, x7
+ li x10, 0
+ ret
diff --git a/src/crypto/randomx/tests/riscv64_zbb.s b/src/crypto/randomx/tests/riscv64_zbb.s
new file mode 100644
index 0000000..d922043
--- /dev/null
+++ b/src/crypto/randomx/tests/riscv64_zbb.s
@@ -0,0 +1,9 @@
+/* RISC-V - test if the Zbb extension is present */
+
+.text
+.global main
+
+main:
+ ror x6, x6, x7
+ li x10, 0
+ ret
Why this scored 14/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.