From 1921783936e7fd4382c0c0c4536838b5ac75c00a Mon Sep 17 00:00:00 2001 From: Mikhail Paulyshka Date: Tue, 11 Mar 2025 00:35:40 +0300 Subject: [PATCH] qsimd: perform RDSEED sanity check in case if available Fixes: QTBUG-134538 Pick-to: 6.8 6.5 Change-Id: Iab51337b9b27ccf48a0e60c08cf14706112b6235 Reviewed-by: Thiago Macieira (cherry picked from commit 1b8d08532227866409088c6c45e215acfbf33cc7) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/global/qsimd.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/corelib/global/qsimd.cpp b/src/corelib/global/qsimd.cpp index 732ce3cf30c..f1a1e5b1358 100644 --- a/src/corelib/global/qsimd.cpp +++ b/src/corelib/global/qsimd.cpp @@ -260,7 +260,7 @@ static inline quint64 detectProcessorFeatures() X86_BASELINE "," QT_FUNCTION_TARGET_STRING_RDRND #endif -static bool checkRdrndWorks() noexcept; +static bool checkRdrndWorks(quint64) noexcept; QT_FUNCTION_TARGET_BASELINE static int maxBasicCpuidSupported() @@ -466,7 +466,7 @@ static quint64 detectProcessorFeatures() features &= ~req.cpu_features; } - if (features & CpuFeatureRDRND && !checkRdrndWorks()) + if (features & CpuFeatureRDRND && !checkRdrndWorks(features)) features &= ~(CpuFeatureRDRND | CpuFeatureRDSEED); return features; @@ -778,7 +778,7 @@ out: } QT_FUNCTION_TARGET(BASELINE_RDRND) Q_DECL_COLD_FUNCTION -static bool checkRdrndWorks() noexcept +static bool checkRdrndWorks(quint64 features) noexcept { /* * Some AMD CPUs (e.g. AMD A4-6250J and AMD Ryzen 3000-series) have a @@ -800,7 +800,12 @@ static bool checkRdrndWorks() noexcept if (_compilerCpuFeatures & CpuFeatureRDRND) return true; - unsigned *end = qt_random_rdrnd(testBuffer, testBuffer + TestBufferSize); + unsigned *end; + if (features & CpuFeatureRDSEED) + end = qt_random_rdseed(testBuffer, testBuffer + TestBufferSize); + else + end = qt_random_rdrnd(testBuffer, testBuffer + TestBufferSize); + if (end < testBuffer + 3) { // Random generation didn't produce enough data for us to make a // determination whether it's working or not. Assume it isn't, but @@ -838,7 +843,7 @@ QT_FUNCTION_TARGET(RDRND) qsizetype qRandomCpu(void *buffer, qsizetype count) no return ptr - reinterpret_cast(buffer); } #elif defined(Q_PROCESSOR_X86) && !defined(Q_PROCESSOR_ARM) -static bool checkRdrndWorks() noexcept { return false; } +static bool checkRdrndWorks(quint64) noexcept { return false; } #endif // Q_PROCESSOR_X86 && RDRND #if QT_SUPPORTS_INIT_PRIORITY