https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/178431
Backport 70ee6e4 Requested by: @aganea >From 38efb374f55ea25a0738fd78c493ff6ecde9bb78 Mon Sep 17 00:00:00 2001 From: Alexandre Ganea <[email protected]> Date: Fri, 23 Jan 2026 10:58:42 -0500 Subject: [PATCH] [Support] Move loadSystemModuleSecure into Process.inc. NFC. (#177598) Move Windows-specific function `llvm::sys::windows::loadSystemModuleSecure` from `lib/Support/Windows/Threading.inc` into `lib/Support/Windows/Process.inc`. This is to fix link problems on Windows, see https://github.com/llvm/llvm-project/pull/169224#issuecomment-3790350128 (cherry picked from commit 70ee6e4427c8f55a910193bbda2eadf75e8a75f2) --- llvm/lib/Support/Windows/Process.inc | 29 ++++++++++++++++++++++++++ llvm/lib/Support/Windows/Threading.inc | 29 -------------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/llvm/lib/Support/Windows/Process.inc b/llvm/lib/Support/Windows/Process.inc index c4d18ca7375f2..01c05bb21628f 100644 --- a/llvm/lib/Support/Windows/Process.inc +++ b/llvm/lib/Support/Windows/Process.inc @@ -521,3 +521,32 @@ bool llvm::RunningWindows11OrGreater() { TerminateProcess(GetCurrentProcess(), RetCode); llvm_unreachable("TerminateProcess doesn't return"); } + +namespace llvm::sys::windows { +HMODULE loadSystemModuleSecure(LPCWSTR lpModuleName) { + // Ensure we load indeed a module from system32 path. + // As per GetModuleHandle documentation: + // "If lpModuleName does not include a path and there is more than one loaded + // module with the same base name and extension, you cannot predict which + // module handle will be returned.". This mitigates + // https://learn.microsoft.com/en-us/security-updates/securityadvisories/2010/2269637 + SmallVector<wchar_t, MAX_PATH> Buf; + size_t Size = MAX_PATH; + do { + Buf.resize_for_overwrite(Size); + SetLastError(NO_ERROR); + Size = ::GetSystemDirectoryW(Buf.data(), Buf.size()); + if (Size == 0) + return NULL; + + // Try again with larger buffer. + } while (Size > Buf.size()); + + Buf.truncate(Size); + Buf.push_back(L'\\'); + Buf.append(lpModuleName, lpModuleName + std::wcslen(lpModuleName)); + Buf.push_back(0); + + return ::GetModuleHandleW(Buf.data()); +} +} // namespace llvm::sys::windows diff --git a/llvm/lib/Support/Windows/Threading.inc b/llvm/lib/Support/Windows/Threading.inc index 968423b98486c..86e46b99cb38b 100644 --- a/llvm/lib/Support/Windows/Threading.inc +++ b/llvm/lib/Support/Windows/Threading.inc @@ -105,35 +105,6 @@ void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); } -namespace llvm::sys::windows { -HMODULE loadSystemModuleSecure(LPCWSTR lpModuleName) { - // Ensure we load indeed a module from system32 path. - // As per GetModuleHandle documentation: - // "If lpModuleName does not include a path and there is more than one loaded - // module with the same base name and extension, you cannot predict which - // module handle will be returned.". This mitigates - // https://learn.microsoft.com/en-us/security-updates/securityadvisories/2010/2269637 - SmallVector<wchar_t, MAX_PATH> Buf; - size_t Size = MAX_PATH; - do { - Buf.resize_for_overwrite(Size); - SetLastError(NO_ERROR); - Size = ::GetSystemDirectoryW(Buf.data(), Buf.size()); - if (Size == 0) - return NULL; - - // Try again with larger buffer. - } while (Size > Buf.size()); - - Buf.truncate(Size); - Buf.push_back(L'\\'); - Buf.append(lpModuleName, lpModuleName + std::wcslen(lpModuleName)); - Buf.push_back(0); - - return ::GetModuleHandleW(Buf.data()); -} -} // namespace llvm::sys::windows - SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) { #ifdef THREAD_POWER_THROTTLING_CURRENT_VERSION HMODULE kernelM = llvm::sys::windows::loadSystemModuleSecure(L"kernel32.dll"); _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
