summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvan Cheng <evan.cheng@apple.com>2008-09-18 07:54:21 +0000
committerEvan Cheng <evan.cheng@apple.com>2008-09-18 07:54:21 +0000
commitbc4707a2554ac04ba006bf70035e7bc7270236a9 (patch)
treede53bff1fc81864b96c267cd930d7ea35a2dad3e
parent057d0c35358841aba449d203622416431163cb83 (diff)
downloadllvm-bc4707a2554ac04ba006bf70035e7bc7270236a9.tar.gz
llvm-bc4707a2554ac04ba006bf70035e7bc7270236a9.tar.bz2
llvm-bc4707a2554ac04ba006bf70035e7bc7270236a9.tar.xz
Preliminary support for systems which require changing JIT memory regions privilege from read / write to read / executable.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@56303 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/System/Memory.h5
-rw-r--r--lib/ExecutionEngine/ExecutionEngine.cpp3
-rw-r--r--lib/ExecutionEngine/JIT/JITEmitter.cpp3
-rw-r--r--lib/ExecutionEngine/JIT/JITMemoryManager.cpp4
-rw-r--r--lib/System/Memory.cpp11
-rw-r--r--lib/System/Unix/Memory.inc29
6 files changed, 54 insertions, 1 deletions
diff --git a/include/llvm/System/Memory.h b/include/llvm/System/Memory.h
index 014633c8c5..86895e2317 100644
--- a/include/llvm/System/Memory.h
+++ b/include/llvm/System/Memory.h
@@ -69,6 +69,11 @@ namespace sys {
/// that has been emitted it must invalidate the instruction cache on some
/// platforms.
static void InvalidateInstructionCache(const void *Addr, size_t Len);
+
+ /// SetRXPrivilege - Before the JIT can run a block of code, it has to be
+ /// given read and executable privilege. Return true if it is already r-x
+ /// or the system is able to change its previlege.
+ static bool SetRXPrivilege(const void *Addr, size_t Size);
};
}
}
diff --git a/lib/ExecutionEngine/ExecutionEngine.cpp b/lib/ExecutionEngine/ExecutionEngine.cpp
index 2a7b914ac3..52a69eaea7 100644
--- a/lib/ExecutionEngine/ExecutionEngine.cpp
+++ b/lib/ExecutionEngine/ExecutionEngine.cpp
@@ -86,7 +86,8 @@ Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
/// existing data in memory.
void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
MutexGuard locked(lock);
-
+
+ DOUT << "Map " << *GV << " to " << Addr << "\n";
void *&CurVal = state.getGlobalAddressMap(locked)[GV];
assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
CurVal = Addr;
diff --git a/lib/ExecutionEngine/JIT/JITEmitter.cpp b/lib/ExecutionEngine/JIT/JITEmitter.cpp
index 02752a01cd..4803fdf153 100644
--- a/lib/ExecutionEngine/JIT/JITEmitter.cpp
+++ b/lib/ExecutionEngine/JIT/JITEmitter.cpp
@@ -925,6 +925,9 @@ bool JITEmitter::finishFunction(MachineFunction &F) {
<< Relocations.size() << " relocations\n";
Relocations.clear();
+ // Mark code region readable and executable if it's not so already.
+ sys::Memory::SetRXPrivilege(FnStart, FnEnd-FnStart);
+
#ifndef NDEBUG
{
DOUT << std::hex;
diff --git a/lib/ExecutionEngine/JIT/JITMemoryManager.cpp b/lib/ExecutionEngine/JIT/JITMemoryManager.cpp
index d2d5c2f8e0..804e88df4c 100644
--- a/lib/ExecutionEngine/JIT/JITMemoryManager.cpp
+++ b/lib/ExecutionEngine/JIT/JITMemoryManager.cpp
@@ -370,7 +370,11 @@ namespace {
DefaultJITMemoryManager::DefaultJITMemoryManager() {
// Allocate a 16M block of memory for functions.
+#if defined(__APPLE__) && defined(__arm__)
+ sys::MemoryBlock MemBlock = getNewMemoryBlock(4 << 20);
+#else
sys::MemoryBlock MemBlock = getNewMemoryBlock(16 << 20);
+#endif
unsigned char *MemBase = static_cast<unsigned char*>(MemBlock.base());
diff --git a/lib/System/Memory.cpp b/lib/System/Memory.cpp
index 3660bcb1a4..2fc6a23a3a 100644
--- a/lib/System/Memory.cpp
+++ b/lib/System/Memory.cpp
@@ -58,3 +58,14 @@ void llvm::sys::Memory::InvalidateInstructionCache(const void *Addr,
#endif // end PPC
}
+
+bool llvm::sys::Memory::SetRXPrivilege(const void *Addr, size_t Size) {
+#if defined(__APPLE__) && defined(__arm__)
+ kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr,
+ (vm_size_t)Size, 0,
+ VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
+ return KERN_SUCCESS == kr;
+#else
+ return true;
+#endif
+}
diff --git a/lib/System/Unix/Memory.inc b/lib/System/Unix/Memory.inc
index 621184e426..cf0157d577 100644
--- a/lib/System/Unix/Memory.inc
+++ b/lib/System/Unix/Memory.inc
@@ -18,6 +18,10 @@
#include <sys/mman.h>
#endif
+#ifdef __APPLE__
+#include <mach/mach.h>
+#endif
+
/// AllocateRWX - Allocate a slab of memory with read/write/execute
/// permissions. This is typically used for JIT applications where we want
/// to emit code to the memory then jump to it. Getting this type of memory
@@ -52,8 +56,13 @@ llvm::sys::Memory::AllocateRWX(unsigned NumBytes, const MemoryBlock* NearBlock,
void* start = NearBlock ? (unsigned char*)NearBlock->base() +
NearBlock->size() : 0;
+#if defined(__APPLE__) && defined(__arm__)
+ void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_EXEC,
+ flags, fd, 0);
+#else
void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
flags, fd, 0);
+#endif
if (pa == MAP_FAILED) {
if (NearBlock) //Try again without a near hint
return AllocateRWX(NumBytes, 0);
@@ -61,9 +70,29 @@ llvm::sys::Memory::AllocateRWX(unsigned NumBytes, const MemoryBlock* NearBlock,
MakeErrMsg(ErrMsg, "Can't allocate RWX Memory");
return MemoryBlock();
}
+
+#if defined(__APPLE__) && defined(__arm__)
+ kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)pa,
+ (vm_size_t)(pageSize*NumPages), 0,
+ VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
+ if (KERN_SUCCESS != kr) {
+ MakeErrMsg(ErrMsg, "vm_protect max RWX failed\n");
+ return sys::MemoryBlock();
+ }
+
+ kr = vm_protect(mach_task_self(), (vm_address_t)pa,
+ (vm_size_t)(pageSize*NumPages), 0,
+ VM_PROT_READ | VM_PROT_WRITE);
+ if (KERN_SUCCESS != kr) {
+ MakeErrMsg(ErrMsg, "vm_protect RW failed\n");
+ return sys::MemoryBlock();
+ }
+#endif
+
MemoryBlock result;
result.Address = pa;
result.Size = NumPages*pageSize;
+
return result;
}