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

Add writeable holders for executable memory #53934

Merged
merged 5 commits into from
Jun 10, 2021
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
72 changes: 72 additions & 0 deletions src/coreclr/inc/executableallocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//

//
// Allocator and holders for double mapped executable memory
//

#pragma once

#include "utilcode.h"
#include "ex.h"

// Holder class to map read-execute memory as read-write so that it can be modified without using read-write-execute mapping.
// At the moment the implementation is dummy, returning the same addresses for both cases and expecting them to be read-write-execute.
// The class uses the move semantics to ensure proper unmapping in case of re-assigning of the holder value.
template<typename T>
class ExecutableWriterHolder
{
T *m_addressRX;
T *m_addressRW;

void Move(ExecutableWriterHolder& other)
{
m_addressRX = other.m_addressRX;
m_addressRW = other.m_addressRW;
other.m_addressRX = NULL;
other.m_addressRW = NULL;
}

void Unmap()
{
// TODO: this will be added with the double mapped allocator addition
}

public:
ExecutableWriterHolder(const ExecutableWriterHolder& other) = delete;
ExecutableWriterHolder& operator=(const ExecutableWriterHolder& other) = delete;

ExecutableWriterHolder(ExecutableWriterHolder&& other)
{
Move(other);
}

ExecutableWriterHolder& operator=(ExecutableWriterHolder&& other)
{
Unmap();
Move(other);
return *this;
}

ExecutableWriterHolder() : m_addressRX(nullptr), m_addressRW(nullptr)
{
}

ExecutableWriterHolder(T* addressRX, size_t size)
{
m_addressRX = addressRX;
m_addressRW = addressRX;
}

~ExecutableWriterHolder()
{
Unmap();
}

// Get the writeable address
inline T *GetRW() const
{
return m_addressRW;
}
};
8 changes: 7 additions & 1 deletion src/coreclr/inc/holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -934,11 +934,17 @@ using NonVMComHolder = SpecializedWrapper<_TYPE, DoTheRelease<_TYPE>>;
// } // foo->DecRef() on out of scope
//
//-----------------------------------------------------------------------------
template<typename _TYPE>
class ExecutableWriterHolder;

template <typename TYPE>
FORCEINLINE void StubRelease(TYPE* value)
{
if (value)
value->DecRef();
{
ExecutableWriterHolder<TYPE> stubWriterHolder(value, sizeof(TYPE));
stubWriterHolder.GetRW()->DecRef();
}
}

template<typename _TYPE>
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/inc/loaderheap.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "utilcode.h"
#include "ex.h"
#include "executableallocator.h"

//==============================================================================
// Interface used to back out loader heap allocations.
Expand Down
30 changes: 25 additions & 5 deletions src/coreclr/utilcode/loaderheap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1330,8 +1330,14 @@ void *UnlockedLoaderHeap::UnlockedAllocMem_NoThrow(size_t dwSize
if (pData)
{
#ifdef _DEBUG
BYTE *pAllocatedBytes = (BYTE*)pData;
ExecutableWriterHolder<void> dataWriterHolder;
if (m_Options & LHF_EXECUTABLE)
{
dataWriterHolder = ExecutableWriterHolder<void>(pData, dwSize);
pAllocatedBytes = (BYTE *)dataWriterHolder.GetRW();
}

BYTE *pAllocatedBytes = (BYTE *)pData;
#if LOADER_HEAP_DEBUG_BOUNDARY > 0
// Don't fill the memory we allocated - it is assumed to be zeroed - fill the memory after it
memset(pAllocatedBytes + dwRequestedSize, 0xEE, LOADER_HEAP_DEBUG_BOUNDARY);
Expand All @@ -1344,7 +1350,7 @@ void *UnlockedLoaderHeap::UnlockedAllocMem_NoThrow(size_t dwSize

if (!m_fExplicitControl)
{
LoaderHeapValidationTag *pTag = AllocMem_GetTag(pData, dwRequestedSize);
LoaderHeapValidationTag *pTag = AllocMem_GetTag(pAllocatedBytes, dwRequestedSize);
pTag->m_allocationType = kAllocMem;
pTag->m_dwRequestedSize = dwRequestedSize;
pTag->m_szFile = szFile;
Expand Down Expand Up @@ -1514,7 +1520,14 @@ void UnlockedLoaderHeap::UnlockedBackoutMem(void *pMem,
{
// Cool. This was the last block allocated. We can just undo the allocation instead
// of going to the freelist.
memset(pMem, 0x00, dwSize); // Fill freed region with 0
void *pMemRW = pMem;
ExecutableWriterHolder<void> memWriterHolder;
if (m_Options & LHF_EXECUTABLE)
{
memWriterHolder = ExecutableWriterHolder<void>(pMem, dwSize);
pMemRW = memWriterHolder.GetRW();
}
memset(pMemRW, 0x00, dwSize); // Fill freed region with 0
m_pAllocPtr = (BYTE*)pMem;
}
else
Expand Down Expand Up @@ -1626,7 +1639,14 @@ void *UnlockedLoaderHeap::UnlockedAllocAlignedMem_NoThrow(size_t dwRequestedSiz
((BYTE*&)pResult) += extra;

#ifdef _DEBUG
BYTE *pAllocatedBytes = (BYTE *)pResult;
BYTE *pAllocatedBytes = (BYTE *)pResult;
ExecutableWriterHolder<void> resultWriterHolder;
if (m_Options & LHF_EXECUTABLE)
{
resultWriterHolder = ExecutableWriterHolder<void>(pResult, dwSize - extra);
pAllocatedBytes = (BYTE *)resultWriterHolder.GetRW();
}

#if LOADER_HEAP_DEBUG_BOUNDARY > 0
// Don't fill the entire memory - we assume it is all zeroed -just the memory after our alloc
memset(pAllocatedBytes + dwRequestedSize, 0xee, LOADER_HEAP_DEBUG_BOUNDARY);
Expand Down Expand Up @@ -1656,7 +1676,7 @@ void *UnlockedLoaderHeap::UnlockedAllocAlignedMem_NoThrow(size_t dwRequestedSiz

if (!m_fExplicitControl)
{
LoaderHeapValidationTag *pTag = AllocMem_GetTag(((BYTE*)pResult) - extra, dwRequestedSize + extra);
LoaderHeapValidationTag *pTag = AllocMem_GetTag(pAllocatedBytes - extra, dwRequestedSize + extra);
pTag->m_allocationType = kAllocMem;
pTag->m_dwRequestedSize = dwRequestedSize + extra;
pTag->m_szFile = szFile;
Expand Down
Loading