Fix memory leak in SuperPMI (#34523)
authorBruce Forstall <brucefo@microsoft.com>
Sun, 5 Apr 2020 17:10:46 +0000 (10:10 -0700)
committerGitHub <noreply@github.com>
Sun, 5 Apr 2020 17:10:46 +0000 (10:10 -0700)
Introduced by change to Heap APIs. Also fixes a long-existing
memory leak on Linux.

Introduce a small, simple class to keep track of memory allocations
associated with the CompileResult that we need to free. In the replay
case, SuperPMI allocates these (such as for the JIT calling allocMem).
In the case of collection, the VM allocates memory for allocMem (and related),
so that memory doesn't need to be tracked.

src/coreclr/src/ToolBox/superpmi/superpmi-shared/compileresult.cpp
src/coreclr/src/ToolBox/superpmi/superpmi-shared/compileresult.h
src/coreclr/src/ToolBox/superpmi/superpmi/icorjitinfo.cpp
src/coreclr/src/ToolBox/superpmi/superpmi/jitinstance.cpp

index c0b17a8..e27491b 100644 (file)
@@ -24,23 +24,24 @@ CompileResult::CompileResult()
     allocMemDets.roDataSize    = 0;
     allocMemDets.xcptnsCount   = 0;
     allocMemDets.flag          = (CorJitAllocMemFlag)0;
-    allocMemDets.hotCodeBlock  = 0;
-    allocMemDets.coldCodeBlock = 0;
-    allocMemDets.roDataBlock   = 0;
+    allocMemDets.hotCodeBlock  = nullptr;
+    allocMemDets.coldCodeBlock = nullptr;
+    allocMemDets.roDataBlock   = nullptr;
 
-    allocGCInfoDets.retval = 0;
+    allocGCInfoDets.retval = nullptr;
     allocGCInfoDets.size   = 0;
+
+    memoryTracker = nullptr;
 }
 
 CompileResult::~CompileResult()
 {
 #define LWM(map, key, value)                                                                                           \
-    if (map != nullptr)                                                                                                \
-        delete map;
+    delete map;
 #include "crlwmlist.h"
 
-    if (CallTargetTypes != nullptr)
-        delete CallTargetTypes;
+    delete CallTargetTypes;
+    delete memoryTracker;
 }
 
 // Is the CompileResult empty? Define this as whether all the maps that store information given by the JIT are empty.
@@ -57,6 +58,14 @@ bool CompileResult::IsEmpty()
     return isEmpty;
 }
 
+// Allocate memory associated with this CompileResult. Keep track of it in a list so we can free it all later.
+void* CompileResult::allocateMemory(size_t sizeInBytes)
+{
+    if (memoryTracker == nullptr)
+        memoryTracker = new MemoryTracker();
+    return memoryTracker->allocate(sizeInBytes);
+}
+
 void CompileResult::recAssert(const char* assertText)
 {
     if (AssertLog == nullptr)
index a8dd0a0..09ec839 100644 (file)
 #include "runtimedetails.h"
 #include "lightweightmap.h"
 
+// MemoryTracker: a very simple allocator and tracker of allocated memory, so it can be deleted when needed.
+class MemoryTracker
+{
+public:
+    MemoryTracker() : m_pHead(nullptr) {}
+    ~MemoryTracker() { freeAll(); }
+
+    void* allocate(size_t sizeInBytes)
+    {
+        BYTE* pNew = new BYTE[sizeInBytes];
+        m_pHead = new MemoryNode(pNew, m_pHead);    // Prepend this new one to the tracked memory list.
+        return pNew;
+    }
+
+private:
+
+    MemoryTracker(const MemoryTracker&) = delete; // no copy ctor
+
+    void freeAll()
+    {
+        for (MemoryNode* p = m_pHead; p != nullptr; )
+        {
+            MemoryNode* pNext = p->m_pNext;
+            delete p;
+            p = pNext;
+        }
+        m_pHead = nullptr;
+    }
+
+    struct MemoryNode
+    {
+        MemoryNode(BYTE* pMem, MemoryNode* pNext) : m_pMem(pMem), m_pNext(pNext) {}
+        ~MemoryNode() { delete[] m_pMem; }
+
+        BYTE*       m_pMem;
+        MemoryNode* m_pNext;
+    };
+
+    MemoryNode* m_pHead;
+};
+
 class CompileResult
 {
 public:
@@ -168,6 +209,8 @@ public:
 
     void dumpToConsole();
 
+    void* allocateMemory(size_t sizeInBytes);
+
     void recAssert(const char* buff);
     void dmpAssertLog(DWORD key, DWORD value);
     const char* repAssert();
@@ -307,6 +350,7 @@ public:
     LightWeightMap<DWORDLONG, DWORD>* CallTargetTypes;
 
 private:
+    MemoryTracker*          memoryTracker;
     Capture_AllocMemDetails allocMemDets;
     allocGCInfoDetails      allocGCInfoDets;
 };
index b8198ad..728bd3b 100644 (file)
@@ -1588,12 +1588,12 @@ void MyICJI::allocMem(ULONG              hotCodeSize,   /* IN */
 {
     jitInstance->mc->cr->AddCall("allocMem");
     // TODO-Cleanup: investigate if we need to check roDataBlock as well. Could hot block size be ever 0?
-    *hotCodeBlock = new BYTE[hotCodeSize];
+    *hotCodeBlock = jitInstance->mc->cr->allocateMemory(hotCodeSize);
     if (coldCodeSize > 0)
-        *coldCodeBlock = new BYTE[coldCodeSize];
+        *coldCodeBlock = jitInstance->mc->cr->allocateMemory(coldCodeSize);
     else
         *coldCodeBlock = nullptr;
-    *roDataBlock       = new BYTE[roDataSize];
+    *roDataBlock       = jitInstance->mc->cr->allocateMemory(roDataSize);
     jitInstance->mc->cr->recAllocMem(hotCodeSize, coldCodeSize, roDataSize, xcptnsCount, flag, hotCodeBlock,
                                      coldCodeBlock, roDataBlock);
 }
@@ -1657,7 +1657,7 @@ void* MyICJI::allocGCInfo(size_t size /* IN */
                           )
 {
     jitInstance->mc->cr->AddCall("allocGCInfo");
-    void* temp = (unsigned char*)new BYTE[size];
+    void* temp = jitInstance->mc->cr->allocateMemory(size);
     jitInstance->mc->cr->recAllocGCInfo(size, temp);
 
     return temp;
index a1afc65..5050f24 100644 (file)
@@ -427,7 +427,7 @@ const WCHAR* JitInstance::getOption(const WCHAR* key, LightWeightMap<DWORD, DWOR
 void* JitInstance::allocateArray(size_t cBytes)
 {
     mc->cr->AddCall("allocateArray");
-    return new BYTE[cBytes];
+    return mc->cr->allocateMemory(cBytes);
 }
 
 // Used to allocate memory that needs to live as long as the jit
@@ -444,7 +444,7 @@ void* JitInstance::allocateLongLivedArray(size_t cBytes)
 void JitInstance::freeArray(void* array)
 {
     mc->cr->AddCall("freeArray");
-    delete [] (BYTE*)array;
+    // We don't bother freeing this until the mc->cr itself gets freed.
 }
 
 // Used to free memory allocated by JitInstance::allocateLongLivedArray.