Skip to content

Commit 284048f

Browse files
committed
Cygwin: dlsym: fetch module list via EnumProcessModules
We're using RtlQueryProcessDebugInformation from dlsym since commit 31ddf45 ("* autoload.cc (EnumProcessModules): Remove.") Observations on the Cygwin mailing list show that calling RtlQueryProcessDebugInformation on a process is neither thread-safe, nor multi-process-safe, see https://cygwin.com/pipermail/cygwin/2025-July/258403.html for details. This patch essentially reverts 31ddf45. Fetch the list of loaded modules in the current process by calling EnumProcessModules again. Reported-by: Christian Franke <[email protected]> Fixes: 31ddf45 ("* autoload.cc (EnumProcessModules): Remove.") Signed-off-by: Corinna Vinschen <[email protected]> (cherry picked from commit 8ac2bc46b186ba3234e5f50931805308ead4af8d)
1 parent 47c7455 commit 284048f

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

winsup/cygwin/autoload.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ LoadDLLfunc (CoInitialize, ole32)
525525
LoadDLLfunc (CoUninitialize, ole32)
526526
LoadDLLfunc (CoTaskMemFree, ole32)
527527

528+
LoadDLLfunc (EnumProcessModules, psapi)
529+
528530
LoadDLLfunc (LsaConnectUntrusted, secur32)
529531
LoadDLLfunc (LsaDeregisterLogonProcess, secur32)
530532
LoadDLLfunc (LsaFreeReturnBuffer, secur32)

winsup/cygwin/dlfcn.cc

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Cygwin license. Please consult the file "CYGWIN_LICENSE" for
77
details. */
88

99
#include "winsup.h"
10+
#include <psapi.h>
1011
#include <stdlib.h>
1112
#include <dlfcn.h>
1213
#include <ctype.h>
@@ -318,32 +319,18 @@ dlsym (void *handle, const char *name)
318319

319320
if (handle == RTLD_DEFAULT)
320321
{ /* search all modules */
321-
PDEBUG_BUFFER buf;
322-
NTSTATUS status;
322+
HMODULE *modules;
323+
tmp_pathbuf tp;
324+
DWORD size;
323325

324-
buf = RtlCreateQueryDebugBuffer (0, FALSE);
325-
if (!buf)
326-
{
327-
set_errno (ENOMEM);
328-
set_dl_error ("dlsym");
329-
return NULL;
330-
}
331-
status = RtlQueryProcessDebugInformation (GetCurrentProcessId (),
332-
PDI_MODULES, buf);
333-
if (!NT_SUCCESS (status))
334-
__seterrno_from_nt_status (status);
326+
modules = (HMODULE *) tp.w_get ();
327+
if (!EnumProcessModules (GetCurrentProcess (), modules,
328+
2 * NT_MAX_PATH, &size))
329+
__seterrno ();
335330
else
336-
{
337-
PDEBUG_MODULE_ARRAY mods = (PDEBUG_MODULE_ARRAY)
338-
buf->ModuleInformation;
339-
for (ULONG i = 0; i < mods->Count; ++i)
340-
if ((ret = (void *)
341-
GetProcAddress ((HMODULE) mods->Modules[i].Base, name)))
342-
break;
343-
if (!ret)
344-
set_errno (ENOENT);
345-
}
346-
RtlDestroyQueryDebugBuffer (buf);
331+
for (uint32_t i = 0; i < size / sizeof (HMODULE); ++i)
332+
if ((ret = (void *) GetProcAddress (modules[i], name)))
333+
break;
347334
}
348335
else
349336
{

0 commit comments

Comments
 (0)