use RtlGenRandom on windows to enable compilation as C++ even with dynamic override
@@ -162,20 +162,29 @@ If we cannot get good randomness, we fall back to weak randomness based on a tim
-----------------------------------------------------------------------------*/
#if defined(_WIN32)
+/*
+// We prefer BCryptGenRandom over RtlGenRandom but it leads to a crash a when using dynamic override combined with the C++ runtime :-(
#pragma comment (lib,"bcrypt.lib")
#include <bcrypt.h>
static bool os_random_buf(void* buf, size_t buf_len) {
return (BCryptGenRandom(NULL, (PUCHAR)buf, (ULONG)buf_len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0);
}
-/*
-#define SystemFunction036 NTAPI SystemFunction036
-#include <NTSecAPI.h>
-#undef SystemFunction036
+*/
+#define RtlGenRandom SystemFunction036
+#ifdef __cplusplus
+extern "C" {
+#endif
+BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
+#ifdef __cplusplus
+}
+#endif
static bool os_random_buf(void* buf, size_t buf_len) {
+ mi_assert_internal(buf_len >= sizeof(uintptr_t));
+ memset(buf, 0, buf_len);
RtlGenRandom(buf, (ULONG)buf_len);
- return true;
+ return (((uintptr_t*)buf)[0] != 0); // sanity check (but RtlGenRandom should never fail)
}
-*/
+
#elif defined(ANDROID) || defined(XP_DARWIN) || defined(__APPLE__) || defined(__DragonFly__) || \
defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
defined(__sun) || defined(__wasi__)
