Add EnableHeapReflection setting #1169

This commit is contained in:
dmex
2022-04-19 17:16:13 +10:00
parent 9069787db2
commit 3f98d403a7
4 changed files with 62 additions and 57 deletions
+45 -2
View File
@@ -407,6 +407,9 @@ VOID PhpEnumerateProcessHeaps(
NTSTATUS status = STATUS_UNSUCCESSFUL;
HANDLE processHandle = NULL;
HANDLE powerRequestHandle = NULL;
RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION reflectionInfo = { 0 };
HANDLE clientProcessId = Context->ProcessItem->ProcessId;
HANDLE processReflectionHandle = NULL;
BOOLEAN sizesInBytes;
sizesInBytes = Button_GetCheck(GetDlgItem(Context->WindowHandle, IDC_SIZESINBYTES)) == BST_CHECKED;
@@ -444,6 +447,35 @@ VOID PhpEnumerateProcessHeaps(
PhCreateExecutionRequiredRequest(processHandle, &powerRequestHandle);
}
if (PhGetIntegerSetting(L"EnableHeapReflection"))
{
status = PhOpenProcess(
&processReflectionHandle,
PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_DUP_HANDLE,
clientProcessId
);
if (NT_SUCCESS(status))
{
// NOTE: RtlQueryProcessDebugInformation injects a thread into the process causing deadlocks and other issues in rare cases.
// We mitigate these problems by reflecting the process and querying heap information from the clone. (dmex)
status = RtlCreateProcessReflection(
processReflectionHandle,
0,
NULL,
NULL,
NULL,
&reflectionInfo
);
if (NT_SUCCESS(status))
{
clientProcessId = reflectionInfo.ReflectionClientId.UniqueProcess;
}
}
}
#ifdef _WIN64
if (Context->ProcessItem->IsWow64)
{
@@ -454,7 +486,7 @@ VOID PhpEnumerateProcessHeaps(
ULONG capturedHeapInfoLength;
status = PhSvcCallQueryProcessHeapInformation(
Context->ProcessItem->ProcessId,
clientProcessId,
&capturedHeapInfoString
);
@@ -556,7 +588,7 @@ VOID PhpEnumerateProcessHeaps(
PPH_PROCESS_DEBUG_HEAP_INFORMATION heapDebugInfo;
status = PhQueryProcessHeapInformation(
Context->ProcessItem->ProcessId,
clientProcessId,
&heapDebugInfo
);
@@ -627,6 +659,17 @@ VOID PhpEnumerateProcessHeaps(
#endif
CleanupExit:
if (reflectionInfo.ReflectionProcessHandle)
{
PhTerminateProcess(reflectionInfo.ReflectionProcessHandle, STATUS_SUCCESS);
NtClose(reflectionInfo.ReflectionProcessHandle);
}
if (reflectionInfo.ReflectionThreadHandle)
NtClose(reflectionInfo.ReflectionThreadHandle);
if (processReflectionHandle)
NtClose(processReflectionHandle);
if (powerRequestHandle)
PhDestroyExecutionRequiredRequest(powerRequestHandle);
ExtendedListView_SortItems(Context->ListViewHandle);
+15 -14
View File
@@ -24,6 +24,7 @@
#include <phapp.h>
#include <memprv.h>
#include <heapstruct.h>
#include <settings.h>
#define MAX_HEAPS 1000
#define WS_REQUEST_COUNT (PAGE_SIZE / sizeof(MEMORY_WORKING_SET_EX_INFORMATION))
@@ -261,21 +262,21 @@ VOID PhpUpdateHeapRegions(
if (WindowsVersion >= WINDOWS_10)
PhCreateExecutionRequiredRequest(processHandle, &powerRequestHandle);
// NOTE: RtlQueryProcessDebugInformation injects a thread into the process causing deadlocks and other issues in rare cases.
// We mitigate these problems by reflecting the process and querying heap information from the clone. (dmex)
status = RtlCreateProcessReflection(
processHandle,
0,
NULL,
NULL,
NULL,
&reflectionInfo
);
if (NT_SUCCESS(status))
if (PhGetIntegerSetting(L"EnableHeapReflection"))
{
clientProcessId = reflectionInfo.ReflectionClientId.UniqueProcess;
status = RtlCreateProcessReflection(
processHandle,
0,
NULL,
NULL,
NULL,
&reflectionInfo
);
if (NT_SUCCESS(status))
{
clientProcessId = reflectionInfo.ReflectionClientId.UniqueProcess;
}
}
}
+1
View File
@@ -45,6 +45,7 @@ VOID PhAddDefaultSettings(
PhpAddIntegerSetting(L"EnableCycleCpuUsage", L"1");
PhpAddIntegerSetting(L"EnableImageCoherencySupport", L"0");
PhpAddIntegerSetting(L"EnableInstantTooltips", L"0");
PhpAddIntegerSetting(L"EnableHeapReflection", L"0");
PhpAddIntegerSetting(L"EnableKph", L"0");
PhpAddIntegerSetting(L"EnableKphWarnings", L"1");
PhpAddIntegerSetting(L"EnableLastProcessShutdown", L"0");
+1 -41
View File
@@ -10318,46 +10318,17 @@ NTSTATUS PhQueryProcessHeapInformation(
)
{
NTSTATUS status;
HANDLE processHandle = NULL;
HANDLE clientProcessId = ProcessId;
RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION reflectionInfo = { 0 };
PRTL_DEBUG_INFORMATION debugBuffer = NULL;
PPH_PROCESS_DEBUG_HEAP_INFORMATION heapDebugInfo = NULL;
ULONG heapEntrySize;
status = PhOpenProcess(
&processHandle,
PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_DUP_HANDLE,
ProcessId
);
if (NT_SUCCESS(status))
{
// NOTE: RtlQueryProcessDebugInformation injects a thread into the process causing deadlocks and other issues in rare cases.
// We mitigate these problems by reflecting the process and querying heap information from the clone. (dmex)
status = RtlCreateProcessReflection(
processHandle,
0,
NULL,
NULL,
NULL,
&reflectionInfo
);
if (NT_SUCCESS(status))
{
clientProcessId = reflectionInfo.ReflectionClientId.UniqueProcess;
}
}
for (ULONG i = 0x400000; ; i *= 2) // rev from Heap32First/Heap32Next (dmex)
{
if (!(debugBuffer = RtlCreateQueryDebugBuffer(i, FALSE)))
return STATUS_UNSUCCESSFUL;
status = RtlQueryProcessDebugInformation(
clientProcessId,
ProcessId,
RTL_QUERY_PROCESS_HEAP_SUMMARY | RTL_QUERY_PROCESS_HEAP_ENTRIES,
debugBuffer
);
@@ -10378,17 +10349,6 @@ NTSTATUS PhQueryProcessHeapInformation(
}
}
if (reflectionInfo.ReflectionProcessHandle)
{
PhTerminateProcess(reflectionInfo.ReflectionProcessHandle, STATUS_SUCCESS);
NtClose(reflectionInfo.ReflectionProcessHandle);
}
if (reflectionInfo.ReflectionThreadHandle)
NtClose(reflectionInfo.ReflectionThreadHandle);
if (processHandle)
NtClose(processHandle);
if (!NT_SUCCESS(status))
return status;