Skip to content

Commit ea877d4

Browse files
committed
Implement getRandomBytes() function
This function allows getting arbitrary sized block of random bytes. Primary motivation is support for --build-id=uuid in lld. Differential revision: https://reviews.llvm.org/D23671 llvm-svn: 279807
1 parent 8f27f51 commit ea877d4

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

llvm/include/llvm/Support/RandomNumberGenerator.h

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/Support/Compiler.h"
2020
#include "llvm/Support/DataTypes.h" // Needed for uint64_t on Windows.
2121
#include <random>
22+
#include <system_error>
2223

2324
namespace llvm {
2425
class StringRef;
@@ -53,6 +54,9 @@ class RandomNumberGenerator {
5354

5455
friend class Module;
5556
};
57+
58+
// Get random vector of specified size
59+
std::error_code getRandomBytes(void *Buffer, size_t Size);
5660
}
5761

5862
#endif

llvm/lib/Support/RandomNumberGenerator.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
#include "llvm/Support/CommandLine.h"
1818
#include "llvm/Support/Debug.h"
1919
#include "llvm/Support/raw_ostream.h"
20+
#ifdef LLVM_ON_WIN32
21+
#include "Windows/WindowsSupport.h"
22+
#else
23+
#include "Unix/Unix.h"
24+
#endif
2025

2126
using namespace llvm;
2227

@@ -55,3 +60,32 @@ RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
5560
uint_fast64_t RandomNumberGenerator::operator()() {
5661
return Generator();
5762
}
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+
}

llvm/lib/Support/Unix/Unix.h

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
# include <dlfcn.h>
4949
#endif
5050

51+
#ifdef HAVE_FCNTL_H
52+
# include <fcntl.h>
53+
#endif
54+
5155
/// This function builds an error message into \p ErrMsg using the \p prefix
5256
/// string and the Unix error number given by \p errnum. If errnum is -1, the
5357
/// default then the value of errno is used.

0 commit comments

Comments
 (0)