From: Lucian Petrut Date: Thu, 2 Apr 2020 10:19:20 +0000 (+0000) Subject: librbd: avoid broken mingw rng X-Git-Tag: v16.1.0~652^2~7 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=70b2504671ab9ae187ab8aaec886843b03556afd;p=ceph.git librbd: avoid broken mingw rng std::random_device is broken when using Mingw < 9.2 [1]. It always produces the same sequence of numbers, so one critical implication is that multiple sessions coming from different processes will use the same identifier. For now, we'll use boost::random::random_device when building with Mingw. [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85494 Signed-off-by: Lucian Petrut --- diff --git a/src/include/random.h b/src/include/random.h index 0501a878049..f2e3e37bcd7 100644 --- a/src/include/random.h +++ b/src/include/random.h @@ -20,6 +20,15 @@ #include #include +// Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85494 +#ifdef __MINGW32__ +#include + +using random_device_t = boost::random::random_device; +#else +using random_device_t = std::random_device; +#endif + // Basic random number facility (see N3551 for inspiration): namespace ceph::util { @@ -91,7 +100,7 @@ void randomize_rng(const SeedT seed, MutexT& m, EngineT& e) template void randomize_rng(MutexT& m, EngineT& e) { - std::random_device rd; + random_device_t rd; std::lock_guard lg(m); e.seed(rd()); @@ -107,7 +116,7 @@ void randomize_rng(const SeedT n) template void randomize_rng() { - std::random_device rd; + random_device_t rd; detail::engine().seed(rd()); } @@ -232,7 +241,7 @@ template class random_number_generator final { std::mutex l; - std::random_device rd; + random_device_t rd; std::default_random_engine e; using seed_type = typename decltype(e)::result_type; diff --git a/src/include/uuid.h b/src/include/uuid.h index bee66c36fa3..091a0c049cb 100644 --- a/src/include/uuid.h +++ b/src/include/uuid.h @@ -7,6 +7,7 @@ */ #include "encoding.h" +#include "random.h" #include #include @@ -32,7 +33,7 @@ struct uuid_d { } void generate_random() { - std::random_device rng; + random_device_t rng; boost::uuids::basic_random_generator gen(rng); uuid = gen(); } diff --git a/src/librbd/Utils.cc b/src/librbd/Utils.cc index 420f73a69bd..36e8fa353a6 100644 --- a/src/librbd/Utils.cc +++ b/src/librbd/Utils.cc @@ -5,6 +5,7 @@ #include #include "librbd/Utils.h" +#include "include/random.h" #include "include/rbd_types.h" #include "include/stringify.h" #include "include/neorados/RADOS.hpp" @@ -55,7 +56,7 @@ std::string generate_image_id(librados::IoCtx &ioctx) { librados::Rados rados(ioctx); uint64_t bid = rados.get_instance_id(); - std::mt19937 generator{std::random_device{}()}; + std::mt19937 generator{random_device_t{}()}; std::uniform_int_distribution distribution{0, 0xFFFFFFFF}; uint32_t extra = distribution(generator); diff --git a/src/mon/MonClient.cc b/src/mon/MonClient.cc index edff5f50958..0840c4641a4 100644 --- a/src/mon/MonClient.cc +++ b/src/mon/MonClient.cc @@ -21,6 +21,7 @@ #include #include "common/weighted_shuffle.h" +#include "include/random.h" #include "include/scope_guard.h" #include "include/stringify.h" @@ -792,7 +793,7 @@ void MonClient::_add_conns(uint64_t global_id) auto rank_name = monmap.get_name(i); weights.push_back(monmap.get_weight(rank_name)); } - std::random_device rd; + random_device_t rd; if (std::accumulate(begin(weights), end(weights), 0u) == 0) { std::shuffle(begin(ranks), end(ranks), std::mt19937{rd()}); } else { diff --git a/src/test/common/CMakeLists.txt b/src/test/common/CMakeLists.txt index 4bf46a00873..6dd405e8d79 100644 --- a/src/test/common/CMakeLists.txt +++ b/src/test/common/CMakeLists.txt @@ -134,6 +134,7 @@ add_executable(unittest_random test_random.cc ) add_ceph_unittest(unittest_random) +target_link_libraries(unittest_random Boost::random) # unittest_throttle add_executable(unittest_throttle diff --git a/src/tools/osdmaptool.cc b/src/tools/osdmaptool.cc index 73ad1a535b6..afd5fe88d76 100644 --- a/src/tools/osdmaptool.cc +++ b/src/tools/osdmaptool.cc @@ -18,6 +18,7 @@ #include "common/ceph_argparse.h" #include "common/errno.h" #include "common/safe_io.h" +#include "include/random.h" #include "mon/health_check.h" #include #include @@ -423,7 +424,7 @@ int main(int argc, const char **argv) int r = clock_gettime(CLOCK_MONOTONIC, &round_start); assert(r == 0); do { - std::random_device rd; + random_device_t rd; std::shuffle(pools.begin(), pools.end(), std::mt19937{rd()}); cout << "pools "; for (auto& i: pools)