13#include <qobjectdefs.h>
17#if QT_CONFIG(getauxval)
21#if QT_CONFIG(getentropy) && __has_include(<sys/random.h>)
22# include <sys/random.h>
23#elif !QT_CONFIG(getentropy) && (!defined(Q_OS_BSD4) || defined(__GLIBC__)) && !defined(Q_OS_WIN)
24# include "qdeadlinetimer.h"
25# include "qhashfunctions.h"
30# include <private/qcore_unix_p.h>
32# include <qt_windows.h>
39DECLSPEC_IMPORT BOOLEAN WINAPI SystemFunction036(PVOID RandomBuffer, ULONG RandomBufferLength);
49#define Q_ASSERT_X(cond, x, msg) assert(cond && msg)
50#if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
59 FillBufferNoexcept =
true
62#if defined(QT_BUILD_INTERNAL)
63QBasicAtomicInteger<uint> qt_randomdevice_control = Q_BASIC_ATOMIC_INITIALIZER(0U);
68#if QT_CONFIG(getentropy)
87#elif defined(Q_OS_UNIX)
138#elif defined(Q_OS_WIN)
155 static_assert(
sizeof(T) >=
sizeof(quint32));
156 if (
sizeof(T) ==
sizeof(quint32)) {
158 generate(
reinterpret_cast<quint32 *>(begin),
reinterpret_cast<quint32 *>(end));
161 std::generate(begin, end, [
this]() {
163 generate(&datum, &datum + 1);
171static void fallback_update_seed(
unsigned) {}
172static void fallback_fill(quint32 *ptr, qsizetype left)
noexcept
176 std::generate(ptr, ptr + left, []() {
182#elif QT_CONFIG(getentropy)
183static void fallback_update_seed(
unsigned) {}
184static void fallback_fill(quint32 *, qsizetype)
noexcept
189#elif defined(Q_OS_BSD4) && !defined(__GLIBC__
)
190static void fallback_update_seed(
unsigned) {}
191static void fallback_fill(quint32 *ptr, qsizetype left)
noexcept
194 arc4random_buf(ptr, left *
sizeof(*ptr));
197Q_CONSTINIT
static QBasicAtomicInteger<
unsigned> seed = Q_BASIC_ATOMIC_INITIALIZER(0U);
203 seed.fetchAndXorRelaxed(value);
213 quint32 *end = scratch;
215 auto foldPointer = [](quintptr v) {
216 if (
sizeof(quintptr) ==
sizeof(quint32)) {
224 return quint32(v >> (32 - 24));
230 *end++ = foldPointer(quintptr(&seed));
231 *end++ = foldPointer(quintptr(&scratch));
232 *end++ = foldPointer(quintptr(&errno));
233 *end++ = foldPointer(quintptr(
reinterpret_cast<
void*>(strerror)));
235#ifndef QT_BOOTSTRAPPED
236 quint64 nsecs = QDeadlineTimer::current(Qt::PreciseTimer).deadline();
237 *end++ = quint32(nsecs);
240 if (quint32 v = seed.loadRelaxed())
243#if QT_CONFIG(getauxval)
248 ulong auxvSeed = getauxval(AT_RANDOM);
250 memcpy(end,
reinterpret_cast<
void *>(auxvSeed), 16);
260 ulong base = getauxval(AT_BASE);
262 *end++ = foldPointer(base);
264# ifdef AT_SYSINFO_EHDR
266 ulong sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
268 *end++ = foldPointer(sysinfo_ehdr);
275 std::seed_seq sseq(scratch, end);
276 std::mt19937 generator(sseq);
277 std::generate(ptr, ptr + left, generator);
279 fallback_update_seed(*ptr);
286 quint32 *buffer = begin;
287 qsizetype count = end - begin;
289 if (Q_UNLIKELY(uint(qt_randomdevice_control.loadAcquire()) &
SetRandomData)) {
290 uint value = uint(qt_randomdevice_control.loadAcquire()) &
RandomDataMask;
291 std::fill_n(buffer, count, value);
295 qsizetype filled = 0;
296 if (qHasHwrng() && (uint(qt_randomdevice_control.loadAcquire()) &
SkipHWRNG) == 0)
297 filled += qRandomCpu(buffer, count);
299 if (filled != count && (uint(qt_randomdevice_control.loadAcquire()) &
SkipSystemRNG) == 0) {
300 qsizetype bytesFilled =
301 fillBuffer(buffer + filled, (count - filled) * qsizetype(
sizeof(*buffer)));
302 filled += bytesFilled / qsizetype(
sizeof(*buffer));
305 fallback_update_seed(*buffer);
307 if (Q_UNLIKELY(filled != count)) {
309 fallback_fill(buffer + filled, count - filled);
339#if !defined(Q_OS_INTEGRITY)
348 Q_CONSTINIT
static SystemAndGlobalGenerators g;
349 static_assert(
sizeof(g) >
sizeof(QRandomGenerator64));
373 new (rng) QRandomGenerator{System{}};
376 new (&rng->storage.engine()) RandomEngine(self()->sys);
392 self()->globalPRNGMutex.unlock();
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
563
566
567
568
571
572
573
574
575
576
577
578
581
582
583
584
585
586
587
588
589
592
593
594
595
596
597
598
599
600
601
602
603
606
607
608
609
610
611
612
613
614
615
616
617
620
621
622
623
624
625
626
627
628
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
649
650
651
652
653
654
655
658
659
660
661
662
663
666
667
668
669
670
671
674
675
676
677
678
679
682
683
684
685
686
687
690
691
692
693
694
695
698
699
700
701
702
703
706
707
708
709
710
711
712
715
716
717
718
721
722
723
724
725
728
729
730
731
732
733
734
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
762
763
764
765
766
767
770
771
772
773
774
775
776
777
778
779
780
781
782
783
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
804
805
806
807
808
809
810
811
812
813
814
815
816
817
820
821
822
823
824
825
826
827
828
829
830
831
832
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
859
860
861
862
863
864
865
866
867
868
869
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
935
936
937
938
939
940
941
942
943
944
945
946
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
991
992
993
994
995
996
997
998
999
1000
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1087
1088
1089
1090
1091
1092
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1110
1111
1112
1113
1114
1115
1117constexpr QRandomGenerator::Storage::Storage()
1124 : QRandomGenerator(s)
1161
1162
1163inline QRandomGenerator::QRandomGenerator(System)
1169QRandomGenerator::QRandomGenerator(
const QRandomGenerator &other)
1177 storage.engine() = other.storage.engine();
1181QRandomGenerator &QRandomGenerator::operator=(
const QRandomGenerator &other)
1183 if (Q_UNLIKELY(
this == system()) || Q_UNLIKELY(
this == SystemAndGlobalGenerators::globalNoInit()))
1184 qFatal(
"Attempted to overwrite a QRandomGenerator to system() or global().");
1188 storage.engine() = other.storage.engine();
1193QRandomGenerator::QRandomGenerator(
std::seed_seq &sseq)
noexcept
1199 new (&storage.engine()) RandomEngine(sseq);
1202QRandomGenerator::QRandomGenerator(
const quint32 *begin,
const quint32 *end)
1208 std::seed_seq s(begin, end);
1209 new (&storage.engine()) RandomEngine(s);
1212void QRandomGenerator::discard(
unsigned long long z)
1218 storage.engine().discard(z);
1221bool operator==(
const QRandomGenerator &rng1,
const QRandomGenerator &rng2)
1223 if (rng1.type != rng2.type)
1230 PRNGLocker locker(&rng1 == QRandomGenerator::global() ? &rng1 : &rng2);
1231 return rng1.storage.engine() == rng2.storage.engine();
1235
1236
1237
1238
1239
1240
1241
1242quint64 QRandomGenerator::_fillRange(
void *buffer, qptrdiff count)
1245 Q_ASSERT(quintptr(buffer) %
sizeof(quint32) == 0);
1250 quint32 *begin =
static_cast<quint32 *>(buffer ? buffer : &dummy);
1251 quint32 *end = begin + count;
1257 std::generate(begin, end, [
this]() {
return storage.engine()(); });
1260 if (end - begin == 1)
1262 return begin[0] | (quint64(begin[1]) << 32);
1267template <
typename Generator,
typename FillBufferType,
typename T>
1270 if constexpr (
std::is_member_function_pointer_v<FillBufferType>) {
1272 return (Generator::self().*f)(v,
sizeof(*v));
1275 return f(v,
sizeof(*v));
1280
1281
1282
1283
1284
1285
1286
1287
1288
1291#if QT_CONFIG(getauxval) && defined(AT_RANDOM)
1292 auto at_random_ptr =
reinterpret_cast<size_t *>(getauxval(AT_RANDOM));
1294 return qFromUnaligned<QRandomGenerator::InitialRandomData>(at_random_ptr);
1299 QRandomGenerator::InitialRandomData v;
1300 for (
int attempts = 16; attempts; --attempts) {
1302 auto fillBuffer = &Generator::fillBuffer;
1303 if (callFillBuffer<Generator>(fillBuffer, &v) !=
sizeof(v))
1309 quint32 data[
sizeof(v) /
sizeof(quint32)];
1310 fallback_fill(data,
std::size(data));
1311 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)