Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/RandomNumberGenerator.cpp
Line
Count
Source (jump to first uncovered line)
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
15
#include "llvm/Support/RandomNumberGenerator.h"
16
#include "llvm/Support/CommandLine.h"
17
#include "llvm/Support/Debug.h"
18
#include "llvm/Support/raw_ostream.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
static cl::opt<uint64_t> Seed("rng-seed", cl::value_desc("seed"), cl::Hidden,
30
                              cl::desc("Seed for the random number generator"),
31
                              cl::init(0));
32
33
2
RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
34
2
  LLVM_DEBUG(if (Seed == 0) dbgs()
35
2
             << "Warning! Using unseeded random number generator.\n");
36
2
37
2
  // Combine seed and salts using std::seed_seq.
38
2
  // Data: Seed-low, Seed-high, Salt
39
2
  // Note: std::seed_seq can only store 32-bit values, even though we
40
2
  // are using a 64-bit RNG. This isn't a problem since the Mersenne
41
2
  // twister constructor copies these correctly into its initial state.
42
2
  std::vector<uint32_t> Data;
43
2
  Data.resize(2 + Salt.size());
44
2
  Data[0] = Seed;
45
2
  Data[1] = Seed >> 32;
46
2
47
2
  llvm::copy(Salt, Data.begin() + 2);
48
2
49
2
  std::seed_seq SeedSeq(Data.begin(), Data.end());
50
2
  Generator.seed(SeedSeq);
51
2
}
52
53
20
RandomNumberGenerator::result_type RandomNumberGenerator::operator()() {
54
20
  return Generator();
55
20
}
56
57
// Get random vector of specified size
58
1
std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) {
59
#ifdef _WIN32
60
  HCRYPTPROV hProvider;
61
  if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
62
                           CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
63
    ScopedCryptContext ScopedHandle(hProvider);
64
    if (CryptGenRandom(hProvider, Size, static_cast<BYTE *>(Buffer)))
65
      return std::error_code();
66
  }
67
  return std::error_code(GetLastError(), std::system_category());
68
#else
69
  int Fd = open("/dev/urandom", O_RDONLY);
70
1
  if (Fd != -1) {
71
1
    std::error_code Ret;
72
1
    ssize_t BytesRead = read(Fd, Buffer, Size);
73
1
    if (BytesRead == -1)
74
0
      Ret = std::error_code(errno, std::system_category());
75
1
    else if (BytesRead != static_cast<ssize_t>(Size))
76
0
      Ret = std::error_code(EIO, std::system_category());
77
1
    if (close(Fd) == -1)
78
0
      Ret = std::error_code(errno, std::system_category());
79
1
80
1
    return Ret;
81
1
  }
82
0
  return std::error_code(errno, std::system_category());
83
0
#endif
84
0
}