Skip to content

Ensure Debug Help library calls on Windows are made in as thread-safe a manner as possible by wrapping them in a scoped lock. #40815

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
Jan 13, 2022

Conversation

grynspan
Copy link
Contributor

Ensure Debug Help library calls on Windows are made in as thread-safe a manner as possible by wrapping them in a scoped lock.

According to the Win32 documentation, the functions in the Debug Help library (DbgHelp.lib) are not thread-safe. That means that calling them across threads may blow up the process.

While we don't have a mechanism to control the use of this library outside the runtime/stdlib, we can at least ensure that all uses of it within the runtime/stdlib are thread-safe. We do so by exposing a new C++ runtime function, swift::withDebugHelpLibrary(), that callers can invoke. This function acquires a lock and ensures SymInitialize() has been called and has succeeded before any work is done that relies on the library.

/// calls into it from the Swift runtime and stdlib should route through this
/// function.
SWIFT_RUNTIME_STDLIB_SPI
bool _swift_withDebugHelpLibrary(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should be exported so that it can be used by other Swift libraries in the repo if needed. It's not sufficient for those libraries to do their own locking; they would need to use the same lock to ensure thread safety.

@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/DbgHelp-threadsafety branch from 09ef4b9 to d7ed5eb Compare January 12, 2022 03:23
@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/DbgHelp-threadsafety branch from d7ed5eb to 52126d6 Compare January 12, 2022 05:18
@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan grynspan force-pushed the jgrynspan/DbgHelp-threadsafety branch from 52126d6 to 896d484 Compare January 12, 2022 07:25
@grynspan
Copy link
Contributor Author

@swift-ci please test

/// On Windows, the Debug Help library (DbgHelp.lib) is not thread-safe. All
/// calls into it from the Swift runtime and stdlib should route through this
/// function.
template <
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this is where I'd put my if constexprif I had one. (Only available in C++17, must use std::enable_if in its absence.)

Copy link
Contributor

@mikeash mikeash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blarg.

@grynspan
Copy link
Contributor Author

Blarg.

Quite.

… a manner as possible by wrapping them in a scoped lock.
@grynspan grynspan force-pushed the jgrynspan/DbgHelp-threadsafety branch from 896d484 to 0db7b5a Compare January 12, 2022 17:37
@grynspan
Copy link
Contributor Author

@swift-ci please smoke test

@grynspan grynspan merged commit dd0f545 into swiftlang:main Jan 13, 2022
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.

4 participants