Skip to content

Commit

Permalink
Add writeable holders for executable memory (#53934)
Browse files Browse the repository at this point in the history
* Add writeable holders for executable memory

This change adds holders for writeable mappings for executable memory.
It is the largest part of the W^X support. The ExecutableWriterHolder
implementation is dummy in this change, but it was fully tested with
coreclr / libraries tests on Windows arm, arm64, x64 and x86 with the
real double memory mapping.
There are few concepts / conventions used:
* When the writeable pointer isn't known at a place where it is needed
  and also not at the caller, the ExecutableWriterHolder instance is
  created.
* When a callee needs writeable pointer to executable memory and caller
  knows RW and RX, the argument is doubled with RX and RW suffixes. For
  constructors and member methods when "this" is the RW one, we pass just
  extra RX argument.
* Locals holding RW  pointer use RW suffix.
* Locals holding RX pointer usually have no suffix to minimize number of
  changes, but in some cases they have a RX suffix
  where I felt like it was better to make things clear.
  • Loading branch information
janvorli committed Jun 10, 2021
1 parent 7b3564a commit d617e83
Show file tree
Hide file tree
Showing 48 changed files with 980 additions and 612 deletions.
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

0 comments on commit d617e83

Please sign in to comment.