From e740242978b9f74a090570738035ea4d5a45602c Mon Sep 17 00:00:00 2001 From: daan Date: Sun, 6 Sep 2020 08:29:08 -0700 Subject: [PATCH 1/3] link with advapi32 on windows --- CMakeLists.txt | 2 +- azure-pipelines.yml | 8 ++++---- src/random.c | 5 +++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 98b55ae0..81362175 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -191,7 +191,7 @@ endif() # extra needed libraries if(WIN32) - list(APPEND mi_libraries psapi shell32 user32 bcrypt) + list(APPEND mi_libraries psapi shell32 user32 advapi32) else() if(NOT ${CMAKE_C_COMPILER} MATCHES "android") list(APPEND mi_libraries pthread) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 5ffb1ef3..43f128f0 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -36,10 +36,10 @@ jobs: inputs: solution: $(BuildType)/libmimalloc.sln configuration: '$(MSBuildConfiguration)' - #- script: | - # cd $(BuildType) - # ctest --verbose --timeout 120 - # displayName: CTest + - script: | + cd $(BuildType) + ctest --verbose --timeout 120 + displayName: CTest #- script: $(BuildType)\$(BuildType)\mimalloc-test-stress # displayName: TestStress #- upload: $(Build.SourcesDirectory)/$(BuildType) diff --git a/src/random.c b/src/random.c index a604b49b..2d15c187 100644 --- a/src/random.c +++ b/src/random.c @@ -170,6 +170,7 @@ 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); } */ +#pragma comment (lib,"advapi32.lib") #define RtlGenRandom SystemFunction036 #ifdef __cplusplus extern "C" { @@ -181,8 +182,8 @@ BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength); 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) + bool ok = (RtlGenRandom(buf, (ULONG)buf_len) != 0); + return ok; } #elif defined(ANDROID) || defined(XP_DARWIN) || defined(__APPLE__) || defined(__DragonFly__) || \ From f7b94fe21c41d5890cda86f689973e554eb945df Mon Sep 17 00:00:00 2001 From: daan Date: Sun, 6 Sep 2020 08:33:27 -0700 Subject: [PATCH 2/3] experiment with bcrypt api again --- CMakeLists.txt | 2 +- src/random.c | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 81362175..35460e85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -191,7 +191,7 @@ endif() # extra needed libraries if(WIN32) - list(APPEND mi_libraries psapi shell32 user32 advapi32) + list(APPEND mi_libraries psapi shell32 user32 advapi32 bcrypt) else() if(NOT ${CMAKE_C_COMPILER} MATCHES "android") list(APPEND mi_libraries pthread) diff --git a/src/random.c b/src/random.c index 2d15c187..a03e80c4 100644 --- a/src/random.c +++ b/src/random.c @@ -162,14 +162,13 @@ 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); } -*/ +/* #pragma comment (lib,"advapi32.lib") #define RtlGenRandom SystemFunction036 #ifdef __cplusplus @@ -185,7 +184,7 @@ static bool os_random_buf(void* buf, size_t buf_len) { bool ok = (RtlGenRandom(buf, (ULONG)buf_len) != 0); return ok; } - +*/ #elif defined(ANDROID) || defined(XP_DARWIN) || defined(__APPLE__) || defined(__DragonFly__) || \ defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \ defined(__sun) || defined(__wasi__) From 9113281165d40d86025f32641f8c14a64e5fd21d Mon Sep 17 00:00:00 2001 From: daan Date: Sun, 6 Sep 2020 08:50:23 -0700 Subject: [PATCH 3/3] switch back to using bcryptrandom number generation on Windows to fix azure pipeline tests --- src/random.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/random.c b/src/random.c index a03e80c4..b9485ea0 100644 --- a/src/random.c +++ b/src/random.c @@ -155,20 +155,23 @@ uintptr_t _mi_random_next(mi_random_ctx_t* ctx) { /* ---------------------------------------------------------------------------- To initialize a fresh random context we rely on the OS: -- Windows : RtlGenRandom +- Windows : BCryptGenRandom (or RtlGenRandom) - osX,bsd,wasi: arc4random_buf - Linux : getrandom,/dev/urandom If we cannot get good randomness, we fall back to weak randomness based on a timer and ASLR. -----------------------------------------------------------------------------*/ #if defined(_WIN32) -// We prefer BCryptGenRandom over RtlGenRandom but it leads to a crash a when using dynamic override combined with the C++ runtime :-( + +#if !defined(MI_USE_RTLGENRANDOM) +// We prefer BCryptGenRandom over RtlGenRandom #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); } -/* +#else +// Use (unofficial) RtlGenRandom #pragma comment (lib,"advapi32.lib") #define RtlGenRandom SystemFunction036 #ifdef __cplusplus @@ -179,12 +182,10 @@ BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength); } #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); - bool ok = (RtlGenRandom(buf, (ULONG)buf_len) != 0); - return ok; + return (RtlGenRandom(buf, (ULONG)buf_len) != 0); } -*/ +#endif + #elif defined(ANDROID) || defined(XP_DARWIN) || defined(__APPLE__) || defined(__DragonFly__) || \ defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \ defined(__sun) || defined(__wasi__) @@ -252,6 +253,7 @@ static bool os_random_buf(void* buf, size_t buf_len) { uintptr_t _os_random_weak(uintptr_t extra_seed) { uintptr_t x = (uintptr_t)&_os_random_weak ^ extra_seed; // ASLR makes the address random + #if defined(_WIN32) LARGE_INTEGER pcount; QueryPerformanceCounter(&pcount);