From 5719404e43d9f5f28673b2fe17d31b461528f2ac Mon Sep 17 00:00:00 2001 From: sbiscigl Date: Wed, 18 Mar 2026 13:08:54 -0400 Subject: [PATCH] remove c rand from the standard retry strategy --- src/aws-cpp-sdk-core/CMakeLists.txt | 6 ++++ .../include/aws/core/utils/local/Random.h | 17 ++++++++++ .../source/client/RetryStrategy.cpp | 22 ++++--------- src/aws-cpp-sdk-core/source/utils/UUID.cpp | 33 +++---------------- .../source/utils/local/Random.cpp | 24 ++++++++++++++ 5 files changed, 58 insertions(+), 44 deletions(-) create mode 100644 src/aws-cpp-sdk-core/include/aws/core/utils/local/Random.h create mode 100644 src/aws-cpp-sdk-core/source/utils/local/Random.cpp diff --git a/src/aws-cpp-sdk-core/CMakeLists.txt b/src/aws-cpp-sdk-core/CMakeLists.txt index a7ba9d2c1661..68d57cf2ab3f 100644 --- a/src/aws-cpp-sdk-core/CMakeLists.txt +++ b/src/aws-cpp-sdk-core/CMakeLists.txt @@ -63,6 +63,7 @@ file(GLOB UTILS_BASE64_HEADERS "include/aws/core/utils/base64/*.h") file(GLOB UTILS_CHECKSUM_HEADERS "include/aws/core/utils/checksum/*.h") file(GLOB UTILS_CRYPTO_HEADERS "include/aws/core/utils/crypto/*.h") file(GLOB UTILS_JSON_HEADERS "include/aws/core/utils/json/*.h") +file(GLOB UTILS_LOCAL_HEADERS "include/aws/core/utils/local/*.h") file(GLOB UTILS_CBOR_HEADERS "include/aws/core/utils/cbor/*.h") file(GLOB UTILS_THREADING_HEADERS "include/aws/core/utils/threading/*.h") file(GLOB UTILS_XML_HEADERS "include/aws/core/utils/xml/*.h") @@ -119,6 +120,7 @@ file(GLOB UTILS_BASE64_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/base64/* file(GLOB UTILS_CHECKSUM_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/checksum/*.cpp") file(GLOB UTILS_CRYPTO_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/crypto/*.cpp") file(GLOB UTILS_JSON_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/json/*.cpp") +file(GLOB UTILS_LOCAL_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/local/*.cpp") file(GLOB UTILS_CBOR_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/cbor/*.cpp") file(GLOB UTILS_THREADING_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/threading/*.cpp") file(GLOB UTILS_XML_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/xml/*.cpp") @@ -274,6 +276,7 @@ file(GLOB AWS_NATIVE_SDK_COMMON_HEADERS ${UTILS_CHECKSUM_HEADERS} ${UTILS_CRYPTO_HEADERS} ${UTILS_JSON_HEADERS} + ${UTILS_LOCAL_HEADERS} ${UTILS_CBOR_HEADERS} ${UTILS_THREADING_HEADERS} ${UTILS_RETRY_HEADERS} @@ -356,6 +359,7 @@ file(GLOB AWS_NATIVE_SDK_NON_UNITY_SRC ${MONITORING_SOURCE} ${UTILS_CRYPTO_FACTORY_SOURCE} ${UTILS_JSON_SOURCE} + ${UTILS_LOCAL_SOURCE} ${UTILS_CBOR_SOURCE} ${UTILS_EVENT_SOURCE} ${UTILS_SOURCE} @@ -417,6 +421,7 @@ if(MSVC) source_group("Header Files\\aws\\core\\utils\\event" FILES ${UTILS_EVENT_HEADERS}) source_group("Header Files\\aws\\core\\utils\\exceptions" FILES ${UTILS_EXCEPTIONS_HEADERS}) source_group("Header Files\\aws\\core\\utils\\json" FILES ${UTILS_JSON_HEADERS}) + source_group("Header Files\\aws\\core\\utils\\local" FILES ${UTILS_LOCAL_HEADERS}) source_group("Header Files\\aws\\core\\utils\\cbor" FILES ${UTILS_CBOR_HEADERS}) source_group("Header Files\\aws\\core\\utils\\threading" FILES ${UTILS_THREADING_HEADERS}) source_group("Header Files\\aws\\core\\utils\\xml" FILES ${UTILS_XML_HEADERS}) @@ -487,6 +492,7 @@ if(MSVC) source_group("Source Files\\utils\\event" FILES ${UTILS_EVENT_SOURCE}) source_group("Source Files\\utils\\exceptions" FILES ${UTILS_EXCEPTIONS_SOURCE}) source_group("Source Files\\utils\\json" FILES ${UTILS_JSON_SOURCE}) + source_group("Source Files\\utils\\local" FILES ${UTILS_LOCAL_SOURCE}) source_group("Source Files\\utils\\cbor" FILES ${UTILS_CBOR_SOURCE}) source_group("Source Files\\utils\\threading" FILES ${UTILS_THREADING_SOURCE}) source_group("Source Files\\utils\\xml" FILES ${UTILS_XML_SOURCE}) diff --git a/src/aws-cpp-sdk-core/include/aws/core/utils/local/Random.h b/src/aws-cpp-sdk-core/include/aws/core/utils/local/Random.h new file mode 100644 index 000000000000..57b1734b555c --- /dev/null +++ b/src/aws-cpp-sdk-core/include/aws/core/utils/local/Random.h @@ -0,0 +1,17 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#pragma once +#include + +#include + +namespace Aws { +namespace Utils { + +AWS_CORE_LOCAL std::mt19937::result_type GetRandomValue(); + +} // namespace Utils +} // namespace Aws diff --git a/src/aws-cpp-sdk-core/source/client/RetryStrategy.cpp b/src/aws-cpp-sdk-core/source/client/RetryStrategy.cpp index 77b6f5abbbdf..2c3b43fc3936 100644 --- a/src/aws-cpp-sdk-core/source/client/RetryStrategy.cpp +++ b/src/aws-cpp-sdk-core/source/client/RetryStrategy.cpp @@ -3,11 +3,11 @@ * SPDX-License-Identifier: Apache-2.0. */ -#include - #include #include +#include #include +#include using namespace Aws::Utils::Threading; @@ -19,19 +19,11 @@ namespace Aws static const int RETRY_COST = 5; static const int TIMEOUT_RETRY_COST = 10; - StandardRetryStrategy::StandardRetryStrategy(long maxAttempts) : - m_retryQuotaContainer(Aws::MakeShared("StandardRetryStrategy")), - m_maxAttempts(maxAttempts) - { - srand((unsigned int)time(NULL)); - } + StandardRetryStrategy::StandardRetryStrategy(long maxAttempts) + : m_retryQuotaContainer(Aws::MakeShared("StandardRetryStrategy")), m_maxAttempts(maxAttempts) {} - StandardRetryStrategy::StandardRetryStrategy(std::shared_ptr retryQuotaContainer, long maxAttempts) : - m_retryQuotaContainer(retryQuotaContainer), - m_maxAttempts(maxAttempts) - { - srand((unsigned int)time(NULL)); - } + StandardRetryStrategy::StandardRetryStrategy(std::shared_ptr retryQuotaContainer, long maxAttempts) + : m_retryQuotaContainer(retryQuotaContainer), m_maxAttempts(maxAttempts) {} void StandardRetryStrategy::RequestBookkeeping(const HttpResponseOutcome& httpResponseOutcome) { @@ -64,7 +56,7 @@ namespace Aws { AWS_UNREFERENCED_PARAM(error); // Maximum left shift factor is capped by ceil(log2(max_delay)), to avoid wrap-around and overflow into negative values: - return (std::min)(rand() % 1000 * (1 << (std::min)(attemptedRetries, 15L)), 20000); + return std::min(static_cast(Aws::Utils::GetRandomValue() % 1000) * (1 << std::min(attemptedRetries, 15L)), 20000); } DefaultRetryQuotaContainer::DefaultRetryQuotaContainer() : m_retryQuota(INITIAL_RETRY_TOKENS) diff --git a/src/aws-cpp-sdk-core/source/utils/UUID.cpp b/src/aws-cpp-sdk-core/source/utils/UUID.cpp index 91737d409940..544363493bcb 100644 --- a/src/aws-cpp-sdk-core/source/utils/UUID.cpp +++ b/src/aws-cpp-sdk-core/source/utils/UUID.cpp @@ -3,16 +3,12 @@ * SPDX-License-Identifier: Apache-2.0. */ -#include #include #include +#include #include #include -#include -#include -#include -#include -#include +#include namespace Aws { @@ -91,33 +87,12 @@ namespace Aws return Aws::Utils::UUID(randomBytes); } -#ifdef UINT64_MAX - using MTEngine = std::mt19937_64; - using RandGenType = uint64_t; -#else - using MTEngine = std::mt19937; - using RandGenType = unsigned int; -#endif - - static size_t GetCurrentThreadRandomSeed() - { - static size_t processRandomSeed = std::random_device{}(); - static MTEngine threadRandomSeedGen(processRandomSeed); - // Threads can be re-used (esp. on OS X), generate a true random per-thread random seed - static std::mutex threadRandomSeedGenMtx; - std::unique_lock lock(threadRandomSeedGenMtx); - return static_cast(std::hash{}(std::this_thread::get_id()) ^ threadRandomSeedGen()); - } - Aws::Utils::UUID UUID::PseudoRandomUUID() { - static const thread_local size_t threadSeed = GetCurrentThreadRandomSeed(); - static thread_local MTEngine gen(threadSeed); - unsigned char randomBytes[UUID_BINARY_SIZE] = {0}; - for (size_t i = 0; i < UUID_BINARY_SIZE / sizeof(RandGenType); i++) { - reinterpret_cast(randomBytes)[i] = gen(); + for (size_t i = 0; i < UUID_BINARY_SIZE / sizeof(decltype(Aws::Utils::GetRandomValue())); i++) { + reinterpret_cast(randomBytes)[i] = GetRandomValue(); } //Set version bits to 0100 diff --git a/src/aws-cpp-sdk-core/source/utils/local/Random.cpp b/src/aws-cpp-sdk-core/source/utils/local/Random.cpp new file mode 100644 index 000000000000..118deae3ad30 --- /dev/null +++ b/src/aws-cpp-sdk-core/source/utils/local/Random.cpp @@ -0,0 +1,24 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include + +#include +#include +#include +#include + +std::mt19937::result_type Aws::Utils::GetRandomValue() { + static size_t const processRandomSeed = std::random_device{}(); + static std::mt19937 threadRandomSeedGen(processRandomSeed); + // Threads can be re-used (esp. on OS X), generate a true random per-thread random seed + static std::mutex threadRandomSeedGenMtx; + thread_local std::mt19937 gen([]() -> size_t { + std::unique_lock const lock(threadRandomSeedGenMtx); + return static_cast(std::hash{}(std::this_thread::get_id()) ^ threadRandomSeedGen()); + }()); + + return gen(); +}