1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
base / allocator / partition_allocator / src / partition_alloc / random.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "partition_alloc/random.h"
#include <type_traits>
#include "partition_alloc/partition_alloc_base/rand_util.h"
#include "partition_alloc/partition_alloc_base/thread_annotations.h"
#include "partition_alloc/partition_lock.h"
namespace partition_alloc {
class RandomGenerator {
public:
constexpr RandomGenerator() {}
uint32_t RandomValue() {
::partition_alloc::internal::ScopedGuard guard(lock_);
return GetGenerator()->RandUint32();
}
void SeedForTesting(uint64_t seed) {
::partition_alloc::internal::ScopedGuard guard(lock_);
GetGenerator()->ReseedForTesting(seed);
}
private:
::partition_alloc::internal::Lock lock_ = {};
bool initialized_ PA_GUARDED_BY(lock_) = false;
union {
internal::base::InsecureRandomGenerator instance_ PA_GUARDED_BY(lock_);
uint8_t instance_buffer_[sizeof(
internal::base::InsecureRandomGenerator)] PA_GUARDED_BY(lock_) = {};
};
internal::base::InsecureRandomGenerator* GetGenerator()
PA_EXCLUSIVE_LOCKS_REQUIRED(lock_) {
if (!initialized_) {
new (instance_buffer_) internal::base::InsecureRandomGenerator();
initialized_ = true;
}
return &instance_;
}
};
// Note: this is redundant, since the anonymous union is incompatible with a
// non-trivial default destructor. Not meant to be destructed anyway.
static_assert(std::is_trivially_destructible_v<RandomGenerator>, "");
namespace {
RandomGenerator g_generator = {};
} // namespace
namespace internal {
uint32_t RandomValue() {
return g_generator.RandomValue();
}
} // namespace internal
void SetMmapSeedForTesting(uint64_t seed) {
return g_generator.SeedForTesting(seed);
}
} // namespace partition_alloc