Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 1 | //===-- JITMemoryManager.cpp - Memory Allocator for JIT'd code ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the DefaultJITMemoryManager class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "jit" |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame] | 15 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallPtrSet.h" |
| 17 | #include "llvm/ADT/Statistic.h" |
Benjamin Kramer | 1bd7335 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Config/config.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/GlobalValue.h" |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Allocator.h" |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 24 | #include "llvm/Support/DynamicLibrary.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Memory.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
Chuck Rose III | 3012ac6 | 2007-12-06 02:03:01 +0000 | [diff] [blame] | 28 | #include <cassert> |
Dan Gohman | de551f9 | 2009-04-01 18:45:54 +0000 | [diff] [blame] | 29 | #include <climits> |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 30 | #include <cstring> |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include <vector> |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 32 | |
| 33 | #if defined(__linux__) |
| 34 | #if defined(HAVE_SYS_STAT_H) |
| 35 | #include <sys/stat.h> |
| 36 | #endif |
| 37 | #include <fcntl.h> |
| 38 | #include <unistd.h> |
| 39 | #endif |
| 40 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 43 | STATISTIC(NumSlabs, "Number of slabs of memory allocated by the JIT"); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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; |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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; |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 67 | /// BlockSize - This is the size in bytes of this memory block, |
| 68 | /// including this header. |
Dan Gohman | de551f9 | 2009-04-01 18:45:54 +0000 | [diff] [blame] | 69 | uintptr_t BlockSize : (sizeof(intptr_t)*CHAR_BIT - 2); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 71 | |
| 72 | /// getBlockAfter - Return the memory block immediately after this one. |
| 73 | /// |
| 74 | MemoryRangeHeader &getBlockAfter() const { |
David Greene | fe1215e | 2013-01-14 21:04:44 +0000 | [diff] [blame] | 75 | return *reinterpret_cast<MemoryRangeHeader *>( |
| 76 | reinterpret_cast<char*>( |
| 77 | const_cast<MemoryRangeHeader *>(this))+BlockSize); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 78 | } |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 80 | /// getFreeBlockBefore - If the block before this one is free, return it, |
| 81 | /// otherwise return null. |
| 82 | FreeRangeHeader *getFreeBlockBefore() const { |
| 83 | if (PrevAllocated) return 0; |
David Greene | fe1215e | 2013-01-14 21:04:44 +0000 | [diff] [blame] | 84 | intptr_t PrevSize = reinterpret_cast<intptr_t *>( |
| 85 | const_cast<MemoryRangeHeader *>(this))[-1]; |
| 86 | return reinterpret_cast<FreeRangeHeader *>( |
| 87 | reinterpret_cast<char*>( |
| 88 | const_cast<MemoryRangeHeader *>(this))-PrevSize); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 89 | } |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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. |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 98 | FreeRangeHeader *TrimAllocationToSize(FreeRangeHeader *FreeList, |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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; |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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 | } |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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 | } |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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; |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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(); |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 165 | assert(ThisAllocated && "This block is already free!"); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 166 | assert(FollowingBlock->PrevAllocated && "Flags out of sync!"); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 168 | FreeRangeHeader *FreeListToReturn = FreeList; |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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 = 0; |
| 178 | assert(&FollowingFreeBlock != FreeList && "No tombstone block?"); |
| 179 | } |
| 180 | FollowingFreeBlock.RemoveFromFreeList(); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 182 | // Include the following block into this one. |
| 183 | BlockSize += FollowingFreeBlock.BlockSize; |
| 184 | FollowingBlock = &FollowingFreeBlock.getBlockAfter(); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 186 | // Tell the block after the block we are coalescing that this block is |
| 187 | // allocated. |
| 188 | FollowingBlock->PrevAllocated = 1; |
| 189 | } |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 191 | assert(FollowingBlock->ThisAllocated && "Missed coalescing?"); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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 | |
Evan Cheng | 60b75f4 | 2008-07-29 07:38:32 +0000 | [diff] [blame] | 229 | // Don't allow blocks to be trimmed below minimum required size |
| 230 | NewSize = std::max<uint64_t>(FreeRangeHeader::getMinBlockSize(), NewSize); |
| 231 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 232 | // Round up size for alignment of header. |
| 233 | unsigned HeaderAlign = __alignof(FreeRangeHeader); |
| 234 | NewSize = (NewSize+ (HeaderAlign-1)) & ~(HeaderAlign-1); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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!"); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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; |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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(); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 249 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 250 | // Change the size of this block. |
| 251 | BlockSize = NewSize; |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 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 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 268 | namespace { |
| 269 | |
| 270 | class DefaultJITMemoryManager; |
| 271 | |
| 272 | class JITSlabAllocator : public SlabAllocator { |
| 273 | DefaultJITMemoryManager &JMM; |
| 274 | public: |
| 275 | JITSlabAllocator(DefaultJITMemoryManager &jmm) : JMM(jmm) { } |
| 276 | virtual ~JITSlabAllocator() { } |
| 277 | virtual MemSlab *Allocate(size_t Size); |
| 278 | virtual void Deallocate(MemSlab *Slab); |
| 279 | }; |
| 280 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 281 | /// DefaultJITMemoryManager - Manage memory for the JIT code generation. |
| 282 | /// This splits a large block of MAP_NORESERVE'd memory into two |
| 283 | /// sections, one for function stubs, one for the functions themselves. We |
| 284 | /// have to do this because we may need to emit a function stub while in the |
| 285 | /// middle of emitting a function, and we don't know how large the function we |
| 286 | /// are emitting is. |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 287 | class DefaultJITMemoryManager : public JITMemoryManager { |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 288 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 289 | // Whether to poison freed memory. |
| 290 | bool PoisonMemory; |
| 291 | |
| 292 | /// LastSlab - This points to the last slab allocated and is used as the |
| 293 | /// NearBlock parameter to AllocateRWX so that we can attempt to lay out all |
| 294 | /// stubs, data, and code contiguously in memory. In general, however, this |
| 295 | /// is not possible because the NearBlock parameter is ignored on Windows |
| 296 | /// platforms and even on Unix it works on a best-effort pasis. |
| 297 | sys::MemoryBlock LastSlab; |
| 298 | |
| 299 | // Memory slabs allocated by the JIT. We refer to them as slabs so we don't |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 300 | // confuse them with the blocks of memory described above. |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 301 | std::vector<sys::MemoryBlock> CodeSlabs; |
| 302 | JITSlabAllocator BumpSlabAllocator; |
| 303 | BumpPtrAllocator StubAllocator; |
| 304 | BumpPtrAllocator DataAllocator; |
| 305 | |
| 306 | // Circular list of free blocks. |
| 307 | FreeRangeHeader *FreeMemoryList; |
| 308 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 309 | // When emitting code into a memory block, this is the block. |
| 310 | MemoryRangeHeader *CurBlock; |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 311 | |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 312 | uint8_t *GOTBase; // Target Specific reserved memory |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 313 | public: |
| 314 | DefaultJITMemoryManager(); |
| 315 | ~DefaultJITMemoryManager(); |
| 316 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 317 | /// allocateNewSlab - Allocates a new MemoryBlock and remembers it as the |
| 318 | /// last slab it allocated, so that subsequent allocations follow it. |
| 319 | sys::MemoryBlock allocateNewSlab(size_t size); |
| 320 | |
| 321 | /// DefaultCodeSlabSize - When we have to go map more memory, we allocate at |
| 322 | /// least this much unless more is requested. |
| 323 | static const size_t DefaultCodeSlabSize; |
| 324 | |
| 325 | /// DefaultSlabSize - Allocate data into slabs of this size unless we get |
| 326 | /// an allocation above SizeThreshold. |
| 327 | static const size_t DefaultSlabSize; |
| 328 | |
| 329 | /// DefaultSizeThreshold - For any allocation larger than this threshold, we |
| 330 | /// should allocate a separate slab. |
| 331 | static const size_t DefaultSizeThreshold; |
| 332 | |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 333 | /// getPointerToNamedFunction - This method returns the address of the |
| 334 | /// specified function by using the dlsym function call. |
| 335 | virtual void *getPointerToNamedFunction(const std::string &Name, |
| 336 | bool AbortOnFailure = true); |
| 337 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 338 | void AllocateGOT(); |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 339 | |
| 340 | // Testing methods. |
| 341 | virtual bool CheckInvariants(std::string &ErrorStr); |
| 342 | size_t GetDefaultCodeSlabSize() { return DefaultCodeSlabSize; } |
| 343 | size_t GetDefaultDataSlabSize() { return DefaultSlabSize; } |
| 344 | size_t GetDefaultStubSlabSize() { return DefaultSlabSize; } |
| 345 | unsigned GetNumCodeSlabs() { return CodeSlabs.size(); } |
| 346 | unsigned GetNumDataSlabs() { return DataAllocator.GetNumSlabs(); } |
| 347 | unsigned GetNumStubSlabs() { return StubAllocator.GetNumSlabs(); } |
| 348 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 349 | /// startFunctionBody - When a function starts, allocate a block of free |
| 350 | /// executable memory, returning a pointer to it and its actual size. |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 351 | uint8_t *startFunctionBody(const Function *F, uintptr_t &ActualSize) { |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 352 | |
Chris Lattner | 96c96b4 | 2009-03-09 21:34:10 +0000 | [diff] [blame] | 353 | FreeRangeHeader* candidateBlock = FreeMemoryList; |
| 354 | FreeRangeHeader* head = FreeMemoryList; |
| 355 | FreeRangeHeader* iter = head->Next; |
| 356 | |
| 357 | uintptr_t largest = candidateBlock->BlockSize; |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 358 | |
Chris Lattner | 96c96b4 | 2009-03-09 21:34:10 +0000 | [diff] [blame] | 359 | // Search for the largest free block |
| 360 | while (iter != head) { |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 361 | if (iter->BlockSize > largest) { |
| 362 | largest = iter->BlockSize; |
| 363 | candidateBlock = iter; |
| 364 | } |
| 365 | iter = iter->Next; |
Chris Lattner | 96c96b4 | 2009-03-09 21:34:10 +0000 | [diff] [blame] | 366 | } |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 367 | |
Nicolas Geoffray | 1b10d79 | 2009-07-29 22:55:02 +0000 | [diff] [blame] | 368 | largest = largest - sizeof(MemoryRangeHeader); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 369 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 370 | // If this block isn't big enough for the allocation desired, allocate |
| 371 | // another block of memory and add it to the free list. |
Nicolas Geoffray | 1b10d79 | 2009-07-29 22:55:02 +0000 | [diff] [blame] | 372 | if (largest < ActualSize || |
| 373 | largest <= FreeRangeHeader::getMinBlockSize()) { |
David Greene | bd8c8af | 2010-01-05 01:23:38 +0000 | [diff] [blame] | 374 | DEBUG(dbgs() << "JIT: Allocating another slab of memory for function."); |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 375 | candidateBlock = allocateNewCodeSlab((size_t)ActualSize); |
| 376 | } |
| 377 | |
Chris Lattner | 96c96b4 | 2009-03-09 21:34:10 +0000 | [diff] [blame] | 378 | // Select this candidate block for allocation |
| 379 | CurBlock = candidateBlock; |
| 380 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 381 | // Allocate the entire memory block. |
Chris Lattner | 96c96b4 | 2009-03-09 21:34:10 +0000 | [diff] [blame] | 382 | FreeMemoryList = candidateBlock->AllocateBlock(); |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 383 | ActualSize = CurBlock->BlockSize - sizeof(MemoryRangeHeader); |
| 384 | return (uint8_t *)(CurBlock + 1); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 385 | } |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 386 | |
| 387 | /// allocateNewCodeSlab - Helper method to allocate a new slab of code |
| 388 | /// memory from the OS and add it to the free list. Returns the new |
| 389 | /// FreeRangeHeader at the base of the slab. |
| 390 | FreeRangeHeader *allocateNewCodeSlab(size_t MinSize) { |
| 391 | // If the user needs at least MinSize free memory, then we account for |
| 392 | // two MemoryRangeHeaders: the one in the user's block, and the one at the |
| 393 | // end of the slab. |
| 394 | size_t PaddedMin = MinSize + 2 * sizeof(MemoryRangeHeader); |
| 395 | size_t SlabSize = std::max(DefaultCodeSlabSize, PaddedMin); |
| 396 | sys::MemoryBlock B = allocateNewSlab(SlabSize); |
| 397 | CodeSlabs.push_back(B); |
| 398 | char *MemBase = (char*)(B.base()); |
| 399 | |
| 400 | // Put a tiny allocated block at the end of the memory chunk, so when |
| 401 | // FreeBlock calls getBlockAfter it doesn't fall off the end. |
| 402 | MemoryRangeHeader *EndBlock = |
| 403 | (MemoryRangeHeader*)(MemBase + B.size()) - 1; |
| 404 | EndBlock->ThisAllocated = 1; |
| 405 | EndBlock->PrevAllocated = 0; |
| 406 | EndBlock->BlockSize = sizeof(MemoryRangeHeader); |
| 407 | |
| 408 | // Start out with a vast new block of free memory. |
| 409 | FreeRangeHeader *NewBlock = (FreeRangeHeader*)MemBase; |
| 410 | NewBlock->ThisAllocated = 0; |
| 411 | // Make sure getFreeBlockBefore doesn't look into unmapped memory. |
| 412 | NewBlock->PrevAllocated = 1; |
| 413 | NewBlock->BlockSize = (uintptr_t)EndBlock - (uintptr_t)NewBlock; |
| 414 | NewBlock->SetEndOfBlockSizeMarker(); |
| 415 | NewBlock->AddToFreeList(FreeMemoryList); |
| 416 | |
| 417 | assert(NewBlock->BlockSize - sizeof(MemoryRangeHeader) >= MinSize && |
| 418 | "The block was too small!"); |
| 419 | return NewBlock; |
| 420 | } |
| 421 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 422 | /// endFunctionBody - The function F is now allocated, and takes the memory |
| 423 | /// in the range [FunctionStart,FunctionEnd). |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 424 | void endFunctionBody(const Function *F, uint8_t *FunctionStart, |
| 425 | uint8_t *FunctionEnd) { |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 426 | assert(FunctionEnd > FunctionStart); |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 427 | assert(FunctionStart == (uint8_t *)(CurBlock+1) && |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 428 | "Mismatched function start/end!"); |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 429 | |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 430 | uintptr_t BlockSize = FunctionEnd - (uint8_t *)CurBlock; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 431 | |
| 432 | // Release the memory at the end of this block that isn't needed. |
| 433 | FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize); |
| 434 | } |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 435 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 436 | /// allocateSpace - Allocate a memory block of the given size. This method |
| 437 | /// cannot be called between calls to startFunctionBody and endFunctionBody. |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 438 | uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) { |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 439 | CurBlock = FreeMemoryList; |
| 440 | FreeMemoryList = FreeMemoryList->AllocateBlock(); |
| 441 | |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 442 | uint8_t *result = (uint8_t *)(CurBlock + 1); |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 443 | |
| 444 | if (Alignment == 0) Alignment = 1; |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 445 | result = (uint8_t*)(((intptr_t)result+Alignment-1) & |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 446 | ~(intptr_t)(Alignment-1)); |
| 447 | |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 448 | uintptr_t BlockSize = result + Size - (uint8_t *)CurBlock; |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 449 | FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize); |
| 450 | |
| 451 | return result; |
| 452 | } |
| 453 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 454 | /// allocateStub - Allocate memory for a function stub. |
| 455 | uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize, |
| 456 | unsigned Alignment) { |
| 457 | return (uint8_t*)StubAllocator.Allocate(StubSize, Alignment); |
| 458 | } |
| 459 | |
| 460 | /// allocateGlobal - Allocate memory for a global. |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 461 | uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) { |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 462 | return (uint8_t*)DataAllocator.Allocate(Size, Alignment); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 465 | /// allocateCodeSection - Allocate memory for a code section. |
| 466 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
| 467 | unsigned SectionID) { |
Sean Callanan | f92bbcd | 2012-08-15 20:53:52 +0000 | [diff] [blame] | 468 | // Grow the required block size to account for the block header |
| 469 | Size += sizeof(*CurBlock); |
| 470 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 471 | // FIXME: Alignement handling. |
| 472 | FreeRangeHeader* candidateBlock = FreeMemoryList; |
| 473 | FreeRangeHeader* head = FreeMemoryList; |
| 474 | FreeRangeHeader* iter = head->Next; |
| 475 | |
| 476 | uintptr_t largest = candidateBlock->BlockSize; |
| 477 | |
| 478 | // Search for the largest free block. |
| 479 | while (iter != head) { |
| 480 | if (iter->BlockSize > largest) { |
| 481 | largest = iter->BlockSize; |
| 482 | candidateBlock = iter; |
| 483 | } |
| 484 | iter = iter->Next; |
| 485 | } |
| 486 | |
| 487 | largest = largest - sizeof(MemoryRangeHeader); |
| 488 | |
| 489 | // If this block isn't big enough for the allocation desired, allocate |
| 490 | // another block of memory and add it to the free list. |
| 491 | if (largest < Size || largest <= FreeRangeHeader::getMinBlockSize()) { |
| 492 | DEBUG(dbgs() << "JIT: Allocating another slab of memory for function."); |
| 493 | candidateBlock = allocateNewCodeSlab((size_t)Size); |
| 494 | } |
| 495 | |
| 496 | // Select this candidate block for allocation |
| 497 | CurBlock = candidateBlock; |
| 498 | |
| 499 | // Allocate the entire memory block. |
| 500 | FreeMemoryList = candidateBlock->AllocateBlock(); |
| 501 | // Release the memory at the end of this block that isn't needed. |
| 502 | FreeMemoryList = CurBlock->TrimAllocationToSize(FreeMemoryList, Size); |
| 503 | return (uint8_t *)(CurBlock + 1); |
| 504 | } |
| 505 | |
| 506 | /// allocateDataSection - Allocate memory for a data section. |
| 507 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
Andrew Kaylor | 53608a3 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 508 | unsigned SectionID, bool IsReadOnly) { |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 509 | return (uint8_t*)DataAllocator.Allocate(Size, Alignment); |
| 510 | } |
| 511 | |
Andrew Kaylor | 53608a3 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 512 | bool applyPermissions(std::string *ErrMsg) { |
| 513 | return false; |
| 514 | } |
| 515 | |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 516 | uint8_t *getGOTBase() const { |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 517 | return GOTBase; |
| 518 | } |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 519 | |
Jeffrey Yasskin | 1e86132 | 2009-10-20 18:13:21 +0000 | [diff] [blame] | 520 | void deallocateBlock(void *Block) { |
| 521 | // Find the block that is allocated for this function. |
| 522 | MemoryRangeHeader *MemRange = static_cast<MemoryRangeHeader*>(Block) - 1; |
| 523 | assert(MemRange->ThisAllocated && "Block isn't allocated!"); |
| 524 | |
| 525 | // Fill the buffer with garbage! |
| 526 | if (PoisonMemory) { |
| 527 | memset(MemRange+1, 0xCD, MemRange->BlockSize-sizeof(*MemRange)); |
| 528 | } |
| 529 | |
| 530 | // Free the memory. |
| 531 | FreeMemoryList = MemRange->FreeBlock(FreeMemoryList); |
| 532 | } |
| 533 | |
| 534 | /// deallocateFunctionBody - Deallocate all memory for the specified |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 535 | /// function body. |
Jeffrey Yasskin | 1e86132 | 2009-10-20 18:13:21 +0000 | [diff] [blame] | 536 | void deallocateFunctionBody(void *Body) { |
Nicolas Geoffray | 71e0b7c | 2009-10-22 14:35:57 +0000 | [diff] [blame] | 537 | if (Body) deallocateBlock(Body); |
Jeffrey Yasskin | 1e86132 | 2009-10-20 18:13:21 +0000 | [diff] [blame] | 538 | } |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 539 | |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 540 | /// setMemoryWritable - When code generation is in progress, |
| 541 | /// the code pages may need permissions changed. |
Dan Gohman | a9ad041 | 2009-08-12 22:10:57 +0000 | [diff] [blame] | 542 | void setMemoryWritable() |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 543 | { |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 544 | for (unsigned i = 0, e = CodeSlabs.size(); i != e; ++i) |
| 545 | sys::Memory::setWritable(CodeSlabs[i]); |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 546 | } |
| 547 | /// setMemoryExecutable - When code generation is done and we're ready to |
| 548 | /// start execution, the code pages may need permissions changed. |
Dan Gohman | a9ad041 | 2009-08-12 22:10:57 +0000 | [diff] [blame] | 549 | void setMemoryExecutable() |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 550 | { |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 551 | for (unsigned i = 0, e = CodeSlabs.size(); i != e; ++i) |
| 552 | sys::Memory::setExecutable(CodeSlabs[i]); |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 553 | } |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 554 | |
| 555 | /// setPoisonMemory - Controls whether we write garbage over freed memory. |
| 556 | /// |
| 557 | void setPoisonMemory(bool poison) { |
| 558 | PoisonMemory = poison; |
| 559 | } |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 560 | }; |
| 561 | } |
| 562 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 563 | MemSlab *JITSlabAllocator::Allocate(size_t Size) { |
| 564 | sys::MemoryBlock B = JMM.allocateNewSlab(Size); |
| 565 | MemSlab *Slab = (MemSlab*)B.base(); |
| 566 | Slab->Size = B.size(); |
| 567 | Slab->NextPtr = 0; |
| 568 | return Slab; |
| 569 | } |
| 570 | |
| 571 | void JITSlabAllocator::Deallocate(MemSlab *Slab) { |
| 572 | sys::MemoryBlock B(Slab, Slab->Size); |
| 573 | sys::Memory::ReleaseRWX(B); |
| 574 | } |
| 575 | |
| 576 | DefaultJITMemoryManager::DefaultJITMemoryManager() |
Dan Gohman | 5b78d50 | 2009-08-27 01:25:57 +0000 | [diff] [blame] | 577 | : |
| 578 | #ifdef NDEBUG |
| 579 | PoisonMemory(false), |
| 580 | #else |
| 581 | PoisonMemory(true), |
| 582 | #endif |
| 583 | LastSlab(0, 0), |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 584 | BumpSlabAllocator(*this), |
| 585 | StubAllocator(DefaultSlabSize, DefaultSizeThreshold, BumpSlabAllocator), |
| 586 | DataAllocator(DefaultSlabSize, DefaultSizeThreshold, BumpSlabAllocator) { |
| 587 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 588 | // Allocate space for code. |
| 589 | sys::MemoryBlock MemBlock = allocateNewSlab(DefaultCodeSlabSize); |
| 590 | CodeSlabs.push_back(MemBlock); |
| 591 | uint8_t *MemBase = (uint8_t*)MemBlock.base(); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 592 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 593 | // We set up the memory chunk with 4 mem regions, like this: |
| 594 | // [ START |
| 595 | // [ Free #0 ] -> Large space to allocate functions from. |
| 596 | // [ Allocated #1 ] -> Tiny space to separate regions. |
| 597 | // [ Free #2 ] -> Tiny space so there is always at least 1 free block. |
| 598 | // [ Allocated #3 ] -> Tiny space to prevent looking past end of block. |
| 599 | // END ] |
| 600 | // |
| 601 | // The last three blocks are never deallocated or touched. |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 602 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 603 | // Add MemoryRangeHeader to the end of the memory region, indicating that |
| 604 | // the space after the block of memory is allocated. This is block #3. |
| 605 | MemoryRangeHeader *Mem3 = (MemoryRangeHeader*)(MemBase+MemBlock.size())-1; |
| 606 | Mem3->ThisAllocated = 1; |
| 607 | Mem3->PrevAllocated = 0; |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 608 | Mem3->BlockSize = sizeof(MemoryRangeHeader); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 609 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 610 | /// Add a tiny free region so that the free list always has one entry. |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 611 | FreeRangeHeader *Mem2 = |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 612 | (FreeRangeHeader *)(((char*)Mem3)-FreeRangeHeader::getMinBlockSize()); |
| 613 | Mem2->ThisAllocated = 0; |
| 614 | Mem2->PrevAllocated = 1; |
| 615 | Mem2->BlockSize = FreeRangeHeader::getMinBlockSize(); |
| 616 | Mem2->SetEndOfBlockSizeMarker(); |
| 617 | Mem2->Prev = Mem2; // Mem2 *is* the free list for now. |
| 618 | Mem2->Next = Mem2; |
| 619 | |
| 620 | /// Add a tiny allocated region so that Mem2 is never coalesced away. |
| 621 | MemoryRangeHeader *Mem1 = (MemoryRangeHeader*)Mem2-1; |
| 622 | Mem1->ThisAllocated = 1; |
| 623 | Mem1->PrevAllocated = 0; |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 624 | Mem1->BlockSize = sizeof(MemoryRangeHeader); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 625 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 626 | // Add a FreeRangeHeader to the start of the function body region, indicating |
| 627 | // that the space is free. Mark the previous block allocated so we never look |
| 628 | // at it. |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 629 | FreeRangeHeader *Mem0 = (FreeRangeHeader*)MemBase; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 630 | Mem0->ThisAllocated = 0; |
| 631 | Mem0->PrevAllocated = 1; |
| 632 | Mem0->BlockSize = (char*)Mem1-(char*)Mem0; |
| 633 | Mem0->SetEndOfBlockSizeMarker(); |
| 634 | Mem0->AddToFreeList(Mem2); |
Eric Christopher | 5cf0aed | 2009-11-12 01:06:08 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 636 | // Start out with the freelist pointing to Mem0. |
| 637 | FreeMemoryList = Mem0; |
| 638 | |
| 639 | GOTBase = NULL; |
| 640 | } |
| 641 | |
| 642 | void DefaultJITMemoryManager::AllocateGOT() { |
| 643 | assert(GOTBase == 0 && "Cannot allocate the got multiple times"); |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 644 | GOTBase = new uint8_t[sizeof(void*) * 8192]; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 645 | HasGOT = true; |
| 646 | } |
| 647 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 648 | DefaultJITMemoryManager::~DefaultJITMemoryManager() { |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 649 | for (unsigned i = 0, e = CodeSlabs.size(); i != e; ++i) |
| 650 | sys::Memory::ReleaseRWX(CodeSlabs[i]); |
| 651 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 652 | delete[] GOTBase; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 655 | sys::MemoryBlock DefaultJITMemoryManager::allocateNewSlab(size_t size) { |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 656 | // Allocate a new block close to the last one. |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 657 | std::string ErrMsg; |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 658 | sys::MemoryBlock *LastSlabPtr = LastSlab.base() ? &LastSlab : 0; |
| 659 | sys::MemoryBlock B = sys::Memory::AllocateRWX(size, LastSlabPtr, &ErrMsg); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 660 | if (B.base() == 0) { |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 661 | report_fatal_error("Allocation failed when allocating new memory in the" |
Benjamin Kramer | 1bd7335 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 662 | " JIT\n" + Twine(ErrMsg)); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 663 | } |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 664 | LastSlab = B; |
| 665 | ++NumSlabs; |
Reid Kleckner | 01248e6 | 2009-08-21 21:03:57 +0000 | [diff] [blame] | 666 | // Initialize the slab to garbage when debugging. |
| 667 | if (PoisonMemory) { |
| 668 | memset(B.base(), 0xCD, B.size()); |
| 669 | } |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 670 | return B; |
| 671 | } |
| 672 | |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 673 | /// CheckInvariants - For testing only. Return "" if all internal invariants |
| 674 | /// are preserved, and a helpful error message otherwise. For free and |
| 675 | /// allocated blocks, make sure that adding BlockSize gives a valid block. |
| 676 | /// For free blocks, make sure they're in the free list and that their end of |
| 677 | /// block size marker is correct. This function should return an error before |
| 678 | /// accessing bad memory. This function is defined here instead of in |
| 679 | /// JITMemoryManagerTest.cpp so that we don't have to expose all of the |
| 680 | /// implementation details of DefaultJITMemoryManager. |
| 681 | bool DefaultJITMemoryManager::CheckInvariants(std::string &ErrorStr) { |
| 682 | raw_string_ostream Err(ErrorStr); |
| 683 | |
| 684 | // Construct a the set of FreeRangeHeader pointers so we can query it |
| 685 | // efficiently. |
| 686 | llvm::SmallPtrSet<MemoryRangeHeader*, 16> FreeHdrSet; |
| 687 | FreeRangeHeader* FreeHead = FreeMemoryList; |
| 688 | FreeRangeHeader* FreeRange = FreeHead; |
| 689 | |
| 690 | do { |
| 691 | // Check that the free range pointer is in the blocks we've allocated. |
| 692 | bool Found = false; |
| 693 | for (std::vector<sys::MemoryBlock>::iterator I = CodeSlabs.begin(), |
| 694 | E = CodeSlabs.end(); I != E && !Found; ++I) { |
| 695 | char *Start = (char*)I->base(); |
| 696 | char *End = Start + I->size(); |
| 697 | Found = (Start <= (char*)FreeRange && (char*)FreeRange < End); |
| 698 | } |
| 699 | if (!Found) { |
| 700 | Err << "Corrupt free list; points to " << FreeRange; |
| 701 | return false; |
| 702 | } |
| 703 | |
| 704 | if (FreeRange->Next->Prev != FreeRange) { |
| 705 | Err << "Next and Prev pointers do not match."; |
| 706 | return false; |
| 707 | } |
| 708 | |
| 709 | // Otherwise, add it to the set. |
| 710 | FreeHdrSet.insert(FreeRange); |
| 711 | FreeRange = FreeRange->Next; |
| 712 | } while (FreeRange != FreeHead); |
| 713 | |
| 714 | // Go over each block, and look at each MemoryRangeHeader. |
| 715 | for (std::vector<sys::MemoryBlock>::iterator I = CodeSlabs.begin(), |
| 716 | E = CodeSlabs.end(); I != E; ++I) { |
| 717 | char *Start = (char*)I->base(); |
| 718 | char *End = Start + I->size(); |
| 719 | |
| 720 | // Check each memory range. |
| 721 | for (MemoryRangeHeader *Hdr = (MemoryRangeHeader*)Start, *LastHdr = NULL; |
| 722 | Start <= (char*)Hdr && (char*)Hdr < End; |
| 723 | Hdr = &Hdr->getBlockAfter()) { |
| 724 | if (Hdr->ThisAllocated == 0) { |
| 725 | // Check that this range is in the free list. |
| 726 | if (!FreeHdrSet.count(Hdr)) { |
| 727 | Err << "Found free header at " << Hdr << " that is not in free list."; |
| 728 | return false; |
| 729 | } |
| 730 | |
| 731 | // Now make sure the size marker at the end of the block is correct. |
| 732 | uintptr_t *Marker = ((uintptr_t*)&Hdr->getBlockAfter()) - 1; |
| 733 | if (!(Start <= (char*)Marker && (char*)Marker < End)) { |
| 734 | Err << "Block size in header points out of current MemoryBlock."; |
| 735 | return false; |
| 736 | } |
| 737 | if (Hdr->BlockSize != *Marker) { |
| 738 | Err << "End of block size marker (" << *Marker << ") " |
| 739 | << "and BlockSize (" << Hdr->BlockSize << ") don't match."; |
| 740 | return false; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | if (LastHdr && LastHdr->ThisAllocated != Hdr->PrevAllocated) { |
| 745 | Err << "Hdr->PrevAllocated (" << Hdr->PrevAllocated << ") != " |
| 746 | << "LastHdr->ThisAllocated (" << LastHdr->ThisAllocated << ")"; |
| 747 | return false; |
| 748 | } else if (!LastHdr && !Hdr->PrevAllocated) { |
| 749 | Err << "The first header should have PrevAllocated true."; |
| 750 | return false; |
| 751 | } |
| 752 | |
| 753 | // Remember the last header. |
| 754 | LastHdr = Hdr; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | // All invariants are preserved. |
| 759 | return true; |
| 760 | } |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 761 | |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 762 | //===----------------------------------------------------------------------===// |
| 763 | // getPointerToNamedFunction() implementation. |
| 764 | //===----------------------------------------------------------------------===// |
| 765 | |
| 766 | // AtExitHandlers - List of functions to call when the program exits, |
| 767 | // registered with the atexit() library function. |
| 768 | static std::vector<void (*)()> AtExitHandlers; |
| 769 | |
| 770 | /// runAtExitHandlers - Run any functions registered by the program's |
| 771 | /// calls to atexit(3), which we intercept and store in |
| 772 | /// AtExitHandlers. |
| 773 | /// |
| 774 | static void runAtExitHandlers() { |
| 775 | while (!AtExitHandlers.empty()) { |
| 776 | void (*Fn)() = AtExitHandlers.back(); |
| 777 | AtExitHandlers.pop_back(); |
| 778 | Fn(); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | //===----------------------------------------------------------------------===// |
| 783 | // Function stubs that are invoked instead of certain library calls |
| 784 | // |
| 785 | // Force the following functions to be linked in to anything that uses the |
| 786 | // JIT. This is a hack designed to work around the all-too-clever Glibc |
| 787 | // strategy of making these functions work differently when inlined vs. when |
| 788 | // not inlined, and hiding their real definitions in a separate archive file |
| 789 | // that the dynamic linker can't see. For more info, search for |
| 790 | // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274. |
| 791 | #if defined(__linux__) |
| 792 | /* stat functions are redirecting to __xstat with a version number. On x86-64 |
| 793 | * linking with libc_nonshared.a and -Wl,--export-dynamic doesn't make 'stat' |
| 794 | * available as an exported symbol, so we have to add it explicitly. |
| 795 | */ |
| 796 | namespace { |
| 797 | class StatSymbols { |
| 798 | public: |
| 799 | StatSymbols() { |
| 800 | sys::DynamicLibrary::AddSymbol("stat", (void*)(intptr_t)stat); |
| 801 | sys::DynamicLibrary::AddSymbol("fstat", (void*)(intptr_t)fstat); |
| 802 | sys::DynamicLibrary::AddSymbol("lstat", (void*)(intptr_t)lstat); |
| 803 | sys::DynamicLibrary::AddSymbol("stat64", (void*)(intptr_t)stat64); |
| 804 | sys::DynamicLibrary::AddSymbol("\x1stat64", (void*)(intptr_t)stat64); |
| 805 | sys::DynamicLibrary::AddSymbol("\x1open64", (void*)(intptr_t)open64); |
| 806 | sys::DynamicLibrary::AddSymbol("\x1lseek64", (void*)(intptr_t)lseek64); |
| 807 | sys::DynamicLibrary::AddSymbol("fstat64", (void*)(intptr_t)fstat64); |
| 808 | sys::DynamicLibrary::AddSymbol("lstat64", (void*)(intptr_t)lstat64); |
| 809 | sys::DynamicLibrary::AddSymbol("atexit", (void*)(intptr_t)atexit); |
| 810 | sys::DynamicLibrary::AddSymbol("mknod", (void*)(intptr_t)mknod); |
| 811 | } |
| 812 | }; |
| 813 | } |
| 814 | static StatSymbols initStatSymbols; |
| 815 | #endif // __linux__ |
| 816 | |
| 817 | // jit_exit - Used to intercept the "exit" library call. |
| 818 | static void jit_exit(int Status) { |
| 819 | runAtExitHandlers(); // Run atexit handlers... |
| 820 | exit(Status); |
| 821 | } |
| 822 | |
| 823 | // jit_atexit - Used to intercept the "atexit" library call. |
| 824 | static int jit_atexit(void (*Fn)()) { |
| 825 | AtExitHandlers.push_back(Fn); // Take note of atexit handler... |
| 826 | return 0; // Always successful |
| 827 | } |
| 828 | |
| 829 | static int jit_noop() { |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | //===----------------------------------------------------------------------===// |
| 834 | // |
| 835 | /// getPointerToNamedFunction - This method returns the address of the specified |
| 836 | /// function by using the dynamic loader interface. As such it is only useful |
| 837 | /// for resolving library symbols, not code generated symbols. |
| 838 | /// |
| 839 | void *DefaultJITMemoryManager::getPointerToNamedFunction(const std::string &Name, |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 840 | bool AbortOnFailure) { |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 841 | // Check to see if this is one of the functions we want to intercept. Note, |
| 842 | // we cast to intptr_t here to silence a -pedantic warning that complains |
| 843 | // about casting a function pointer to a normal pointer. |
| 844 | if (Name == "exit") return (void*)(intptr_t)&jit_exit; |
| 845 | if (Name == "atexit") return (void*)(intptr_t)&jit_atexit; |
| 846 | |
| 847 | // We should not invoke parent's ctors/dtors from generated main()! |
| 848 | // On Mingw and Cygwin, the symbol __main is resolved to |
| 849 | // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors |
| 850 | // (and register wrong callee's dtors with atexit(3)). |
| 851 | // We expect ExecutionEngine::runStaticConstructorsDestructors() |
| 852 | // is called before ExecutionEngine::runFunctionAsMain() is called. |
| 853 | if (Name == "__main") return (void*)(intptr_t)&jit_noop; |
| 854 | |
| 855 | const char *NameStr = Name.c_str(); |
| 856 | // If this is an asm specifier, skip the sentinal. |
| 857 | if (NameStr[0] == 1) ++NameStr; |
| 858 | |
| 859 | // If it's an external function, look it up in the process image... |
| 860 | void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr); |
| 861 | if (Ptr) return Ptr; |
| 862 | |
| 863 | // If it wasn't found and if it starts with an underscore ('_') character, |
| 864 | // try again without the underscore. |
| 865 | if (NameStr[0] == '_') { |
| 866 | Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1); |
| 867 | if (Ptr) return Ptr; |
| 868 | } |
| 869 | |
| 870 | // Darwin/PPC adds $LDBLStub suffixes to various symbols like printf. These |
| 871 | // are references to hidden visibility symbols that dlsym cannot resolve. |
| 872 | // If we have one of these, strip off $LDBLStub and try again. |
| 873 | #if defined(__APPLE__) && defined(__ppc__) |
| 874 | if (Name.size() > 9 && Name[Name.size()-9] == '$' && |
| 875 | memcmp(&Name[Name.size()-8], "LDBLStub", 8) == 0) { |
| 876 | // First try turning $LDBLStub into $LDBL128. If that fails, strip it off. |
| 877 | // This mirrors logic in libSystemStubs.a. |
| 878 | std::string Prefix = std::string(Name.begin(), Name.end()-9); |
| 879 | if (void *Ptr = getPointerToNamedFunction(Prefix+"$LDBL128", false)) |
| 880 | return Ptr; |
| 881 | if (void *Ptr = getPointerToNamedFunction(Prefix, false)) |
| 882 | return Ptr; |
| 883 | } |
| 884 | #endif |
| 885 | |
| 886 | if (AbortOnFailure) { |
| 887 | report_fatal_error("Program used external function '"+Name+ |
| 888 | "' which could not be resolved!"); |
| 889 | } |
| 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | |
| 894 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 895 | JITMemoryManager *JITMemoryManager::CreateDefaultMemManager() { |
| 896 | return new DefaultJITMemoryManager(); |
| 897 | } |
Reid Kleckner | 10b4fc5 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 898 | |
| 899 | // Allocate memory for code in 512K slabs. |
| 900 | const size_t DefaultJITMemoryManager::DefaultCodeSlabSize = 512 * 1024; |
| 901 | |
| 902 | // Allocate globals and stubs in slabs of 64K. (probably 16 pages) |
| 903 | const size_t DefaultJITMemoryManager::DefaultSlabSize = 64 * 1024; |
| 904 | |
| 905 | // Waste at most 16K at the end of each bump slab. (probably 4 pages) |
| 906 | const size_t DefaultJITMemoryManager::DefaultSizeThreshold = 16 * 1024; |