llvm.org GIT mirror llvm / 0717be9
[MCJIT] Delete the JTIMemoryManager and associated APIs. This patch removes the old JIT memory manager (which does not provide any useful functionality now that the old JIT is gone), and migrates the few remaining clients over to SectionMemoryManager. http://llvm.org/PR20848 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@218316 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 6 years ago
10 changed file(s) with 15 addition(s) and 1153 deletion(s). Raw diff Collapse all Expand all
493493 std::string *ErrorStr;
494494 CodeGenOpt::Level OptLevel;
495495 RTDyldMemoryManager *MCJMM;
496 JITMemoryManager *JMM;
497496 TargetOptions Options;
498497 Reloc::Model RelocModel;
499498 CodeModel::Model CMModel;
527526 /// the setJITMemoryManager() option.
528527 EngineBuilder &setMCJITMemoryManager(RTDyldMemoryManager *mcjmm) {
529528 MCJMM = mcjmm;
530 JMM = nullptr;
531 return *this;
532 }
533
534 /// setJITMemoryManager - Sets the JIT memory manager to use. This allows
535 /// clients to customize their memory allocation policies. This is only
536 /// appropriate for either JIT or MCJIT; setting this and configuring the
537 /// builder to create an interpreter will cause a runtime error. If create()
538 /// is called and is successful, the created engine takes ownership of the
539 /// memory manager. This option defaults to NULL. This option overrides
540 /// setMCJITMemoryManager() as well.
541 EngineBuilder &setJITMemoryManager(JITMemoryManager *jmm) {
542 MCJMM = nullptr;
543 JMM = jmm;
544529 return *this;
545530 }
546531
+0
-164
include/llvm/ExecutionEngine/JITMemoryManager.h less more
None //===-- JITMemoryManager.h - Interface JIT uses to Allocate Mem -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_EXECUTIONENGINE_JITMEMORYMANAGER_H
10 #define LLVM_EXECUTIONENGINE_JITMEMORYMANAGER_H
11
12 #include "llvm/ExecutionEngine/RuntimeDyld.h"
13 #include "llvm/Support/DataTypes.h"
14 #include
15
16 namespace llvm {
17
18 class Function;
19 class GlobalValue;
20
21 /// JITMemoryManager - This interface is used by the JIT to allocate and manage
22 /// memory for the code generated by the JIT. This can be reimplemented by
23 /// clients that have a strong desire to control how the layout of JIT'd memory
24 /// works.
25 class JITMemoryManager : public RTDyldMemoryManager {
26 protected:
27 bool HasGOT;
28
29 public:
30 JITMemoryManager() : HasGOT(false) {}
31 virtual ~JITMemoryManager();
32
33 /// CreateDefaultMemManager - This is used to create the default
34 /// JIT Memory Manager if the client does not provide one to the JIT.
35 static JITMemoryManager *CreateDefaultMemManager();
36
37 /// setMemoryWritable - When code generation is in progress,
38 /// the code pages may need permissions changed.
39 virtual void setMemoryWritable() = 0;
40
41 /// setMemoryExecutable - When code generation is done and we're ready to
42 /// start execution, the code pages may need permissions changed.
43 virtual void setMemoryExecutable() = 0;
44
45 /// setPoisonMemory - Setting this flag to true makes the memory manager
46 /// garbage values over freed memory. This is useful for testing and
47 /// debugging, and may be turned on by default in debug mode.
48 virtual void setPoisonMemory(bool poison) = 0;
49
50 //===--------------------------------------------------------------------===//
51 // Global Offset Table Management
52 //===--------------------------------------------------------------------===//
53
54 /// AllocateGOT - If the current table requires a Global Offset Table, this
55 /// method is invoked to allocate it. This method is required to set HasGOT
56 /// to true.
57 virtual void AllocateGOT() = 0;
58
59 /// isManagingGOT - Return true if the AllocateGOT method is called.
60 bool isManagingGOT() const {
61 return HasGOT;
62 }
63
64 /// getGOTBase - If this is managing a Global Offset Table, this method should
65 /// return a pointer to its base.
66 virtual uint8_t *getGOTBase() const = 0;
67
68 //===--------------------------------------------------------------------===//
69 // Main Allocation Functions
70 //===--------------------------------------------------------------------===//
71
72 /// startFunctionBody - When we start JITing a function, the JIT calls this
73 /// method to allocate a block of free RWX memory, which returns a pointer to
74 /// it. If the JIT wants to request a block of memory of at least a certain
75 /// size, it passes that value as ActualSize, and this method returns a block
76 /// with at least that much space. If the JIT doesn't know ahead of time how
77 /// much space it will need to emit the function, it passes 0 for the
78 /// ActualSize. In either case, this method is required to pass back the size
79 /// of the allocated block through ActualSize. The JIT will be careful to
80 /// not write more than the returned ActualSize bytes of memory.
81 virtual uint8_t *startFunctionBody(const Function *F,
82 uintptr_t &ActualSize) = 0;
83
84 /// allocateStub - This method is called by the JIT to allocate space for a
85 /// function stub (used to handle limited branch displacements) while it is
86 /// JIT compiling a function. For example, if foo calls bar, and if bar
87 /// either needs to be lazily compiled or is a native function that exists too
88 /// far away from the call site to work, this method will be used to make a
89 /// thunk for it. The stub should be "close" to the current function body,
90 /// but should not be included in the 'actualsize' returned by
91 /// startFunctionBody.
92 virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
93 unsigned Alignment) = 0;
94
95 /// endFunctionBody - This method is called when the JIT is done codegen'ing
96 /// the specified function. At this point we know the size of the JIT
97 /// compiled function. This passes in FunctionStart (which was returned by
98 /// the startFunctionBody method) and FunctionEnd which is a pointer to the
99 /// actual end of the function. This method should mark the space allocated
100 /// and remember where it is in case the client wants to deallocate it.
101 virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
102 uint8_t *FunctionEnd) = 0;
103
104 /// allocateSpace - Allocate a memory block of the given size. This method
105 /// cannot be called between calls to startFunctionBody and endFunctionBody.
106 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) = 0;
107
108 /// allocateGlobal - Allocate memory for a global.
109 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0;
110
111 /// deallocateFunctionBody - Free the specified function body. The argument
112 /// must be the return value from a call to startFunctionBody() that hasn't
113 /// been deallocated yet. This is never called when the JIT is currently
114 /// emitting a function.
115 virtual void deallocateFunctionBody(void *Body) = 0;
116
117 /// CheckInvariants - For testing only. Return true if all internal
118 /// invariants are preserved, or return false and set ErrorStr to a helpful
119 /// error message.
120 virtual bool CheckInvariants(std::string &) {
121 return true;
122 }
123
124 /// GetDefaultCodeSlabSize - For testing only. Returns DefaultCodeSlabSize
125 /// from DefaultJITMemoryManager.
126 virtual size_t GetDefaultCodeSlabSize() {
127 return 0;
128 }
129
130 /// GetDefaultDataSlabSize - For testing only. Returns DefaultCodeSlabSize
131 /// from DefaultJITMemoryManager.
132 virtual size_t GetDefaultDataSlabSize() {
133 return 0;
134 }
135
136 /// GetDefaultStubSlabSize - For testing only. Returns DefaultCodeSlabSize
137 /// from DefaultJITMemoryManager.
138 virtual size_t GetDefaultStubSlabSize() {
139 return 0;
140 }
141
142 /// GetNumCodeSlabs - For testing only. Returns the number of MemoryBlocks
143 /// allocated for code.
144 virtual unsigned GetNumCodeSlabs() {
145 return 0;
146 }
147
148 /// GetNumDataSlabs - For testing only. Returns the number of MemoryBlocks
149 /// allocated for data.
150 virtual unsigned GetNumDataSlabs() {
151 return 0;
152 }
153
154 /// GetNumStubSlabs - For testing only. Returns the number of MemoryBlocks
155 /// allocated for function stubs.
156 virtual unsigned GetNumStubSlabs() {
157 return 0;
158 }
159 };
160
161 } // end namespace llvm.
162
163 #endif
1616 #include "llvm/ADT/Statistic.h"
1717 #include "llvm/ExecutionEngine/GenericValue.h"
1818 #include "llvm/ExecutionEngine/JITMemoryManager.h"
19 #include "llvm/ExecutionEngine/ObjectBuffer.h"
1920 #include "llvm/ExecutionEngine/ObjectCache.h"
2021 #include "llvm/IR/Constants.h"
2122 #include "llvm/IR/DataLayout.h"
400401 ErrorStr = nullptr;
401402 OptLevel = CodeGenOpt::Default;
402403 MCJMM = nullptr;
403 JMM = nullptr;
404404 Options = TargetOptions();
405405 RelocModel = Reloc::Default;
406406 CMModel = CodeModel::JITDefault;
421421 // to the function tells DynamicLibrary to load the program, not a library.
422422 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
423423 return nullptr;
424
425 assert(!(JMM && MCJMM));
426424
427425 // If the user specified a memory manager but didn't specify which engine to
428426 // create, we assume they only want the JIT, and we fail if they only want
429427 // the interpreter.
430 if (JMM || MCJMM) {
428 if (MCJMM) {
431429 if (WhichEngine & EngineKind::JIT)
432430 WhichEngine = EngineKind::JIT;
433431 else {
449447
450448 ExecutionEngine *EE = nullptr;
451449 if (ExecutionEngine::MCJITCtor)
452 EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr,
453 MCJMM ? MCJMM : JMM, std::move(TheTM));
450 EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, MCJMM,
451 std::move(TheTM));
454452 if (EE) {
455453 EE->setVerifyModules(VerifyModules);
456454 return EE;
0 add_llvm_library(LLVMMCJIT
1 JITMemoryManager.cpp
21 MCJIT.cpp
32 SectionMemoryManager.cpp
43 )
+0
-900
lib/ExecutionEngine/MCJIT/JITMemoryManager.cpp less more
None //===-- JITMemoryManager.cpp - Memory Allocator for JIT'd code ------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the DefaultJITMemoryManager class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/ExecutionEngine/JITMemoryManager.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Config/config.h"
18 #include "llvm/IR/GlobalValue.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/DynamicLibrary.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/Memory.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include
27 #include
28 #include
29 #include
30
31 #if defined(__linux__)
32 #if defined(HAVE_SYS_STAT_H)
33 #include
34 #endif
35 #include
36 #include
37 #endif
38
39 using namespace llvm;
40
41 #define DEBUG_TYPE "jit"
42
43 STATISTIC(NumSlabs, "Number of slabs of memory allocated by the JIT");
44
45 JITMemoryManager::~JITMemoryManager() {}
46
47 //===----------------------------------------------------------------------===//
48 // Memory Block Implementation.
49 //===----------------------------------------------------------------------===//
50
51 namespace {
52 /// MemoryRangeHeader - For a range of memory, this is the header that we put
53 /// on the block of memory. It is carefully crafted to be one word of memory.
54 /// Allocated blocks have just this header, free'd blocks have FreeRangeHeader
55 /// which starts with this.
56 struct FreeRangeHeader;
57 struct MemoryRangeHeader {
58 /// ThisAllocated - This is true if this block is currently allocated. If
59 /// not, this can be converted to a FreeRangeHeader.
60 unsigned ThisAllocated : 1;
61
62 /// PrevAllocated - Keep track of whether the block immediately before us is
63 /// allocated. If not, the word immediately before this header is the size
64 /// of the previous block.
65 unsigned PrevAllocated : 1;
66
67 /// BlockSize - This is the size in bytes of this memory block,
68 /// including this header.
69 uintptr_t BlockSize : (sizeof(intptr_t)*CHAR_BIT - 2);
70
71
72 /// getBlockAfter - Return the memory block immediately after this one.
73 ///
74 MemoryRangeHeader &getBlockAfter() const {
75 return *reinterpret_cast(
76 reinterpret_cast(
77 const_cast(this))+BlockSize);
78 }
79
80 /// getFreeBlockBefore - If the block before this one is free, return it,
81 /// otherwise return null.
82 FreeRangeHeader *getFreeBlockBefore() const {
83 if (PrevAllocated) return nullptr;
84 intptr_t PrevSize = reinterpret_cast(
85 const_cast(this))[-1];
86 return reinterpret_cast(
87 reinterpret_cast(
88 const_cast(this))-PrevSize);
89 }
90
91 /// FreeBlock - Turn an allocated block into a free block, adjusting
92 /// bits in the object headers, and adding an end of region memory block.
93 FreeRangeHeader *FreeBlock(FreeRangeHeader *FreeList);
94
95 /// TrimAllocationToSize - If this allocated block is significantly larger
96 /// than NewSize, split it into two pieces (where the former is NewSize
97 /// bytes, including the header), and add the new block to the free list.
98 FreeRangeHeader *TrimAllocationToSize(FreeRangeHeader *FreeList,
99 uint64_t NewSize);
100 };
101
102 /// FreeRangeHeader - For a memory block that isn't already allocated, this
103 /// keeps track of the current block and has a pointer to the next free block.
104 /// Free blocks are kept on a circularly linked list.
105 struct FreeRangeHeader : public MemoryRangeHeader {
106 FreeRangeHeader *Prev;
107 FreeRangeHeader *Next;
108
109 /// getMinBlockSize - Get the minimum size for a memory block. Blocks
110 /// smaller than this size cannot be created.
111 static unsigned getMinBlockSize() {
112 return sizeof(FreeRangeHeader)+sizeof(intptr_t);
113 }
114
115 /// SetEndOfBlockSizeMarker - The word at the end of every free block is
116 /// known to be the size of the free block. Set it for this block.
117 void SetEndOfBlockSizeMarker() {
118 void *EndOfBlock = (char*)this + BlockSize;
119 ((intptr_t *)EndOfBlock)[-1] = BlockSize;
120 }
121
122 FreeRangeHeader *RemoveFromFreeList() {
123 assert(Next->Prev == this && Prev->Next == this && "Freelist broken!");
124 Next->Prev = Prev;
125 return Prev->Next = Next;
126 }
127
128 void AddToFreeList(FreeRangeHeader *FreeList) {
129 Next = FreeList;
130 Prev = FreeList->Prev;
131 Prev->Next = this;
132 Next->Prev = this;
133 }
134
135 /// GrowBlock - The block after this block just got deallocated. Merge it
136 /// into the current block.
137 void GrowBlock(uintptr_t NewSize);
138
139 /// AllocateBlock - Mark this entire block allocated, updating freelists
140 /// etc. This returns a pointer to the circular free-list.
141 FreeRangeHeader *AllocateBlock();
142 };
143 }
144
145
146 /// AllocateBlock - Mark this entire block allocated, updating freelists
147 /// etc. This returns a pointer to the circular free-list.
148 FreeRangeHeader *FreeRangeHeader::AllocateBlock() {
149 assert(!ThisAllocated && !getBlockAfter().PrevAllocated &&
150 "Cannot allocate an allocated block!");
151 // Mark this block allocated.
152 ThisAllocated = 1;
153 getBlockAfter().PrevAllocated = 1;
154
155 // Remove it from the free list.
156 return RemoveFromFreeList();
157 }
158
159 /// FreeBlock - Turn an allocated block into a free block, adjusting
160 /// bits in the object headers, and adding an end of region memory block.
161 /// If possible, coalesce this block with neighboring blocks. Return the
162 /// FreeRangeHeader to allocate from.
163 FreeRangeHeader *MemoryRangeHeader::FreeBlock(FreeRangeHeader *FreeList) {
164 MemoryRangeHeader *FollowingBlock = &getBlockAfter();
165 assert(ThisAllocated && "This block is already free!");
166 assert(FollowingBlock->PrevAllocated && "Flags out of sync!");
167
168 FreeRangeHeader *FreeListToReturn = FreeList;
169
170 // If the block after this one is free, merge it into this block.
171 if (!FollowingBlock->ThisAllocated) {
172 FreeRangeHeader &FollowingFreeBlock = *(FreeRangeHeader *)FollowingBlock;
173 // "FreeList" always needs to be a valid free block. If we're about to
174 // coalesce with it, update our notion of what the free list is.
175 if (&FollowingFreeBlock == FreeList) {
176 FreeList = FollowingFreeBlock.Next;
177 FreeListToReturn = nullptr;
178 assert(&FollowingFreeBlock != FreeList && "No tombstone block?");
179 }
180 FollowingFreeBlock.RemoveFromFreeList();
181
182 // Include the following block into this one.
183 BlockSize += FollowingFreeBlock.BlockSize;
184 FollowingBlock = &FollowingFreeBlock.getBlockAfter();
185
186 // Tell the block after the block we are coalescing that this block is
187 // allocated.
188 FollowingBlock->PrevAllocated = 1;
189 }
190
191 assert(FollowingBlock->ThisAllocated && "Missed coalescing?");
192
193 if (FreeRangeHeader *PrevFreeBlock = getFreeBlockBefore()) {
194 PrevFreeBlock->GrowBlock(PrevFreeBlock->BlockSize + BlockSize);
195 return FreeListToReturn ? FreeListToReturn : PrevFreeBlock;
196 }
197
198 // Otherwise, mark this block free.
199 FreeRangeHeader &FreeBlock = *(FreeRangeHeader*)this;
200 FollowingBlock->PrevAllocated = 0;
201 FreeBlock.ThisAllocated = 0;
202
203 // Link this into the linked list of free blocks.
204 FreeBlock.AddToFreeList(FreeList);
205
206 // Add a marker at the end of the block, indicating the size of this free
207 // block.
208 FreeBlock.SetEndOfBlockSizeMarker();
209 return FreeListToReturn ? FreeListToReturn : &FreeBlock;
210 }
211
212 /// GrowBlock - The block after this block just got deallocated. Merge it
213 /// into the current block.
214 void FreeRangeHeader::GrowBlock(uintptr_t NewSize) {
215 assert(NewSize > BlockSize && "Not growing block?");
216 BlockSize = NewSize;
217 SetEndOfBlockSizeMarker();
218 getBlockAfter().PrevAllocated = 0;
219 }
220
221 /// TrimAllocationToSize - If this allocated block is significantly larger
222 /// than NewSize, split it into two pieces (where the former is NewSize
223 /// bytes, including the header), and add the new block to the free list.
224 FreeRangeHeader *MemoryRangeHeader::
225 TrimAllocationToSize(FreeRangeHeader *FreeList, uint64_t NewSize) {
226 assert(ThisAllocated && getBlockAfter().PrevAllocated &&
227 "Cannot deallocate part of an allocated block!");
228
229 // Don't allow blocks to be trimmed below minimum required size
230 NewSize = std::max(FreeRangeHeader::getMinBlockSize(), NewSize);
231
232 // Round up size for alignment of header.
233 unsigned HeaderAlign = __alignof(FreeRangeHeader);
234 NewSize = (NewSize+ (HeaderAlign-1)) & ~(HeaderAlign-1);
235
236 // Size is now the size of the block we will remove from the start of the
237 // current block.
238 assert(NewSize <= BlockSize &&
239 "Allocating more space from this block than exists!");
240
241 // If splitting this block will cause the remainder to be too small, do not
242 // split the block.
243 if (BlockSize <= NewSize+FreeRangeHeader::getMinBlockSize())
244 return FreeList;
245
246 // Otherwise, we splice the required number of bytes out of this block, form
247 // a new block immediately after it, then mark this block allocated.
248 MemoryRangeHeader &FormerNextBlock = getBlockAfter();
249
250 // Change the size of this block.
251 BlockSize = NewSize;
252
253 // Get the new block we just sliced out and turn it into a free block.
254 FreeRangeHeader &NewNextBlock = (FreeRangeHeader &)getBlockAfter();
255 NewNextBlock.BlockSize = (char*)&FormerNextBlock - (char*)&NewNextBlock;
256 NewNextBlock.ThisAllocated = 0;
257 NewNextBlock.PrevAllocated = 1;
258 NewNextBlock.SetEndOfBlockSizeMarker();
259 FormerNextBlock.PrevAllocated = 0;
260 NewNextBlock.AddToFreeList(FreeList);
261 return &NewNextBlock;
262 }
263
264 //===----------------------------------------------------------------------===//
265 // Memory Block Implementation.
266 //===----------------------------------------------------------------------===//
267
268 namespace {
269
270 class DefaultJITMemoryManager;
271
272 class JITAllocator {
273 DefaultJITMemoryManager &JMM;
274 public:
275 JITAllocator(DefaultJITMemoryManager &jmm) : JMM(jmm) { }
276 void *Allocate(size_t Size, size_t /*Alignment*/);
277 void Deallocate(void *Slab, size_t Size);
278 };
279
280 /// DefaultJITMemoryManager - Manage memory for the JIT code generation.
281 /// This splits a large block of MAP_NORESERVE'd memory into two
282 /// sections, one for function stubs, one for the functions themselves. We
283 /// have to do this because we may need to emit a function stub while in the
284 /// middle of emitting a function, and we don't know how large the function we
285 /// are emitting is.
286 class DefaultJITMemoryManager : public JITMemoryManager {
287 public:
288 /// DefaultCodeSlabSize - When we have to go map more memory, we allocate at
289 /// least this much unless more is requested. Currently, in 512k slabs.
290 static const size_t DefaultCodeSlabSize = 512 * 1024;
291
292 /// DefaultSlabSize - Allocate globals and stubs into slabs of 64K (probably
293 /// 16 pages) unless we get an allocation above SizeThreshold.
294 static const size_t DefaultSlabSize = 64 * 1024;
295
296 /// DefaultSizeThreshold - For any allocation larger than 16K (probably
297 /// 4 pages), we should allocate a separate slab to avoid wasted space at
298 /// the end of a normal slab.
299 static const size_t DefaultSizeThreshold = 16 * 1024;
300
301 private:
302 // Whether to poison freed memory.
303 bool PoisonMemory;
304
305 /// LastSlab - This points to the last slab allocated and is used as the
306 /// NearBlock parameter to AllocateRWX so that we can attempt to lay out all
307 /// stubs, data, and code contiguously in memory. In general, however, this
308 /// is not possible because the NearBlock parameter is ignored on Windows
309 /// platforms and even on Unix it works on a best-effort pasis.
310 sys::MemoryBlock LastSlab;
311
312 // Memory slabs allocated by the JIT. We refer to them as slabs so we don't
313 // confuse them with the blocks of memory described above.
314 std::vector CodeSlabs;
315 BumpPtrAllocatorImpl
316 DefaultSizeThreshold> StubAllocator;
317 BumpPtrAllocatorImpl
318 DefaultSizeThreshold> DataAllocator;
319
320 // Circular list of free blocks.
321 FreeRangeHeader *FreeMemoryList;
322
323 // When emitting code into a memory block, this is the block.
324 MemoryRangeHeader *CurBlock;
325
326 std::unique_ptr GOTBase; // Target Specific reserved memory
327 public:
328 DefaultJITMemoryManager();
329 ~DefaultJITMemoryManager();
330
331 /// allocateNewSlab - Allocates a new MemoryBlock and remembers it as the
332 /// last slab it allocated, so that subsequent allocations follow it.
333 sys::MemoryBlock allocateNewSlab(size_t size);
334
335 /// getPointerToNamedFunction - This method returns the address of the
336 /// specified function by using the dlsym function call.
337 void *getPointerToNamedFunction(const std::string &Name,
338 bool AbortOnFailure = true) override;
339
340 void AllocateGOT() override;
341
342 // Testing methods.
343 bool CheckInvariants(std::string &ErrorStr) override;
344 size_t GetDefaultCodeSlabSize() override { return DefaultCodeSlabSize; }
345 size_t GetDefaultDataSlabSize() override { return DefaultSlabSize; }
346 size_t GetDefaultStubSlabSize() override { return DefaultSlabSize; }
347 unsigned GetNumCodeSlabs() override { return CodeSlabs.size(); }
348 unsigned GetNumDataSlabs() override { return DataAllocator.GetNumSlabs(); }
349 unsigned GetNumStubSlabs() override { return StubAllocator.GetNumSlabs(); }
350
351 /// startFunctionBody - When a function starts, allocate a block of free
352 /// executable memory, returning a pointer to it and its actual size.
353 uint8_t *startFunctionBody(const Function *F,
354 uintptr_t &ActualSize) override {
355
356 FreeRangeHeader* candidateBlock = FreeMemoryList;
357 FreeRangeHeader* head = FreeMemoryList;
358 FreeRangeHeader* iter = head->Next;
359
360 uintptr_t largest = candidateBlock->BlockSize;
361
362 // Search for the largest free block
363 while (iter != head) {
364 if (iter->BlockSize > largest) {
365 largest = iter->BlockSize;
366 candidateBlock = iter;
367 }
368 iter = iter->Next;
369 }
370
371 largest = largest - sizeof(MemoryRangeHeader);
372
373 // If this block isn't big enough for the allocation desired, allocate
374 // another block of memory and add it to the free list.
375 if (largest < ActualSize ||
376 largest <= FreeRangeHeader::getMinBlockSize()) {
377 DEBUG(dbgs() << "JIT: Allocating another slab of memory for function.");
378 candidateBlock = allocateNewCodeSlab((size_t)ActualSize);
379 }
380
381 // Select this candidate block for allocation
382 CurBlock = candidateBlock;
383
384 // Allocate the entire memory block.
385 FreeMemoryList = candidateBlock->AllocateBlock();
386 ActualSize = CurBlock->BlockSize - sizeof(MemoryRangeHeader);
387 return (uint8_t *)(CurBlock + 1);
388 }
389
390 /// allocateNewCodeSlab - Helper method to allocate a new slab of code
391 /// memory from the OS and add it to the free list. Returns the new
392 /// FreeRangeHeader at the base of the slab.
393 FreeRangeHeader *allocateNewCodeSlab(size_t MinSize) {
394 // If the user needs at least MinSize free memory, then we account for
395 // two MemoryRangeHeaders: the one in the user's block, and the one at the
396 // end of the slab.
397 size_t PaddedMin = MinSize + 2 * sizeof(MemoryRangeHeader);
398 size_t SlabSize = std::max(DefaultCodeSlabSize, PaddedMin);
399 sys::MemoryBlock B = allocateNewSlab(SlabSize);
400 CodeSlabs.push_back(B);
401 char *MemBase = (char*)(B.base());
402
403 // Put a tiny allocated block at the end of the memory chunk, so when
404 // FreeBlock calls getBlockAfter it doesn't fall off the end.
405 MemoryRangeHeader *EndBlock =
406 (MemoryRangeHeader*)(MemBase + B.size()) - 1;
407 EndBlock->ThisAllocated = 1;
408 EndBlock->PrevAllocated = 0;
409 EndBlock->BlockSize = sizeof(MemoryRangeHeader);
410
411 // Start out with a vast new block of free memory.
412 FreeRangeHeader *NewBlock = (FreeRangeHeader*)MemBase;
413 NewBlock->ThisAllocated = 0;
414 // Make sure getFreeBlockBefore doesn't look into unmapped memory.
415 NewBlock->PrevAllocated = 1;
416 NewBlock->BlockSize = (uintptr_t)EndBlock - (uintptr_t)NewBlock;
417 NewBlock->SetEndOfBlockSizeMarker();
418 NewBlock->AddToFreeList(FreeMemoryList);
419
420 assert(NewBlock->BlockSize - sizeof(MemoryRangeHeader) >= MinSize &&
421 "The block was too small!");
422 return NewBlock;
423 }
424
425 /// endFunctionBody - The function F is now allocated, and takes the memory
426 /// in the range [FunctionStart,FunctionEnd).
427 void endFunctionBody(const Function *F, uint8_t *FunctionStart,
428 uint8_t *FunctionEnd) override {
429 assert(FunctionEnd > FunctionStart);
430 assert(FunctionStart == (uint8_t *)(CurBlock+1) &&
431 "Mismatched function start/end!");
432
433 uintptr_t BlockSize = FunctionEnd - (uint8_t *)CurBlock;
434
435 // Release the memory at the end of this block that isn't needed.
436 FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize);
437 }
438
439 /// allocateSpace - Allocate a memory block of the given size. This method
440 /// cannot be called between calls to startFunctionBody and endFunctionBody.
441 uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) override {
442 CurBlock = FreeMemoryList;
443 FreeMemoryList = FreeMemoryList->AllocateBlock();
444
445 uint8_t *result = (uint8_t *)(CurBlock + 1);
446
447 if (Alignment == 0) Alignment = 1;
448 result = (uint8_t*)(((intptr_t)result+Alignment-1) &
449 ~(intptr_t)(Alignment-1));
450
451 uintptr_t BlockSize = result + Size - (uint8_t *)CurBlock;
452 FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize);
453
454 return result;
455 }
456
457 /// allocateStub - Allocate memory for a function stub.
458 uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
459 unsigned Alignment) override {
460 return (uint8_t*)StubAllocator.Allocate(StubSize, Alignment);
461 }
462
463 /// allocateGlobal - Allocate memory for a global.
464 uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) override {
465 return (uint8_t*)DataAllocator.Allocate(Size, Alignment);
466 }
467
468 /// allocateCodeSection - Allocate memory for a code section.
469 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
470 unsigned SectionID,
471 StringRef SectionName) override {
472 // Grow the required block size to account for the block header
473 Size += sizeof(*CurBlock);
474
475 // Alignment handling.
476 if (!Alignment)
477 Alignment = 16;
478 Size += Alignment - 1;
479
480 FreeRangeHeader* candidateBlock = FreeMemoryList;
481 FreeRangeHeader* head = FreeMemoryList;
482 FreeRangeHeader* iter = head->Next;
483
484 uintptr_t largest = candidateBlock->BlockSize;
485
486 // Search for the largest free block.
487 while (iter != head) {
488 if (iter->BlockSize > largest) {
489 largest = iter->BlockSize;
490 candidateBlock = iter;
491 }
492 iter = iter->Next;
493 }
494
495 largest = largest - sizeof(MemoryRangeHeader);
496
497 // If this block isn't big enough for the allocation desired, allocate
498 // another block of memory and add it to the free list.
499 if (largest < Size || largest <= FreeRangeHeader::getMinBlockSize()) {
500 DEBUG(dbgs() << "JIT: Allocating another slab of memory for function.");
501 candidateBlock = allocateNewCodeSlab((size_t)Size);
502 }
503
504 // Select this candidate block for allocation
505 CurBlock = candidateBlock;
506
507 // Allocate the entire memory block.
508 FreeMemoryList = candidateBlock->AllocateBlock();
509 // Release the memory at the end of this block that isn't needed.
510 FreeMemoryList = CurBlock->TrimAllocationToSize(FreeMemoryList, Size);
511 uintptr_t unalignedAddr = (uintptr_t)CurBlock + sizeof(*CurBlock);
512 return (uint8_t*)RoundUpToAlignment((uint64_t)unalignedAddr, Alignment);
513 }
514
515 /// allocateDataSection - Allocate memory for a data section.
516 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
517 unsigned SectionID, StringRef SectionName,
518 bool IsReadOnly) override {
519 return (uint8_t*)DataAllocator.Allocate(Size, Alignment);
520 }
521
522 bool finalizeMemory(std::string *ErrMsg) override {
523 return false;
524 }
525
526 uint8_t *getGOTBase() const override {
527 return GOTBase.get();
528 }
529
530 void deallocateBlock(void *Block) {
531 // Find the block that is allocated for this function.
532 MemoryRangeHeader *MemRange = static_cast(Block) - 1;
533 assert(MemRange->ThisAllocated && "Block isn't allocated!");
534
535 // Fill the buffer with garbage!
536 if (PoisonMemory) {
537 memset(MemRange+1, 0xCD, MemRange->BlockSize-sizeof(*MemRange));
538 }
539
540 // Free the memory.
541 FreeMemoryList = MemRange->FreeBlock(FreeMemoryList);
542 }
543
544 /// deallocateFunctionBody - Deallocate all memory for the specified
545 /// function body.
546 void deallocateFunctionBody(void *Body) override {
547 if (Body) deallocateBlock(Body);
548 }
549
550 /// setMemoryWritable - When code generation is in progress,
551 /// the code pages may need permissions changed.
552 void setMemoryWritable() override {
553 for (unsigned i = 0, e = CodeSlabs.size(); i != e; ++i)
554 sys::Memory::setWritable(CodeSlabs[i]);
555 }
556 /// setMemoryExecutable - When code generation is done and we're ready to
557 /// start execution, the code pages may need permissions changed.
558 void setMemoryExecutable() override {
559 for (unsigned i = 0, e = CodeSlabs.size(); i != e; ++i)
560 sys::Memory::setExecutable(CodeSlabs[i]);
561 }
562
563 /// setPoisonMemory - Controls whether we write garbage over freed memory.
564 ///
565 void setPoisonMemory(bool poison) override {
566 PoisonMemory = poison;
567 }
568 };
569 }
570
571 void *JITAllocator::Allocate(size_t Size, size_t /*Alignment*/) {
572 sys::MemoryBlock B = JMM.allocateNewSlab(Size);
573 return B.base();
574 }
575
576 void JITAllocator::Deallocate(void *Slab, size_t Size) {
577 sys::MemoryBlock B(Slab, Size);
578 sys::Memory::ReleaseRWX(B);
579 }
580
581 DefaultJITMemoryManager::DefaultJITMemoryManager()
582 :
583 #ifdef NDEBUG
584 PoisonMemory(false),
585 #else
586 PoisonMemory(true),
587 #endif
588 LastSlab(nullptr, 0), StubAllocator(*this), DataAllocator(*this) {
589
590 // Allocate space for code.
591 sys::MemoryBlock MemBlock = allocateNewSlab(DefaultCodeSlabSize);
592 CodeSlabs.push_back(MemBlock);
593 uint8_t *MemBase = (uint8_t*)MemBlock.base();
594
595 // We set up the memory chunk with 4 mem regions, like this:
596 // [ START
597 // [ Free #0 ] -> Large space to allocate functions from.
598 // [ Allocated #1 ] -> Tiny space to separate regions.
599 // [ Free #2 ] -> Tiny space so there is always at least 1 free block.
600 // [ Allocated #3 ] -> Tiny space to prevent looking past end of block.
601 // END ]
602 //
603 // The last three blocks are never deallocated or touched.
604
605 // Add MemoryRangeHeader to the end of the memory region, indicating that
606 // the space after the block of memory is allocated. This is block #3.
607 MemoryRangeHeader *Mem3 = (MemoryRangeHeader*)(MemBase+MemBlock.size())-1;
608 Mem3->ThisAllocated = 1;
609 Mem3->PrevAllocated = 0;
610 Mem3->BlockSize = sizeof(MemoryRangeHeader);
611
612 /// Add a tiny free region so that the free list always has one entry.
613 FreeRangeHeader *Mem2 =
614 (FreeRangeHeader *)(((char*)Mem3)-FreeRangeHeader::getMinBlockSize());
615 Mem2->ThisAllocated = 0;
616 Mem2->PrevAllocated = 1;
617 Mem2->BlockSize = FreeRangeHeader::getMinBlockSize();
618 Mem2->SetEndOfBlockSizeMarker();
619 Mem2->Prev = Mem2; // Mem2 *is* the free list for now.
620 Mem2->Next = Mem2;
621
622 /// Add a tiny allocated region so that Mem2 is never coalesced away.
623 MemoryRangeHeader *Mem1 = (MemoryRangeHeader*)Mem2-1;
624 Mem1->ThisAllocated = 1;
625 Mem1->PrevAllocated = 0;
626 Mem1->BlockSize = sizeof(MemoryRangeHeader);
627
628 // Add a FreeRangeHeader to the start of the function body region, indicating
629 // that the space is free. Mark the previous block allocated so we never look
630 // at it.
631 FreeRangeHeader *Mem0 = (FreeRangeHeader*)MemBase;
632 Mem0->ThisAllocated = 0;
633 Mem0->PrevAllocated = 1;
634 Mem0->BlockSize = (char*)Mem1-(char*)Mem0;
635 Mem0->SetEndOfBlockSizeMarker();
636 Mem0->AddToFreeList(Mem2);
637
638 // Start out with the freelist pointing to Mem0.
639 FreeMemoryList = Mem0;
640 }
641
642 void DefaultJITMemoryManager::AllocateGOT() {
643 assert(!GOTBase && "Cannot allocate the got multiple times");
644 GOTBase = make_unique(sizeof(void*) * 8192);
645 HasGOT = true;
646 }
647
648 DefaultJITMemoryManager::~DefaultJITMemoryManager() {
649 for (unsigned i = 0, e = CodeSlabs.size(); i != e; ++i)
650 sys::Memory::ReleaseRWX(CodeSlabs[i]);
651 }
652
653 sys::MemoryBlock DefaultJITMemoryManager::allocateNewSlab(size_t size) {
654 // Allocate a new block close to the last one.
655 std::string ErrMsg;
656 sys::MemoryBlock *LastSlabPtr = LastSlab.base() ? &LastSlab : nullptr;
657 sys::MemoryBlock B = sys::Memory::AllocateRWX(size, LastSlabPtr, &ErrMsg);
658 if (!B.base()) {
659 report_fatal_error("Allocation failed when allocating new memory in the"
660 " JIT\n" + Twine(ErrMsg));
661 }
662 LastSlab = B;
663 ++NumSlabs;
664 // Initialize the slab to garbage when debugging.
665 if (PoisonMemory) {
666 memset(B.base(), 0xCD, B.size());
667 }
668 return B;
669 }
670
671 /// CheckInvariants - For testing only. Return "" if all internal invariants
672 /// are preserved, and a helpful error message otherwise. For free and
673 /// allocated blocks, make sure that adding BlockSize gives a valid block.
674 /// For free blocks, make sure they're in the free list and that their end of
675 /// block size marker is correct. This function should return an error before
676 /// accessing bad memory. This function is defined here instead of in
677 /// JITMemoryManagerTest.cpp so that we don't have to expose all of the
678 /// implementation details of DefaultJITMemoryManager.
679 bool DefaultJITMemoryManager::CheckInvariants(std::string &ErrorStr) {
680 raw_string_ostream Err(ErrorStr);
681
682 // Construct the set of FreeRangeHeader pointers so we can query it
683 // efficiently.
684 llvm::SmallPtrSet FreeHdrSet;
685 FreeRangeHeader* FreeHead = FreeMemoryList;
686 FreeRangeHeader* FreeRange = FreeHead;
687
688 do {
689 // Check that the free range pointer is in the blocks we've allocated.
690 bool Found = false;
691 for (std::vector::iterator I = CodeSlabs.begin(),
692 E = CodeSlabs.end(); I != E && !Found; ++I) {
693 char *Start = (char*)I->base();
694 char *End = Start + I->size();
695 Found = (Start <= (char*)FreeRange && (char*)FreeRange < End);
696 }
697 if (!Found) {
698 Err << "Corrupt free list; points to " << FreeRange;
699 return false;
700 }
701
702 if (FreeRange->Next->Prev != FreeRange) {
703 Err << "Next and Prev pointers do not match.";
704 return false;
705 }
706
707 // Otherwise, add it to the set.
708 FreeHdrSet.insert(FreeRange);
709 FreeRange = FreeRange->Next;
710 } while (FreeRange != FreeHead);
711
712 // Go over each block, and look at each MemoryRangeHeader.
713 for (std::vector::iterator I = CodeSlabs.begin(),
714 E = CodeSlabs.end(); I != E; ++I) {
715 char *Start = (char*)I->base();
716 char *End = Start + I->size();
717
718 // Check each memory range.
719 for (MemoryRangeHeader *Hdr = (MemoryRangeHeader*)Start, *LastHdr = nullptr;
720 Start <= (char*)Hdr && (char*)Hdr < End;
721 Hdr = &Hdr->getBlockAfter()) {
722 if (Hdr->ThisAllocated == 0) {
723 // Check that this range is in the free list.
724 if (!FreeHdrSet.count(Hdr)) {
725 Err << "Found free header at " << Hdr << " that is not in free list.";
726 return false;
727 }
728
729 // Now make sure the size marker at the end of the block is correct.
730 uintptr_t *Marker = ((uintptr_t*)&Hdr->getBlockAfter()) - 1;
731 if (!(Start <= (char*)Marker && (char*)Marker < End)) {
732 Err << "Block size in header points out of current MemoryBlock.";
733 return false;
734 }
735 if (Hdr->BlockSize != *Marker) {
736 Err << "End of block size marker (" << *Marker << ") "
737 << "and BlockSize (" << Hdr->BlockSize << ") don't match.";
738 return false;
739 }
740 }
741
742 if (LastHdr && LastHdr->ThisAllocated != Hdr->PrevAllocated) {
743 Err << "Hdr->PrevAllocated (" << Hdr->PrevAllocated << ") != "
744 << "LastHdr->ThisAllocated (" << LastHdr->ThisAllocated << ")";
745 return false;
746 } else if (!LastHdr && !Hdr->PrevAllocated) {
747 Err << "The first header should have PrevAllocated true.";
748 return false;
749 }
750
751 // Remember the last header.
752 LastHdr = Hdr;
753 }
754 }
755
756 // All invariants are preserved.
757 return true;
758 }
759
760 //===----------------------------------------------------------------------===//
761 // getPointerToNamedFunction() implementation.
762 //===----------------------------------------------------------------------===//
763
764 // AtExitHandlers - List of functions to call when the program exits,
765 // registered with the atexit() library function.
766 static std::vector AtExitHandlers;
767
768 /// runAtExitHandlers - Run any functions registered by the program's
769 /// calls to atexit(3), which we intercept and store in
770 /// AtExitHandlers.
771 ///
772 static void runAtExitHandlers() {
773 while (!AtExitHandlers.empty()) {
774 void (*Fn)() = AtExitHandlers.back();
775 AtExitHandlers.pop_back();
776 Fn();
777 }
778 }
779
780 //===----------------------------------------------------------------------===//
781 // Function stubs that are invoked instead of certain library calls
782 //
783 // Force the following functions to be linked in to anything that uses the
784 // JIT. This is a hack designed to work around the all-too-clever Glibc
785 // strategy of making these functions work differently when inlined vs. when
786 // not inlined, and hiding their real definitions in a separate archive file
787 // that the dynamic linker can't see. For more info, search for
788 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
789 #if defined(__linux__) && defined(__GLIBC__)
790 /* stat functions are redirecting to __xstat with a version number. On x86-64
791 * linking with libc_nonshared.a and -Wl,--export-dynamic doesn't make 'stat'
792 * available as an exported symbol, so we have to add it explicitly.
793 */
794 namespace {
795 class StatSymbols {
796 public:
797 StatSymbols() {
798 sys::DynamicLibrary::AddSymbol("stat", (void*)(intptr_t)stat);
799 sys::DynamicLibrary::AddSymbol("fstat", (void*)(intptr_t)fstat);
800 sys::DynamicLibrary::AddSymbol("lstat", (void*)(intptr_t)lstat);
801 sys::DynamicLibrary::AddSymbol("stat64", (void*)(intptr_t)stat64);
802 sys::DynamicLibrary::AddSymbol("\x1stat64", (void*)(intptr_t)stat64);
803 sys::DynamicLibrary::AddSymbol("\x1open64", (void*)(intptr_t)open64);
804 sys::DynamicLibrary::AddSymbol("\x1lseek64", (void*)(intptr_t)lseek64);
805 sys::DynamicLibrary::AddSymbol("fstat64", (void*)(intptr_t)fstat64);
806 sys::DynamicLibrary::AddSymbol("lstat64", (void*)(intptr_t)lstat64);
807 sys::DynamicLibrary::AddSymbol("atexit", (void*)(intptr_t)atexit);
808 sys::DynamicLibrary::AddSymbol("mknod", (void*)(intptr_t)mknod);
809 }
810 };
811 }
812 static StatSymbols initStatSymbols;
813 #endif // __linux__
814
815 // jit_exit - Used to intercept the "exit" library call.
816 static void jit_exit(int Status) {
817 runAtExitHandlers(); // Run atexit handlers...
818 exit(Status);
819 }
820
821 // jit_atexit - Used to intercept the "atexit" library call.
822 static int jit_atexit(void (*Fn)()) {
823 AtExitHandlers.push_back(Fn); // Take note of atexit handler...
824 return 0; // Always successful
825 }
826
827 static int jit_noop() {
828 return 0;
829 }
830
831 //===----------------------------------------------------------------------===//
832 //
833 /// getPointerToNamedFunction - This method returns the address of the specified
834 /// function by using the dynamic loader interface. As such it is only useful
835 /// for resolving library symbols, not code generated symbols.
836 ///
837 void *DefaultJITMemoryManager::getPointerToNamedFunction(const std::string &Name,
838 bool AbortOnFailure) {
839 // Check to see if this is one of the functions we want to intercept. Note,
840 // we cast to intptr_t here to silence a -pedantic warning that complains
841 // about casting a function pointer to a normal pointer.
842 if (Name == "exit") return (void*)(intptr_t)&jit_exit;
843 if (Name == "atexit") return (void*)(intptr_t)&jit_atexit;
844
845 // We should not invoke parent's ctors/dtors from generated main()!
846 // On Mingw and Cygwin, the symbol __main is resolved to
847 // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
848 // (and register wrong callee's dtors with atexit(3)).
849 // We expect ExecutionEngine::runStaticConstructorsDestructors()
850 // is called before ExecutionEngine::runFunctionAsMain() is called.
851 if (Name == "__main") return (void*)(intptr_t)&jit_noop;
852
853 const char *NameStr = Name.c_str();
854 // If this is an asm specifier, skip the sentinal.
855 if (NameStr[0] == 1) ++NameStr;
856
857 // If it's an external function, look it up in the process image...
858 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
859 if (Ptr) return Ptr;
860
861 // If it wasn't found and if it starts with an underscore ('_') character,
862 // try again without the underscore.
863 if (NameStr[0] == '_') {
864 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
865 if (Ptr) return Ptr;
866 }
867
868 // Darwin/PPC adds $LDBLStub suffixes to various symbols like printf. These
869 // are references to hidden visibility symbols that dlsym cannot resolve.
870 // If we have one of these, strip off $LDBLStub and try again.
871 #if defined(__APPLE__) && defined(__ppc__)
872 if (Name.size() > 9 && Name[Name.size()-9] == '$' &&
873 memcmp(&Name[Name.size()-8], "LDBLStub", 8) == 0) {
874 // First try turning $LDBLStub into $LDBL128. If that fails, strip it off.
875 // This mirrors logic in libSystemStubs.a.
876 std::string Prefix = std::string(Name.begin(), Name.end()-9);
877 if (void *Ptr = getPointerToNamedFunction(Prefix+"$LDBL128", false))
878 return Ptr;
879 if (void *Ptr = getPointerToNamedFunction(Prefix, false))
880 return Ptr;
881 }
882 #endif
883
884 if (AbortOnFailure) {
885 report_fatal_error("Program used external function '"+Name+
886 "' which could not be resolved!");
887 }
888 return nullptr;
889 }
890
891
892
893 JITMemoryManager *JITMemoryManager::CreateDefaultMemManager() {
894 return new DefaultJITMemoryManager();
895 }
896
897 const size_t DefaultJITMemoryManager::DefaultCodeSlabSize;
898 const size_t DefaultJITMemoryManager::DefaultSlabSize;
899 const size_t DefaultJITMemoryManager::DefaultSizeThreshold;
171171
172172 return false;
173173 }
174
175 void RemoteMemoryManager::setMemoryWritable() { llvm_unreachable("Unexpected!"); }
176 void RemoteMemoryManager::setMemoryExecutable() { llvm_unreachable("Unexpected!"); }
177 void RemoteMemoryManager::setPoisonMemory(bool poison) { llvm_unreachable("Unexpected!"); }
178 void RemoteMemoryManager::AllocateGOT() { llvm_unreachable("Unexpected!"); }
179 uint8_t *RemoteMemoryManager::getGOTBase() const {
180 llvm_unreachable("Unexpected!");
181 return nullptr;
182 }
183 uint8_t *RemoteMemoryManager::startFunctionBody(const Function *F, uintptr_t &ActualSize){
184 llvm_unreachable("Unexpected!");
185 return nullptr;
186 }
187 uint8_t *RemoteMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
188 unsigned Alignment) {
189 llvm_unreachable("Unexpected!");
190 return nullptr;
191 }
192 void RemoteMemoryManager::endFunctionBody(const Function *F, uint8_t *FunctionStart,
193 uint8_t *FunctionEnd) {
194 llvm_unreachable("Unexpected!");
195 }
196 uint8_t *RemoteMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment) {
197 llvm_unreachable("Unexpected!");
198 return nullptr;
199 }
200 uint8_t *RemoteMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment) {
201 llvm_unreachable("Unexpected!");
202 return nullptr;
203 }
204 void RemoteMemoryManager::deallocateFunctionBody(void *Body) {
205 llvm_unreachable("Unexpected!");
206 }
1717 #include "RemoteTarget.h"
1818 #include "llvm/ADT/DenseMap.h"
1919 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ExecutionEngine/JITMemoryManager.h"
20 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
2121 #include "llvm/Support/ErrorHandling.h"
2222 #include "llvm/Support/Memory.h"
2323 #include
2424
2525 namespace llvm {
2626
27 class RemoteMemoryManager : public JITMemoryManager {
27 class RemoteMemoryManager : public RTDyldMemoryManager {
2828 public:
2929 // Notice that this structure takes ownership of the memory allocated.
3030 struct Allocation {
9292
9393 // This is a non-interface function used by lli
9494 void setRemoteTarget(RemoteTarget *T) { Target = T; }
95
96 // The following obsolete JITMemoryManager calls are stubbed out for
97 // this model.
98 void setMemoryWritable() override;
99 void setMemoryExecutable() override;
100 void setPoisonMemory(bool poison) override;
101 void AllocateGOT() override;
102 uint8_t *getGOTBase() const override;
103 uint8_t *startFunctionBody(const Function *F, uintptr_t &ActualSize) override;
104 uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
105 unsigned Alignment) override;
106 void endFunctionBody(const Function *F, uint8_t *FunctionStart,
107 uint8_t *FunctionEnd) override;
108 uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) override;
109 uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) override;
110 void deallocateFunctionBody(void *Body) override;
11195 };
11296
11397 } // end namespace llvm
443443 else
444444 RTDyldMM = new SectionMemoryManager();
445445 builder.setMCJITMemoryManager(RTDyldMM);
446 } else {
447 if (RemoteMCJIT) {
448 errs() << "error: Remote process execution requires -use-mcjit\n";
449 exit(1);
450 }
451 builder.setJITMemoryManager(ForceInterpreter ? nullptr :
452 JITMemoryManager::CreateDefaultMemManager());
446 } else if (RemoteMCJIT) {
447 errs() << "error: Remote process execution does not work with the "
448 "interpreter.\n";
449 exit(1);
453450 }
454451
455452 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
1111
1212 include $(LEVEL)/Makefile.config
1313
14 LINK_COMPONENTS := mcjit jit interpreter nativecodegen bitreader asmparser irreader selectiondag Object
14 LINK_COMPONENTS := mcjit interpreter nativecodegen bitreader asmparser irreader selectiondag Object
1515
1616 # If Intel JIT Events support is configured, link against the LLVM Intel JIT
1717 # Events interface library. If not, this tool will do nothing useful, but it
1616 #include "../../lib/ExecutionEngine/IntelJITEvents/IntelJITEventsWrapper.h"
1717 #include "llvm/ADT/Triple.h"
1818 #include "llvm/ExecutionEngine/JITEventListener.h"
19 #include "llvm/ExecutionEngine/JITMemoryManager.h"
19 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2020 #include "llvm/ExecutionEngine/MCJIT.h"
2121 #include "llvm/ExecutionEngine/ObjectImage.h"
2222 #include "llvm/IR/Module.h"
112112
113113 // Parse the bitcode...
114114 SMDiagnostic Err;
115 std::unique_ptr TheModule(ParseIRFile(IRFile, Err, Context));
115 std::unique_ptr TheModule(parseIRFile(IRFile, Err, Context));
116116 if (!TheModule) {
117117 errs() << Err.getMessage();
118118 return;
122122 // supports poison memory. At some point, we'll need to update this to
123123 // use an MCJIT-specific memory manager. It might be nice to have the
124124 // poison memory option there too.
125 JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
125 RTDyldMemoryManager *MemMgr = new SectionMemoryManager();
126126 if (!MemMgr) {
127127 errs() << "Unable to create memory manager.";
128128 return;
129129 }
130
131 // Tell the memory manager to poison freed memory so that accessing freed
132 // memory is more easily tested.
133 MemMgr->setPoisonMemory(true);
134130
135131 // Override the triple to generate ELF on Windows since that's supported
136132 Triple Tuple(TheModule->getTargetTriple());
147143 TheJIT.reset(EngineBuilder(std::move(TheModule))
148144 .setEngineKind(EngineKind::JIT)
149145 .setErrorStr(&Error)
150 .setJITMemoryManager(MemMgr)
146 .setMCJITMemoryManager(MemMgr)
151147 .create());
152148 if (Error.empty() == false)
153149 errs() << Error;