1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef PARTITION_ALLOC_PARTITION_ALLOC_BASE_RAND_UTIL_H_
6 #define PARTITION_ALLOC_PARTITION_ALLOC_BASE_RAND_UTIL_H_
7 
8 #include <cstddef>
9 #include <cstdint>
10 
11 #include "build/build_config.h"
12 #include "partition_alloc/partition_alloc_base/component_export.h"
13 
14 namespace partition_alloc {
15 class RandomGenerator;
16 
17 namespace internal {
18 class LightweightQuarantineBranch;
19 }
20 }  // namespace partition_alloc
21 
22 namespace partition_alloc::internal::base {
23 
24 // Returns a random number in range [0, UINT64_MAX]. Thread-safe.
25 PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE) uint64_t RandUint64();
26 
27 // Returns a random number in range [0, range).  Thread-safe.
28 PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE)
29 uint64_t RandGenerator(uint64_t range);
30 
31 // Fills |output_length| bytes of |output| with random data. Thread-safe.
32 //
33 // Although implementations are required to use a cryptographically secure
34 // random number source, code outside of base/ that relies on this should use
35 // crypto::RandBytes instead to ensure the requirement is easily discoverable.
36 PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE)
37 void RandBytes(void* output, size_t output_length);
38 
39 // Fast, insecure pseudo-random number generator.
40 //
41 // WARNING: This is not the generator you are looking for. This has significant
42 // caveats:
43 //   - It is non-cryptographic, so easy to miuse
44 //   - It is neither fork() nor clone()-safe.
45 //   - Synchronization is up to the client.
46 //
47 // Always prefer base::Rand*() above, unless you have a use case where its
48 // overhead is too high, or system calls are disallowed.
49 //
50 // Performance: As of 2021, rough overhead on Linux on a desktop machine of
51 // base::RandUint64() is ~800ns per call (it performs a system call). On Windows
52 // it is lower. On the same machine, this generator's cost is ~2ns per call,
53 // regardless of platform.
54 //
55 // This is different from |Rand*()| above as it is guaranteed to never make a
56 // system call to generate a new number, except to seed it.  This should *never*
57 // be used for cryptographic applications, and is not thread-safe.
58 //
59 // It is seeded using base::RandUint64() in the constructor, meaning that it
60 // doesn't need to be seeded. It can be re-seeded though, with
61 // ReseedForTesting(). Its period is long enough that it should not need to be
62 // re-seeded during use.
63 //
64 // Uses the XorShift128+ generator under the hood.
PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE)65 class PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE) InsecureRandomGenerator {
66  public:
67   // Never use outside testing, not enough entropy.
68   void ReseedForTesting(uint64_t seed);
69 
70   uint32_t RandUint32();
71   uint64_t RandUint64();
72 
73   static InsecureRandomGenerator ConstructForTesting() {
74     return InsecureRandomGenerator();
75   }
76 
77  private:
78   InsecureRandomGenerator();
79   // State.
80   uint64_t a_ = 0, b_ = 0;
81 
82   // Before adding a new friend class, make sure that the overhead of
83   // base::Rand*() is too high, using something more representative than a
84   // microbenchmark.
85   //
86   // PartitionAlloc allocations should not take more than 40-50ns per
87   // malloc()/free() pair, otherwise high-level benchmarks regress, and does not
88   // need a secure PRNG, as it's used for ASLR and zeroing some allocations at
89   // free() time.
90   friend class ::partition_alloc::RandomGenerator;
91   friend class ::partition_alloc::internal::LightweightQuarantineBranch;
92 };
93 
94 }  // namespace partition_alloc::internal::base
95 
96 #endif  // PARTITION_ALLOC_PARTITION_ALLOC_BASE_RAND_UTIL_H_
97