2012-12-06 14:05:11 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Copyright (C) 2012 Tino Kluge <tino.kluge@hrz.tu-chemnitz.de>
|
|
|
|
*/
|
|
|
|
|
2014-05-13 08:42:21 +02:00
|
|
|
#include <comphelper/random.hxx>
|
2015-05-06 13:46:41 +02:00
|
|
|
#include <sal/log.hxx>
|
2015-01-06 11:19:31 +00:00
|
|
|
#include <assert.h>
|
2015-01-06 15:33:18 +00:00
|
|
|
#include <time.h>
|
Lock comphelper::rng internals for multi-threaded access
With `--convert-to pdf xlsx/tdf116206-1.xlsx` with xlsx/tdf116206-1.xlsx as
obtained by bin/get-bugzilla-attachments-by-mimetype (i.e., the attachment
"Example file with lots of random data" at
<https://bugs.documentfoundation.org/show_bug.cgi?id=116206#c0>), my ASan+UBSan
build will eventually fail with
> .../include/c++/10.0.0/bits/random.tcc:461:25: runtime error: index 624 out of bounds for type 'unsigned long [624]'
> #0 in std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>::operator()() at .../include/c++/10.0.0/bits/random.tcc:461:25
> #1 in double std::generate_canonical<double, 53ul, std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) at .../include/c++/10.0.0/bits/random.tcc:3336:23
> #2 in std::__detail::_Adaptor<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>, double>::operator()() at .../include/c++/10.0.0/bits/random.h:179:11
> #3 in double std::uniform_real_distribution<double>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&, std::uniform_real_distribution<double>::param_type const&) at .../include/c++/10.0.0/bits/random.h:1857:12
> #4 in double std::uniform_real_distribution<double>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) at .../include/c++/10.0.0/bits/random.h:1848:24
> #5 in comphelper::rng::uniform_real_distribution(double, double) at comphelper/source/misc/random.cxx:104:12
> #6 in ScInterpreter::ScRandom() at sc/source/core/tool/interpr1.cxx:1768:21
> #7 in ScInterpreter::Interpret() at sc/source/core/tool/interpr4.cxx:4026:43
> #8 in ScFormulaCell::InterpretTail(ScInterpreterContext&, ScFormulaCell::ScInterpretTailParameter) at sc/source/core/data/formulacell.cxx:1905:23
> #9 in ScColumn::CalculateInThread(ScInterpreterContext&, int, unsigned long, unsigned int, unsigned int) at sc/source/core/data/column2.cxx:2964:15
> #10 in ScTable::CalculateInColumnInThread(ScInterpreterContext&, short, int, unsigned long, unsigned int, unsigned int) at sc/source/core/data/table1.cxx:2476:16
> #11 in ScDocument::CalculateInColumnInThread(ScInterpreterContext&, ScAddress const&, unsigned long, unsigned int, unsigned int) at sc/source/core/data/documen8.cxx:422:11
> #12 in ScFormulaCell::InterpretFormulaGroupThreading(sc::FormulaLogger::GroupScope&, bool&, bool&, int, int)::Executor::doWork() at sc/source/core/data/formulacell.cxx:4714:29
> #13 in comphelper::ThreadTask::exec() at comphelper/source/misc/threadpool.cxx:279:9
> #14 in comphelper::ThreadPool::ThreadWorker::execute() at comphelper/source/misc/threadpool.cxx:83:24
> #15 in salhelper::Thread::run() at salhelper/source/thread.cxx:40:9
[...]
suggesting that there is racy concurrent access to the internals of singleton
std::mt19937 comphelper::rng::RandomNumberGenerator::global_rng.
Change-Id: I8209b3903918c567fc832abbb84c8fbcc08e444b
Reviewed-on: https://gerrit.libreoffice.org/74368
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-06-19 18:20:35 +02:00
|
|
|
#include <mutex>
|
2015-01-06 11:19:31 +00:00
|
|
|
#include <random>
|
2024-05-03 14:17:27 +01:00
|
|
|
#include <rtl/random.h>
|
2015-05-06 12:34:17 +03:00
|
|
|
#include <stdexcept>
|
2018-04-24 11:37:03 +02:00
|
|
|
#if defined HAVE_VALGRIND_HEADERS
|
|
|
|
#include <valgrind/memcheck.h>
|
|
|
|
#endif
|
2014-04-14 15:41:13 +02:00
|
|
|
|
2012-12-06 14:05:11 +00:00
|
|
|
// this is nothing but a simple wrapper around
|
2015-01-06 11:19:31 +00:00
|
|
|
// the std::random generators
|
2012-12-06 14:05:11 +00:00
|
|
|
|
2020-01-14 16:25:06 +02:00
|
|
|
namespace comphelper::rng
|
2012-12-06 14:05:11 +00:00
|
|
|
{
|
|
|
|
// underlying random number generator
|
2015-01-06 11:19:31 +00:00
|
|
|
// std::mt19937 implements the Mersenne twister algorithm which
|
2012-12-06 14:05:11 +00:00
|
|
|
// is fast and has good statistical properties, it produces integers
|
|
|
|
// in the range of [0, 2^32-1] internally
|
|
|
|
// memory requirement: 625*sizeof(uint32_t)
|
|
|
|
// http://en.wikipedia.org/wiki/Mersenne_twister
|
2015-01-06 11:19:31 +00:00
|
|
|
#define STD_RNG_ALGO std::mt19937
|
Extend loplugin:external to warn about classes
...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend
loplugin:external to warn about enums".
Cases where free functions were moved into an unnamed namespace along with a
class, to not break ADL, are in:
filter/source/svg/svgexport.cxx
sc/source/filter/excel/xelink.cxx
sc/source/filter/excel/xilink.cxx
svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx
All other free functions mentioning moved classes appear to be harmless and not
give rise to (silent, even) ADL breakage. (One remaining TODO in
compilerplugins/clang/external.cxx is that derived classes are not covered by
computeAffectedTypes, even though they could also be affected by ADL-breakage---
but don't seem to be in any acutal case across the code base.)
For friend declarations using elaborate type specifiers, like
class C1 {};
class C2 { friend class C1; };
* If C2 (but not C1) is moved into an unnamed namespace, the friend declaration
must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see
C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither
qualified nor a template-id and the declaration is a function or an
elaborated-type-specifier, the lookup to determine whether the entity has been
previously declared shall not consider any scopes outside the innermost
enclosing namespace.")
* If C1 (but not C2) is moved into an unnamed namespace, the friend declaration
must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882>
"elaborated-type-specifier friend not looked up in unnamed namespace".
Apart from that, to keep changes simple and mostly mechanical (which should help
avoid regressions), out-of-line definitions of class members have been left in
the enclosing (named) namespace. But explicit specializations of class
templates had to be moved into the unnamed namespace to appease
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of
template from unnamed namespace using unqualified-id in enclosing namespace".
Also, accompanying declarations (of e.g. typedefs or static variables) that
could arguably be moved into the unnamed namespace too have been left alone.
And in some cases, mention of affected types in blacklists in other loplugins
needed to be adapted.
And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which
is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is
not moved into an unnamed namespace (because it is declared in
sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about
such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler
doesn’t give this warning for types defined in the main .C file, as those are
unlikely to have multiple definitions."
(<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The
warned-about classes also don't have multiple definitions in the given test, so
disable the warning when including the .cxx.
Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4
Reviewed-on: https://gerrit.libreoffice.org/83239
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
|
|
|
|
|
|
|
namespace
|
2020-11-12 12:51:27 +01:00
|
|
|
{
|
2014-10-03 17:12:23 +01:00
|
|
|
struct RandomNumberGenerator
|
|
|
|
{
|
Lock comphelper::rng internals for multi-threaded access
With `--convert-to pdf xlsx/tdf116206-1.xlsx` with xlsx/tdf116206-1.xlsx as
obtained by bin/get-bugzilla-attachments-by-mimetype (i.e., the attachment
"Example file with lots of random data" at
<https://bugs.documentfoundation.org/show_bug.cgi?id=116206#c0>), my ASan+UBSan
build will eventually fail with
> .../include/c++/10.0.0/bits/random.tcc:461:25: runtime error: index 624 out of bounds for type 'unsigned long [624]'
> #0 in std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>::operator()() at .../include/c++/10.0.0/bits/random.tcc:461:25
> #1 in double std::generate_canonical<double, 53ul, std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) at .../include/c++/10.0.0/bits/random.tcc:3336:23
> #2 in std::__detail::_Adaptor<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>, double>::operator()() at .../include/c++/10.0.0/bits/random.h:179:11
> #3 in double std::uniform_real_distribution<double>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&, std::uniform_real_distribution<double>::param_type const&) at .../include/c++/10.0.0/bits/random.h:1857:12
> #4 in double std::uniform_real_distribution<double>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) at .../include/c++/10.0.0/bits/random.h:1848:24
> #5 in comphelper::rng::uniform_real_distribution(double, double) at comphelper/source/misc/random.cxx:104:12
> #6 in ScInterpreter::ScRandom() at sc/source/core/tool/interpr1.cxx:1768:21
> #7 in ScInterpreter::Interpret() at sc/source/core/tool/interpr4.cxx:4026:43
> #8 in ScFormulaCell::InterpretTail(ScInterpreterContext&, ScFormulaCell::ScInterpretTailParameter) at sc/source/core/data/formulacell.cxx:1905:23
> #9 in ScColumn::CalculateInThread(ScInterpreterContext&, int, unsigned long, unsigned int, unsigned int) at sc/source/core/data/column2.cxx:2964:15
> #10 in ScTable::CalculateInColumnInThread(ScInterpreterContext&, short, int, unsigned long, unsigned int, unsigned int) at sc/source/core/data/table1.cxx:2476:16
> #11 in ScDocument::CalculateInColumnInThread(ScInterpreterContext&, ScAddress const&, unsigned long, unsigned int, unsigned int) at sc/source/core/data/documen8.cxx:422:11
> #12 in ScFormulaCell::InterpretFormulaGroupThreading(sc::FormulaLogger::GroupScope&, bool&, bool&, int, int)::Executor::doWork() at sc/source/core/data/formulacell.cxx:4714:29
> #13 in comphelper::ThreadTask::exec() at comphelper/source/misc/threadpool.cxx:279:9
> #14 in comphelper::ThreadPool::ThreadWorker::execute() at comphelper/source/misc/threadpool.cxx:83:24
> #15 in salhelper::Thread::run() at salhelper/source/thread.cxx:40:9
[...]
suggesting that there is racy concurrent access to the internals of singleton
std::mt19937 comphelper::rng::RandomNumberGenerator::global_rng.
Change-Id: I8209b3903918c567fc832abbb84c8fbcc08e444b
Reviewed-on: https://gerrit.libreoffice.org/74368
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-06-19 18:20:35 +02:00
|
|
|
std::mutex mutex;
|
2015-01-06 11:19:31 +00:00
|
|
|
STD_RNG_ALGO global_rng;
|
2024-05-03 14:17:27 +01:00
|
|
|
RandomNumberGenerator() { reseed(); }
|
|
|
|
|
|
|
|
void reseed()
|
2014-10-03 17:12:23 +01:00
|
|
|
{
|
2021-10-18 12:41:36 +02:00
|
|
|
// make RR easier to use, breaks easily without the RNG being repeatable
|
|
|
|
bool bRepeatable = (getenv("SAL_RAND_REPEATABLE") != nullptr) || (getenv("RR") != nullptr);
|
2018-04-24 11:37:03 +02:00
|
|
|
// valgrind on some platforms (e.g.Ubuntu16.04) does not support the new Intel RDRAND instructions,
|
|
|
|
// which leads to "Illegal Opcode" errors, so just turn off randomness.
|
|
|
|
#if defined HAVE_VALGRIND_HEADERS
|
|
|
|
if (RUNNING_ON_VALGRIND)
|
|
|
|
bRepeatable = true;
|
|
|
|
#endif
|
2015-07-09 14:48:31 +01:00
|
|
|
if (bRepeatable)
|
|
|
|
{
|
|
|
|
global_rng.seed(42);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-03 14:17:27 +01:00
|
|
|
size_t seed = 0;
|
2024-05-03 16:33:11 +01:00
|
|
|
if (rtl_random_getBytes(nullptr, &seed, sizeof(seed)) != rtl_Random_E_None)
|
2024-05-03 14:17:27 +01:00
|
|
|
seed = 0;
|
|
|
|
|
|
|
|
// 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));
|
2014-10-03 17:12:23 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-20 21:11:11 +02:00
|
|
|
RandomNumberGenerator& GetTheRandomNumberGenerator()
|
2014-10-03 17:12:23 +01:00
|
|
|
{
|
2021-08-20 21:11:11 +02:00
|
|
|
static RandomNumberGenerator RANDOM;
|
|
|
|
return RANDOM;
|
|
|
|
}
|
Extend loplugin:external to warn about classes
...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend
loplugin:external to warn about enums".
Cases where free functions were moved into an unnamed namespace along with a
class, to not break ADL, are in:
filter/source/svg/svgexport.cxx
sc/source/filter/excel/xelink.cxx
sc/source/filter/excel/xilink.cxx
svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx
All other free functions mentioning moved classes appear to be harmless and not
give rise to (silent, even) ADL breakage. (One remaining TODO in
compilerplugins/clang/external.cxx is that derived classes are not covered by
computeAffectedTypes, even though they could also be affected by ADL-breakage---
but don't seem to be in any acutal case across the code base.)
For friend declarations using elaborate type specifiers, like
class C1 {};
class C2 { friend class C1; };
* If C2 (but not C1) is moved into an unnamed namespace, the friend declaration
must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see
C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither
qualified nor a template-id and the declaration is a function or an
elaborated-type-specifier, the lookup to determine whether the entity has been
previously declared shall not consider any scopes outside the innermost
enclosing namespace.")
* If C1 (but not C2) is moved into an unnamed namespace, the friend declaration
must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882>
"elaborated-type-specifier friend not looked up in unnamed namespace".
Apart from that, to keep changes simple and mostly mechanical (which should help
avoid regressions), out-of-line definitions of class members have been left in
the enclosing (named) namespace. But explicit specializations of class
templates had to be moved into the unnamed namespace to appease
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of
template from unnamed namespace using unqualified-id in enclosing namespace".
Also, accompanying declarations (of e.g. typedefs or static variables) that
could arguably be moved into the unnamed namespace too have been left alone.
And in some cases, mention of affected types in blacklists in other loplugins
needed to be adapted.
And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which
is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is
not moved into an unnamed namespace (because it is declared in
sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about
such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler
doesn’t give this warning for types defined in the main .C file, as those are
unlikely to have multiple definitions."
(<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The
warned-about classes also don't have multiple definitions in the given test, so
disable the warning when including the .cxx.
Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4
Reviewed-on: https://gerrit.libreoffice.org/83239
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
|
|
|
}
|
|
|
|
|
2024-05-03 14:17:27 +01:00
|
|
|
void reseed() { GetTheRandomNumberGenerator().reseed(); }
|
|
|
|
|
2014-10-03 17:12:23 +01:00
|
|
|
// uniform ints [a,b] distribution
|
|
|
|
int uniform_int_distribution(int a, int b)
|
|
|
|
{
|
2015-01-06 11:19:31 +00:00
|
|
|
std::uniform_int_distribution<int> dist(a, b);
|
2021-08-20 21:11:11 +02:00
|
|
|
auto& gen = GetTheRandomNumberGenerator();
|
Lock comphelper::rng internals for multi-threaded access
With `--convert-to pdf xlsx/tdf116206-1.xlsx` with xlsx/tdf116206-1.xlsx as
obtained by bin/get-bugzilla-attachments-by-mimetype (i.e., the attachment
"Example file with lots of random data" at
<https://bugs.documentfoundation.org/show_bug.cgi?id=116206#c0>), my ASan+UBSan
build will eventually fail with
> .../include/c++/10.0.0/bits/random.tcc:461:25: runtime error: index 624 out of bounds for type 'unsigned long [624]'
> #0 in std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>::operator()() at .../include/c++/10.0.0/bits/random.tcc:461:25
> #1 in double std::generate_canonical<double, 53ul, std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) at .../include/c++/10.0.0/bits/random.tcc:3336:23
> #2 in std::__detail::_Adaptor<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>, double>::operator()() at .../include/c++/10.0.0/bits/random.h:179:11
> #3 in double std::uniform_real_distribution<double>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&, std::uniform_real_distribution<double>::param_type const&) at .../include/c++/10.0.0/bits/random.h:1857:12
> #4 in double std::uniform_real_distribution<double>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) at .../include/c++/10.0.0/bits/random.h:1848:24
> #5 in comphelper::rng::uniform_real_distribution(double, double) at comphelper/source/misc/random.cxx:104:12
> #6 in ScInterpreter::ScRandom() at sc/source/core/tool/interpr1.cxx:1768:21
> #7 in ScInterpreter::Interpret() at sc/source/core/tool/interpr4.cxx:4026:43
> #8 in ScFormulaCell::InterpretTail(ScInterpreterContext&, ScFormulaCell::ScInterpretTailParameter) at sc/source/core/data/formulacell.cxx:1905:23
> #9 in ScColumn::CalculateInThread(ScInterpreterContext&, int, unsigned long, unsigned int, unsigned int) at sc/source/core/data/column2.cxx:2964:15
> #10 in ScTable::CalculateInColumnInThread(ScInterpreterContext&, short, int, unsigned long, unsigned int, unsigned int) at sc/source/core/data/table1.cxx:2476:16
> #11 in ScDocument::CalculateInColumnInThread(ScInterpreterContext&, ScAddress const&, unsigned long, unsigned int, unsigned int) at sc/source/core/data/documen8.cxx:422:11
> #12 in ScFormulaCell::InterpretFormulaGroupThreading(sc::FormulaLogger::GroupScope&, bool&, bool&, int, int)::Executor::doWork() at sc/source/core/data/formulacell.cxx:4714:29
> #13 in comphelper::ThreadTask::exec() at comphelper/source/misc/threadpool.cxx:279:9
> #14 in comphelper::ThreadPool::ThreadWorker::execute() at comphelper/source/misc/threadpool.cxx:83:24
> #15 in salhelper::Thread::run() at salhelper/source/thread.cxx:40:9
[...]
suggesting that there is racy concurrent access to the internals of singleton
std::mt19937 comphelper::rng::RandomNumberGenerator::global_rng.
Change-Id: I8209b3903918c567fc832abbb84c8fbcc08e444b
Reviewed-on: https://gerrit.libreoffice.org/74368
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-06-19 18:20:35 +02:00
|
|
|
std::scoped_lock<std::mutex> g(gen.mutex);
|
|
|
|
return dist(gen.global_rng);
|
2014-10-03 17:12:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// uniform ints [a,b] distribution
|
2014-10-07 13:45:15 +01:00
|
|
|
unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
|
2014-10-03 17:12:23 +01:00
|
|
|
{
|
2015-01-06 11:19:31 +00:00
|
|
|
std::uniform_int_distribution<unsigned int> dist(a, b);
|
2021-08-20 21:11:11 +02:00
|
|
|
auto& gen = GetTheRandomNumberGenerator();
|
Lock comphelper::rng internals for multi-threaded access
With `--convert-to pdf xlsx/tdf116206-1.xlsx` with xlsx/tdf116206-1.xlsx as
obtained by bin/get-bugzilla-attachments-by-mimetype (i.e., the attachment
"Example file with lots of random data" at
<https://bugs.documentfoundation.org/show_bug.cgi?id=116206#c0>), my ASan+UBSan
build will eventually fail with
> .../include/c++/10.0.0/bits/random.tcc:461:25: runtime error: index 624 out of bounds for type 'unsigned long [624]'
> #0 in std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>::operator()() at .../include/c++/10.0.0/bits/random.tcc:461:25
> #1 in double std::generate_canonical<double, 53ul, std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) at .../include/c++/10.0.0/bits/random.tcc:3336:23
> #2 in std::__detail::_Adaptor<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>, double>::operator()() at .../include/c++/10.0.0/bits/random.h:179:11
> #3 in double std::uniform_real_distribution<double>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&, std::uniform_real_distribution<double>::param_type const&) at .../include/c++/10.0.0/bits/random.h:1857:12
> #4 in double std::uniform_real_distribution<double>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) at .../include/c++/10.0.0/bits/random.h:1848:24
> #5 in comphelper::rng::uniform_real_distribution(double, double) at comphelper/source/misc/random.cxx:104:12
> #6 in ScInterpreter::ScRandom() at sc/source/core/tool/interpr1.cxx:1768:21
> #7 in ScInterpreter::Interpret() at sc/source/core/tool/interpr4.cxx:4026:43
> #8 in ScFormulaCell::InterpretTail(ScInterpreterContext&, ScFormulaCell::ScInterpretTailParameter) at sc/source/core/data/formulacell.cxx:1905:23
> #9 in ScColumn::CalculateInThread(ScInterpreterContext&, int, unsigned long, unsigned int, unsigned int) at sc/source/core/data/column2.cxx:2964:15
> #10 in ScTable::CalculateInColumnInThread(ScInterpreterContext&, short, int, unsigned long, unsigned int, unsigned int) at sc/source/core/data/table1.cxx:2476:16
> #11 in ScDocument::CalculateInColumnInThread(ScInterpreterContext&, ScAddress const&, unsigned long, unsigned int, unsigned int) at sc/source/core/data/documen8.cxx:422:11
> #12 in ScFormulaCell::InterpretFormulaGroupThreading(sc::FormulaLogger::GroupScope&, bool&, bool&, int, int)::Executor::doWork() at sc/source/core/data/formulacell.cxx:4714:29
> #13 in comphelper::ThreadTask::exec() at comphelper/source/misc/threadpool.cxx:279:9
> #14 in comphelper::ThreadPool::ThreadWorker::execute() at comphelper/source/misc/threadpool.cxx:83:24
> #15 in salhelper::Thread::run() at salhelper/source/thread.cxx:40:9
[...]
suggesting that there is racy concurrent access to the internals of singleton
std::mt19937 comphelper::rng::RandomNumberGenerator::global_rng.
Change-Id: I8209b3903918c567fc832abbb84c8fbcc08e444b
Reviewed-on: https://gerrit.libreoffice.org/74368
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-06-19 18:20:35 +02:00
|
|
|
std::scoped_lock<std::mutex> g(gen.mutex);
|
|
|
|
return dist(gen.global_rng);
|
2014-10-03 17:12:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// uniform size_t [a,b] distribution
|
2014-10-07 13:45:15 +01:00
|
|
|
size_t uniform_size_distribution(size_t a, size_t b)
|
2012-12-06 14:05:11 +00:00
|
|
|
{
|
2015-01-06 11:19:31 +00:00
|
|
|
std::uniform_int_distribution<size_t> dist(a, b);
|
2021-08-20 21:11:11 +02:00
|
|
|
auto& gen = GetTheRandomNumberGenerator();
|
Lock comphelper::rng internals for multi-threaded access
With `--convert-to pdf xlsx/tdf116206-1.xlsx` with xlsx/tdf116206-1.xlsx as
obtained by bin/get-bugzilla-attachments-by-mimetype (i.e., the attachment
"Example file with lots of random data" at
<https://bugs.documentfoundation.org/show_bug.cgi?id=116206#c0>), my ASan+UBSan
build will eventually fail with
> .../include/c++/10.0.0/bits/random.tcc:461:25: runtime error: index 624 out of bounds for type 'unsigned long [624]'
> #0 in std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>::operator()() at .../include/c++/10.0.0/bits/random.tcc:461:25
> #1 in double std::generate_canonical<double, 53ul, std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) at .../include/c++/10.0.0/bits/random.tcc:3336:23
> #2 in std::__detail::_Adaptor<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>, double>::operator()() at .../include/c++/10.0.0/bits/random.h:179:11
> #3 in double std::uniform_real_distribution<double>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&, std::uniform_real_distribution<double>::param_type const&) at .../include/c++/10.0.0/bits/random.h:1857:12
> #4 in double std::uniform_real_distribution<double>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) at .../include/c++/10.0.0/bits/random.h:1848:24
> #5 in comphelper::rng::uniform_real_distribution(double, double) at comphelper/source/misc/random.cxx:104:12
> #6 in ScInterpreter::ScRandom() at sc/source/core/tool/interpr1.cxx:1768:21
> #7 in ScInterpreter::Interpret() at sc/source/core/tool/interpr4.cxx:4026:43
> #8 in ScFormulaCell::InterpretTail(ScInterpreterContext&, ScFormulaCell::ScInterpretTailParameter) at sc/source/core/data/formulacell.cxx:1905:23
> #9 in ScColumn::CalculateInThread(ScInterpreterContext&, int, unsigned long, unsigned int, unsigned int) at sc/source/core/data/column2.cxx:2964:15
> #10 in ScTable::CalculateInColumnInThread(ScInterpreterContext&, short, int, unsigned long, unsigned int, unsigned int) at sc/source/core/data/table1.cxx:2476:16
> #11 in ScDocument::CalculateInColumnInThread(ScInterpreterContext&, ScAddress const&, unsigned long, unsigned int, unsigned int) at sc/source/core/data/documen8.cxx:422:11
> #12 in ScFormulaCell::InterpretFormulaGroupThreading(sc::FormulaLogger::GroupScope&, bool&, bool&, int, int)::Executor::doWork() at sc/source/core/data/formulacell.cxx:4714:29
> #13 in comphelper::ThreadTask::exec() at comphelper/source/misc/threadpool.cxx:279:9
> #14 in comphelper::ThreadPool::ThreadWorker::execute() at comphelper/source/misc/threadpool.cxx:83:24
> #15 in salhelper::Thread::run() at salhelper/source/thread.cxx:40:9
[...]
suggesting that there is racy concurrent access to the internals of singleton
std::mt19937 comphelper::rng::RandomNumberGenerator::global_rng.
Change-Id: I8209b3903918c567fc832abbb84c8fbcc08e444b
Reviewed-on: https://gerrit.libreoffice.org/74368
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-06-19 18:20:35 +02:00
|
|
|
std::scoped_lock<std::mutex> g(gen.mutex);
|
|
|
|
return dist(gen.global_rng);
|
2012-12-06 14:05:11 +00:00
|
|
|
}
|
|
|
|
|
2014-10-03 17:12:23 +01:00
|
|
|
// uniform size_t [a,b) distribution
|
|
|
|
double uniform_real_distribution(double a, double b)
|
2012-12-06 14:05:11 +00:00
|
|
|
{
|
2014-10-07 10:32:52 +03:00
|
|
|
assert(a < b);
|
2015-01-06 11:19:31 +00:00
|
|
|
std::uniform_real_distribution<double> dist(a, b);
|
2021-08-20 21:11:11 +02:00
|
|
|
auto& gen = GetTheRandomNumberGenerator();
|
Lock comphelper::rng internals for multi-threaded access
With `--convert-to pdf xlsx/tdf116206-1.xlsx` with xlsx/tdf116206-1.xlsx as
obtained by bin/get-bugzilla-attachments-by-mimetype (i.e., the attachment
"Example file with lots of random data" at
<https://bugs.documentfoundation.org/show_bug.cgi?id=116206#c0>), my ASan+UBSan
build will eventually fail with
> .../include/c++/10.0.0/bits/random.tcc:461:25: runtime error: index 624 out of bounds for type 'unsigned long [624]'
> #0 in std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>::operator()() at .../include/c++/10.0.0/bits/random.tcc:461:25
> #1 in double std::generate_canonical<double, 53ul, std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) at .../include/c++/10.0.0/bits/random.tcc:3336:23
> #2 in std::__detail::_Adaptor<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>, double>::operator()() at .../include/c++/10.0.0/bits/random.h:179:11
> #3 in double std::uniform_real_distribution<double>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&, std::uniform_real_distribution<double>::param_type const&) at .../include/c++/10.0.0/bits/random.h:1857:12
> #4 in double std::uniform_real_distribution<double>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul> >(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>&) at .../include/c++/10.0.0/bits/random.h:1848:24
> #5 in comphelper::rng::uniform_real_distribution(double, double) at comphelper/source/misc/random.cxx:104:12
> #6 in ScInterpreter::ScRandom() at sc/source/core/tool/interpr1.cxx:1768:21
> #7 in ScInterpreter::Interpret() at sc/source/core/tool/interpr4.cxx:4026:43
> #8 in ScFormulaCell::InterpretTail(ScInterpreterContext&, ScFormulaCell::ScInterpretTailParameter) at sc/source/core/data/formulacell.cxx:1905:23
> #9 in ScColumn::CalculateInThread(ScInterpreterContext&, int, unsigned long, unsigned int, unsigned int) at sc/source/core/data/column2.cxx:2964:15
> #10 in ScTable::CalculateInColumnInThread(ScInterpreterContext&, short, int, unsigned long, unsigned int, unsigned int) at sc/source/core/data/table1.cxx:2476:16
> #11 in ScDocument::CalculateInColumnInThread(ScInterpreterContext&, ScAddress const&, unsigned long, unsigned int, unsigned int) at sc/source/core/data/documen8.cxx:422:11
> #12 in ScFormulaCell::InterpretFormulaGroupThreading(sc::FormulaLogger::GroupScope&, bool&, bool&, int, int)::Executor::doWork() at sc/source/core/data/formulacell.cxx:4714:29
> #13 in comphelper::ThreadTask::exec() at comphelper/source/misc/threadpool.cxx:279:9
> #14 in comphelper::ThreadPool::ThreadWorker::execute() at comphelper/source/misc/threadpool.cxx:83:24
> #15 in salhelper::Thread::run() at salhelper/source/thread.cxx:40:9
[...]
suggesting that there is racy concurrent access to the internals of singleton
std::mt19937 comphelper::rng::RandomNumberGenerator::global_rng.
Change-Id: I8209b3903918c567fc832abbb84c8fbcc08e444b
Reviewed-on: https://gerrit.libreoffice.org/74368
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-06-19 18:20:35 +02:00
|
|
|
std::scoped_lock<std::mutex> g(gen.mutex);
|
|
|
|
return dist(gen.global_rng);
|
2012-12-06 14:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|