fix Windows C++ compilation in combination with dynamic overriding by preferring RtlGenRandom
This commit is contained in:
parent
e18a8cd72e
commit
adc8b3187c
20
src/random.c
20
src/random.c
@ -168,16 +168,10 @@ If we cannot get good randomness, we fall back to weak randomness based on a tim
|
|||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
|
||||||
#if !defined(MI_USE_RTLGENRANDOM)
|
#if defined(MI_USE_RTLGENRANDOM) || defined(__cplusplus)
|
||||||
// We prefer to use BCryptGenRandom instead of RtlGenRandom but it can lead to a deadlock
|
// We prefer to use BCryptGenRandom instead of (the unofficial) RtlGenRandom but when using
|
||||||
// under the VS debugger when using dynamic overriding.
|
// dynamic overriding, we observed it can raise an exception when compiled with C++, and
|
||||||
#pragma comment (lib,"bcrypt.lib")
|
// sometimes deadlocks when also running under the VS debugger.
|
||||||
#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);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
// Use (unofficial) RtlGenRandom
|
|
||||||
#pragma comment (lib,"advapi32.lib")
|
#pragma comment (lib,"advapi32.lib")
|
||||||
#define RtlGenRandom SystemFunction036
|
#define RtlGenRandom SystemFunction036
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@ -190,6 +184,12 @@ BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
|
|||||||
static bool os_random_buf(void* buf, size_t buf_len) {
|
static bool os_random_buf(void* buf, size_t buf_len) {
|
||||||
return (RtlGenRandom(buf, (ULONG)buf_len) != 0);
|
return (RtlGenRandom(buf, (ULONG)buf_len) != 0);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
#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);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
|
Loading…
Reference in New Issue
Block a user