From 5839714d986f28412c9f9ed4801d1bf9378f7b51 Mon Sep 17 00:00:00 2001 From: Dmitry Kazakov Date: Thu, 5 Sep 2019 10:23:08 +0300 Subject: Fix QRandomGenerator initialization on AMD CPUs Some AMD CPUs (e.g. AMD A4-6250J and AMD Ryzen 3000-series) have a failing random generation instruction, which always returns 0xffffffff, even when generation was "successful". This code checks if hardware random generator generates four consecutive equal numbers. If it does, then we probably have a failing one and should disable it completely. Change-Id: I38c87920ca2e8cce4143afbff5e453ce3845d11a Fixes: QTBUG-69423 Reviewed-by: Edward Welbourne --- src/corelib/tools/qsimd_p.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/corelib/tools/qsimd_p.h') diff --git a/src/corelib/tools/qsimd_p.h b/src/corelib/tools/qsimd_p.h index c36e1e484f..d603631a24 100644 --- a/src/corelib/tools/qsimd_p.h +++ b/src/corelib/tools/qsimd_p.h @@ -346,6 +346,15 @@ extern Q_CORE_EXPORT QBasicAtomicInteger qt_cpu_features[2]; #endif Q_CORE_EXPORT quint64 qDetectCpuFeatures(); +#if defined(Q_PROCESSOR_X86) && QT_COMPILER_SUPPORTS_HERE(RDRND) +Q_CORE_EXPORT qsizetype qRandomCpu(void *, qsizetype) Q_DECL_NOTHROW; +#else +static inline qsizetype qRandomCpu(void *, qsizetype) Q_DECL_NOTHROW +{ + return 0; +} +#endif + static inline quint64 qCpuFeatures() { quint64 features = qt_cpu_features[0].load(); @@ -362,6 +371,15 @@ static inline quint64 qCpuFeatures() #define qCpuHasFeature(feature) (((qCompilerCpuFeatures & CpuFeature ## feature) == CpuFeature ## feature) \ || ((qCpuFeatures() & CpuFeature ## feature) == CpuFeature ## feature)) +inline bool qHasHwrng() +{ +#if defined(Q_PROCESSOR_X86) && QT_COMPILER_SUPPORTS_HERE(RDRND) + return qCpuHasFeature(RDRND); +#else + return false; +#endif +} + #define ALIGNMENT_PROLOGUE_16BYTES(ptr, i, length) \ for (; i < static_cast(qMin(static_cast(length), ((4 - ((reinterpret_cast(ptr) >> 2) & 0x3)) & 0x3))); ++i) -- cgit v1.2.3