summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qrandom.cpp
Commit message (Collapse)AuthorAgeFilesLines
* doc: Correct remaining qdoc warnings in qstring.cppMartin Smith2018-01-051-1/+1
| | | | | | | | | | | Added missing template clause to \fn commands required by clang-qdoc. Removed several obsolete \sa targets. Added a few missing return types to \fn commands. Also changed a } to ] in a \fn command in qrandom.cpp. Change-Id: I0f004a1e57507ff5fb035e822ca8b1baa7faad9b Reviewed-by: Topi Reiniö <topi.reinio@qt.io>
* doc: Correct remaining qdoc warnings in QRandomGeneratorMartin Smith2018-01-021-15/+8
| | | | | | | | | Due to the integration of clang in qdoc, several qdoc warnings appeared for class QRandomGenerator. This update corrects all the remaining qdoc warnings. Change-Id: I92fe9f7d9cb193e145ee8ad0e7198625b9a5bf7b Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Merge remote-tracking branch 'origin/5.10' into devLars Knoll2018-01-021-31/+54
|\ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Conflicts: .qmake.conf sc/corelib/io/qfsfileengine_p.h src/corelib/io/qstorageinfo_unix.cpp src/platformsupport/eglconvenience/qeglpbuffer_p.h src/platformsupport/input/libinput/qlibinputkeyboard.cpp src/platformsupport/input/libinput/qlibinputpointer.cpp src/plugins/platforms/cocoa/qcocoamenu.mm src/plugins/platforms/ios/qiosscreen.h src/plugins/platforms/ios/qioswindow.h src/plugins/platforms/ios/quiview.mm src/printsupport/dialogs/qpagesetupdialog_unix_p.h src/printsupport/dialogs/qprintpreviewdialog.cpp src/printsupport/widgets/qcupsjobwidget_p.h src/widgets/widgets/qmenu.cpp tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp Change-Id: Iecb4883122efe97ef0ed850271e6c51bab568e9c
| * Doc: attempt to fix some qdoc errors for QRandomGeneratorThiago Macieira2017-12-241-5/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | This fixes only those that look fixable. There are a number of errors in the online documentation that look like qdoc tool bugs, like missing functions (operator==) and documentation text that does not exist in the source code. This fixes: - QRandomGenerator(System) constructor showing up - Links for C++ reference documentation not showing up as links Change-Id: I9e2892cb6c374e93bcb7fffd14fe21db5a6969d9 Reviewed-by: Martin Smith <martin.smith@qt.io>
| * Merge remote-tracking branch 'origin/5.10.0' into 5.10Liang Qi2017-12-081-20/+20
| |\ | | | | | | | | | Change-Id: I6dcf85067ec226136c207ea69ca9d66736c84db5
| | * Improve readability of code that uses the Qt signed size typev5.10.0-rc2Simon Hausmann2017-11-281-20/+20
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | During the container BoF session at the Qt Contributor Summit 2017 the name of the signed size type became a subject of discussion in the context of readability of code using this type and the intention of using it for all length, size and count properties throughout the entire framework in future versions of Qt. This change proposes qsizetype as new name for qssize_t to emphasize the readability of code over POSIX compatibility, the former being potentially more relevant than the latter to the majority of users of Qt. Change-Id: Idb99cb4a8782703c054fa463a9e5af23a918e7f3 Reviewed-by: Samuel Gaist <samuel.gaist@edeltech.ch> Reviewed-by: David Faure <david.faure@kdab.com>
| * | Fix qdoc warnings for 5.10Friedemann Kleint2017-12-041-6/+27
| |/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | src/corelib/global/qrandom.cpp:915: warning: Cannot find 'bounded(...)' in '\fn' qreal QRandomGenerator::bounded(qreal highest) src/corelib/tools/qstring.cpp:774: warning: Command '\snippet (//! [qCompareStrings-QSV-QSV])' failed at end of file 'qstring/main.cpp' src/corelib/tools/qstring.cpp:5281: warning: Cannot find 'qTrimmed(...)' in '\fn' QStringView qTrimmed(QStringView s) src/corelib/tools/qstring.cpp:5281: warning: Cannot find 'qTrimmed(...)' in '\fn' QLatin1String qTrimmed(QLatin1String s) src/corelib/global/qrandom.h:171: warning: No documentation for 'QRandomGenerator::System' src/corelib/global/qrandom.h:105: warning: No documentation for 'QRandomGenerator::bounded(double highest)' src/corelib/global/qrandom.h:84: warning: No documentation for 'QRandomGenerator::generate64()' src/corelib/global/qrandom.h:77: warning: No documentation for 'QRandomGenerator::generate()' src/corelib/global/qrandom.cpp:799: warning: No such parameter 'sseq' in QRandomGenerator::seed() src/corelib/global/qrandom.cpp:1096: warning: Can't link to 'operator()()' src/corelib/tools/qstring.cpp:8982: warning: Can't link to 'qStartsWith()' src/corelib/tools/qstring.cpp:9203: warning: Can't link to 'qTrimmed()' src/corelib/tools/qstring.cpp:4798: warning: Can't link to 'qConvertToLatin1()' src/corelib/tools/qstring.cpp:4825: warning: Can't link to 'qConvertToLocal8Bit()' src/corelib/tools/qstring.cpp:4928: warning: Can't link to 'qConvertToUcs4()' src/corelib/tools/qstring.cpp:4884: warning: Can't link to 'qConvertToUtf8()' Change-Id: I5c7c89b230d3d1de8a679c10833319a470a44e80 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* / Replace qrand() engine with C++11 <random> LCG and deprecateThiago Macieira2017-11-301-83/+59
|/ | | | | | | | | | | | | | | | Instead of trying to adapt to whatever the C library may have and using QThreadLocalStorage, let's use a simple linear congruential generator engine from <random>. We can't use a single instance because qsrand() is documented to work per thread. I thought of using QRandomEngine, but had to make the choice between growing the QtCore code size and growing the per-thread data size. Code is sharable and is actually smaller than the sizeof(QRandomEngine), which is over 2500 bytes. sizeof(std::minstd_rand) is just sizeof(uint_fast32_t). Change-Id: I0a103569c81b4711a649fffd14ec8e641d02bf20 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* A brute-force solution to get QRandomGenerator build on IntegrityLiang Qi2017-11-151-2/+3
| | | | | | Task-number: QTBUG-64451 Change-Id: Ife11c3448f54609ba6e85a269a3b5376c43a075f Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QRandomGenerator: optimize the global() and system() storageThiago Macieira2017-11-111-55/+137
| | | | | | | | | | | | We store both in a single memory structure, instead of two local statics. By construction, we also ensure that the global PRNG mutex is in a different cacheline from the global PRNG state itself. Finally, we don't store the full system QRandomGenerator, since we only need the type member from it. Change-Id: Icaa86fc7b54d4b368c0efffd14eecc48ff05ec27 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* QRandomGenerator: add more of the std Random Engine APIThiago Macieira2017-11-111-9/+81
| | | | | | | | | | | This brings us to almost parity with the C++11 Random Engine API requirements (see chapter 26.5.1.4 [rand.req.eng]). We don't implement the templated Sseq requirements because it would require moving the implementation details to the public API. And we don't implement the <iostreams> code because we don't want to. Change-Id: Icaa86fc7b54d4b368c0efffd14f05ff813ebd759 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* QRandomGenerator: add securelySeeded(), to ensure appropriate seedingThiago Macieira2017-11-111-9/+51
| | | | | | | | | | | | | | | Since we don't document how many bytes one needs (it's 2496), it's difficult for the caller to provide just enough data in the seed sequence. Moreover, since std::mt19937 doesn't make it easy to provide the ideal size either, we can't actually write code that operates optimally given a quint32 range either -- we only provide it via std::seed_seq, which is inefficient. However, we can do it internally by passing QRandomGenerator to the std::mersenne_twister_engine constructor, as it's designed to work. Change-Id: Icaa86fc7b54d4b368c0efffd14f0613c10998321 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Change QRandomGenerator to have a deterministic modeThiago Macieira2017-11-111-261/+446
| | | | | | | | | Now only QRandomGenerator::system() will access the system-wide RNG, which we document to be cryptographically-safe and possibly backed by a true HWRNG. Everything else just wraps a Mersenne Twister. Change-Id: I0a103569c81b4711a649fffd14ec8cd3469425df Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* QRandomGenerator: remove the per-thread bufferThiago Macieira2017-11-111-64/+8
| | | | | | | | | | | Since we're adding a deterministic generator that inherently does not use syscalls, and people should really use that one by default, there is no point in optimizing the secure generator wrt syscalls. Besides, keeping the random data in memory for longer than needed is likely inadviseable. Change-Id: Ib17dde1a1dbb49a7bba8fffd14ed0871117fe930 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Fix unused variable warning when compiling in release mode with GCC 7Thiago Macieira2017-11-031-0/+2
| | | | | | | error: unused variable ‘ret’ [-Werror=unused-variable] Change-Id: I6e9274c1e7444ad48c81fffd14dcff9278d4376b Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Fix build on Integrity: the compiler doesn't understand this constructThiago Macieira2017-10-231-1/+5
| | | | | | | | | "global/qrandom.cpp", line 155: error #2000-D: attribute "destructor" is not implemented and will be ignored Task-number: QTBUG-63948 Change-Id: Icaa86fc7b54d4b368c0efffd14efa35381d4e797 Reviewed-by: Liang Qi <liang.qi@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* QRandomGenerator: fix the timing of the closing of the Unix random fdThiago Macieira2017-10-201-2/+2
| | | | | | | | | | Let's make it happen even later: at the time of QtCore's unloading from memory. This prevents issues with something using QRandomGenerator after the global static destructor would have run. Change-Id: Icaa86fc7b54d4b368c0efffd14eed56bbbb51cb6 Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io>
* configure: make C++11 <random> a required functionalityThiago Macieira2017-10-161-13/+6
| | | | | | | | | Error out if it's missing or broken (Mersenne Twister not present). This ensures that we never have a low-quality random generator in Qt. Change-Id: I0a103569c81b4711a649fffd14ec80649df7087e Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Doc: fix QRandomGenerator missing updates after renameThiago Macieira2017-10-141-10/+10
| | | | | | | | | Commit 282065d443c2a2d6b9e32d786c2c1c7552ba3cb5 renamed the generator functions but we didn't update all the docs. Change-Id: I0a103569c81b4711a649fffd14ec877ffbfe710d Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io>
* QRandomGenerator: improve internal noexceptnessThiago Macieira2017-10-141-5/+7
| | | | | | | | | There are a couple of Q_ASSERT wrapped by the new noexcepts, but most of those aren't validation of external parameters, only of internal construction. The two exceptions are the checks for pointer alignment. Change-Id: I0a103569c81b4711a649fffd14ec8523d741dfb6 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Fix element overwrite in QRandomGenerator's fallback functionEirik Aavitsland2017-09-281-1/+1
| | | | | | | | Copy into the intended array elements, not into the start of the array. Change-Id: I05c4ece2450583c9358aa1eddfd702262e995146 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QRandomGenerator: update API to better nameThiago Macieira2017-09-221-26/+43
| | | | | | | | | | | | "generate" is better than "get", and we already have "generate(it, it)" which uses std::generate(). This changes: - get32() → generate() - get64() → generate64() and QRandomGenerator64::generate() - getReal() → generateDouble() Change-Id: I6e1fe42ae4b742a7b811fffd14e5d7bd69abcdb3 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Merge remote-tracking branch 'origin/5.9' into devLiang Qi2017-08-311-36/+37
| | | | | | | | | | | | | | | | | | | | Conflicts: examples/examples.pro qmake/library/qmakebuiltins.cpp src/corelib/global/qglobal.cpp Re-apply b525ec2 to qrandom.cpp(code movement in 030782e) src/corelib/global/qnamespace.qdoc src/corelib/global/qrandom.cpp src/gui/kernel/qwindow.cpp Re-apply a3d59c7 to QWindowPrivate::setVisible() (code movement in d7a9e08) src/network/ssl/qsslkey_openssl.cpp src/plugins/platforms/android/androidjniinput.cpp src/plugins/platforms/xcb/qxcbconnection.cpp src/plugins/platforms/xcb/qxcbconnection_xi2.cpp src/widgets/widgets/qmenu.cpp tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp Change-Id: If7ab427804408877a93cbe02079fca58e568bfd3
* Doc: add warning against bulk random data creation to QRandomGeneratorSamuel Gaist2017-08-051-1/+4
| | | | | | | | | The full documentation explains it well but a more prominent warning will help avoid the wrong use of that class and encourage user to read further the documentation. Change-Id: I3178749f2b1b0350040f81eef253fd85c7ba0a5f Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Fix use of getentropy on larger blocksThiago Macieira2017-07-271-2/+16
| | | | | | | | | | | Found while working on suppressing the warning about the return value (which is either 0 or -1) was being ignored. Task-number: QTBUG-61968 Change-Id: I02d22222fff64d4dbda4fffd14d148b1724547ca Reviewed-by: Florian Bruhin <qt-project.org@the-compiler.org> Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Rename the "sys_auxv" feature to "getauxval"Thiago Macieira2017-07-061-2/+2
| | | | | Change-Id: I8d96dea9955d4c749b99fffd14cdbd1e69940d33 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io>
* QRandomGenerator: use getentropy on Linux & OpenBSDThiago Macieira2017-06-301-9/+38
| | | | | | | | | | | | | | | | | | | | | | | The getentropy function, first found in OpenBSD, is present in glibc since version 2.25 and Bionic since Android 6.0 and NDK r11. It uses the Linux 3.17 getrandom system call. Unlike glibc's getrandom() wrapper, the glibc implementation of getentropy() function is not a POSIX thread cancellation point, so we prefer to use that even though we have to break the reading into 256-byte blocks. The big advantage is that these functions work even in the absence of a /dev/urandom device node, in addition to a few cycles shaved off by not having to open a file descriptor and close it at exit. What's more, the glibc implementation blocks until entropy is available on early boot, so we don't have to worry about a failure mode. The Bionic implementation will fall back by itself to /dev/urandom and, failing that, gathering entropy from elsewhere in the system in a way it cannot fail either. uClibc has a wrapper to getrandom(2) but no getentropy(3). MUSL has neither. Change-Id: Ia53158e207a94bf49489fffd14c8cee1b968a619 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* QRandomGenerator: Rework the fallback seedingThiago Macieira2017-06-251-38/+58
| | | | | | | | | | | | | | | qdeadlinetimer.cpp and qelapsedtimer_*.cpp are not part of the bootstrap library. Instead of adding them there, just remove this entropy source from bootstrapped builds (instead of just qmake). To compensate, store all the bits instead of trying to combine them into just one 32-bit word. We've got a few new sources from the stack and libc, plus two more ELF auxvec values that the Linux kernel supplies (inspired by OpenBSD's getentropy_linux.c, which is used in Bionic). Task-number: QTBUG-61492 Change-Id: I1d5a585d4af842f9a66ffffd14c999ae8c44f46c Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Doc: add a note about QRandomGenerator returning the sign bit setThiago Macieira2017-06-201-0/+18
| | | | | | | | Found this in a few uses of qrand() that assumed the result would be non-negative. Change-Id: Ia53158e207a94bf49489fffd14c7c029515cf42c Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* QRandomGenerator: don't internally rely on QT_HAS_INCLUDEThiago Macieira2017-06-201-3/+3
| | | | | | | | | | | | | | GCC didn't support it until version 5 or 6, so add configure tests for both <random> and <sys/auxv.h>. Normally I'd say "upgrade", but this is too low-level and important a feature. There's a good chance that all our supported compilers have <random> anyway. As for <sys/auxv.h>, it's present on Glibc, Bionic and MUSL, but I don't see it in uClibc (AT_RANDOM is a Linux-specific feature). Change-Id: Ia3e896da908f42939148fffd14c5b2af491f7a77 Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* Fix some qdoc warnings for 5.10Friedemann Kleint2017-06-201-27/+27
| | | | | | | | | | | | | | | | Fix comments for QRandomGenerator. src/corelib/io/qprocess.cpp:453: warning: Cannot find 'startDetached(...)' in '\fn' bool QProcessPrivate::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory, qint64 *pid) src/corelib/tools/qstringiterator.qdoc:122: warning: Cannot find 'QStringIterator(...)' in '\fn' QStringIterator::QStringIterator(QStringView string, QStringView::size_type idx) src/corelib/global/qrandom.cpp:902: warning: Can't link to 'QRandomGenerator' src/sql/kernel/qsqlerror.cpp:123: warning: Unknown command '\other' src/network/kernel/qhostinfo.cpp:296: warning: Unknown command '\other' src/gui/kernel/qplatformcursor.cpp:97: warning: Cannot find 'QPlatformCursor::OverrideCursor' specified with '\enum' in any header file src/network/access/qnetworkreply.cpp:307: warning: Can't link to 'QNetworkAccessManager::setRedirectsPolicy()' src/network/access/qnetworkreply.cpp:307: warning: Can't link to 'QNetworkRequest::RedirectsPolicyAttribute' Change-Id: Ibc2455b1b657716dfb6192615738dc7f924dbab3 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QRandomGenerator: mix the Linux AT_RANDOM random bytes in the fallbackThiago Macieira2017-06-121-3/+26
| | | | | | | | | | | | | | | | | | The Linux kernel gives us 16 bytes of random data and sets a pointer to it in the ELF AuxV (the same one that allows us to get HWCAP on ARM systems). So if we end up in the fallback generator, at leat we'll get a good amount of entropy to seed the Mersenne Twister. This could happen if the application is run in a chroot(2) or container without /dev/random or /dev/urandom. That is probably an installation mistake, so we don't optimize this case for performance. With this commit, we have now good, high-quality fallbacks for Windows (rand_s), for BSDs (arc4random) and for Linux. The only missing, supported OS without a good entropy source is QNX. Change-Id: Ia3e896da908f42939148fffd14c5b1084051f1a8 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* QRandomGenerator: improve the fallback seeding in emergenciesThiago Macieira2017-06-121-1/+31
| | | | | | | | | | | | | | | | If we've never read any samples from hardware RNG or from /dev/urandom, then there are no stored samples for us to seed the Mersenne Twister. In that case, attempt to obtain an emergency sample that consists of some random bits from the variable addresses (ASLR should help) and from the clock (using nanosecond quality, instead of seconds). There's still the possibility that we'll get poor entropy: very close to boot, if the kernel entropy pool is empty, ASLR could be poor and the monotonic clock value could be consistent from boot to boot. There's nothing we can do about that. Change-Id: Ia3e896da908f42939148fffd14c5b0c7b608371b Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* QRandomGenerator: use arc4random as fallback on BSDs (including Darwin)Thiago Macieira2017-06-121-0/+7
| | | | | | | | | This function works even in chroots where /dev is not available. Since it's part of the OS, it may have better ways of gathering entropy even when we don't. Change-Id: Ia3e896da908f42939148fffd14c5afedc8569dbc Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* QRandomGenerator: add support for hardware-based random generatorsThiago Macieira2017-06-121-2/+46
| | | | | | | | | | | This commit adds support for the x86 RDRAND instruction for QRandomGenerator. This is the same that libstdc++-v3 uses for std::random_device() by default. If it fails because the hardware does not have enough entropy collected, we fall back to the operating system generator, which often has more entropy collected from other sources. Change-Id: Icd0e0d4b27cb4e5eb892fffd14b5167214e1ea3f Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Use QtPrivate::FunctionPointer to get the type of srand's first argThiago Macieira2017-06-121-7/+2
| | | | | | | Avoids #ifdefs and guesswork. Change-Id: Icd0e0d4b27cb4e5eb892fffd14b5210efaea7d00 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Move qrand() & qsrand() to qrandom.cppThiago Macieira2017-06-121-1/+140
| | | | | | | Now that we have the file, may as well consolidate Change-Id: Icd0e0d4b27cb4e5eb892fffd14b51d3e701c6a94 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Long live QRandomGeneratorThiago Macieira2017-06-121-0/+727
This class provides a reasonably-secure random number generator that does not need seeding. That is quite unlike qrand(), which requires a seed and is low-quality (definitely not secure). This class is also like std::random_device, but better. It provides an operator() like std::random_device, but unlike that, it also provides a way to fill a buffer with random data, not just one 32-bit quantity. It's also stateless. Finally, it also implements std::seed_seq-like generate(). It obeys the standard requirement of the range (32-bit) but not that of the algorithm (if you wanted that, you'd use std::seed_seq itself). Instead, generate() fills with pure random data. Change-Id: Icd0e0d4b27cb4e5eb892fffd14b4e3ba9ea04da8 Reviewed-by: Lars Knoll <lars.knoll@qt.io>