-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add writeable holders for executable memory (#53934)
* 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
Showing
48 changed files
with
980 additions
and
612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.