From 5bc89fdc8b276bbecb62d868c86cccf7b01f7e34 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Tue, 21 Jul 2020 10:10:07 +0200 Subject: [PATCH] Fixed RandomX initialization for VS debug builds --- src/crypto/randomx/randomx.cpp | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/crypto/randomx/randomx.cpp b/src/crypto/randomx/randomx.cpp index f0adf541..7bb7df7f 100644 --- a/src/crypto/randomx/randomx.cpp +++ b/src/crypto/randomx/randomx.cpp @@ -165,32 +165,43 @@ RandomX_ConfigurationBase::RandomX_ConfigurationBase() fillAes4Rx4_Key[6] = rx_set_int_vec_i128(0xf63befa7, 0x2ba9660a, 0xf765a38b, 0xf273c9e7); fillAes4Rx4_Key[7] = rx_set_int_vec_i128(0xc0b0762d, 0x0c06d1fd, 0x915839de, 0x7a7cd609); + // Workaround for Visual Studio placing trampoline in debug builds. + auto addr = [](void (*func)()) { + const uint8_t* p = reinterpret_cast(func); +# if defined(_MSC_VER) + if (p[0] == 0xE9) { + p += *(const int32_t*)(p + 1) + 5; + } +# endif + return p; + }; + #if defined(_M_X64) || defined(__x86_64__) { - const uint8_t* a = (const uint8_t*)&randomx_sshash_prefetch; - const uint8_t* b = (const uint8_t*)&randomx_sshash_end; + const uint8_t* a = addr(randomx_sshash_prefetch); + const uint8_t* b = addr(randomx_sshash_end); memcpy(codeShhPrefetchTweaked, a, b - a); } { - const uint8_t* a = (const uint8_t*)&randomx_program_read_dataset; - const uint8_t* b = (const uint8_t*)&randomx_program_read_dataset_ryzen; + const uint8_t* a = addr(randomx_program_read_dataset); + const uint8_t* b = addr(randomx_program_read_dataset_ryzen); memcpy(codeReadDatasetTweaked, a, b - a); codeReadDatasetTweakedSize = b - a; } { - const uint8_t* a = (const uint8_t*)&randomx_program_read_dataset_ryzen; - const uint8_t* b = (const uint8_t*)&randomx_program_read_dataset_sshash_init; + const uint8_t* a = addr(randomx_program_read_dataset_ryzen); + const uint8_t* b = addr(randomx_program_read_dataset_sshash_init); memcpy(codeReadDatasetRyzenTweaked, a, b - a); codeReadDatasetRyzenTweakedSize = b - a; } { - const uint8_t* a = (const uint8_t*)&randomx_program_read_dataset_sshash_init; - const uint8_t* b = (const uint8_t*)&randomx_program_read_dataset_sshash_fin; + const uint8_t* a = addr(randomx_program_read_dataset_sshash_init); + const uint8_t* b = addr(randomx_program_read_dataset_sshash_fin); memcpy(codeReadDatasetLightSshInitTweaked, a, b - a); } { - const uint8_t* a = (const uint8_t*)&randomx_prefetch_scratchpad; - const uint8_t* b = (const uint8_t*)&randomx_prefetch_scratchpad_end; + const uint8_t* a = addr(randomx_prefetch_scratchpad); + const uint8_t* b = addr(randomx_prefetch_scratchpad_end); memcpy(codePrefetchScratchpadTweaked, a, b - a); } #endif