diff options
author | tevador <tevador@gmail.com> | 2019-10-11 12:56:15 +0200 |
---|---|---|
committer | tevador <tevador@gmail.com> | 2019-10-13 18:43:27 +0200 |
commit | ea813cfb1bfef3d268aeb6b42e9387cf9b938670 (patch) | |
tree | fa66b01d9b42e187f12f8802d935aa6d098bcd42 /src/crypto/rx-slow-hash.c | |
parent | Merge pull request #5969 (diff) | |
download | monero-ea813cfb1bfef3d268aeb6b42e9387cf9b938670.tar.xz |
RandomX: update to v1.1.4
* Faster cache initialization with SSSE3/AVX2
* Automatic detection of CPU capabilities in RandomX
* Fixed a possible out-of-bounds access in superscalar program generator
* Use MONERO_RANDOMX_UMASK to manually disable RandomX flags in monerod
Diffstat (limited to '')
-rw-r--r-- | src/crypto/rx-slow-hash.c | 106 |
1 files changed, 30 insertions, 76 deletions
diff --git a/src/crypto/rx-slow-hash.c b/src/crypto/rx-slow-hash.c index 59bd89d13..a7a459ad3 100644 --- a/src/crypto/rx-slow-hash.c +++ b/src/crypto/rx-slow-hash.c @@ -34,6 +34,7 @@ #include <stdio.h> #include <stdlib.h> #include <unistd.h> +#include <limits.h> #include "randomx.h" #include "c_threads.h" @@ -74,84 +75,41 @@ static void local_abort(const char *msg) #endif } -/** - * @brief uses cpuid to determine if the CPU supports the AES instructions - * @return true if the CPU supports AES, false otherwise - */ +static inline int disabled_flags(void) { + static int flags = -1; -static inline int force_software_aes(void) -{ - static int use = -1; - - if (use != -1) - return use; + if (flags != -1) { + return flags; + } - const char *env = getenv("MONERO_USE_SOFTWARE_AES"); + const char *env = getenv("MONERO_RANDOMX_UMASK"); if (!env) { - use = 0; - } - else if (!strcmp(env, "0") || !strcmp(env, "no")) { - use = 0; + flags = 0; } else { - use = 1; + char* endptr; + long int value = strtol(env, &endptr, 0); + if (endptr != env && value >= 0 && value < INT_MAX) { + flags = value; + } + else { + flags = 0; + } } - return use; -} -static void cpuid(int CPUInfo[4], int InfoType) -{ -#if defined(__x86_64__) - __asm __volatile__ - ( - "cpuid": - "=a" (CPUInfo[0]), - "=b" (CPUInfo[1]), - "=c" (CPUInfo[2]), - "=d" (CPUInfo[3]) : - "a" (InfoType), "c" (0) - ); -#endif + return flags; } -static inline int check_aes_hw(void) -{ -#if defined(__x86_64__) - int cpuid_results[4]; - static int supported = -1; - if(supported >= 0) - return supported; +static inline int enabled_flags(void) { + static int flags = -1; - cpuid(cpuid_results,1); - return supported = cpuid_results[2] & (1 << 25); -#else - return 0; -#endif -} - -static volatile int use_rx_jit_flag = -1; - -static inline int use_rx_jit(void) -{ -#if defined(__x86_64__) + if (flags != -1) { + return flags; + } - if (use_rx_jit_flag != -1) - return use_rx_jit_flag; + flags = randomx_get_flags(); - const char *env = getenv("MONERO_USE_RX_JIT"); - if (!env) { - use_rx_jit_flag = 1; - } - else if (!strcmp(env, "0") || !strcmp(env, "no")) { - use_rx_jit_flag = 0; - } - else { - use_rx_jit_flag = 1; - } - return use_rx_jit_flag; -#else - return 0; -#endif + return flags; } #define SEEDHASH_EPOCH_BLOCKS 2048 /* Must be same as BLOCKS_SYNCHRONIZING_MAX_COUNT in cryptonote_config.h */ @@ -236,7 +194,7 @@ void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const ch char *hash, int miners, int is_alt) { uint64_t s_height = rx_seedheight(mainheight); int toggle = (s_height & SEEDHASH_EPOCH_BLOCKS) != 0; - randomx_flags flags = RANDOMX_FLAG_DEFAULT; + randomx_flags flags = enabled_flags() & ~disabled_flags(); rx_state *rx_sp; randomx_cache *cache; @@ -263,8 +221,6 @@ void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const ch cache = rx_sp->rs_cache; if (cache == NULL) { - if (use_rx_jit()) - flags |= RANDOMX_FLAG_JIT; if (cache == NULL) { cache = randomx_alloc_cache(flags | RANDOMX_FLAG_LARGE_PAGES); if (cache == NULL) { @@ -282,14 +238,12 @@ void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const ch memcpy(rx_sp->rs_hash, seedhash, HASH_SIZE); } if (rx_vm == NULL) { - randomx_flags flags = RANDOMX_FLAG_DEFAULT; - if (use_rx_jit()) { - flags |= RANDOMX_FLAG_JIT; - if (!miners) - flags |= RANDOMX_FLAG_SECURE; + if ((flags & RANDOMX_FLAG_JIT) && !miners) { + flags |= RANDOMX_FLAG_SECURE & ~disabled_flags(); + } + if (miners && (disabled_flags() & RANDOMX_FLAG_FULL_MEM)) { + miners = 0; } - if(!force_software_aes() && check_aes_hw()) - flags |= RANDOMX_FLAG_HARD_AES; if (miners) { CTHR_MUTEX_LOCK(rx_dataset_mutex); if (rx_dataset == NULL) { |