[llvm] [Support][Cygwin] Fix handling of Process symbol lookup. (PR #143072)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 7 10:45:30 PDT 2025


https://github.com/jeremyd2019 updated https://github.com/llvm/llvm-project/pull/143072

>From 929527a9ddfe427631f13d52ac7ba5a293a7e8ec Mon Sep 17 00:00:00 2001
From: Jeremy Drake <github at jdrake.com>
Date: Thu, 5 Jun 2025 23:01:09 -0700
Subject: [PATCH 1/3] [Support][Cygwin] Fix handling of Process symbol lookup.

In Unix/DynamicLibrary.inc, it was already known that Cygwin required
use of RTLD_DEFAULT as the "Handle" parameter to DLSym to search all
modules for a symbol.  Unfortunately RTLD_DEFAULT is defined as NULL, so
the existing checks of the "Process" handle meant DLSym would never be
called on Cygwin.  Add a bool to indicate whether the Process handle was
set instead.
---
 llvm/lib/Support/DynamicLibrary.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Support/DynamicLibrary.cpp b/llvm/lib/Support/DynamicLibrary.cpp
index 531c035ab9266..34c2dc2e56227 100644
--- a/llvm/lib/Support/DynamicLibrary.cpp
+++ b/llvm/lib/Support/DynamicLibrary.cpp
@@ -26,6 +26,7 @@ class DynamicLibrary::HandleSet {
   typedef std::vector<void *> HandleList;
   HandleList Handles;
   void *Process = nullptr;
+  bool ProcessAdded = false;
 
 public:
   static void *DLOpen(const char *Filename, std::string *Err);
@@ -66,6 +67,7 @@ class DynamicLibrary::HandleSet {
       }
 #endif
       Process = Handle;
+      ProcessAdded = true;
     }
     return true;
   }
@@ -97,11 +99,11 @@ class DynamicLibrary::HandleSet {
     assert(!((Order & SO_LoadedFirst) && (Order & SO_LoadedLast)) &&
            "Invalid Ordering");
 
-    if (!Process || (Order & SO_LoadedFirst)) {
+    if (!ProcessAdded || (Order & SO_LoadedFirst)) {
       if (void *Ptr = LibLookup(Symbol, Order))
         return Ptr;
     }
-    if (Process) {
+    if (ProcessAdded) {
       // Use OS facilities to search the current binary and all loaded libs.
       if (void *Ptr = DLSym(Process, Symbol))
         return Ptr;

>From 67456eff7af30fa90823f2e3ee6dc70d1cdfb93e Mon Sep 17 00:00:00 2001
From: Jeremy Drake <github at jdrake.com>
Date: Fri, 6 Jun 2025 22:43:26 -0700
Subject: [PATCH 2/3] Initialize Process to &Invalid instead of nullptr, so an
 additional bool is not needed

---
 llvm/lib/Support/DynamicLibrary.cpp | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Support/DynamicLibrary.cpp b/llvm/lib/Support/DynamicLibrary.cpp
index 34c2dc2e56227..f1c15c00cedea 100644
--- a/llvm/lib/Support/DynamicLibrary.cpp
+++ b/llvm/lib/Support/DynamicLibrary.cpp
@@ -25,8 +25,7 @@ using namespace llvm::sys;
 class DynamicLibrary::HandleSet {
   typedef std::vector<void *> HandleList;
   HandleList Handles;
-  void *Process = nullptr;
-  bool ProcessAdded = false;
+  void *Process = &Invalid;
 
 public:
   static void *DLOpen(const char *Filename, std::string *Err);
@@ -59,7 +58,7 @@ class DynamicLibrary::HandleSet {
       Handles.push_back(Handle);
     } else {
 #ifndef _WIN32
-      if (Process) {
+      if (Process != &Invalid) {
         if (CanClose)
           DLClose(Process);
         if (Process == Handle)
@@ -67,7 +66,6 @@ class DynamicLibrary::HandleSet {
       }
 #endif
       Process = Handle;
-      ProcessAdded = true;
     }
     return true;
   }
@@ -99,11 +97,11 @@ class DynamicLibrary::HandleSet {
     assert(!((Order & SO_LoadedFirst) && (Order & SO_LoadedLast)) &&
            "Invalid Ordering");
 
-    if (!ProcessAdded || (Order & SO_LoadedFirst)) {
+    if (Process == &Invalid || (Order & SO_LoadedFirst)) {
       if (void *Ptr = LibLookup(Symbol, Order))
         return Ptr;
     }
-    if (ProcessAdded) {
+    if (Process != &Invalid) {
       // Use OS facilities to search the current binary and all loaded libs.
       if (void *Ptr = DLSym(Process, Symbol))
         return Ptr;

>From 7c658e7d484cb816daaaefb5dfd59970a74a4ce0 Mon Sep 17 00:00:00 2001
From: Jeremy Drake <github at jdrake.com>
Date: Sat, 7 Jun 2025 10:44:54 -0700
Subject: [PATCH 3/3] fixup! Initialize Process to &Invalid instead of nullptr,
 so an additional bool is not needed

also fix platform .inc files
---
 llvm/lib/Support/Unix/DynamicLibrary.inc    | 2 +-
 llvm/lib/Support/Windows/DynamicLibrary.inc | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Support/Unix/DynamicLibrary.inc b/llvm/lib/Support/Unix/DynamicLibrary.inc
index 7452913049ebb..b6f8e38a66122 100644
--- a/llvm/lib/Support/Unix/DynamicLibrary.inc
+++ b/llvm/lib/Support/Unix/DynamicLibrary.inc
@@ -17,7 +17,7 @@ DynamicLibrary::HandleSet::~HandleSet() {
   // Close the libraries in reverse order.
   for (void *Handle : llvm::reverse(Handles))
     ::dlclose(Handle);
-  if (Process)
+  if (Process != &Invalid)
     ::dlclose(Process);
 
   // llvm_shutdown called, Return to default
diff --git a/llvm/lib/Support/Windows/DynamicLibrary.inc b/llvm/lib/Support/Windows/DynamicLibrary.inc
index c434bd62f04c5..4f8c96e78f6ce 100644
--- a/llvm/lib/Support/Windows/DynamicLibrary.inc
+++ b/llvm/lib/Support/Windows/DynamicLibrary.inc
@@ -26,7 +26,7 @@ DynamicLibrary::HandleSet::~HandleSet() {
     FreeLibrary(HMODULE(Handle));
 
   // 'Process' should not be released on Windows.
-  assert((!Process || Process == this) && "Bad Handle");
+  assert((Process == &Invalid || Process == this) && "Bad Handle");
   // llvm_shutdown called, Return to default
   DynamicLibrary::SearchOrder = DynamicLibrary::SO_Linker;
 }
@@ -60,7 +60,7 @@ static DynamicLibrary::HandleSet *IsOpenedHandlesInstance(void *Handle) {
 
 void DynamicLibrary::HandleSet::DLClose(void *Handle) {
   if (HandleSet *HS = IsOpenedHandlesInstance(Handle))
-    HS->Process = nullptr; // Just drop the *Process* handle.
+    HS->Process = &Invalid; // Just drop the *Process* handle.
   else
     FreeLibrary((HMODULE)Handle);
 }
@@ -89,7 +89,7 @@ void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
     return (void *)uintptr_t(GetProcAddress((HMODULE)Handle, Symbol));
 
   // Could have done a dlclose on the *Process* handle
-  if (!HS->Process)
+  if (HS->Process == &Invalid)
     return nullptr;
 
   // Trials indicate EnumProcessModulesEx is consistantly faster than using



More information about the llvm-commits mailing list