src: Fix bugs and refactor NativeSymbolDebuggingContext::GetLoadedLibraries #57738
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
tldr:
GetLoadedLibraries is used in process.report.GetReport to generate a list of loaded libraries. This PR fixes the following:
--
Refactor:
Sources from Docs + Raymond Chen
"To retrieve the name of a module in the current process, use the GetModuleFileName function" -- WinApi Docs: Remarks
Raymond Chen explains how GetModuleFileNameExW can fail randomly, in a way that GetModuleFileNameW avoids -- Article
Bug Fixes:
modules
allocates memory by multiplying the constructor argsize_t n
by thesizeof
it's type. In this case,modules
would be allocated withsize_1 * sizeof(HMODULE)
bytes. But EnumProcessModules setssize_1
to the number of bytes that need to be allocated for themodule
buffer. Usingsize_1/sizeof(HMODULE)
gives the constructor what it's asking for i.e the number of HMODULES to allocate space for.Exapnd to Step through MallocedBuffer constructor behavior
src/util.h MallocedBuffer
src/util-inl.h UncheckedMalloc
src/util-inl.h UncheckedRealloc
src/util-inl.h MultiplyWithOverflowCheck
Unnecessary Module path truncation: GetModuleFileName is called using
array_size(module_name) / sizeof(WCHAR)
for thenSize
param.nSize
is supposed to be the number of characters in the wchar array supplied.module_name
is already a WCHAR array, sonSize
should just bearray_size(module_name)
. The current code causes GetModuleFileName to truncate after reaching half the capacity of the buffer (i.e half of MAX_PATH)Memory Leak: When converting the file path written by GetModuleFileName from a wchar array to a char array with UTF8 encoding, a char array
str
is added to the heap usingnew
. Next, it's added tolist
, astd::vector<std::string>
usingemplace_back
.emplace_back
will passstr
intro thestd::string
constructor. This constructor is defined to copy values from a null terminatedchar * array
into thestd::string
. I assumestr
was created on the heap so that it could be dynamically allocated based on the number of bytes returned byWideCharToMultiByte
. Once it's added tolist
, the memory is no longer needed, but is never deleted from the heap.