Skip to content

Commit f772f7c

Browse files
committed
The handle we pass to DbgHelp should meet the requirements of that library.
1 parent 5217e6e commit f772f7c

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

stdlib/public/runtime/ImageInspectionCOFF.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,11 @@ int swift::lookupSymbol(const void *address, SymbolInfo *info) {
4747
return 0;
4848
}
4949

50-
char buffer[sizeof(SYMBOL_INFO) + kSymbolMaxNameLen];
51-
PSYMBOL_INFO pSymbol = reinterpret_cast<PSYMBOL_INFO>(buffer);
50+
SYMBOL_INFO_PACKAGE symbol = {};
5251
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
53-
pSymbol->MaxNameLen = kSymbolMaxNameLen;
54-
55-
DWORD64 dwDisplacement = 0;
56-
57-
if (SymFromAddr(GetCurrentProcess(),
58-
reinterpret_cast<const DWORD64>(address),
59-
&dwDisplacement, pSymbol) == FALSE) {
52+
pSymbol->MaxNameLen = MAX_SYM_NAME;
53+
if (SymFromAddr(hProcess, reinterpret_cast<const DWORD64>(address),
54+
&nullptr, pSymbol) == FALSE) {
6055
return 0;
6156
}
6257

@@ -83,7 +78,18 @@ void swift::_swift_withWin32DbgHelpLibrary(
8378
// now. This handle belongs to the Swift runtime and should not be closed by
8479
// `body` (or anybody else.)
8580
if (!dbgHelpHandle) {
86-
dbgHelpHandle = HeapAlloc(GetProcessHeap(), 0, 1);
81+
// Per the documentation for the Debug Help library, we should not use the
82+
// current process handle because other subsystems might also use it and
83+
// end up stomping on each other. So we'll try to duplicate that handle to
84+
// get a unique one that still fulfills the needs of the library. If that
85+
// fails (presumably because the current process doesn't have the
86+
// PROCESS_DUP_HANDLE access right) then fall back to using the original
87+
// process handle and hope nobody else is using it too.
88+
HANDLE currentProcess = GetCurrentProcess();
89+
if (!DuplicateHandle(currentProcess, currentProcess, currentProcess,
90+
&dbgHelpHandle, 0, false, DUPLICATE_SAME_ACCESS)) {
91+
dbgHelpHandle = currentProcess;
92+
}
8793
}
8894

8995
// If we have not previously initialized the Debug Help library, do so now.
@@ -96,7 +102,7 @@ void swift::_swift_withWin32DbgHelpLibrary(
96102
// Set the library's options to what the Swift runtime generally expects.
97103
// If the options aren't going to change, we can skip the call and save a
98104
// few CPU cycles on the library call.
99-
static constexpr const DWORD options = SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS;
105+
constexpr const DWORD options = SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS;
100106
DWORD oldOptions = SymGetOptions();
101107
if (oldOptions != options) {
102108
SymSetOptions(options);

0 commit comments

Comments
 (0)