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
diff --git a/llvm/lib/Support/RandomNumberGenerator.cpp b/llvm/lib/Support/RandomNumberGenerator.cpp
index 81d0411..087c671 100644
--- a/llvm/lib/Support/RandomNumberGenerator.cpp
+++ b/llvm/lib/Support/RandomNumberGenerator.cpp
@@ -17,6 +17,11 @@
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
+#ifdef LLVM_ON_WIN32
+#include "Windows/WindowsSupport.h"
+#else
+#include "Unix/Unix.h"
+#endif
 
 using namespace llvm;
 
@@ -55,3 +60,32 @@
 uint_fast64_t RandomNumberGenerator::operator()() {
   return Generator();
 }
+
+// Get random vector of specified size
+std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) {
+#ifdef LLVM_ON_WIN32
+  HCRYPTPROV hProvider;
+  if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
+                           CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
+    ScopedCryptContext ScopedHandle(hProvider);
+    if (CryptGenRandom(hProvider, Size, static_cast<BYTE *>(Buffer)))
+      return std::error_code();
+  }
+  return std::error_code(GetLastError(), std::system_category());
+#else
+  int Fd = open("/dev/urandom", O_RDONLY);
+  if (Fd != -1) {
+    std::error_code Ret;
+    ssize_t BytesRead = read(Fd, Buffer, Size);
+    if (BytesRead == -1)
+      Ret = std::error_code(errno, std::system_category());
+    else if (BytesRead != static_cast<ssize_t>(Size))
+      Ret = std::error_code(EIO, std::system_category());
+    if (close(Fd) == -1)
+      Ret = std::error_code(errno, std::system_category());
+
+    return Ret;
+  }
+  return std::error_code(errno, std::system_category());
+#endif
+}