[PATCH] D23671: libLLVMSupport: Generate random block of arbitrary size
Eugene Leviant via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 18 08:58:51 PDT 2016
evgeny777 created this revision.
evgeny777 added a reviewer: lattner.
evgeny777 added subscribers: llvm-commits, grimar.
evgeny777 set the repository for this revision to rL LLVM.
ruiu suggested doing this here:
https://reviews.llvm.org/D23349
Repository:
rL LLVM
https://reviews.llvm.org/D23671
Files:
include/llvm/Support/RandomNumberGenerator.h
lib/Support/RandomNumberGenerator.cpp
lib/Support/Unix/Unix.h
Index: lib/Support/Unix/Unix.h
===================================================================
--- lib/Support/Unix/Unix.h
+++ lib/Support/Unix/Unix.h
@@ -48,6 +48,10 @@
# include <dlfcn.h>
#endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+
/// This function builds an error message into \p ErrMsg using the \p prefix
/// string and the Unix error number given by \p errnum. If errnum is -1, the
/// default then the value of errno is used.
Index: lib/Support/RandomNumberGenerator.cpp
===================================================================
--- lib/Support/RandomNumberGenerator.cpp
+++ 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,24 @@
uint_fast64_t RandomNumberGenerator::operator()() {
return Generator();
}
+
+// Get random vector of specified size
+std::error_code getRandomBytes(void *Buffer, size_t Size) {
+#ifdef LLVM_ON_WIN32
+ HCRYPTPROV hProvider;
+ if (CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL,
+ CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
+ ScopedCryptContext ScopedHandle(hProvider);
+ if (CryptGenRandom(hProvider, Size, Buffer))
+ return std::error_code();
+ }
+ return std::error_code(GetLastError(), std::system_category());
+#else
+ int Fd = open("/dev/urandom", O_RDONLY);
+ if (Fd == -1)
+ return std::error_code(errno, std::system_category());
+ read(Fd, Buffer, Size);
+ close(Fd);
+ return std::error_code();
+#endif
+}
Index: include/llvm/Support/RandomNumberGenerator.h
===================================================================
--- include/llvm/Support/RandomNumberGenerator.h
+++ include/llvm/Support/RandomNumberGenerator.h
@@ -19,6 +19,7 @@
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h" // Needed for uint64_t on Windows.
#include <random>
+#include <system_error>
namespace llvm {
class StringRef;
@@ -53,6 +54,9 @@
friend class Module;
};
+
+// Get random vector of specified size
+std::error_code getRandomBytes(void *Buffer, size_t Size);
}
#endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23671.68546.patch
Type: text/x-patch
Size: 2289 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160818/9cd5a945/attachment.bin>
More information about the llvm-commits
mailing list