Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the Toolhelp32 API to enumerate loaded Win32 modules. #892

Merged
merged 1 commit into from
Jan 6, 2025

Conversation

grynspan
Copy link
Contributor

@grynspan grynspan commented Jan 6, 2025

This PR replaces our call to EnumProcessModules() with one to CreateToolhelp32Snapshot(TH32CS_SNAPMODULE). Three reasons:

  1. EnumProcessModules() requires us to specify a large, fixed-size buffer to contain all the HMODULE handles;
  2. EnumProcessModules() does not own any references to the handles it returns, meaning that a module can be unloaded while we are iterating over them (while CreateToolhelp32Snapshot() temporarily bumps the refcounts of the handles it produces); and
  3. CreateToolhelp32Snapshot() lets us produce a lazy sequence of HMODULE values rather than an array, letting us write somewhat Swiftier code that uses it.

The overhead of using CreateToolhelp32Snapshot() was negligible (below the noise level when measuring).

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

This PR replaces our call to `EnumProcessModules()` with one to
`CreateToolhelp32Snapshot(TH32CS_SNAPMODULE)`. Three reasons:

1. `EnumProcessModules()` requires us to specify a large, fixed-size buffer to
   contain all the `HMODULE` handles;
2. `EnumProcessModules()` does not own any references to the handles it returns,
   meaning that a module can be unloaded while we are iterating over them (while
   `CreateToolhelp32Snapshot()` temporarily bumps the refcounts of the handles
   it produces); and
3. `CreateToolhelp32Snapshot()` lets us produce a lazy sequence of `HMODULE`
   values rather than an array, letting us write somewhat Swiftier code that
   uses it.

The overhead of using `CreateToolhelp32Snapshot()` was negligible (below the
noise level when measuring).
@grynspan grynspan added enhancement New feature or request windows 🪟 Windows support labels Jan 6, 2025
@grynspan grynspan added this to the Swift 6.x milestone Jan 6, 2025
@grynspan grynspan requested a review from compnerd January 6, 2025 20:28
@grynspan grynspan self-assigned this Jan 6, 2025
@grynspan
Copy link
Contributor Author

grynspan commented Jan 6, 2025

@swift-ci test

@grynspan grynspan merged commit 65b7ef2 into main Jan 6, 2025
3 checks passed
@grynspan grynspan deleted the jgrynspan/use-toolhelp-to-enumerate-win32-modules branch January 6, 2025 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request windows 🪟 Windows support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants