From 30d40a889003b7a9e54aa07031ad404569d1b6e2 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 27 Sep 2024 09:52:15 -0700 Subject: [PATCH] Remove dead code in checkRdrndWorks() if __RDRND__ is defined The only place where we call qRandomCpu() in qrandom.cpp is guarded by a check using qCpuHasFeature(): if (qHasHwrng() && (uint(qt_randomdevice_control.loadAcquire()) & SkipHWRNG) == 0) filled += qRandomCpu(buffer, count); static inline bool qHasHwrng() { return qCpuHasFeature(RDRND); } Since qCpuHasFeature() also checks if __RDRND__ was defined, we ignore the result of checkRdrndWorks(). Users of CPUs without RNG must compile with an -march= flag that doesn't enable it or must pass -mno-rdrnd to keep the runtime detection code. Task-number: QTBUG-69423 Task-number: QTBUG-129193 Change-Id: Ib7819f80041fe330d033fffda7e7c160141d7dd8 Reviewed-by: Allan Sandfeld Jensen (cherry picked from commit 6543f50536f3547b43a27b32af59d68f28a60247) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/global/qsimd.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/corelib/global/qsimd.cpp b/src/corelib/global/qsimd.cpp index 5e0e8380d49..201afcc7e8a 100644 --- a/src/corelib/global/qsimd.cpp +++ b/src/corelib/global/qsimd.cpp @@ -720,6 +720,12 @@ static bool checkRdrndWorks() noexcept constexpr qsizetype TestBufferSize = 4; unsigned testBuffer[TestBufferSize] = {}; + // But if the RDRND feature was statically enabled by the compiler, we + // assume that the RNG works. That's because the calls to qRandomCpu() will + // be guarded by qCpuHasFeature(RDRND) and that will be a constant true. + if (_compilerCpuFeatures & CpuFeatureRDRND) + return true; + unsigned *end = qt_random_rdrnd(testBuffer, testBuffer + TestBufferSize); if (end < testBuffer + 3) { // Random generation didn't produce enough data for us to make a