[llvm-commits] [llvm] r56303 - in /llvm/trunk: include/llvm/System/Memory.h lib/ExecutionEngine/ExecutionEngine.cpp lib/ExecutionEngine/JIT/JITEmitter.cpp lib/ExecutionEngine/JIT/JITMemoryManager.cpp lib/System/Memory.cpp lib/System/Unix/Memory.inc
Evan Cheng
evan.cheng at apple.com
Thu Sep 18 00:54:21 PDT 2008
Author: evancheng
Date: Thu Sep 18 02:54:21 2008
New Revision: 56303
URL: http://llvm.org/viewvc/llvm-project?rev=56303&view=rev
Log:
Preliminary support for systems which require changing JIT memory regions privilege from read / write to read / executable.
Modified:
llvm/trunk/include/llvm/System/Memory.h
llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp
llvm/trunk/lib/ExecutionEngine/JIT/JITEmitter.cpp
llvm/trunk/lib/ExecutionEngine/JIT/JITMemoryManager.cpp
llvm/trunk/lib/System/Memory.cpp
llvm/trunk/lib/System/Unix/Memory.inc
Modified: llvm/trunk/include/llvm/System/Memory.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/System/Memory.h?rev=56303&r1=56302&r2=56303&view=diff
==============================================================================
--- llvm/trunk/include/llvm/System/Memory.h (original)
+++ llvm/trunk/include/llvm/System/Memory.h Thu Sep 18 02:54:21 2008
@@ -69,6 +69,11 @@
/// 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);
};
}
}
Modified: llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp?rev=56303&r1=56302&r2=56303&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp Thu Sep 18 02:54:21 2008
@@ -86,7 +86,8 @@
/// 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;
Modified: llvm/trunk/lib/ExecutionEngine/JIT/JITEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/JIT/JITEmitter.cpp?rev=56303&r1=56302&r2=56303&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/JIT/JITEmitter.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/JIT/JITEmitter.cpp Thu Sep 18 02:54:21 2008
@@ -925,6 +925,9 @@
<< 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;
Modified: llvm/trunk/lib/ExecutionEngine/JIT/JITMemoryManager.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/JIT/JITMemoryManager.cpp?rev=56303&r1=56302&r2=56303&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/JIT/JITMemoryManager.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/JIT/JITMemoryManager.cpp Thu Sep 18 02:54:21 2008
@@ -370,7 +370,11 @@
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());
Modified: llvm/trunk/lib/System/Memory.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Memory.cpp?rev=56303&r1=56302&r2=56303&view=diff
==============================================================================
--- llvm/trunk/lib/System/Memory.cpp (original)
+++ llvm/trunk/lib/System/Memory.cpp Thu Sep 18 02:54:21 2008
@@ -58,3 +58,14 @@
#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
+}
Modified: llvm/trunk/lib/System/Unix/Memory.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Unix/Memory.inc?rev=56303&r1=56302&r2=56303&view=diff
==============================================================================
--- llvm/trunk/lib/System/Unix/Memory.inc (original)
+++ llvm/trunk/lib/System/Unix/Memory.inc Thu Sep 18 02:54:21 2008
@@ -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 @@
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 @@
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;
}
More information about the llvm-commits
mailing list