15 #include "llvm/Support/DataTypes.h"
19 #ifdef HAVE_SYS_MMAN_H
24 #include <mach/mach.h>
28 # if defined(__OpenBSD__)
29 # include <mips64/sysarch.h>
31 # include <sys/cachectl.h>
36 extern "C" void sys_icache_invalidate(
const void *Addr,
size_t len);
38 extern "C" void __clear_cache(
void *,
void*);
43 int getPosixProtectionFlags(
unsigned Flags) {
50 return PROT_READ | PROT_WRITE;
52 return PROT_READ | PROT_EXEC;
56 return PROT_READ | PROT_WRITE | PROT_EXEC;
58 #if defined(__FreeBSD__)
66 return PROT_READ | PROT_EXEC;
84 const MemoryBlock *
const NearBlock,
86 std::error_code &EC) {
87 EC = std::error_code();
92 const size_t NumPages = (NumBytes+PageSize-1)/PageSize;
95 #ifdef NEED_DEV_ZERO_FOR_MMAP
96 static int zero_fd = open(
"/dev/zero", O_RDWR);
98 EC = std::error_code(errno, std::generic_category());
104 int MMFlags = MAP_PRIVATE |
105 #ifdef HAVE_MMAP_ANONYMOUS
112 int Protect = getPosixProtectionFlags(PFlags);
115 uintptr_t Start = NearBlock ?
reinterpret_cast<uintptr_t
>(NearBlock->base()) +
116 NearBlock->size() : 0;
117 if (Start && Start % PageSize)
118 Start += PageSize - Start %
PageSize;
120 void *Addr = ::mmap(reinterpret_cast<void*>(Start), PageSize*NumPages,
121 Protect, MMFlags, fd, 0);
122 if (Addr == MAP_FAILED) {
126 EC = std::error_code(errno, std::generic_category());
127 return MemoryBlock();
131 Result.Address = Addr;
142 if (M.Address ==
nullptr || M.Size == 0)
143 return std::error_code();
145 if (0 != ::munmap(M.Address, M.Size))
146 return std::error_code(errno, std::generic_category());
151 return std::error_code();
156 if (M.Address ==
nullptr || M.Size == 0)
157 return std::error_code();
160 return std::error_code(EINVAL, std::generic_category());
162 int Protect = getPosixProtectionFlags(Flags);
164 int Result = ::mprotect(M.Address, M.Size, Protect);
166 return std::error_code(errno, std::generic_category());
171 return std::error_code();
181 std::string *ErrMsg) {
182 if (NumBytes == 0)
return MemoryBlock();
185 size_t NumPages = (NumBytes+PageSize-1)/PageSize;
188 #ifdef NEED_DEV_ZERO_FOR_MMAP
189 static int zero_fd = open(
"/dev/zero", O_RDWR);
191 MakeErrMsg(ErrMsg,
"Can't open /dev/zero device");
192 return MemoryBlock();
197 int flags = MAP_PRIVATE |
198 #ifdef HAVE_MMAP_ANONYMOUS
205 void* start = NearBlock ? (
unsigned char*)NearBlock->base() +
206 NearBlock->size() :
nullptr;
208 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
209 void *pa = ::mmap(start, PageSize*NumPages, PROT_READ|PROT_EXEC,
212 void *pa = ::mmap(start, PageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
215 if (pa == MAP_FAILED) {
219 MakeErrMsg(ErrMsg,
"Can't allocate RWX Memory");
220 return MemoryBlock();
223 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
224 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)pa,
225 (vm_size_t)(PageSize*NumPages), 0,
227 if (KERN_SUCCESS != kr) {
228 MakeErrMsg(ErrMsg,
"vm_protect max RX failed");
229 return MemoryBlock();
232 kr = vm_protect(mach_task_self(), (vm_address_t)pa,
233 (vm_size_t)(PageSize*NumPages), 0,
235 if (KERN_SUCCESS != kr) {
237 return MemoryBlock();
249 if (M.Address ==
nullptr || M.Size == 0)
return false;
250 if (0 != ::munmap(M.Address, M.Size))
251 return MakeErrMsg(ErrMsg,
"Can't release RWX Memory");
256 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
257 if (M.Address == 0 || M.Size == 0)
return false;
259 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)M.Address,
261 return KERN_SUCCESS == kr;
268 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
269 if (M.Address == 0 || M.Size == 0)
return false;
271 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)M.Address,
273 return KERN_SUCCESS == kr;
274 #elif defined(__arm__) || defined(__aarch64__)
283 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
284 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr,
287 return KERN_SUCCESS == kr;
294 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
295 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr,
298 return KERN_SUCCESS == kr;
311 #if defined(__APPLE__)
313 # if (defined(__POWERPC__) || defined (__ppc__) || \
314 defined(_POWER) || defined(_ARCH_PPC) || defined(__arm__) || \
316 sys_icache_invalidate(const_cast<void *>(Addr), Len);
321 # if (defined(__POWERPC__) || defined (__ppc__) || \
322 defined(_POWER) || defined(_ARCH_PPC)) && defined(__GNUC__)
323 const size_t LineSize = 32;
325 const intptr_t Mask = ~(LineSize - 1);
329 for (
intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
330 asm volatile(
"dcbf 0, %0" : :
"r"(Line));
331 asm volatile(
"sync");
333 for (
intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
334 asm volatile(
"icbi 0, %0" : :
"r"(Line));
335 asm volatile(
"isync");
336 # elif (defined(__arm__) || defined(__aarch64__) || defined(__mips__)) && \
339 const char *Start =
static_cast<const char *
>(Addr);
340 const char *End = Start + Len;
341 __clear_cache(const_cast<char *>(Start), const_cast<char *>(End));
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...
void ValgrindDiscardTranslations(const void *Addr, size_t Len)
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...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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...
static bool setRangeExecutable(const void *Addr, size_t Size)
setRangeExecutable - Mark the page containing a range of addresses as executable. ...
static unsigned getPageSize()
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 cl::opt< unsigned > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in ""bytes"), cl::init(4096))
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...