12#include <qobjectdefs.h>
16#if QT_CONFIG(getauxval)
20#if QT_CONFIG(getentropy) && __has_include(<sys/random.h>)
21# include <sys/random.h>
22#elif !QT_CONFIG(getentropy) && (!defined(Q_OS_BSD4) || defined(__GLIBC__)) && !defined(Q_OS_WIN)
23# include "qdeadlinetimer.h"
24# include "qhashfunctions.h"
29# include <private/qcore_unix_p.h>
31# include <qt_windows.h>
38DECLSPEC_IMPORT BOOLEAN WINAPI SystemFunction036(PVOID RandomBuffer, ULONG RandomBufferLength);
48#define Q_ASSERT_X(cond, x, msg) assert(cond && msg)
49#if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
58 FillBufferNoexcept =
true
61#if defined(QT_BUILD_INTERNAL)
62QBasicAtomicInteger<uint> qt_randomdevice_control = Q_BASIC_ATOMIC_INITIALIZER(0U);
67#if QT_CONFIG(getentropy)
86#elif defined(Q_OS_UNIX)
137#elif defined(Q_OS_WIN)
154 static_assert(
sizeof(T) >=
sizeof(quint32));
155 if (
sizeof(T) ==
sizeof(quint32)) {
157 generate(
reinterpret_cast<quint32 *>(begin),
reinterpret_cast<quint32 *>(end));
160 std::generate(begin, end, [
this]() {
162 generate(&datum, &datum + 1);
170static void fallback_update_seed(
unsigned) {}
171static void fallback_fill(quint32 *ptr, qsizetype left)
noexcept
175 std::generate(ptr, ptr + left, []() {
181#elif QT_CONFIG(getentropy)
182static void fallback_update_seed(
unsigned) {}
183static void fallback_fill(quint32 *, qsizetype)
noexcept
188#elif defined(Q_OS_BSD4) && !defined(__GLIBC__
)
189static void fallback_update_seed(
unsigned) {}
190static void fallback_fill(quint32 *ptr, qsizetype left)
noexcept
193 arc4random_buf(ptr, left *
sizeof(*ptr));
196Q_CONSTINIT
static QBasicAtomicInteger<
unsigned> seed = Q_BASIC_ATOMIC_INITIALIZER(0U);
202 seed.fetchAndXorRelaxed(value);
212 quint32 *end = scratch;
214 auto foldPointer = [](quintptr v) {
215 if (
sizeof(quintptr) ==
sizeof(quint32)) {
223 return quint32(v >> (32 - 24));
229 *end++ = foldPointer(quintptr(&seed));
230 *end++ = foldPointer(quintptr(&scratch));
231 *end++ = foldPointer(quintptr(&errno));
232 *end++ = foldPointer(quintptr(
reinterpret_cast<
void*>(strerror)));
234#ifndef QT_BOOTSTRAPPED
235 quint64 nsecs = QDeadlineTimer::current(Qt::PreciseTimer).deadline();
236 *end++ = quint32(nsecs);
239 if (quint32 v = seed.loadRelaxed())
242#if QT_CONFIG(getauxval)
247 ulong auxvSeed = getauxval(AT_RANDOM);
249 memcpy(end,
reinterpret_cast<
void *>(auxvSeed), 16);
259 ulong base = getauxval(AT_BASE);
261 *end++ = foldPointer(base);
263# ifdef AT_SYSINFO_EHDR
265 ulong sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
267 *end++ = foldPointer(sysinfo_ehdr);
274 std::seed_seq sseq(scratch, end);
275 std::mt19937 generator(sseq);
276 std::generate(ptr, ptr + left, generator);
278 fallback_update_seed(*ptr);
285 quint32 *buffer = begin;
286 qsizetype count = end - begin;
288 if (Q_UNLIKELY(uint(qt_randomdevice_control.loadAcquire()) &
SetRandomData)) {
289 uint value = uint(qt_randomdevice_control.loadAcquire()) &
RandomDataMask;
290 std::fill_n(buffer, count, value);
294 qsizetype filled = 0;
295 if (qHasHwrng() && (uint(qt_randomdevice_control.loadAcquire()) &
SkipHWRNG) == 0)
296 filled += qRandomCpu(buffer, count);
298 if (filled != count && (uint(qt_randomdevice_control.loadAcquire()) &
SkipSystemRNG) == 0) {
299 qsizetype bytesFilled =
300 fillBuffer(buffer + filled, (count - filled) * qsizetype(
sizeof(*buffer)));
301 filled += bytesFilled / qsizetype(
sizeof(*buffer));
304 fallback_update_seed(*buffer);
306 if (Q_UNLIKELY(filled != count)) {
308 fallback_fill(buffer + filled, count - filled);
338#if !defined(Q_OS_INTEGRITY)
347 Q_CONSTINIT
static SystemAndGlobalGenerators g;
348 static_assert(
sizeof(g) >
sizeof(QRandomGenerator64));
372 new (rng) QRandomGenerator{System{}};
375 new (&rng->storage.engine()) RandomEngine(self()->sys);
391 self()->globalPRNGMutex.unlock();
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
565
566
567
570
571
572
573
574
575
576
577
580
581
582
583
584
585
586
587
588
591
592
593
594
595
596
597
598
599
600
601
602
605
606
607
608
609
610
611
612
613
614
615
616
619
620
621
622
623
624
625
626
627
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
648
649
650
651
652
653
654
657
658
659
660
661
662
665
666
667
668
669
670
673
674
675
676
677
678
681
682
683
684
685
686
689
690
691
692
693
694
697
698
699
700
701
702
705
706
707
708
709
710
711
714
715
716
717
720
721
722
723
724
727
728
729
730
731
732
733
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
761
762
763
764
765
766
769
770
771
772
773
774
775
776
777
778
779
780
781
782
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
803
804
805
806
807
808
809
810
811
812
813
814
815
816
819
820
821
822
823
824
825
826
827
828
829
830
831
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
858
859
860
861
862
863
864
865
866
867
868
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
934
935
936
937
938
939
940
941
942
943
944
945
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
990
991
992
993
994
995
996
997
998
999
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1086
1087
1088
1089
1090
1091
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1109
1110
1111
1112
1113
1114
1116constexpr QRandomGenerator::Storage::Storage()
1123 : QRandomGenerator(s)
1160
1161
1162inline QRandomGenerator::QRandomGenerator(System)
1168QRandomGenerator::QRandomGenerator(
const QRandomGenerator &other)
1176 storage.engine() = other.storage.engine();
1180QRandomGenerator &QRandomGenerator::operator=(
const QRandomGenerator &other)
1182 if (Q_UNLIKELY(
this == system()) || Q_UNLIKELY(
this == SystemAndGlobalGenerators::globalNoInit()))
1183 qFatal(
"Attempted to overwrite a QRandomGenerator to system() or global().");
1187 storage.engine() = other.storage.engine();
1192QRandomGenerator::QRandomGenerator(
std::seed_seq &sseq)
noexcept
1198 new (&storage.engine()) RandomEngine(sseq);
1201QRandomGenerator::QRandomGenerator(
const quint32 *begin,
const quint32 *end)
1207 std::seed_seq s(begin, end);
1208 new (&storage.engine()) RandomEngine(s);
1211void QRandomGenerator::discard(
unsigned long long z)
1217 storage.engine().discard(z);
1220bool operator==(
const QRandomGenerator &rng1,
const QRandomGenerator &rng2)
1222 if (rng1.type != rng2.type)
1229 PRNGLocker locker(&rng1 == QRandomGenerator::global() ? &rng1 : &rng2);
1230 return rng1.storage.engine() == rng2.storage.engine();
1234
1235
1236
1237
1238
1239
1240
1241quint64 QRandomGenerator::_fillRange(
void *buffer, qptrdiff count)
1244 Q_ASSERT(quintptr(buffer) %
sizeof(quint32) == 0);
1249 quint32 *begin =
static_cast<quint32 *>(buffer ? buffer : &dummy);
1250 quint32 *end = begin + count;
1256 std::generate(begin, end, [
this]() {
return storage.engine()(); });
1259 if (end - begin == 1)
1261 return begin[0] | (quint64(begin[1]) << 32);
1266template <
typename Generator,
typename FillBufferType,
typename T>
1269 if constexpr (
std::is_member_function_pointer_v<FillBufferType>) {
1271 return (Generator::self().*f)(v,
sizeof(*v));
1274 return f(v,
sizeof(*v));
1279
1280
1281
1282
1283
1284
1285
1286
1287
1290#if QT_CONFIG(getauxval) && defined(AT_RANDOM)
1291 auto at_random_ptr =
reinterpret_cast<size_t *>(getauxval(AT_RANDOM));
1293 return qFromUnaligned<QRandomGenerator::InitialRandomData>(at_random_ptr);
1298 QRandomGenerator::InitialRandomData v;
1299 for (
int attempts = 16; attempts; --attempts) {
1301 auto fillBuffer = &Generator::fillBuffer;
1302 if (callFillBuffer<Generator>(fillBuffer, &v) !=
sizeof(v))
1308 quint32 data[
sizeof(v) /
sizeof(quint32)];
1309 fallback_fill(data,
std::size(data));
1310 memcpy(v.data, data,
sizeof(v.data));
static qsizetype callFillBuffer(FillBufferType f, T *v)
static Q_NEVER_INLINE void fallback_fill(quint32 *ptr, qsizetype left) noexcept
static void fallback_update_seed(unsigned value)
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
QRandomGenerator::InitialRandomData qt_initial_random_value() noexcept
constexpr SystemAndGlobalGenerators()
static QRandomGenerator64 * system()
static void securelySeed(QRandomGenerator *rng)
QBasicMutex globalPRNGMutex
static SystemAndGlobalGenerators * self()
static QRandomGenerator64 * globalNoInit()
uchar data[sizeof(QRandomGenerator64)]
void generate(quint32 *begin, quint32 *end) noexcept(FillBufferNoexcept)
static SystemGenerator & self()
void generate(T *begin, T *end)