From a5ff71578da5f6441ab8b07ae1fbf736d7f1e3ba Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 2 Apr 2021 17:25:12 -0700 Subject: [PATCH] QRandomGenerator: let qt_initial_random_value() return 128 bits of data It's how much there is in Linux's AT_RANDOM block. I've also removed the check for validity. It's highly unlikely that 128 bits are bad. Change-Id: Id2983978ad544ff79911fffd16723161ea7ec315 Reviewed-by: Qt CI Bot Reviewed-by: Lars Knoll Reviewed-by: Allan Sandfeld Jensen --- src/corelib/global/qrandom.cpp | 36 ++++++++++------------------------ src/corelib/global/qrandom.h | 5 ++++- src/corelib/global/qrandom_p.h | 3 ++- src/corelib/tools/qhash.cpp | 3 ++- 4 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/corelib/global/qrandom.cpp b/src/corelib/global/qrandom.cpp index cbc7551b6fb..b699dcbece1 100644 --- a/src/corelib/global/qrandom.cpp +++ b/src/corelib/global/qrandom.cpp @@ -1296,8 +1296,8 @@ quint64 QRandomGenerator::_fillRange(void *buffer, qptrdiff count) // helper function to call fillBuffer, since we need something to be // argument-dependent -template -static qsizetype callFillBuffer(FillBufferType f, quintptr *v) +template +static qsizetype callFillBuffer(FillBufferType f, T *v) { if constexpr (std::is_member_function_pointer_v) { // member function, need an object @@ -1318,45 +1318,29 @@ static qsizetype callFillBuffer(FillBufferType f, quintptr *v) Note: on some systems, this functionn may rerturn the same value every time it is called. */ -quintptr qt_initial_random_value() noexcept +QRandomGenerator::InitialRandomData qt_initial_random_value() noexcept { - auto acceptableSeed = [](size_t v) { - // two values are reserved: 0 to indicate uninitialized and 1 to - // indicate deterministic seed - return Q_LIKELY(v > 1); - }; - quintptr v = 0; - #if QT_CONFIG(getauxval) && defined(AT_RANDOM) - // We actually have 16 bytes, but this will do auto at_random_ptr = reinterpret_cast(getauxval(AT_RANDOM)); - if (at_random_ptr) { - v = qFromUnaligned(at_random_ptr); - if (acceptableSeed(v)) - return v; - } + if (at_random_ptr) + return qFromUnaligned(at_random_ptr); #endif // bypass the hardware RNG, which would mean initializing qsimd.cpp + QRandomGenerator::InitialRandomData v; for (int attempts = 16; attempts; --attempts) { using Generator = QRandomGenerator::SystemGenerator; auto fillBuffer = &Generator::fillBuffer; if (callFillBuffer(fillBuffer, &v) != sizeof(v)) continue; - // check if it is static - if (acceptableSeed(v)) - return v; + return v; } - quint32 u32[2] = {}; - forever { - fallback_fill(u32, sizeof(v) / sizeof(quint32)); - v = u32[0] | (quint64(u32[1]) << 32); - if (acceptableSeed(v)) - break; - } + quint32 data[sizeof(v) / sizeof(quint32)]; + fallback_fill(data, std::size(data)); + memcpy(v.data, data, sizeof(v.data)); return v; } diff --git a/src/corelib/global/qrandom.h b/src/corelib/global/qrandom.h index c59ae4d42d2..bed873a7bcc 100644 --- a/src/corelib/global/qrandom.h +++ b/src/corelib/global/qrandom.h @@ -217,7 +217,10 @@ protected: private: Q_CORE_EXPORT quint64 _fillRange(void *buffer, qptrdiff count); - friend quintptr qt_initial_random_value() noexcept; + struct InitialRandomData { + quintptr data[16 / sizeof(quintptr)]; + }; + friend InitialRandomData qt_initial_random_value() noexcept; friend class QRandomGenerator64; struct SystemGenerator; struct SystemAndGlobalGenerators; diff --git a/src/corelib/global/qrandom_p.h b/src/corelib/global/qrandom_p.h index 9c90967df8a..b8968c703e9 100644 --- a/src/corelib/global/qrandom_p.h +++ b/src/corelib/global/qrandom_p.h @@ -52,6 +52,7 @@ // #include "qglobal_p.h" +#include #include QT_BEGIN_NAMESPACE @@ -80,7 +81,7 @@ static const struct } qt_randomdevice_control; #endif -quintptr qt_initial_random_value() noexcept; +QRandomGenerator::InitialRandomData qt_initial_random_value() noexcept; QT_END_NAMESPACE diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index 380bc0d7eae..dcb90a530d7 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -743,7 +743,8 @@ static size_t qt_create_qhash_seed(HashCreationMode mode) } seed = 1; // QHashSeed::globalSeed subtracts 1 } else if (mode == Initial) { - seed = qt_initial_random_value(); + auto data = qt_initial_random_value(); + seed = data.data[0] ^ data.data[1]; } else if (sizeof(seed) > sizeof(uint)) { seed = QRandomGenerator::system()->generate64(); } else {