--- /dev/null
+//===- SectionMemoryManager.h - Memory manager for MCJIT/RtDyld -*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the declaration of a section-based memory manager used by
+// the MCJIT execution engine and RuntimeDyld.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTION_ENGINE_SECTION_MEMORY_MANAGER_H
+#define LLVM_EXECUTION_ENGINE_SECTION_MEMORY_MANAGER_H
+
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ExecutionEngine/JITMemoryManager.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/Memory.h"
+
+namespace llvm {
+
+/// This is a simple memory manager which implements the methods called by
+/// the RuntimeDyld class to allocate memory for section-based loading of
+/// objects, usually those generated by the MCJIT execution engine.
+///
+/// This memory manager allocates all section memory as read-write. The
+/// RuntimeDyld will copy JITed section memory into these allocated blocks
+/// and perform any necessary linking and relocations.
+///
+/// Any client using this memory manager MUST ensure that section-specific
+/// page permissions have been applied before attempting to execute functions
+/// in the JITed object. Permissions can be applied either by calling
+/// MCJIT::finalizeObject or by calling SectionMemoryManager::applyPermissions
+/// directly. Clients of MCJIT should call MCJIT::finalizeObject.
+class SectionMemoryManager : public JITMemoryManager {
+ SectionMemoryManager(const SectionMemoryManager&) LLVM_DELETED_FUNCTION;
+ void operator=(const SectionMemoryManager&) LLVM_DELETED_FUNCTION;
+
+public:
+ SectionMemoryManager() { }
+ virtual ~SectionMemoryManager();
+
+ /// \brief Allocates a memory block of (at least) the given size suitable for
+ /// executable code.
+ ///
+ /// The value of \p Alignment must be a power of two. If \p Alignment is zero
+ /// a default alignment of 16 will be used.
+ virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
+ unsigned SectionID);
+
+ /// \brief Allocates a memory block of (at least) the given size suitable for
+ /// executable code.
+ ///
+ /// The value of \p Alignment must be a power of two. If \p Alignment is zero
+ /// a default alignment of 16 will be used.
+ virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
+ unsigned SectionID,
+ bool isReadOnly);
+
+ /// \brief Applies section-specific memory permissions.
+ ///
+ /// This method is called when object loading is complete and section page
+ /// permissions can be applied. It is up to the memory manager implementation
+ /// to decide whether or not to act on this method. The memory manager will
+ /// typically allocate all sections as read-write and then apply specific
+ /// permissions when this method is called. Code sections cannot be executed
+ /// until this function has been called.
+ ///
+ /// \returns true if an error occurred, false otherwise.
+ virtual bool applyPermissions(std::string *ErrMsg = 0);
+
+ /// This method returns the address of the specified function. As such it is
+ /// only useful for resolving library symbols, not code generated symbols.
+ ///
+ /// If \p AbortOnFailure is false and no function with the given name is
+ /// found, this function returns a null pointer. Otherwise, it prints a
+ /// message to stderr and aborts.
+ virtual void *getPointerToNamedFunction(const std::string &Name,
+ bool AbortOnFailure = true);
+
+ /// \brief Invalidate instruction cache for code sections.
+ ///
+ /// Some platforms with separate data cache and instruction cache require
+ /// explicit cache flush, otherwise JIT code manipulations (like resolved
+ /// relocations) will get to the data cache but not to the instruction cache.
+ ///
+ /// This method is not called by RuntimeDyld or MCJIT during the load
+ /// process. Clients may call this function when needed. See the lli
+ /// tool for example use.
+ virtual void invalidateInstructionCache();
+
+private:
+ struct MemoryGroup {
+ SmallVector<sys::MemoryBlock, 16> AllocatedMem;
+ SmallVector<sys::MemoryBlock, 16> FreeMem;
+ sys::MemoryBlock Near;
+ };
+
+ uint8_t *allocateSection(MemoryGroup &MemGroup, uintptr_t Size,
+ unsigned Alignment);
+
+ error_code applyMemoryGroupPermissions(MemoryGroup &MemGroup,
+ unsigned Permissions);
+
+ MemoryGroup CodeMem;
+ MemoryGroup RWDataMem;
+ MemoryGroup RODataMem;
+
+public:
+ ///
+ /// Functions below are not used by MCJIT or RuntimeDyld, but must be
+ /// implemented because they are declared as pure virtuals in the base class.
+ ///
+
+ virtual void setMemoryWritable() {
+ llvm_unreachable("Unexpected call!");
+ }
+ virtual void setMemoryExecutable() {
+ llvm_unreachable("Unexpected call!");
+ }
+ virtual void setPoisonMemory(bool poison) {
+ llvm_unreachable("Unexpected call!");
+ }
+ virtual void AllocateGOT() {
+ llvm_unreachable("Unexpected call!");
+ }
+ virtual uint8_t *getGOTBase() const {
+ llvm_unreachable("Unexpected call!");
+ return 0;
+ }
+ virtual uint8_t *startFunctionBody(const Function *F,
+ uintptr_t &ActualSize){
+ llvm_unreachable("Unexpected call!");
+ return 0;
+ }
+ virtual uint8_t *allocateStub(const GlobalValue *F, unsigned StubSize,
+ unsigned Alignment) {
+ llvm_unreachable("Unexpected call!");
+ return 0;
+ }
+ virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
+ uint8_t *FunctionEnd) {
+ llvm_unreachable("Unexpected call!");
+ }
+ virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
+ llvm_unreachable("Unexpected call!");
+ return 0;
+ }
+ virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) {
+ llvm_unreachable("Unexpected call!");
+ return 0;
+ }
+ virtual void deallocateFunctionBody(void *Body) {
+ llvm_unreachable("Unexpected call!");
+ }
+ virtual uint8_t *startExceptionTable(const Function *F,
+ uintptr_t &ActualSize) {
+ llvm_unreachable("Unexpected call!");
+ return 0;
+ }
+ virtual void endExceptionTable(const Function *F, uint8_t *TableStart,
+ uint8_t *TableEnd, uint8_t *FrameRegister) {
+ llvm_unreachable("Unexpected call!");
+ }
+ virtual void deallocateExceptionTable(void *ET) {
+ llvm_unreachable("Unexpected call!");
+ }
+};
+
+}
+
+#endif // LLVM_EXECUTION_ENGINE_SECTION_MEMORY_MANAGER_H
+
RuntimeDyld.cpp
RuntimeDyldELF.cpp
RuntimeDyldMachO.cpp
+ SectionMemoryManager.cpp
)
--- /dev/null
+//===- SectionMemoryManager.cpp - Memory manager for MCJIT/RtDyld *- C++ -*-==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the section-based memory manager used by the MCJIT
+// execution engine and RuntimeDyld
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Config/config.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/Support/DynamicLibrary.h"
+#include "llvm/Support/MathExtras.h"
+
+#ifdef __linux__
+ // These includes used by SectionMemoryManager::getPointerToNamedFunction()
+ // for Glibc trickery. See comments in this function for more information.
+ #ifdef HAVE_SYS_STAT_H
+ #include <sys/stat.h>
+ #endif
+ #include <fcntl.h>
+ #include <unistd.h>
+#endif
+
+namespace llvm {
+
+uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
+ unsigned Alignment,
+ unsigned SectionID,
+ bool IsReadOnly) {
+ if (IsReadOnly)
+ return allocateSection(RODataMem, Size, Alignment);
+ return allocateSection(RWDataMem, Size, Alignment);
+}
+
+uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
+ unsigned Alignment,
+ unsigned SectionID) {
+ return allocateSection(CodeMem, Size, Alignment);
+}
+
+uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
+ uintptr_t Size,
+ unsigned Alignment) {
+ if (!Alignment)
+ Alignment = 16;
+
+ assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
+
+ uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
+ uintptr_t Addr = 0;
+
+ // Look in the list of free memory regions and use a block there if one
+ // is available.
+ for (int i = 0, e = MemGroup.FreeMem.size(); i != e; ++i) {
+ sys::MemoryBlock &MB = MemGroup.FreeMem[i];
+ if (MB.size() >= RequiredSize) {
+ Addr = (uintptr_t)MB.base();
+ uintptr_t EndOfBlock = Addr + MB.size();
+ // Align the address.
+ Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
+ // Store cutted free memory block.
+ MemGroup.FreeMem[i] = sys::MemoryBlock((void*)(Addr + Size),
+ EndOfBlock - Addr - Size);
+ return (uint8_t*)Addr;
+ }
+ }
+
+ // No pre-allocated free block was large enough. Allocate a new memory region.
+ // Note that all sections get allocated as read-write. The permissions will
+ // be updated later based on memory group.
+ //
+ // FIXME: It would be useful to define a default allocation size (or add
+ // it as a constructor parameter) to minimize the number of allocations.
+ //
+ // FIXME: Initialize the Near member for each memory group to avoid
+ // interleaving.
+ error_code ec;
+ sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(RequiredSize,
+ &MemGroup.Near,
+ sys::Memory::MF_READ |
+ sys::Memory::MF_WRITE,
+ ec);
+ if (ec) {
+ // FIXME: Add error propogation to the interface.
+ return NULL;
+ }
+
+ // Save this address as the basis for our next request
+ MemGroup.Near = MB;
+
+ MemGroup.AllocatedMem.push_back(MB);
+ Addr = (uintptr_t)MB.base();
+ uintptr_t EndOfBlock = Addr + MB.size();
+
+ // Align the address.
+ Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
+
+ // The allocateMappedMemory may allocate much more memory than we need. In
+ // this case, we store the unused memory as a free memory block.
+ unsigned FreeSize = EndOfBlock-Addr-Size;
+ if (FreeSize > 16)
+ MemGroup.FreeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
+
+ // Return aligned address
+ return (uint8_t*)Addr;
+}
+
+bool SectionMemoryManager::applyPermissions(std::string *ErrMsg)
+{
+ // FIXME: Should in-progress permissions be reverted if an error occurs?
+ error_code ec;
+
+ // Make code memory executable.
+ ec = applyMemoryGroupPermissions(CodeMem,
+ sys::Memory::MF_READ | sys::Memory::MF_EXEC);
+ if (ec) {
+ if (ErrMsg) {
+ *ErrMsg = ec.message();
+ }
+ return true;
+ }
+
+ // Make read-only data memory read-only.
+ ec = applyMemoryGroupPermissions(RODataMem,
+ sys::Memory::MF_READ | sys::Memory::MF_EXEC);
+ if (ec) {
+ if (ErrMsg) {
+ *ErrMsg = ec.message();
+ }
+ return true;
+ }
+
+ // Read-write data memory already has the correct permissions
+
+ return false;
+}
+
+error_code SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
+ unsigned Permissions) {
+
+ for (int i = 0, e = MemGroup.AllocatedMem.size(); i != e; ++i) {
+ error_code ec;
+ ec = sys::Memory::protectMappedMemory(MemGroup.AllocatedMem[i],
+ Permissions);
+ if (ec) {
+ return ec;
+ }
+ }
+
+ return error_code::success();
+}
+
+void SectionMemoryManager::invalidateInstructionCache() {
+ for (int i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
+ sys::Memory::InvalidateInstructionCache(CodeMem.AllocatedMem[i].base(),
+ CodeMem.AllocatedMem[i].size());
+}
+
+static int jit_noop() {
+ return 0;
+}
+
+void *SectionMemoryManager::getPointerToNamedFunction(const std::string &Name,
+ bool AbortOnFailure) {
+#if defined(__linux__)
+ //===--------------------------------------------------------------------===//
+ // Function stubs that are invoked instead of certain library calls
+ //
+ // Force the following functions to be linked in to anything that uses the
+ // JIT. This is a hack designed to work around the all-too-clever Glibc
+ // strategy of making these functions work differently when inlined vs. when
+ // not inlined, and hiding their real definitions in a separate archive file
+ // that the dynamic linker can't see. For more info, search for
+ // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
+ if (Name == "stat") return (void*)(intptr_t)&stat;
+ if (Name == "fstat") return (void*)(intptr_t)&fstat;
+ if (Name == "lstat") return (void*)(intptr_t)&lstat;
+ if (Name == "stat64") return (void*)(intptr_t)&stat64;
+ if (Name == "fstat64") return (void*)(intptr_t)&fstat64;
+ if (Name == "lstat64") return (void*)(intptr_t)&lstat64;
+ if (Name == "atexit") return (void*)(intptr_t)&atexit;
+ if (Name == "mknod") return (void*)(intptr_t)&mknod;
+#endif // __linux__
+
+ // We should not invoke parent's ctors/dtors from generated main()!
+ // On Mingw and Cygwin, the symbol __main is resolved to
+ // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
+ // (and register wrong callee's dtors with atexit(3)).
+ // We expect ExecutionEngine::runStaticConstructorsDestructors()
+ // is called before ExecutionEngine::runFunctionAsMain() is called.
+ if (Name == "__main") return (void*)(intptr_t)&jit_noop;
+
+ const char *NameStr = Name.c_str();
+ void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
+ if (Ptr) return Ptr;
+
+ // If it wasn't found and if it starts with an underscore ('_') character,
+ // try again without the underscore.
+ if (NameStr[0] == '_') {
+ Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
+ if (Ptr) return Ptr;
+ }
+
+ if (AbortOnFailure)
+ report_fatal_error("Program used external function '" + Name +
+ "' which could not be resolved!");
+ return 0;
+}
+
+SectionMemoryManager::~SectionMemoryManager() {
+ for (unsigned i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
+ sys::Memory::releaseMappedMemory(CodeMem.AllocatedMem[i]);
+ for (unsigned i = 0, e = RWDataMem.AllocatedMem.size(); i != e; ++i)
+ sys::Memory::releaseMappedMemory(RWDataMem.AllocatedMem[i]);
+ for (unsigned i = 0, e = RODataMem.AllocatedMem.size(); i != e; ++i)
+ sys::Memory::releaseMappedMemory(RODataMem.AllocatedMem[i]);
+}
+
+} // namespace llvm
+
asmparser
bitreader
bitwriter
- mcjit
jit
+ mcjit
nativecodegen
)
set(MCJITTestsSources
MCJITTest.cpp
- SectionMemoryManager.cpp
+ MCJITMemoryManagerTest.cpp
)
if(MSVC)
--- /dev/null
+//===- MCJITMemoryManagerTest.cpp - Unit tests for the JIT memory manager -===//\r
+//\r
+// The LLVM Compiler Infrastructure\r
+//\r
+// This file is distributed under the University of Illinois Open Source\r
+// License. See LICENSE.TXT for details.\r
+//\r
+//===----------------------------------------------------------------------===//\r
+\r
+#include "gtest/gtest.h"\r
+#include "llvm/ADT/OwningPtr.h"\r
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"\r
+#include "llvm/ExecutionEngine/JIT.h"\r
+\r
+using namespace llvm;\r
+\r
+namespace {\r
+\r
+TEST(MCJITMemoryManagerTest, BasicAllocations) {\r
+ OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());\r
+\r
+ uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1);\r
+ uint8_t *data1 = MemMgr->allocateDataSection(256, 0, 2, true);\r
+ uint8_t *code2 = MemMgr->allocateCodeSection(256, 0, 3);\r
+ uint8_t *data2 = MemMgr->allocateDataSection(256, 0, 4, false);\r
+\r
+ EXPECT_NE((uint8_t*)0, code1);\r
+ EXPECT_NE((uint8_t*)0, code2);\r
+ EXPECT_NE((uint8_t*)0, data1);\r
+ EXPECT_NE((uint8_t*)0, data2);\r
+\r
+ // Initialize the data\r
+ for (unsigned i = 0; i < 256; ++i) {\r
+ code1[i] = 1;\r
+ code2[i] = 2;\r
+ data1[i] = 3;\r
+ data2[i] = 4;\r
+ }\r
+\r
+ // Verify the data (this is checking for overlaps in the addresses)\r
+ for (unsigned i = 0; i < 256; ++i) {\r
+ EXPECT_EQ(1, code1[i]);\r
+ EXPECT_EQ(2, code2[i]);\r
+ EXPECT_EQ(3, data1[i]);\r
+ EXPECT_EQ(4, data2[i]);\r
+ }\r
+\r
+ std::string Error;\r
+ EXPECT_FALSE(MemMgr->applyPermissions(&Error));\r
+}\r
+\r
+TEST(MCJITMemoryManagerTest, LargeAllocations) {\r
+ OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());\r
+\r
+ uint8_t *code1 = MemMgr->allocateCodeSection(0x100000, 0, 1);\r
+ uint8_t *data1 = MemMgr->allocateDataSection(0x100000, 0, 2, true);\r
+ uint8_t *code2 = MemMgr->allocateCodeSection(0x100000, 0, 3);\r
+ uint8_t *data2 = MemMgr->allocateDataSection(0x100000, 0, 4, false);\r
+\r
+ EXPECT_NE((uint8_t*)0, code1);\r
+ EXPECT_NE((uint8_t*)0, code2);\r
+ EXPECT_NE((uint8_t*)0, data1);\r
+ EXPECT_NE((uint8_t*)0, data2);\r
+\r
+ // Initialize the data\r
+ for (unsigned i = 0; i < 0x100000; ++i) {\r
+ code1[i] = 1;\r
+ code2[i] = 2;\r
+ data1[i] = 3;\r
+ data2[i] = 4;\r
+ }\r
+\r
+ // Verify the data (this is checking for overlaps in the addresses)\r
+ for (unsigned i = 0; i < 0x100000; ++i) {\r
+ EXPECT_EQ(1, code1[i]);\r
+ EXPECT_EQ(2, code2[i]);\r
+ EXPECT_EQ(3, data1[i]);\r
+ EXPECT_EQ(4, data2[i]);\r
+ }\r
+\r
+ std::string Error;\r
+ EXPECT_FALSE(MemMgr->applyPermissions(&Error));\r
+}\r
+\r
+TEST(MCJITMemoryManagerTest, ManyAllocations) {\r
+ OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());\r
+\r
+ uint8_t* code[10000];\r
+ uint8_t* data[10000];\r
+\r
+ for (unsigned i = 0; i < 10000; ++i) {\r
+ const bool isReadOnly = i % 2 == 0;\r
+\r
+ code[i] = MemMgr->allocateCodeSection(32, 0, 1);\r
+ data[i] = MemMgr->allocateDataSection(32, 0, 2, isReadOnly);\r
+\r
+ for (unsigned j = 0; j < 32; j++) {\r
+ code[i][j] = 1 + (i % 254);\r
+ data[i][j] = 2 + (i % 254);\r
+ }\r
+\r
+ EXPECT_NE((uint8_t *)0, code[i]);\r
+ EXPECT_NE((uint8_t *)0, data[i]);\r
+ }\r
+\r
+ // Verify the data (this is checking for overlaps in the addresses)\r
+ for (unsigned i = 0; i < 10000; ++i) {\r
+ for (unsigned j = 0; j < 32;j++ ) {\r
+ uint8_t ExpectedCode = 1 + (i % 254);\r
+ uint8_t ExpectedData = 2 + (i % 254);\r
+ EXPECT_EQ(ExpectedCode, code[i][j]);\r
+ EXPECT_EQ(ExpectedData, data[i][j]);\r
+ }\r
+ }\r
+\r
+ std::string Error;\r
+ EXPECT_FALSE(MemMgr->applyPermissions(&Error));\r
+}\r
+\r
+TEST(MCJITMemoryManagerTest, ManyVariedAllocations) {\r
+ OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());\r
+\r
+ uint8_t* code[10000];\r
+ uint8_t* data[10000];\r
+\r
+ for (unsigned i = 0; i < 10000; ++i) {\r
+ uintptr_t CodeSize = i % 16 + 1;\r
+ uintptr_t DataSize = i % 8 + 1;\r
+\r
+ bool isReadOnly = i % 3 == 0;\r
+ unsigned Align = 8 << (i % 4);\r
+\r
+ code[i] = MemMgr->allocateCodeSection(CodeSize, Align, i);\r
+ data[i] = MemMgr->allocateDataSection(DataSize, Align, i + 10000,\r
+ isReadOnly);\r
+\r
+ for (unsigned j = 0; j < CodeSize; j++) {\r
+ code[i][j] = 1 + (i % 254);\r
+ }\r
+\r
+ for (unsigned j = 0; j < DataSize; j++) {\r
+ data[i][j] = 2 + (i % 254);\r
+ }\r
+\r
+ EXPECT_NE((uint8_t *)0, code[i]);\r
+ EXPECT_NE((uint8_t *)0, data[i]);\r
+\r
+ uintptr_t CodeAlign = Align ? (uintptr_t)code[i] % Align : 0;\r
+ uintptr_t DataAlign = Align ? (uintptr_t)data[i] % Align : 0;\r
+\r
+ EXPECT_EQ((uintptr_t)0, CodeAlign);\r
+ EXPECT_EQ((uintptr_t)0, DataAlign);\r
+ }\r
+\r
+ for (unsigned i = 0; i < 10000; ++i) {\r
+ uintptr_t CodeSize = i % 16 + 1;\r
+ uintptr_t DataSize = i % 8 + 1;\r
+\r
+ for (unsigned j = 0; j < CodeSize; j++) {\r
+ uint8_t ExpectedCode = 1 + (i % 254);\r
+ EXPECT_EQ(ExpectedCode, code[i][j]);\r
+ }\r
+\r
+ for (unsigned j = 0; j < DataSize; j++) {\r
+ uint8_t ExpectedData = 2 + (i % 254);\r
+ EXPECT_EQ(ExpectedData, data[i][j]); \r
+ }\r
+ }\r
+}\r
+\r
+} // Namespace\r
+\r
#include "llvm/ADT/SmallVector.h"
#include "llvm/Config/config.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/Function.h"
#include "llvm/IRBuilder.h"
#include "llvm/LLVMContext.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/TypeBuilder.h"
-#include "SectionMemoryManager.h"
-
// Used to skip tests on unsupported architectures and operating systems.
// To skip a test, add this macro at the top of a test-case in a suite that
// inherits from MCJITTestBase. See MCJITTest.cpp for examples.
+++ /dev/null
-//===- SectionMemoryManager.cpp - Memory manager for MCJIT/RtDyld *- C++ -*-==//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements the section-based memory manager used by the MCJIT
-// execution engine and RuntimeDyld
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/Config/config.h"
-#include "llvm/Support/DynamicLibrary.h"
-#include "llvm/Support/MathExtras.h"
-#include "SectionMemoryManager.h"
-
-#ifdef __linux__
- // These includes used by SectionMemoryManager::getPointerToNamedFunction()
- // for Glibc trickery. See comments in this function for more information.
- #ifdef HAVE_SYS_STAT_H
- #include <sys/stat.h>
- #endif
- #include <fcntl.h>
- #include <unistd.h>
-#endif
-
-namespace llvm {
-
-uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
- unsigned Alignment,
- unsigned SectionID,
- bool IsReadOnly) {
- if (IsReadOnly)
- return allocateSection(RODataMem, Size, Alignment);
- return allocateSection(RWDataMem, Size, Alignment);
-}
-
-uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
- unsigned Alignment,
- unsigned SectionID) {
- return allocateSection(CodeMem, Size, Alignment);
-}
-
-uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
- uintptr_t Size,
- unsigned Alignment) {
- if (!Alignment)
- Alignment = 16;
-
- assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
-
- uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
- uintptr_t Addr = 0;
-
- // Look in the list of free memory regions and use a block there if one
- // is available.
- for (int i = 0, e = MemGroup.FreeMem.size(); i != e; ++i) {
- sys::MemoryBlock &MB = MemGroup.FreeMem[i];
- if (MB.size() >= RequiredSize) {
- Addr = (uintptr_t)MB.base();
- uintptr_t EndOfBlock = Addr + MB.size();
- // Align the address.
- Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
- // Store cutted free memory block.
- MemGroup.FreeMem[i] = sys::MemoryBlock((void*)(Addr + Size),
- EndOfBlock - Addr - Size);
- return (uint8_t*)Addr;
- }
- }
-
- // No pre-allocated free block was large enough. Allocate a new memory region.
- // Note that all sections get allocated as read-write. The permissions will
- // be updated later based on memory group.
- //
- // FIXME: It would be useful to define a default allocation size (or add
- // it as a constructor parameter) to minimize the number of allocations.
- //
- // FIXME: Initialize the Near member for each memory group to avoid
- // interleaving.
- error_code ec;
- sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(RequiredSize,
- &MemGroup.Near,
- sys::Memory::MF_READ |
- sys::Memory::MF_WRITE,
- ec);
- if (ec) {
- // FIXME: Add error propogation to the interface.
- return NULL;
- }
-
- // Save this address as the basis for our next request
- MemGroup.Near = MB;
-
- MemGroup.AllocatedMem.push_back(MB);
- Addr = (uintptr_t)MB.base();
- uintptr_t EndOfBlock = Addr + MB.size();
-
- // Align the address.
- Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
-
- // The allocateMappedMemory may allocate much more memory than we need. In
- // this case, we store the unused memory as a free memory block.
- unsigned FreeSize = EndOfBlock-Addr-Size;
- if (FreeSize > 16)
- MemGroup.FreeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
-
- // Return aligned address
- return (uint8_t*)Addr;
-}
-
-bool SectionMemoryManager::applyPermissions(std::string *ErrMsg)
-{
- // FIXME: Should in-progress permissions be reverted if an error occurs?
- error_code ec;
-
- // Make code memory executable.
- ec = applyMemoryGroupPermissions(CodeMem,
- sys::Memory::MF_READ | sys::Memory::MF_EXEC);
- if (ec) {
- if (ErrMsg) {
- *ErrMsg = ec.message();
- }
- return true;
- }
-
- // Make read-only data memory read-only.
- ec = applyMemoryGroupPermissions(RODataMem,
- sys::Memory::MF_READ | sys::Memory::MF_EXEC);
- if (ec) {
- if (ErrMsg) {
- *ErrMsg = ec.message();
- }
- return true;
- }
-
- // Read-write data memory already has the correct permissions
-
- return false;
-}
-
-error_code SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
- unsigned Permissions) {
-
- for (int i = 0, e = MemGroup.AllocatedMem.size(); i != e; ++i) {
- error_code ec;
- ec = sys::Memory::protectMappedMemory(MemGroup.AllocatedMem[i],
- Permissions);
- if (ec) {
- return ec;
- }
- }
-
- return error_code::success();
-}
-
-void SectionMemoryManager::invalidateInstructionCache() {
- for (int i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
- sys::Memory::InvalidateInstructionCache(CodeMem.AllocatedMem[i].base(),
- CodeMem.AllocatedMem[i].size());
-}
-
-static int jit_noop() {
- return 0;
-}
-
-void *SectionMemoryManager::getPointerToNamedFunction(const std::string &Name,
- bool AbortOnFailure) {
-#if defined(__linux__)
- //===--------------------------------------------------------------------===//
- // Function stubs that are invoked instead of certain library calls
- //
- // Force the following functions to be linked in to anything that uses the
- // JIT. This is a hack designed to work around the all-too-clever Glibc
- // strategy of making these functions work differently when inlined vs. when
- // not inlined, and hiding their real definitions in a separate archive file
- // that the dynamic linker can't see. For more info, search for
- // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
- if (Name == "stat") return (void*)(intptr_t)&stat;
- if (Name == "fstat") return (void*)(intptr_t)&fstat;
- if (Name == "lstat") return (void*)(intptr_t)&lstat;
- if (Name == "stat64") return (void*)(intptr_t)&stat64;
- if (Name == "fstat64") return (void*)(intptr_t)&fstat64;
- if (Name == "lstat64") return (void*)(intptr_t)&lstat64;
- if (Name == "atexit") return (void*)(intptr_t)&atexit;
- if (Name == "mknod") return (void*)(intptr_t)&mknod;
-#endif // __linux__
-
- // We should not invoke parent's ctors/dtors from generated main()!
- // On Mingw and Cygwin, the symbol __main is resolved to
- // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
- // (and register wrong callee's dtors with atexit(3)).
- // We expect ExecutionEngine::runStaticConstructorsDestructors()
- // is called before ExecutionEngine::runFunctionAsMain() is called.
- if (Name == "__main") return (void*)(intptr_t)&jit_noop;
-
- const char *NameStr = Name.c_str();
- void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
- if (Ptr) return Ptr;
-
- // If it wasn't found and if it starts with an underscore ('_') character,
- // try again without the underscore.
- if (NameStr[0] == '_') {
- Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
- if (Ptr) return Ptr;
- }
-
- if (AbortOnFailure)
- report_fatal_error("Program used external function '" + Name +
- "' which could not be resolved!");
- return 0;
-}
-
-SectionMemoryManager::~SectionMemoryManager() {
- for (unsigned i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
- sys::Memory::releaseMappedMemory(CodeMem.AllocatedMem[i]);
- for (unsigned i = 0, e = RWDataMem.AllocatedMem.size(); i != e; ++i)
- sys::Memory::releaseMappedMemory(RWDataMem.AllocatedMem[i]);
- for (unsigned i = 0, e = RODataMem.AllocatedMem.size(); i != e; ++i)
- sys::Memory::releaseMappedMemory(RODataMem.AllocatedMem[i]);
-}
-
-} // namespace llvm
-
+++ /dev/null
-//===- SectionMemoryManager.h - Memory manager for MCJIT/RtDyld -*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file contains the declaration of a section-based memory manager used by
-// the MCJIT execution engine and RuntimeDyld.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_EXECUTION_ENGINE_SECTION_MEMORY_MANAGER_H
-#define LLVM_EXECUTION_ENGINE_SECTION_MEMORY_MANAGER_H
-
-#include "llvm/ADT/SmallVector.h"
-#include "llvm/ExecutionEngine/JITMemoryManager.h"
-#include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/Memory.h"
-
-namespace llvm {
-
-/// This is a simple memory manager which implements the methods called by
-/// the RuntimeDyld class to allocate memory for section-based loading of
-/// objects, usually those generated by the MCJIT execution engine.
-///
-/// This memory manager allocates all section memory as read-write. The
-/// RuntimeDyld will copy JITed section memory into these allocated blocks
-/// and perform any necessary linking and relocations.
-///
-/// Any client using this memory manager MUST ensure that section-specific
-/// page permissions have been applied before attempting to execute functions
-/// in the JITed object. Permissions can be applied either by calling
-/// MCJIT::finalizeObject or by calling SectionMemoryManager::applyPermissions
-/// directly. Clients of MCJIT should call MCJIT::finalizeObject.
-class SectionMemoryManager : public JITMemoryManager {
- SectionMemoryManager(const SectionMemoryManager&) LLVM_DELETED_FUNCTION;
- void operator=(const SectionMemoryManager&) LLVM_DELETED_FUNCTION;
-
-public:
- SectionMemoryManager() { }
- virtual ~SectionMemoryManager();
-
- /// \brief Allocates a memory block of (at least) the given size suitable for
- /// executable code.
- ///
- /// The value of \p Alignment must be a power of two. If \p Alignment is zero
- /// a default alignment of 16 will be used.
- virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
- unsigned SectionID);
-
- /// \brief Allocates a memory block of (at least) the given size suitable for
- /// executable code.
- ///
- /// The value of \p Alignment must be a power of two. If \p Alignment is zero
- /// a default alignment of 16 will be used.
- virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
- unsigned SectionID,
- bool isReadOnly);
-
- /// \brief Applies section-specific memory permissions.
- ///
- /// This method is called when object loading is complete and section page
- /// permissions can be applied. It is up to the memory manager implementation
- /// to decide whether or not to act on this method. The memory manager will
- /// typically allocate all sections as read-write and then apply specific
- /// permissions when this method is called. Code sections cannot be executed
- /// until this function has been called.
- ///
- /// \returns true if an error occurred, false otherwise.
- virtual bool applyPermissions(std::string *ErrMsg = 0);
-
- /// This method returns the address of the specified function. As such it is
- /// only useful for resolving library symbols, not code generated symbols.
- ///
- /// If \p AbortOnFailure is false and no function with the given name is
- /// found, this function returns a null pointer. Otherwise, it prints a
- /// message to stderr and aborts.
- virtual void *getPointerToNamedFunction(const std::string &Name,
- bool AbortOnFailure = true);
-
- /// \brief Invalidate instruction cache for code sections.
- ///
- /// Some platforms with separate data cache and instruction cache require
- /// explicit cache flush, otherwise JIT code manipulations (like resolved
- /// relocations) will get to the data cache but not to the instruction cache.
- ///
- /// This method is not called by RuntimeDyld or MCJIT during the load
- /// process. Clients may call this function when needed. See the lli
- /// tool for example use.
- virtual void invalidateInstructionCache();
-
-private:
- struct MemoryGroup {
- SmallVector<sys::MemoryBlock, 16> AllocatedMem;
- SmallVector<sys::MemoryBlock, 16> FreeMem;
- sys::MemoryBlock Near;
- };
-
- uint8_t *allocateSection(MemoryGroup &MemGroup, uintptr_t Size,
- unsigned Alignment);
-
- error_code applyMemoryGroupPermissions(MemoryGroup &MemGroup,
- unsigned Permissions);
-
- MemoryGroup CodeMem;
- MemoryGroup RWDataMem;
- MemoryGroup RODataMem;
-
-public:
- ///
- /// Functions below are not used by MCJIT or RuntimeDyld, but must be
- /// implemented because they are declared as pure virtuals in the base class.
- ///
-
- virtual void setMemoryWritable() {
- llvm_unreachable("Unexpected call!");
- }
- virtual void setMemoryExecutable() {
- llvm_unreachable("Unexpected call!");
- }
- virtual void setPoisonMemory(bool poison) {
- llvm_unreachable("Unexpected call!");
- }
- virtual void AllocateGOT() {
- llvm_unreachable("Unexpected call!");
- }
- virtual uint8_t *getGOTBase() const {
- llvm_unreachable("Unexpected call!");
- return 0;
- }
- virtual uint8_t *startFunctionBody(const Function *F,
- uintptr_t &ActualSize){
- llvm_unreachable("Unexpected call!");
- return 0;
- }
- virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
- unsigned Alignment) {
- llvm_unreachable("Unexpected call!");
- return 0;
- }
- virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
- uint8_t *FunctionEnd) {
- llvm_unreachable("Unexpected call!");
- }
- virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
- llvm_unreachable("Unexpected call!");
- return 0;
- }
- virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) {
- llvm_unreachable("Unexpected call!");
- return 0;
- }
- virtual void deallocateFunctionBody(void *Body) {
- llvm_unreachable("Unexpected call!");
- }
- virtual uint8_t *startExceptionTable(const Function *F,
- uintptr_t &ActualSize) {
- llvm_unreachable("Unexpected call!");
- return 0;
- }
- virtual void endExceptionTable(const Function *F, uint8_t *TableStart,
- uint8_t *TableEnd, uint8_t *FrameRegister) {
- llvm_unreachable("Unexpected call!");
- }
- virtual void deallocateExceptionTable(void *ET) {
- llvm_unreachable("Unexpected call!");
- }
-};
-
-}
-
-#endif // LLVM_EXECUTION_ENGINE_SECTION_MEMORY_MANAGER_H
-