Skip to content

[Support] Move loadSystemModuleSecure into Process.inc. NFC. - #177598

Merged
aganea merged 1 commit into
llvm:mainfrom
aganea:fix/move_loadSystemModuleSecure
Jan 23, 2026
Merged

aganea merged 1 commit into
llvm:mainfrom
aganea:fix/move_loadSystemModuleSecure

Conversation

@aganea

@aganea aganea commented Jan 23, 2026

Copy link
Copy Markdown
Member

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 #169224 (comment)

@aganea aganea changed the title [Support] Move loadSystemModuleSecure into Process.inc [Support] Move loadSystemModuleSecure into Process.inc. NFC. Jan 23, 2026
@llvmbot

llvmbot commented Jan 23, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-platform-windows

Author: Alexandre Ganea (aganea)

Changes

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 #169224 (comment)


Full diff: https://github.com/llvm/llvm-project/pull/177598.diff

2 Files Affected:

  • (modified) llvm/lib/Support/Windows/Process.inc (+29)
  • (modified) llvm/lib/Support/Windows/Threading.inc (-29)
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");

@llvmbot

llvmbot commented Jan 23, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-llvm-support

Author: Alexandre Ganea (aganea)

Changes

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 #169224 (comment)


Full diff: https://github.com/llvm/llvm-project/pull/177598.diff

2 Files Affected:

  • (modified) llvm/lib/Support/Windows/Process.inc (+29)
  • (modified) llvm/lib/Support/Windows/Threading.inc (-29)
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");

@Meinersbur Meinersbur left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@aganea
aganea merged commit 70ee6e4 into llvm:main Jan 23, 2026
@sean-mcmanus

Copy link
Copy Markdown

@aganea Can you port this to release/22.x ? llvmorg-22-init was good and this issue started reproing with llvmorg-22.1.0-rc1.

@aganea

aganea commented Jan 28, 2026

Copy link
Copy Markdown
Member Author

/cherry-pick 70ee6e4

@llvmbot

llvmbot commented Jan 28, 2026

Copy link
Copy Markdown
Member

@llvmbot

llvmbot commented Jan 28, 2026

Copy link
Copy Markdown
Member

/pull-request #178431

c-rhodes pushed a commit to llvmbot/llvm-project that referenced this pull request Feb 2, 2026
…7598)

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
llvm#169224 (comment)

(cherry picked from commit 70ee6e4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

4 participants