Skip to content

Commit d619b3f

Browse files
authored
Merge pull request #42582 from apple/jgrynspan/fix-win32-dbghelp-signature
Change the signature of `_swift_withWin32DbgHelpLibrary()` to not use `std::function` (which is not ABI-safe in C functions.)
2 parents 67af3a1 + 70062a8 commit d619b3f

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

stdlib/public/runtime/ImageInspection.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,31 @@ int lookupSymbol(const void *address, SymbolInfo *info);
114114
/// \param body A function to invoke. This function attempts to first initialize
115115
/// the Debug Help library. The result of that operation is passed to this
116116
/// function.
117+
/// \param context A caller-supplied value to pass to \a body.
117118
///
118119
/// On Windows, the Debug Help library (DbgHelp.lib) is not thread-safe. All
119120
/// calls into it from the Swift runtime and stdlib should route through this
120121
/// function.
121122
SWIFT_RUNTIME_STDLIB_SPI
122123
void _swift_withWin32DbgHelpLibrary(
123-
const std::function<void(bool /*isInitialized*/)>& body);
124+
void (* body)(bool isInitialized, void *context), void *context);
125+
126+
/// Configure the environment to allow calling into the Debug Help library.
127+
///
128+
/// \param body A function to invoke. This function attempts to first initialize
129+
/// the Debug Help library. The result of that operation is passed to this
130+
/// function.
131+
///
132+
/// On Windows, the Debug Help library (DbgHelp.lib) is not thread-safe. All
133+
/// calls into it from the Swift runtime and stdlib should route through this
134+
/// function.
135+
static inline void _swift_withWin32DbgHelpLibrary(
136+
const std::function<void(bool /*isInitialized*/)> &body) {
137+
_swift_withWin32DbgHelpLibrary([](bool isInitialized, void *context) {
138+
auto bodyp = reinterpret_cast<std::function<void(bool)> *>(context);
139+
(* bodyp)(isInitialized);
140+
}, const_cast<void *>(reinterpret_cast<const void *>(&body)));
141+
}
124142

125143
/// Configure the environment to allow calling into the Debug Help library.
126144
///

stdlib/public/runtime/ImageInspectionCOFF.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ static StaticMutex mutex;
7777
static bool isDbgHelpInitialized = false;
7878

7979
void swift::_swift_withWin32DbgHelpLibrary(
80-
const std::function<void(bool /* isInitialized */)>& body) {
81-
mutex.withLock([&body] () {
80+
void (* body)(bool isInitialized, void *context), void *context) {
81+
mutex.withLock([=] () {
8282
if (!isDbgHelpInitialized) {
8383
SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
8484
isDbgHelpInitialized = SymInitialize(GetCurrentProcess(), nullptr, true);
8585
}
86-
body(isDbgHelpInitialized);
86+
body(isDbgHelpInitialized, context);
8787
});
8888
}
8989
#endif

0 commit comments

Comments
 (0)