From c3aaed1f23ddf347e27745a35a449622e92728da Mon Sep 17 00:00:00 2001 From: Justin Smith Date: Mon, 11 May 2026 14:00:58 -0400 Subject: [PATCH] Enable Windows 7 compat path on MinGW builds The `AWSLC_WINDOWS_7_COMPAT` branch in crypto/rand_extra/windows.c was guarded by `!defined(__MINGW32__)`, which prevented it from being taken on MinGW even though the top-level CMakeLists.txt already sets `-D_WIN32_WINNT=_WIN32_WINNT_WIN7` for MINGW-and-not-Clang builds. As a result, MinGW builds took the ProcessPrng path and aborted at runtime on Windows 7 (GetProcAddress for ProcessPrng returns NULL on Win7). Drop the MinGW guard so the Win7 path is selected when _WIN32_WINNT targets Win7, and add an explicit `bcrypt` link for MinGW since it ignores the `#pragma comment(lib, "bcrypt.lib")` used by MSVC. Addresses aws/aws-lc-rs#1111 --- crypto/CMakeLists.txt | 9 +++++++++ crypto/rand_extra/windows.c | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt index c7f3c22f4c3..9c5e63628a7 100644 --- a/crypto/CMakeLists.txt +++ b/crypto/CMakeLists.txt @@ -607,6 +607,15 @@ function(build_libcrypto) endif() if(WIN32) target_link_libraries(${arg_NAME} PUBLIC ws2_32) + # MinGW (non-Clang) builds target Windows 7 (see the top-level + # CMakeLists.txt, which adds -D_WIN32_WINNT=_WIN32_WINNT_WIN7), so + # `crypto/rand_extra/windows.c` compiles under the AWSLC_WINDOWS_7_COMPAT + # branch and calls BCryptGenRandom from bcrypt.dll. On MSVC this is + # handled by `#pragma comment(lib, "bcrypt.lib")` in that file, but + # MinGW ignores that pragma so we need to add the link explicitly. + if(MINGW) + target_link_libraries(${arg_NAME} PUBLIC bcrypt) + endif() endif() if(AWSLC_LINK_THREADS) diff --git a/crypto/rand_extra/windows.c b/crypto/rand_extra/windows.c index 91bd66469dc..cdbb0826a19 100644 --- a/crypto/rand_extra/windows.c +++ b/crypto/rand_extra/windows.c @@ -16,7 +16,10 @@ OPENSSL_MSVC_PRAGMA(warning(push, 3)) #include // ProcessPrng (from `bcryptprimitives.dll`) is only available on Windows 8+. -#if !defined(__MINGW32__) && defined(_WIN32_WINNT) && _WIN32_WINNT <= _WIN32_WINNT_WIN7 +// Fall back to BCryptGenRandom when targeting Windows 7 or earlier. This applies +// to both MSVC and MinGW-w64 toolchains; MinGW-w64 ships `bcrypt.h` and can link +// against `libbcrypt.a` / `bcrypt.dll`. +#if defined(_WIN32_WINNT) && _WIN32_WINNT <= _WIN32_WINNT_WIN7 #define AWSLC_WINDOWS_7_COMPAT #endif