diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2021-04-02 17:25:12 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2021-05-23 12:08:43 -0700 |
commit | a5ff71578da5f6441ab8b07ae1fbf736d7f1e3ba (patch) | |
tree | 9c7dafcc4de56e2d26bf2f7ff2a795fd70ea6be3 /src/corelib/global | |
parent | 928ce402a41c291e6f746cac903a4a44702c4689 (diff) |
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 <qt_ci_bot@qt-project.org>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/corelib/global')
-rw-r--r-- | src/corelib/global/qrandom.cpp | 36 | ||||
-rw-r--r-- | src/corelib/global/qrandom.h | 5 | ||||
-rw-r--r-- | src/corelib/global/qrandom_p.h | 3 |
3 files changed, 16 insertions, 28 deletions
diff --git a/src/corelib/global/qrandom.cpp b/src/corelib/global/qrandom.cpp index cbc7551b6f..b699dcbece 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 <typename Generator, typename FillBufferType> -static qsizetype callFillBuffer(FillBufferType f, quintptr *v) +template <typename Generator, typename FillBufferType, typename T> +static qsizetype callFillBuffer(FillBufferType f, T *v) { if constexpr (std::is_member_function_pointer_v<FillBufferType>) { // 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<size_t *>(getauxval(AT_RANDOM)); - if (at_random_ptr) { - v = qFromUnaligned<quintptr>(at_random_ptr); - if (acceptableSeed(v)) - return v; - } + if (at_random_ptr) + return qFromUnaligned<QRandomGenerator::InitialRandomData>(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<Generator>(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 c59ae4d42d..bed873a7bc 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 9c90967df8..b8968c703e 100644 --- a/src/corelib/global/qrandom_p.h +++ b/src/corelib/global/qrandom_p.h @@ -52,6 +52,7 @@ // #include "qglobal_p.h" +#include <qrandom.h> #include <private/qsimd_p.h> 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 |