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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions stdlib/public/runtime/Errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ static bool getSymbolNameAddr(llvm::StringRef libraryName,
// providing failure status instead of just returning the original string like
// swift demangle.
#if defined(_WIN32)
static StaticMutex mutex;

char szUndName[1024];
DWORD dwResult = mutex.withLock([&syminfo, &szUndName]() {
DWORD dwResult;
dwResult = _swift_withWin32DbgHelpLibrary([&] (bool isInitialized) -> DWORD {
if (!isInitialized) {
return 0;
}

DWORD dwFlags = UNDNAME_COMPLETE;
#if !defined(_WIN64)
dwFlags |= UNDNAME_32_BIT_DECODE;
Expand All @@ -98,7 +101,7 @@ static bool getSymbolNameAddr(llvm::StringRef libraryName,
sizeof(szUndName), dwFlags);
});

if (dwResult == TRUE) {
if (dwResult) {
symbolName += szUndName;
return true;
}
Expand Down
47 changes: 45 additions & 2 deletions stdlib/public/runtime/ImageInspection.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
#ifndef SWIFT_RUNTIME_IMAGEINSPECTION_H
#define SWIFT_RUNTIME_IMAGEINSPECTION_H

#include "swift/Runtime/Config.h"

#include <cstdint>
#include <cstddef>
#if defined(__cplusplus)
#include <functional>
#include <memory>
#endif
#include <type_traits>

namespace swift {

Expand Down Expand Up @@ -106,6 +108,47 @@ void addImageAccessibleFunctionsBlockCallbackUnsafe(const void *baseAddress,

int lookupSymbol(const void *address, SymbolInfo *info);

#if defined(_WIN32)
/// Configure the environment to allow calling into the Debug Help library.
///
/// \param body A function to invoke. This function attempts to first initialize
/// the Debug Help library. The result of that operation is passed to this
/// function.
///
/// 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.
SWIFT_RUNTIME_STDLIB_SPI
void _swift_withWin32DbgHelpLibrary(
const std::function<void(bool /*isInitialized*/)>& body);

/// Configure the environment to allow calling into the Debug Help library.
///
/// \param body A function to invoke. This function attempts to first initialize
/// the Debug Help library. The result of that operation is passed to this
/// function.
///
/// \returns Whatever is returned from \a body.
///
/// 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.)

typename F,
typename R = typename std::result_of_t<F&(bool /*isInitialized*/)>,
typename = typename std::enable_if_t<!std::is_same<void, R>::value>
>
static inline R _swift_withWin32DbgHelpLibrary(const F& body) {
R result;

_swift_withWin32DbgHelpLibrary([&body, &result] (bool isInitialized) {
result = body(isInitialized);
});

return result;
}
#endif

} // end namespace swift

#endif
58 changes: 38 additions & 20 deletions stdlib/public/runtime/ImageInspectionCOFF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <DbgHelp.h>
#endif

#include "swift/Runtime/Mutex.h"

using namespace swift;

int swift::lookupSymbol(const void *address, SymbolInfo *info) {
Expand All @@ -38,36 +40,52 @@ int swift::lookupSymbol(const void *address, SymbolInfo *info) {
info->symbolAddress = dli_saddr;
return 1;
#elif defined(_WIN32)
static const constexpr size_t kSymbolMaxNameLen = 1024;
static bool bInitialized = false;
return _swift_withWin32DbgHelpLibrary([&] (bool isInitialized) {
static const constexpr size_t kSymbolMaxNameLen = 1024;

if (bInitialized == false) {
if (SymInitialize(GetCurrentProcess(), /*UserSearchPath=*/NULL,
/*fInvadeProcess=*/TRUE) == FALSE)
if (!isInitialized) {
return 0;
bInitialized = true;
}
}

char buffer[sizeof(SYMBOL_INFO) + kSymbolMaxNameLen];
PSYMBOL_INFO pSymbol = reinterpret_cast<PSYMBOL_INFO>(buffer);
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = kSymbolMaxNameLen;
char buffer[sizeof(SYMBOL_INFO) + kSymbolMaxNameLen];
PSYMBOL_INFO pSymbol = reinterpret_cast<PSYMBOL_INFO>(buffer);
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = kSymbolMaxNameLen;

DWORD64 dwDisplacement = 0;
DWORD64 dwDisplacement = 0;

if (SymFromAddr(GetCurrentProcess(), reinterpret_cast<const DWORD64>(address),
&dwDisplacement, pSymbol) == FALSE)
return 0;
if (SymFromAddr(GetCurrentProcess(),
reinterpret_cast<const DWORD64>(address),
&dwDisplacement, pSymbol) == FALSE) {
return 0;
}

info->fileName = NULL;
info->baseAddress = reinterpret_cast<void *>(pSymbol->ModBase);
info->symbolName.reset(_strdup(pSymbol->Name));
info->symbolAddress = reinterpret_cast<void *>(pSymbol->Address);
info->fileName = NULL;
info->baseAddress = reinterpret_cast<void *>(pSymbol->ModBase);
info->symbolName.reset(_strdup(pSymbol->Name));
info->symbolAddress = reinterpret_cast<void *>(pSymbol->Address);

return 1;
return 1;
});
#else
return 0;
#endif // defined(__CYGWIN__) || defined(_WIN32)
}

#if defined(_WIN32)
static StaticMutex mutex;
static bool isDbgHelpInitialized = false;

void swift::_swift_withWin32DbgHelpLibrary(
const std::function<void(bool /* isInitialized */)>& body) {
mutex.withLock([&body] () {
if (!isDbgHelpInitialized) {
SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
isDbgHelpInitialized = SymInitialize(GetCurrentProcess(), nullptr, true);
}
body(isDbgHelpInitialized);
});
}
#endif

#endif // !defined(__ELF__) && !defined(__MACH__)