Skip to content

[swift-inspect] Fix corpse leaks when target doesn't have libswiftCore loaded. #81457

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

Merged
merged 1 commit into from
May 16, 2025

Conversation

mikeash
Copy link
Contributor

@mikeash mikeash commented May 12, 2025

In DarwinRemoteProcess's early return when libswiftCore.dylib isn't loaded in the process, we hadn't initialized all stored properties yet, which means our deinit didn't run. This leaked the task. This is especially bad when forking a corpse, as the system has a very limited number of corpses available.

Add a Cleanup helper to manage resources that need explicit cleanup. This is a noncopyable struct which takes a cleanup function and calls it whenever a new value is set or when the struct is destroyed. Use this for the DarwinRemoteProcess properties that need explicit cleanup. This allows us to remove DarwinRemoteProcess's deinit and always run these cleanups regardless of how we exit the initializer.

While we're here, fix the truncations of buffer in getAllProcesses.

rdar://151170155

@mikeash mikeash requested review from al45tair and compnerd as code owners May 12, 2025 20:58
@mikeash mikeash force-pushed the swift-inspect-corpse-leak branch 2 times, most recently from 0554c47 to 670d441 Compare May 13, 2025 15:21
…e loaded.

In DarwinRemoteProcess's early return when libswiftCore.dylib isn't loaded in the process, we hadn't initialized all stored properties yet, which means our deinit didn't run. This leaked the task. This is especially bad when forking a corpse, as the system has a very limited number of corpses available.

Add a Cleanup helper to manage resources that need explicit cleanup. This is a noncopyable struct which takes a cleanup function and calls it whenever a new value is set or when the struct is destroyed. Use this for the DarwinRemoteProcess properties that need explicit cleanup. This allows us to remove DarwinRemoteProcess's deinit and always run these cleanups regardless of how we exit the initializer.

While we're here, fix the truncations of buffer in getAllProcesses.

rdar://151170155
@mikeash mikeash force-pushed the swift-inspect-corpse-leak branch from 670d441 to 9671d6b Compare May 13, 2025 17:53
@mikeash
Copy link
Contributor Author

mikeash commented May 13, 2025

@swift-ci please test

@mikeash
Copy link
Contributor Author

mikeash commented May 14, 2025

@swift-ci please test macos platform

@mikeash
Copy link
Contributor Author

mikeash commented May 16, 2025

@swift-ci please test macos platform

@mikeash mikeash enabled auto-merge May 16, 2025 18:43
@mikeash mikeash merged commit 0ed232a into swiftlang:main May 16, 2025
4 of 5 checks passed
hamishknight added a commit to hamishknight/swift that referenced this pull request May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants