comphelper/source/misc/random.cxx | 34 ++++++++++++++++++---------------- desktop/source/lib/init.cxx | 4 ++++ include/comphelper/random.hxx | 2 ++ 3 files changed, 24 insertions(+), 16 deletions(-)
New commits: commit 6d9228d6b14d968fa92df3ca018a555f8652e579 Author: Michael Meeks <michael.me...@collabora.com> AuthorDate: Fri May 3 14:17:27 2024 +0100 Commit: Michael Meeks <michael.me...@collabora.com> CommitDate: Fri May 3 19:54:47 2024 +0200 lok: reseed comphelper's random number generator on fork. Also avoid std::random_device it doesn't work in a COOL kit process. Change-Id: Ie2d063611a73e734afd92d6fd779f34a2f316230 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/167058 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com> Reviewed-by: Michael Meeks <michael.me...@collabora.com> Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com> diff --git a/comphelper/source/misc/random.cxx b/comphelper/source/misc/random.cxx index 96d466641dfb..b8358344e5b0 100644 --- a/comphelper/source/misc/random.cxx +++ b/comphelper/source/misc/random.cxx @@ -16,6 +16,7 @@ #include <time.h> #include <mutex> #include <random> +#include <rtl/random.h> #include <stdexcept> #if defined HAVE_VALGRIND_HEADERS #include <valgrind/memcheck.h> @@ -40,7 +41,9 @@ struct RandomNumberGenerator { std::mutex mutex; STD_RNG_ALGO global_rng; - RandomNumberGenerator() + RandomNumberGenerator() { reseed(); } + + void reseed() { // make RR easier to use, breaks easily without the RNG being repeatable bool bRepeatable = (getenv("SAL_RAND_REPEATABLE") != nullptr) || (getenv("RR") != nullptr); @@ -56,21 +59,18 @@ struct RandomNumberGenerator return; } - try - { - std::random_device rd; - // initialises the state of the global random number generator - // should only be called once. - // (note, a few std::variate_generator<> (like normal) have their - // own state which would need a reset as well to guarantee identical - // sequence of numbers, e.g. via myrand.distribution().reset()) - global_rng.seed(rd() ^ time(nullptr)); - } - catch (std::runtime_error& e) - { - SAL_WARN("comphelper", "Using std::random_device failed: " << e.what()); - global_rng.seed(time(nullptr)); - } + size_t seed = -1; + rtlRandomPool aRandomPool = rtl_random_createPool(); + if (rtl_random_getBytes(aRandomPool, &seed, sizeof(seed)) != rtl_Random_E_None) + seed = -1; + rtl_random_destroyPool(aRandomPool); + + // initialises the state of the global random number generator + // should only be called once. + // (note, a few std::variate_generator<> (like normal) have their + // own state which would need a reset as well to guarantee identical + // sequence of numbers, e.g. via myrand.distribution().reset()) + global_rng.seed(seed ^ time(nullptr)); } }; @@ -81,6 +81,8 @@ RandomNumberGenerator& GetTheRandomNumberGenerator() } } +void reseed() { GetTheRandomNumberGenerator().reseed(); } + // uniform ints [a,b] distribution int uniform_int_distribution(int a, int b) { diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index 79e5af2fdedd..081166baeb0b 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -80,6 +80,7 @@ #include <svl/zforlist.hxx> #include <linguistic/misc.hxx> #include <cppuhelper/bootstrap.hxx> +#include <comphelper/random.hxx> #include <comphelper/base64.hxx> #include <comphelper/dispatchcommand.hxx> #include <comphelper/lok.hxx> @@ -8024,7 +8025,10 @@ static int lo_initialize(LibreOfficeKit* pThis, const char* pAppPath, const char } rtl::Bootstrap::set(u"UserInstallation"_ustr, url); if (eStage == SECOND_INIT) + { + comphelper::rng::reseed(); utl::Bootstrap::reloadData(); + } } OUString aAppPath; diff --git a/include/comphelper/random.hxx b/include/comphelper/random.hxx index 345d57c7158d..1fe4dc5fdf58 100644 --- a/include/comphelper/random.hxx +++ b/include/comphelper/random.hxx @@ -17,6 +17,8 @@ namespace comphelper::rng // These functions obey the SAL_RAND_REPEATABLE environment // variable: If it is set, use a fixed seed. +COMPHELPER_DLLPUBLIC void reseed(); + // note that uniform_int_distribution is inclusive of b, i.e. [a,b] while // uniform_real_distribution is exclusive of b, i.e. [a,b), std::nextafter may be your friend there