Bug 1235982: Work around GetProcessHeaps giving us some read-only heaps with CFG. draft
authorDavid Major <dmajor@mozilla.com>
Sat, 30 Dec 2017 13:10:54 -0600
changeset 749536 afd8543a205758329d761c8dea5ce9f02b4c299a
parent 749535 7c0bc5a890f33f123d7383d11eb59c1f35c6becb
push id97415
push userbmo:tom@mozilla.com
push dateWed, 31 Jan 2018 16:34:20 +0000
bugs1235982
milestone60.0a1
Bug 1235982: Work around GetProcessHeaps giving us some read-only heaps with CFG. MozReview-Commit-ID: JUq2aJSMemM
xpcom/base/nsMemoryReporterManager.cpp
--- 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.