Bug 1235982: Work around GetProcessHeaps giving us some read-only heaps with CFG.
MozReview-Commit-ID: JUq2aJSMemM
--- a/xpcom/base/nsMemoryReporterManager.cpp
+++ b/xpcom/base/nsMemoryReporterManager.cpp
@@ -719,16 +719,24 @@ SystemHeapSize(int64_t* aSizeOut)
DWORD nHeaps2 = GetProcessHeaps(nHeaps, heaps.get());
NS_ENSURE_TRUE(nHeaps2 != 0 && nHeaps2 == nHeaps, NS_ERROR_FAILURE);
// Lock and iterate over each heap to get its size.
int64_t heapsSize = 0;
for (DWORD i = 0; i < nHeaps; i++) {
HANDLE heap = heaps[i];
+ // Bug 1235982: When Control Flow Guard is enabled for the process,
+ // GetProcessHeap may return some protected heaps that are in read-only
+ // memory and thus crash in HeapLock. Ignore such heaps.
+ MEMORY_BASIC_INFORMATION mbi = {0};
+ if (VirtualQuery(heap, &mbi, sizeof(mbi)) && mbi.Protect == PAGE_READONLY) {
+ continue;
+ }
+
NS_ENSURE_TRUE(HeapLock(heap), NS_ERROR_FAILURE);
int64_t heapSize = 0;
PROCESS_HEAP_ENTRY entry;
entry.lpData = nullptr;
while (HeapWalk(heap, &entry)) {
// We don't count entry.cbOverhead, because we just want to measure the
// space available to the program.