diff --git a/src/random.c b/src/random.c index 5c093a91..be95fc46 100644 --- a/src/random.c +++ b/src/random.c @@ -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 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 -#undef SystemFunction036 -static bool os_random_buf(void* buf, size_t buf_len) { - RtlGenRandom(buf, (ULONG)buf_len); - return true; -} */ +#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 (((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__)