JF Bastien | 144829d | 2014-06-25 15:21:42 +0000 | [diff] [blame] | 1 | //===-- RandomNumberGenerator.cpp - Implement RNG class -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
JF Bastien | e6acbdc | 2014-12-17 18:12:10 +0000 | [diff] [blame] | 10 | // This file implements deterministic random number generation (RNG). |
JF Bastien | 144829d | 2014-06-25 15:21:42 +0000 | [diff] [blame] | 11 | // The current implementation is NOT cryptographically secure as it uses |
| 12 | // the C++11 <random> facilities. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Benjamin Kramer | 4073ce8 | 2015-03-23 18:19:41 +0000 | [diff] [blame] | 16 | #include "llvm/Support/RandomNumberGenerator.h" |
JF Bastien | 144829d | 2014-06-25 15:21:42 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
| 18 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 4073ce8 | 2015-03-23 18:19:41 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Eugene Leviant | ea877d4 | 2016-08-26 08:14:54 +0000 | [diff] [blame] | 20 | #ifdef LLVM_ON_WIN32 |
| 21 | #include "Windows/WindowsSupport.h" |
| 22 | #else |
| 23 | #include "Unix/Unix.h" |
| 24 | #endif |
JF Bastien | 144829d | 2014-06-25 15:21:42 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
Benjamin Kramer | 4073ce8 | 2015-03-23 18:19:41 +0000 | [diff] [blame] | 28 | #define DEBUG_TYPE "rng" |
| 29 | |
JF Bastien | 144829d | 2014-06-25 15:21:42 +0000 | [diff] [blame] | 30 | // Tracking BUG: 19665 |
| 31 | // http://llvm.org/bugs/show_bug.cgi?id=19665 |
| 32 | // |
| 33 | // Do not change to cl::opt<uint64_t> since this silently breaks argument parsing. |
| 34 | static cl::opt<unsigned long long> |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 35 | Seed("rng-seed", cl::value_desc("seed"), cl::Hidden, |
| 36 | cl::desc("Seed for the random number generator"), cl::init(0)); |
JF Bastien | 144829d | 2014-06-25 15:21:42 +0000 | [diff] [blame] | 37 | |
| 38 | RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) { |
| 39 | DEBUG( |
| 40 | if (Seed == 0) |
JF Bastien | e6acbdc | 2014-12-17 18:12:10 +0000 | [diff] [blame] | 41 | dbgs() << "Warning! Using unseeded random number generator.\n" |
JF Bastien | 144829d | 2014-06-25 15:21:42 +0000 | [diff] [blame] | 42 | ); |
| 43 | |
JF Bastien | e6acbdc | 2014-12-17 18:12:10 +0000 | [diff] [blame] | 44 | // Combine seed and salts using std::seed_seq. |
| 45 | // Data: Seed-low, Seed-high, Salt |
| 46 | // Note: std::seed_seq can only store 32-bit values, even though we |
| 47 | // are using a 64-bit RNG. This isn't a problem since the Mersenne |
| 48 | // twister constructor copies these correctly into its initial state. |
JF Bastien | 144829d | 2014-06-25 15:21:42 +0000 | [diff] [blame] | 49 | std::vector<uint32_t> Data; |
Zachary Turner | 76e007e | 2016-10-11 18:17:26 +0000 | [diff] [blame] | 50 | Data.resize(2 + Salt.size()); |
| 51 | Data[0] = Seed; |
| 52 | Data[1] = Seed >> 32; |
JF Bastien | 144829d | 2014-06-25 15:21:42 +0000 | [diff] [blame] | 53 | |
Zachary Turner | 76e007e | 2016-10-11 18:17:26 +0000 | [diff] [blame] | 54 | std::copy(Salt.begin(), Salt.end(), Data.begin() + 2); |
JF Bastien | 144829d | 2014-06-25 15:21:42 +0000 | [diff] [blame] | 55 | |
| 56 | std::seed_seq SeedSeq(Data.begin(), Data.end()); |
| 57 | Generator.seed(SeedSeq); |
| 58 | } |
| 59 | |
Mehdi Amini | ea8e979 | 2016-10-11 07:13:01 +0000 | [diff] [blame] | 60 | RandomNumberGenerator::result_type RandomNumberGenerator::operator()() { |
JF Bastien | e6acbdc | 2014-12-17 18:12:10 +0000 | [diff] [blame] | 61 | return Generator(); |
JF Bastien | 144829d | 2014-06-25 15:21:42 +0000 | [diff] [blame] | 62 | } |
Eugene Leviant | ea877d4 | 2016-08-26 08:14:54 +0000 | [diff] [blame] | 63 | |
| 64 | // Get random vector of specified size |
| 65 | std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) { |
| 66 | #ifdef LLVM_ON_WIN32 |
| 67 | HCRYPTPROV hProvider; |
| 68 | if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, |
| 69 | CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) { |
| 70 | ScopedCryptContext ScopedHandle(hProvider); |
| 71 | if (CryptGenRandom(hProvider, Size, static_cast<BYTE *>(Buffer))) |
| 72 | return std::error_code(); |
| 73 | } |
| 74 | return std::error_code(GetLastError(), std::system_category()); |
| 75 | #else |
| 76 | int Fd = open("/dev/urandom", O_RDONLY); |
| 77 | if (Fd != -1) { |
| 78 | std::error_code Ret; |
| 79 | ssize_t BytesRead = read(Fd, Buffer, Size); |
| 80 | if (BytesRead == -1) |
| 81 | Ret = std::error_code(errno, std::system_category()); |
| 82 | else if (BytesRead != static_cast<ssize_t>(Size)) |
| 83 | Ret = std::error_code(EIO, std::system_category()); |
| 84 | if (close(Fd) == -1) |
| 85 | Ret = std::error_code(errno, std::system_category()); |
| 86 | |
| 87 | return Ret; |
| 88 | } |
| 89 | return std::error_code(errno, std::system_category()); |
| 90 | #endif |
| 91 | } |