Skip to content

gh-131565: Implement ctypes.util.dllist() in the _ctypes extension#154255

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:ctypes-dllist-chelper
Open

gh-131565: Implement ctypes.util.dllist() in the _ctypes extension#154255
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:ctypes-dllist-chelper

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

ctypes.util.dllist() returned only the main executable on NetBSD, because NetBSD's dl_iterate_phdr() enumerates only the link-map group of the calling object. Called through ctypes, the caller is libffi's closure trampoline, which belongs to no object, so nothing else was reported (verified on NetBSD 10: a C helper .so calling dl_iterate_phdr() reports all objects, while the ctypes path reports 1).

Implement dllist() as a small C helper in the _ctypes extension, so the caller is _ctypes and all loaded shared libraries are reported. The pure-Python dl_iterate_phdr() wrapper in ctypes.util is replaced by from _ctypes import dllist. Apple platforms keep their dyld-based implementation (the C helper is gated out there).

Verified on NetBSD 10 (now 24 objects, was 1), Linux (20), OpenBSD 7 (23) and FreeBSD 15 (26); test_dllist passes on all four.

Fixes the root cause investigated in gh-131565. CC @WardBrian

🤖 Generated with Claude Code

On NetBSD dl_iterate_phdr() reports only the link-map group of the calling
object.  Called through ctypes, the caller is libffi's closure trampoline,
which belongs to no object, so only the main executable was reported.
Calling dl_iterate_phdr() from the _ctypes extension module makes _ctypes
the caller and reports all loaded shared libraries.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@johnslavik

Copy link
Copy Markdown
Member

(Tagging Peter for his interest in ctypes.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review extension-modules C modules in the Modules dir needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes topic-ctypes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants