LCOV - code coverage report
Current view: top level - lib/Support - RandomNumberGenerator.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 19 22 86.4 %
Date: 2018-06-17 00:07:59 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : //
      10             : // This file implements deterministic random number generation (RNG).
      11             : // The current implementation is NOT cryptographically secure as it uses
      12             : // the C++11 <random> facilities.
      13             : //
      14             : //===----------------------------------------------------------------------===//
      15             : 
      16             : #include "llvm/Support/RandomNumberGenerator.h"
      17             : #include "llvm/Support/CommandLine.h"
      18             : #include "llvm/Support/Debug.h"
      19             : #include "llvm/Support/raw_ostream.h"
      20             : #ifdef _WIN32
      21             : #include "Windows/WindowsSupport.h"
      22             : #else
      23             : #include "Unix/Unix.h"
      24             : #endif
      25             : 
      26             : using namespace llvm;
      27             : 
      28             : #define DEBUG_TYPE "rng"
      29             : 
      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>
      35      202386 :     Seed("rng-seed", cl::value_desc("seed"), cl::Hidden,
      36      404772 :          cl::desc("Seed for the random number generator"), cl::init(0));
      37             : 
      38           2 : RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
      39             :   LLVM_DEBUG(if (Seed == 0) dbgs()
      40             :              << "Warning! Using unseeded random number generator.\n");
      41             : 
      42             :   // Combine seed and salts using std::seed_seq.
      43             :   // Data: Seed-low, Seed-high, Salt
      44             :   // Note: std::seed_seq can only store 32-bit values, even though we
      45             :   // are using a 64-bit RNG. This isn't a problem since the Mersenne
      46             :   // twister constructor copies these correctly into its initial state.
      47             :   std::vector<uint32_t> Data;
      48           2 :   Data.resize(2 + Salt.size());
      49           2 :   Data[0] = Seed;
      50           2 :   Data[1] = Seed >> 32;
      51             : 
      52             :   std::copy(Salt.begin(), Salt.end(), Data.begin() + 2);
      53             : 
      54             :   std::seed_seq SeedSeq(Data.begin(), Data.end());
      55           2 :   Generator.seed(SeedSeq);
      56           2 : }
      57             : 
      58          20 : RandomNumberGenerator::result_type RandomNumberGenerator::operator()() {
      59          20 :   return Generator();
      60             : }
      61             : 
      62             : // Get random vector of specified size
      63          57 : std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) {
      64             : #ifdef _WIN32
      65             :   HCRYPTPROV hProvider;
      66             :   if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
      67             :                            CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
      68             :     ScopedCryptContext ScopedHandle(hProvider);
      69             :     if (CryptGenRandom(hProvider, Size, static_cast<BYTE *>(Buffer)))
      70             :       return std::error_code();
      71             :   }
      72             :   return std::error_code(GetLastError(), std::system_category());
      73             : #else
      74          57 :   int Fd = open("/dev/urandom", O_RDONLY);
      75          57 :   if (Fd != -1) {
      76             :     std::error_code Ret;
      77          57 :     ssize_t BytesRead = read(Fd, Buffer, Size);
      78          57 :     if (BytesRead == -1)
      79           0 :       Ret = std::error_code(errno, std::system_category());
      80          57 :     else if (BytesRead != static_cast<ssize_t>(Size))
      81             :       Ret = std::error_code(EIO, std::system_category());
      82          57 :     if (close(Fd) == -1)
      83           0 :       Ret = std::error_code(errno, std::system_category());
      84             : 
      85          57 :     return Ret;
      86             :   }
      87           0 :   return std::error_code(errno, std::system_category());
      88             : #endif
      89      303579 : }

Generated by: LCOV version 1.13