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 ((uint(qt_randomdevice_control.loadAcquire()) &
SkipSystemRNG) == 0) {
297 qsizetype bytesFilled =
298 fillBuffer(buffer + filled, (count - filled) * qsizetype(
sizeof(*buffer)));
299 filled += bytesFilled / qsizetype(
sizeof(*buffer));
302 fallback_update_seed(*buffer);
304 if (Q_UNLIKELY(filled != count)) {
306 fallback_fill(buffer + filled, count - filled);
336#if !defined(Q_OS_INTEGRITY)
345 Q_CONSTINIT
static SystemAndGlobalGenerators g;
346 static_assert(
sizeof(g) >
sizeof(QRandomGenerator64));
370 new (rng) QRandomGenerator{System{}};
373 new (&rng->storage.engine()) RandomEngine(self()->sys);
389 self()->globalPRNGMutex.unlock();
400
401
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
563
564
565
568
569
570
571
572
573
574
575
578
579
580
581
582
583
584
585
586
589
590
591
592
593
594
595
596
597
598
599
600
603
604
605
606
607
608
609
610
611
612
613
614
617
618
619
620
621
622
623
624
625
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
646
647
648
649
650
651
652
655
656
657
658
659
660
663
664
665
666
667
668
671
672
673
674
675
676
679
680
681
682
683
684
687
688
689
690
691
692
695
696
697
698
699
700
703
704
705
706
707
708
709
712
713
714
715
718
719
720
721
722
725
726
727
728
729
730
731
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
759
760
761
762
763
764
767
768
769
770
771
772
773
774
775
776
777
778
779
780
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
801
802
803
804
805
806
807
808
809
810
811
812
813
814
817
818
819
820
821
822
823
824
825
826
827
828
829
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
856
857
858
859
860
861
862
863
864
865
866
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
932
933
934
935
936
937
938
939
940
941
942
943
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
988
989
990
991
992
993
994
995
996
997
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1084
1085
1086
1087
1088
1089
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1107
1108
1109
1110
1111
1112
1114constexpr QRandomGenerator::Storage::Storage()
1121 : QRandomGenerator(s)
1158
1159
1160inline QRandomGenerator::QRandomGenerator(System)
1166QRandomGenerator::QRandomGenerator(
const QRandomGenerator &other)
1174 storage.engine() = other.storage.engine();
1178QRandomGenerator &QRandomGenerator::operator=(
const QRandomGenerator &other)
1181 qFatal(
"Attempted to overwrite a QRandomGenerator to system() or global().");
1185 storage.engine() = other.storage.engine();
1190QRandomGenerator::QRandomGenerator(
std::seed_seq &sseq)
noexcept
1196 new (&storage.engine()) RandomEngine(sseq);
1199QRandomGenerator::QRandomGenerator(
const quint32 *begin,
const quint32 *end)
1205 std::seed_seq s(begin, end);
1206 new (&storage.engine()) RandomEngine(s);
1209void QRandomGenerator::discard(
unsigned long long z)
1215 storage.engine().discard(z);
1218bool operator==(
const QRandomGenerator &rng1,
const QRandomGenerator &rng2)
1220 if (rng1.type != rng2.type)
1227 PRNGLocker locker(&rng1 == QRandomGenerator::global() ? &rng1 : &rng2);
1228 return rng1.storage.engine() == rng2.storage.engine();
1232
1233
1234
1235
1236
1237
1238
1239quint64 QRandomGenerator::_fillRange(
void *buffer, qptrdiff count)
1242 Q_ASSERT(quintptr(buffer) %
sizeof(quint32) == 0);
1247 quint32 *begin =
static_cast<quint32 *>(buffer ? buffer : &dummy);
1248 quint32 *end = begin + count;
1254 std::generate(begin, end, [
this]() {
return storage.engine()(); });
1257 if (end - begin == 1)
1259 return begin[0] | (quint64(begin[1]) << 32);
1264template <
typename Generator,
typename FillBufferType,
typename T>
1267 if constexpr (
std::is_member_function_pointer_v<FillBufferType>) {
1269 return (Generator::self().*f)(v,
sizeof(*v));
1272 return f(v,
sizeof(*v));
1277
1278
1279
1280
1281
1282
1283
1284
1285
1288#if QT_CONFIG(getauxval) && defined(AT_RANDOM)
1289 auto at_random_ptr =
reinterpret_cast<size_t *>(getauxval(AT_RANDOM));
1291 return qFromUnaligned<QRandomGenerator::InitialRandomData>(at_random_ptr);
1296 QRandomGenerator::InitialRandomData v;
1297 for (
int attempts = 16; attempts; --attempts) {
1299 auto fillBuffer = &Generator::fillBuffer;
1300 if (callFillBuffer<Generator>(fillBuffer, &v) !=
sizeof(v))
1306 quint32 data[
sizeof(v) /
sizeof(quint32)];
1307 fallback_fill(data,
std::size(data));
1308 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)