Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 3 | * All rights reserved. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 4 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 14 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include "linker_allocator.h" |
Dimitry Ivanov | f857211 | 2016-07-13 10:24:06 -0700 | [diff] [blame] | 30 | #include "linker_debug.h" |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 31 | #include "linker.h" |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | #include <sys/mman.h> |
Elliott Hughes | 99d5465 | 2018-08-22 10:36:23 -0700 | [diff] [blame] | 38 | #include <sys/prctl.h> |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 39 | #include <unistd.h> |
| 40 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 41 | #include <async_safe/log.h> |
| 42 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 43 | // |
| 44 | // LinkerMemeoryAllocator is general purpose allocator |
| 45 | // designed to provide the same functionality as the malloc/free/realloc |
| 46 | // libc functions. |
| 47 | // |
| 48 | // On alloc: |
| 49 | // If size is >= 1k allocator proxies malloc call directly to mmap |
| 50 | // If size < 1k allocator uses SmallObjectAllocator for the size |
| 51 | // rounded up to the nearest power of two. |
| 52 | // |
| 53 | // On free: |
| 54 | // |
| 55 | // For a pointer allocated using proxy-to-mmap allocator unmaps |
| 56 | // the memory. |
| 57 | // |
| 58 | // For a pointer allocated using SmallObjectAllocator it adds |
| 59 | // the block to free_blocks_list_. If the number of free pages reaches 2, |
| 60 | // SmallObjectAllocator munmaps one of the pages keeping the other one |
| 61 | // in reserve. |
| 62 | |
| 63 | static const char kSignature[4] = {'L', 'M', 'A', 1}; |
| 64 | |
| 65 | static const size_t kSmallObjectMaxSize = 1 << kSmallObjectMaxSizeLog2; |
| 66 | |
| 67 | // This type is used for large allocations (with size >1k) |
| 68 | static const uint32_t kLargeObject = 111; |
| 69 | |
| 70 | bool operator<(const small_object_page_record& one, const small_object_page_record& two) { |
| 71 | return one.page_addr < two.page_addr; |
| 72 | } |
| 73 | |
| 74 | static inline uint16_t log2(size_t number) { |
| 75 | uint16_t result = 0; |
| 76 | number--; |
| 77 | |
| 78 | while (number != 0) { |
| 79 | result++; |
| 80 | number >>= 1; |
| 81 | } |
| 82 | |
| 83 | return result; |
| 84 | } |
| 85 | |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 86 | LinkerSmallObjectAllocator::LinkerSmallObjectAllocator(uint32_t type, size_t block_size) |
| 87 | : type_(type), block_size_(block_size), free_pages_cnt_(0), free_blocks_list_(nullptr) {} |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 88 | |
| 89 | void* LinkerSmallObjectAllocator::alloc() { |
Dimitry Ivanov | f857211 | 2016-07-13 10:24:06 -0700 | [diff] [blame] | 90 | CHECK(block_size_ != 0); |
| 91 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 92 | if (free_blocks_list_ == nullptr) { |
| 93 | alloc_page(); |
| 94 | } |
| 95 | |
| 96 | small_object_block_record* block_record = free_blocks_list_; |
| 97 | if (block_record->free_blocks_cnt > 1) { |
| 98 | small_object_block_record* next_free = reinterpret_cast<small_object_block_record*>( |
| 99 | reinterpret_cast<uint8_t*>(block_record) + block_size_); |
| 100 | next_free->next = block_record->next; |
| 101 | next_free->free_blocks_cnt = block_record->free_blocks_cnt - 1; |
| 102 | free_blocks_list_ = next_free; |
| 103 | } else { |
| 104 | free_blocks_list_ = block_record->next; |
| 105 | } |
| 106 | |
| 107 | // bookkeeping... |
| 108 | auto page_record = find_page_record(block_record); |
| 109 | |
| 110 | if (page_record->allocated_blocks_cnt == 0) { |
| 111 | free_pages_cnt_--; |
| 112 | } |
| 113 | |
| 114 | page_record->free_blocks_cnt--; |
| 115 | page_record->allocated_blocks_cnt++; |
| 116 | |
| 117 | memset(block_record, 0, block_size_); |
| 118 | |
| 119 | return block_record; |
| 120 | } |
| 121 | |
| 122 | void LinkerSmallObjectAllocator::free_page(linker_vector_t::iterator page_record) { |
| 123 | void* page_start = reinterpret_cast<void*>(page_record->page_addr); |
| 124 | void* page_end = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(page_start) + PAGE_SIZE); |
| 125 | |
| 126 | while (free_blocks_list_ != nullptr && |
| 127 | free_blocks_list_ > page_start && |
| 128 | free_blocks_list_ < page_end) { |
| 129 | free_blocks_list_ = free_blocks_list_->next; |
| 130 | } |
| 131 | |
| 132 | small_object_block_record* current = free_blocks_list_; |
| 133 | |
| 134 | while (current != nullptr) { |
| 135 | while (current->next > page_start && current->next < page_end) { |
| 136 | current->next = current->next->next; |
| 137 | } |
| 138 | |
| 139 | current = current->next; |
| 140 | } |
| 141 | |
| 142 | munmap(page_start, PAGE_SIZE); |
| 143 | page_records_.erase(page_record); |
| 144 | free_pages_cnt_--; |
| 145 | } |
| 146 | |
| 147 | void LinkerSmallObjectAllocator::free(void* ptr) { |
| 148 | auto page_record = find_page_record(ptr); |
| 149 | |
| 150 | ssize_t offset = reinterpret_cast<uintptr_t>(ptr) - sizeof(page_info); |
| 151 | |
| 152 | if (offset % block_size_ != 0) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 153 | async_safe_fatal("invalid pointer: %p (block_size=%zd)", ptr, block_size_); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | memset(ptr, 0, block_size_); |
| 157 | small_object_block_record* block_record = reinterpret_cast<small_object_block_record*>(ptr); |
| 158 | |
| 159 | block_record->next = free_blocks_list_; |
| 160 | block_record->free_blocks_cnt = 1; |
| 161 | |
| 162 | free_blocks_list_ = block_record; |
| 163 | |
| 164 | page_record->free_blocks_cnt++; |
| 165 | page_record->allocated_blocks_cnt--; |
| 166 | |
| 167 | if (page_record->allocated_blocks_cnt == 0) { |
| 168 | if (free_pages_cnt_++ > 1) { |
| 169 | // if we already have a free page - unmap this one. |
| 170 | free_page(page_record); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 175 | linker_vector_t::iterator LinkerSmallObjectAllocator::find_page_record(void* ptr) { |
| 176 | void* addr = reinterpret_cast<void*>(PAGE_START(reinterpret_cast<uintptr_t>(ptr))); |
| 177 | small_object_page_record boundary; |
| 178 | boundary.page_addr = addr; |
| 179 | linker_vector_t::iterator it = std::lower_bound( |
| 180 | page_records_.begin(), page_records_.end(), boundary); |
| 181 | |
| 182 | if (it == page_records_.end() || it->page_addr != addr) { |
| 183 | // not found... |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 184 | async_safe_fatal("page record for %p was not found (block_size=%zd)", ptr, block_size_); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return it; |
| 188 | } |
| 189 | |
| 190 | void LinkerSmallObjectAllocator::create_page_record(void* page_addr, size_t free_blocks_cnt) { |
| 191 | small_object_page_record record; |
| 192 | record.page_addr = page_addr; |
| 193 | record.free_blocks_cnt = free_blocks_cnt; |
| 194 | record.allocated_blocks_cnt = 0; |
| 195 | |
| 196 | linker_vector_t::iterator it = std::lower_bound( |
| 197 | page_records_.begin(), page_records_.end(), record); |
| 198 | page_records_.insert(it, record); |
| 199 | } |
| 200 | |
| 201 | void LinkerSmallObjectAllocator::alloc_page() { |
Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 202 | static_assert(sizeof(page_info) % 16 == 0, "sizeof(page_info) is not multiple of 16"); |
| 203 | void* map_ptr = mmap(nullptr, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 204 | if (map_ptr == MAP_FAILED) { |
Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 205 | async_safe_fatal("mmap failed: %s", strerror(errno)); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Dimitry Ivanov | d9d6a84 | 2016-01-26 17:53:17 -0800 | [diff] [blame] | 208 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, PAGE_SIZE, "linker_alloc_small_objects"); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 209 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 210 | page_info* info = reinterpret_cast<page_info*>(map_ptr); |
| 211 | memcpy(info->signature, kSignature, sizeof(kSignature)); |
| 212 | info->type = type_; |
| 213 | info->allocator_addr = this; |
| 214 | |
| 215 | size_t free_blocks_cnt = (PAGE_SIZE - sizeof(page_info))/block_size_; |
| 216 | |
| 217 | create_page_record(map_ptr, free_blocks_cnt); |
| 218 | |
| 219 | small_object_block_record* first_block = reinterpret_cast<small_object_block_record*>(info + 1); |
| 220 | |
| 221 | first_block->next = free_blocks_list_; |
| 222 | first_block->free_blocks_cnt = free_blocks_cnt; |
| 223 | |
| 224 | free_blocks_list_ = first_block; |
Vic Yang | de69660 | 2018-11-27 13:34:44 -0800 | [diff] [blame^] | 225 | |
| 226 | free_pages_cnt_++; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 230 | void LinkerMemoryAllocator::initialize_allocators() { |
| 231 | if (allocators_ != nullptr) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | LinkerSmallObjectAllocator* allocators = |
| 236 | reinterpret_cast<LinkerSmallObjectAllocator*>(allocators_buf_); |
| 237 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 238 | for (size_t i = 0; i < kSmallObjectAllocatorsCount; ++i) { |
| 239 | uint32_t type = i + kSmallObjectMinSizeLog2; |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 240 | new (allocators + i) LinkerSmallObjectAllocator(type, 1 << type); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 241 | } |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 242 | |
| 243 | allocators_ = allocators; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void* LinkerMemoryAllocator::alloc_mmap(size_t size) { |
| 247 | size_t allocated_size = PAGE_END(size + sizeof(page_info)); |
Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 248 | void* map_ptr = mmap(nullptr, allocated_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, |
| 249 | -1, 0); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 250 | |
| 251 | if (map_ptr == MAP_FAILED) { |
Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 252 | async_safe_fatal("mmap failed: %s", strerror(errno)); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, allocated_size, "linker_alloc_lob"); |
| 256 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 257 | page_info* info = reinterpret_cast<page_info*>(map_ptr); |
| 258 | memcpy(info->signature, kSignature, sizeof(kSignature)); |
| 259 | info->type = kLargeObject; |
| 260 | info->allocated_size = allocated_size; |
| 261 | |
| 262 | return info + 1; |
| 263 | } |
| 264 | |
| 265 | void* LinkerMemoryAllocator::alloc(size_t size) { |
| 266 | // treat alloc(0) as alloc(1) |
| 267 | if (size == 0) { |
| 268 | size = 1; |
| 269 | } |
| 270 | |
| 271 | if (size > kSmallObjectMaxSize) { |
| 272 | return alloc_mmap(size); |
| 273 | } |
| 274 | |
| 275 | uint16_t log2_size = log2(size); |
| 276 | |
| 277 | if (log2_size < kSmallObjectMinSizeLog2) { |
| 278 | log2_size = kSmallObjectMinSizeLog2; |
| 279 | } |
| 280 | |
| 281 | return get_small_object_allocator(log2_size)->alloc(); |
| 282 | } |
| 283 | |
| 284 | page_info* LinkerMemoryAllocator::get_page_info(void* ptr) { |
| 285 | page_info* info = reinterpret_cast<page_info*>(PAGE_START(reinterpret_cast<size_t>(ptr))); |
| 286 | if (memcmp(info->signature, kSignature, sizeof(kSignature)) != 0) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 287 | async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | return info; |
| 291 | } |
| 292 | |
| 293 | void* LinkerMemoryAllocator::realloc(void* ptr, size_t size) { |
| 294 | if (ptr == nullptr) { |
| 295 | return alloc(size); |
| 296 | } |
| 297 | |
| 298 | if (size == 0) { |
| 299 | free(ptr); |
| 300 | return nullptr; |
| 301 | } |
| 302 | |
| 303 | page_info* info = get_page_info(ptr); |
| 304 | |
| 305 | size_t old_size = 0; |
| 306 | |
| 307 | if (info->type == kLargeObject) { |
| 308 | old_size = info->allocated_size - sizeof(page_info); |
| 309 | } else { |
| 310 | LinkerSmallObjectAllocator* allocator = get_small_object_allocator(info->type); |
| 311 | if (allocator != info->allocator_addr) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 312 | async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | old_size = allocator->get_block_size(); |
| 316 | } |
| 317 | |
| 318 | if (old_size < size) { |
| 319 | void *result = alloc(size); |
| 320 | memcpy(result, ptr, old_size); |
| 321 | free(ptr); |
| 322 | return result; |
| 323 | } |
| 324 | |
| 325 | return ptr; |
| 326 | } |
| 327 | |
| 328 | void LinkerMemoryAllocator::free(void* ptr) { |
| 329 | if (ptr == nullptr) { |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | page_info* info = get_page_info(ptr); |
| 334 | |
| 335 | if (info->type == kLargeObject) { |
| 336 | munmap(info, info->allocated_size); |
| 337 | } else { |
| 338 | LinkerSmallObjectAllocator* allocator = get_small_object_allocator(info->type); |
| 339 | if (allocator != info->allocator_addr) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 340 | async_safe_fatal("invalid pointer %p (invalid allocator address for the page)", ptr); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | allocator->free(ptr); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | LinkerSmallObjectAllocator* LinkerMemoryAllocator::get_small_object_allocator(uint32_t type) { |
| 348 | if (type < kSmallObjectMinSizeLog2 || type > kSmallObjectMaxSizeLog2) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 349 | async_safe_fatal("invalid type: %u", type); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 352 | initialize_allocators(); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 353 | return &allocators_[type - kSmallObjectMinSizeLog2]; |
| 354 | } |