From 9c56565bf8ef0e3d5579763e68c6dca3ecac7ee1 Mon Sep 17 00:00:00 2001 From: sqwishy Date: Fri, 4 Oct 2024 02:58:09 -0700 Subject: [PATCH] FIX(plugins): Load correct pages for modules This `VirtualQueryEx()` loop is called for each module in a process. It reads pages starting at the module address but seems to continue past into other modules and into dynamic allocations also. This check stops enumerating pages once it encounters one that no longer belongs to the module for which pages are being collected. (Also this function opens two handles, this adds a clean up for the first handle if opening the second fails.) Fixes #6558 (cherry picked from commit 1498b83559b474cc246dfb38a6e4067b0102fed8) --- plugins/HostWindows.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugins/HostWindows.cpp b/plugins/HostWindows.cpp index e05e2cf86fe..18c4030692f 100644 --- a/plugins/HostWindows.cpp +++ b/plugins/HostWindows.cpp @@ -34,6 +34,7 @@ Modules HostWindows::modules() const { const auto snapshotHandle = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, m_pid); if (snapshotHandle == INVALID_HANDLE_VALUE) { + CloseHandle(processHandle); return {}; } @@ -51,7 +52,11 @@ Modules HostWindows::modules() const { MEMORY_BASIC_INFORMATION64 mbi; auto address = reinterpret_cast< procptr_t >(me.modBaseAddr); while (VirtualQueryEx(processHandle, reinterpret_cast< LPCVOID >(address), - reinterpret_cast< PMEMORY_BASIC_INFORMATION >(&mbi), sizeof(mbi))) { + reinterpret_cast< PMEMORY_BASIC_INFORMATION >(&mbi), sizeof(mbi)) + /* Only enumerate pages that belong to the allocation for this module. + * This stops if it sees a page for a different allocation, belonging + * to another module or dynamic memory, or gap between pages. */ + && (mbi.AllocationBase == reinterpret_cast< procptr_t >(me.modBaseAddr))) { MemoryRegion region{}; region.address = address; region.size = mbi.RegionSize;