LLVM  9.0.0svn
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 
17 #include "llvm/Support/Debug.h"
19 #ifdef _WIN32
20 #include "Windows/WindowsSupport.h"
21 #else
22 #include "Unix/Unix.h"
23 #endif
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "rng"
28 
29 // Tracking BUG: 19665
30 // http://llvm.org/bugs/show_bug.cgi?id=19665
31 //
32 // Do not change to cl::opt<uint64_t> since this silently breaks argument parsing.
34  Seed("rng-seed", cl::value_desc("seed"), cl::Hidden,
35  cl::desc("Seed for the random number generator"), cl::init(0));
36 
37 RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
38  LLVM_DEBUG(if (Seed == 0) dbgs()
39  << "Warning! Using unseeded random number generator.\n");
40 
41  // Combine seed and salts using std::seed_seq.
42  // Data: Seed-low, Seed-high, Salt
43  // Note: std::seed_seq can only store 32-bit values, even though we
44  // are using a 64-bit RNG. This isn't a problem since the Mersenne
45  // twister constructor copies these correctly into its initial state.
46  std::vector<uint32_t> Data;
47  Data.resize(2 + Salt.size());
48  Data[0] = Seed;
49  Data[1] = Seed >> 32;
50 
51  llvm::copy(Salt, Data.begin() + 2);
52 
53  std::seed_seq SeedSeq(Data.begin(), Data.end());
54  Generator.seed(SeedSeq);
55 }
56 
58  return Generator();
59 }
60 
61 // Get random vector of specified size
62 std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) {
63 #ifdef _WIN32
64  HCRYPTPROV hProvider;
65  if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
66  CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
68  if (CryptGenRandom(hProvider, Size, static_cast<BYTE *>(Buffer)))
69  return std::error_code();
70  }
71  return std::error_code(GetLastError(), std::system_category());
72 #else
73  int Fd = open("/dev/urandom", O_RDONLY);
74  if (Fd != -1) {
75  std::error_code Ret;
76  ssize_t BytesRead = read(Fd, Buffer, Size);
77  if (BytesRead == -1)
78  Ret = std::error_code(errno, std::system_category());
79  else if (BytesRead != static_cast<ssize_t>(Size))
80  Ret = std::error_code(EIO, std::system_category());
81  if (close(Fd) == -1)
82  Ret = std::error_code(errno, std::system_category());
83 
84  return Ret;
85  }
86  return std::error_code(errno, std::system_category());
87 #endif
88 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:427
std::error_code getRandomBytes(void *Buffer, size_t Size)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
generator_type::result_type result_type
iterator begin() const
Definition: StringRef.h:101
result_type operator()()
Returns a random number in the range [0, Max).
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition: Endian.h:65
uint32_t Size
Definition: Profile.cpp:46
static cl::opt< unsigned long long > Seed("rng-seed", cl::value_desc("seed"), cl::Hidden, cl::desc("Seed for the random number generator"), cl::init(0))
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
#define LLVM_DEBUG(X)
Definition: Debug.h:122
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1237