Skip to content

Commit e705fed

Browse files
yfeldblummeta-codesync[bot]
authored andcommitted
fix seeding in folly::Random
Summary: Just generate from the random device directly, bypassing `std::seed_seq`. Reviewed By: Orvid Differential Revision: D92200312 fbshipit-source-id: 21ff184f6301673f4a1a5abf1baaadabca66410c
1 parent 1546ddc commit e705fed

2 files changed

Lines changed: 11 additions & 61 deletions

File tree

third-party/folly/src/folly/Random-inl.h

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,65 +22,28 @@ namespace folly {
2222

2323
namespace detail {
2424

25-
// Return the state size needed by RNG, expressed as a number of uint32_t
26-
// integers. Specialized for all templates specified in the C++11 standard.
27-
// For some (mersenne_twister_engine), this is exported as a state_size static
28-
// data member; for others, the standard shows formulas.
29-
30-
template <class RNG, typename = void>
31-
struct StateSize {
32-
// A sane default.
33-
using type = std::integral_constant<size_t, 512>;
34-
};
35-
36-
template <class RNG>
37-
struct StateSize<RNG, void_t<decltype(RNG::state_size)>> {
38-
using type = std::integral_constant<size_t, RNG::state_size>;
39-
};
40-
41-
template <class UIntType, UIntType a, UIntType c, UIntType m>
42-
struct StateSize<std::linear_congruential_engine<UIntType, a, c, m>> {
43-
// From the standard [rand.eng.lcong], this is ceil(log2(m) / 32) + 3,
44-
// which is the same as ceil(ceil(log2(m) / 32) + 3, and
45-
// ceil(log2(m)) <= std::numeric_limits<UIntType>::digits
46-
using type = std::integral_constant<
47-
size_t,
48-
(std::numeric_limits<UIntType>::digits + 31) / 32 + 3>;
49-
};
50-
51-
template <class UIntType, size_t w, size_t s, size_t r>
52-
struct StateSize<std::subtract_with_carry_engine<UIntType, w, s, r>> {
53-
// [rand.eng.sub]: r * ceil(w / 32)
54-
using type = std::integral_constant<size_t, r*((w + 31) / 32)>;
55-
};
56-
57-
template <typename RNG>
58-
using StateSizeT = _t<StateSize<RNG>>;
59-
60-
template <class RNG>
61-
struct SeedData {
62-
SeedData() {
63-
Random::secureRandom(seedData.data(), seedData.size() * sizeof(uint32_t));
25+
struct SeedSeqSecureRandom {
26+
using result_type = uint32_t;
27+
template <typename Word>
28+
void generate(Word* b, Word* e) {
29+
static_assert(is_non_bool_integral_v<Word>);
30+
static_assert(sizeof(Word) >= sizeof(result_type));
31+
Random::secureRandom(b, (e - b) * sizeof(Word));
6432
}
65-
66-
static constexpr size_t stateSize = StateSizeT<RNG>::value;
67-
std::array<uint32_t, stateSize> seedData;
6833
};
6934

7035
} // namespace detail
7136

7237
template <class RNG, class /* EnableIf */>
7338
void Random::seed(RNG& rng) {
74-
detail::SeedData<RNG> sd;
75-
std::seed_seq s(std::begin(sd.seedData), std::end(sd.seedData));
76-
rng.seed(s);
39+
detail::SeedSeqSecureRandom seq;
40+
rng.seed(seq);
7741
}
7842

7943
template <class RNG, class /* EnableIf */>
8044
auto Random::create() -> RNG {
81-
detail::SeedData<RNG> sd;
82-
std::seed_seq s(std::begin(sd.seedData), std::end(sd.seedData));
83-
return RNG(s);
45+
detail::SeedSeqSecureRandom seq;
46+
return RNG(seq);
8447
}
8548

8649
} // namespace folly

third-party/folly/src/folly/test/RandomTest.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,6 @@
3636

3737
using namespace folly;
3838

39-
TEST(Random, StateSize) {
40-
using namespace folly::detail;
41-
42-
// uint_fast32_t is uint64_t on x86_64, w00t
43-
EXPECT_EQ(
44-
sizeof(uint_fast32_t) / 4 + 3, StateSizeT<std::minstd_rand0>::value);
45-
EXPECT_EQ(624, StateSizeT<std::mt19937>::value);
46-
#if FOLLY_HAVE_EXTRANDOM_SFMT19937
47-
EXPECT_EQ(624, StateSizeT<__gnu_cxx::sfmt19937>::value);
48-
#endif
49-
EXPECT_EQ(24, StateSizeT<std::ranlux24_base>::value);
50-
}
51-
5239
TEST(Random, Simple) {
5340
uint32_t prev = 0, seed = 0;
5441
for (int i = 0; i < 1024; ++i) {

0 commit comments

Comments
 (0)