LLVM  14.0.0git
RandomNumberGenerator.cpp
Go to the documentation of this file.
1 //===-- RandomNumberGenerator.cpp - Implement RNG class -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements deterministic random number generation (RNG).
10 // The current implementation is NOT cryptographically secure as it uses
11 // the C++11 <random> facilities.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 
17 #include "DebugOptions.h"
18 
20 #include "llvm/Support/Debug.h"
22 #ifdef _WIN32
24 #else
25 #include "Unix/Unix.h"
26 #endif
27 
28 using namespace llvm;
29 
30 #define DEBUG_TYPE "rng"
31 namespace {
32 struct CreateSeed {
33  static void *call() {
34  return new cl::opt<uint64_t>(
35  "rng-seed", cl::value_desc("seed"), cl::Hidden,
36  cl::desc("Seed for the random number generator"), cl::init(0));
37  }
38 };
39 } // namespace
42 
43 RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
44  LLVM_DEBUG(if (*Seed == 0) dbgs()
45  << "Warning! Using unseeded random number generator.\n");
46 
47  // Combine seed and salts using std::seed_seq.
48  // Data: Seed-low, Seed-high, Salt
49  // Note: std::seed_seq can only store 32-bit values, even though we
50  // are using a 64-bit RNG. This isn't a problem since the Mersenne
51  // twister constructor copies these correctly into its initial state.
52  std::vector<uint32_t> Data;
53  Data.resize(2 + Salt.size());
54  Data[0] = *Seed;
55  Data[1] = *Seed >> 32;
56 
57  llvm::copy(Salt, Data.begin() + 2);
58 
59  std::seed_seq SeedSeq(Data.begin(), Data.end());
60  Generator.seed(SeedSeq);
61 }
62 
64  return Generator();
65 }
66 
67 // Get random vector of specified size
68 std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) {
69 #ifdef _WIN32
70  HCRYPTPROV hProvider;
71  if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
72  CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
74  if (CryptGenRandom(hProvider, Size, static_cast<BYTE *>(Buffer)))
75  return std::error_code();
76  }
77  return std::error_code(GetLastError(), std::system_category());
78 #else
79  int Fd = open("/dev/urandom", O_RDONLY);
80  if (Fd != -1) {
81  std::error_code Ret;
82  ssize_t BytesRead = read(Fd, Buffer, Size);
83  if (BytesRead == -1)
84  Ret = std::error_code(errno, std::system_category());
85  else if (BytesRead != static_cast<ssize_t>(Size))
86  Ret = std::error_code(EIO, std::system_category());
87  if (close(Fd) == -1)
88  Ret = std::error_code(errno, std::system_category());
89 
90  return Ret;
91  }
92  return std::error_code(errno, std::system_category());
93 #endif
94 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::RandomNumberGenerator::operator()
result_type operator()()
Returns a random number in the range [0, Max).
Definition: RandomNumberGenerator.cpp:63
RandomNumberGenerator.h
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
WindowsSupport.h
DebugOptions.h
llvm::copy
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1602
llvm::getRandomBytes
std::error_code getRandomBytes(void *Buffer, size_t Size)
Definition: RandomNumberGenerator.cpp:68
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:116
Unix.h
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
CommandLine.h
llvm::RandomNumberGenerator::result_type
generator_type::result_type result_type
Definition: RandomNumberGenerator.h:41
llvm::ManagedStatic
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:83
llvm::ScopedHandle
Definition: WindowsSupport.h:78
llvm::cl::opt
Definition: CommandLine.h:1434
Seed
static ManagedStatic< cl::opt< uint64_t >, CreateSeed > Seed
Definition: RandomNumberGenerator.cpp:40
call
S is passed via registers r2 But gcc stores them to the and then reload them to and r3 before issuing the call(r0 contains the address of the format string)
Definition: README.txt:190
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::initRandomSeedOptions
void initRandomSeedOptions()
Definition: RandomNumberGenerator.cpp:41
llvm::support::endian::read
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition: Endian.h:63
llvm::cl::value_desc
Definition: CommandLine.h:424
llvm::cl::desc
Definition: CommandLine.h:414
raw_ostream.h
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
Debug.h