Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in SuperPMI #34523

Merged
merged 1 commit into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/coreclr/src/ToolBox/superpmi/superpmi-shared/compileresult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions src/coreclr/src/ToolBox/superpmi/superpmi-shared/compileresult.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,47 @@
#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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This has memory leak if new MemoryNode throws, but I assume that we do not care about hardening like this in superpmi.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm not too worried about hardening superpmi for OOM

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:
Expand Down Expand Up @@ -168,6 +209,8 @@ class CompileResult

void dumpToConsole();

void* allocateMemory(size_t sizeInBytes);

void recAssert(const char* buff);
void dmpAssertLog(DWORD key, DWORD value);
const char* repAssert();
Expand Down Expand Up @@ -307,6 +350,7 @@ class CompileResult
LightWeightMap<DWORDLONG, DWORD>* CallTargetTypes;

private:
MemoryTracker* memoryTracker;
Capture_AllocMemDetails allocMemDets;
allocGCInfoDetails allocGCInfoDets;
};
Expand Down
8 changes: 4 additions & 4 deletions src/coreclr/src/ToolBox/superpmi/superpmi/icorjitinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/src/ToolBox/superpmi/superpmi/jitinstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down