15 #include "llvm/Support/DataTypes.h"
25 DWORD getWindowsProtectionFlags(
unsigned Flags) {
33 return PAGE_READWRITE;
35 return PAGE_READWRITE;
37 return PAGE_EXECUTE_READ;
41 return PAGE_EXECUTE_READWRITE;
51 size_t getAllocationGranularity() {
53 ::GetSystemInfo(&Info);
54 if (Info.dwPageSize > Info.dwAllocationGranularity)
55 return Info.dwPageSize;
57 return Info.dwAllocationGranularity;
71 const MemoryBlock *
const NearBlock,
73 std::error_code &EC) {
74 EC = std::error_code();
83 static volatile size_t GranularityCached;
84 size_t Granularity = GranularityCached;
85 if (Granularity == 0) {
86 Granularity = getAllocationGranularity();
87 GranularityCached = Granularity;
90 const size_t NumBlocks = (NumBytes+Granularity-1)/Granularity;
92 uintptr_t Start = NearBlock ?
reinterpret_cast<uintptr_t
>(NearBlock->base()) +
98 if (Start && Start % Granularity != 0)
99 Start += Granularity - Start % Granularity;
101 DWORD Protect = getWindowsProtectionFlags(Flags);
103 void *PA = ::VirtualAlloc(reinterpret_cast<void*>(Start),
104 NumBlocks*Granularity,
105 MEM_RESERVE | MEM_COMMIT, Protect);
112 return MemoryBlock();
117 Result.Size = NumBlocks*Granularity;
126 if (M.Address == 0 || M.Size == 0)
127 return std::error_code();
129 if (!VirtualFree(M.Address, 0, MEM_RELEASE))
135 return std::error_code();
140 if (M.Address == 0 || M.Size == 0)
141 return std::error_code();
143 DWORD Protect = getWindowsProtectionFlags(Flags);
146 if (!VirtualProtect(M.Address, M.Size, Protect, &OldFlags))
152 return std::error_code();
159 const void *Addr,
size_t Len) {
160 FlushInstructionCache(GetCurrentProcess(), Addr, Len);
165 const MemoryBlock *NearBlock,
166 std::string *ErrMsg) {
171 if (EC != std::error_code() && ErrMsg) {
179 if (EC == std::error_code())
185 static DWORD getProtection(
const void *addr) {
186 MEMORY_BASIC_INFORMATION
info;
187 if (
sizeof(info) == ::VirtualQuery(addr, &info,
sizeof(info))) {
195 return MakeErrMsg(ErrMsg,
"Cannot set memory to writeable");
202 return MakeErrMsg(ErrMsg,
"Cannot set memory to executable");
208 DWORD prot = getProtection(Addr);
212 if (prot == PAGE_EXECUTE || prot == PAGE_EXECUTE_READ) {
213 prot = PAGE_EXECUTE_READWRITE;
214 }
else if (prot == PAGE_NOACCESS || prot == PAGE_READONLY) {
215 prot = PAGE_READWRITE;
220 return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)
225 DWORD prot = getProtection(Addr);
229 if (prot == PAGE_NOACCESS) {
231 }
else if (prot == PAGE_READONLY) {
232 prot = PAGE_EXECUTE_READ;
233 }
else if (prot == PAGE_READWRITE) {
234 prot = PAGE_EXECUTE_READWRITE;
239 return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)
static bool setRangeWritable(const void *Addr, size_t Size)
setRangeWritable - Mark the page containing a range of addresses as writable.
static std::error_code releaseMappedMemory(MemoryBlock &Block)
This method releases a block of memory that was allocated with the allocateMappedMemory method...
static MemoryBlock allocateMappedMemory(size_t NumBytes, const MemoryBlock *const NearBlock, unsigned Flags, std::error_code &EC)
This method allocates a block of memory that is suitable for loading dynamically generated code (e...
struct fuzzer::@269 Flags
static void InvalidateInstructionCache(const void *Addr, size_t Len)
InvalidateInstructionCache - Before the JIT can run a block of code that has been emitted it must inv...
static bool ReleaseRWX(MemoryBlock &block, std::string *ErrMsg=nullptr)
This method releases a block of Read/Write/Execute memory that was allocated with the AllocateRWX met...
std::error_code mapWindowsError(unsigned EV)
static bool setRangeExecutable(const void *Addr, size_t Size)
setRangeExecutable - Mark the page containing a range of addresses as executable. ...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static MemoryBlock AllocateRWX(size_t NumBytes, const MemoryBlock *NearBlock, std::string *ErrMsg=nullptr)
This method allocates a block of Read/Write/Execute memory that is suitable for executing dynamically...
bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix)
Provides a library for accessing information about this process and other processes on the operating ...
static bool setExecutable(MemoryBlock &M, std::string *ErrMsg=nullptr)
setExecutable - Before the JIT can run a block of code, it has to be given read and executable privil...
static bool setWritable(MemoryBlock &M, std::string *ErrMsg=nullptr)
setWritable - When adding to a block of code, the JIT may need to mark a block of code as RW since th...
static std::error_code protectMappedMemory(const MemoryBlock &Block, unsigned Flags)
This method sets the protection flags for a block of memory to the state specified by /p Flags...