| 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 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 29 | #include "private/bionic_allocator.h" | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 30 |  | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 31 | #include <stdlib.h> | 
| Ryan Prichard | 52165b3 | 2019-01-23 17:46:24 -0800 | [diff] [blame] | 32 | #include <string.h> | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 33 | #include <sys/mman.h> | 
| Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 34 | #include <sys/param.h> | 
| Elliott Hughes | 99d5465 | 2018-08-22 10:36:23 -0700 | [diff] [blame] | 35 | #include <sys/prctl.h> | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 36 | #include <unistd.h> | 
|  | 37 |  | 
| Ryan Prichard | 52165b3 | 2019-01-23 17:46:24 -0800 | [diff] [blame] | 38 | #include <new> | 
|  | 39 |  | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 40 | #include <async_safe/log.h> | 
| Elliott Hughes | 3019d78 | 2019-02-13 12:39:07 -0800 | [diff] [blame] | 41 | #include <async_safe/CHECK.h> | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 42 |  | 
| Elliott Hughes | cdb52fc | 2019-12-12 15:26:14 -0800 | [diff] [blame] | 43 | #include "platform/bionic/page.h" | 
| Josh Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 44 | #include "platform/bionic/macros.h" | 
| Ryan Prichard | 52165b3 | 2019-01-23 17:46:24 -0800 | [diff] [blame] | 45 |  | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 46 | // | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 47 | // BionicAllocator is a general purpose allocator designed to provide the same | 
| Ryan Prichard | db6edcc | 2019-04-01 16:16:05 -0700 | [diff] [blame] | 48 | // functionality as the malloc/free/realloc/memalign libc functions. | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 49 | // | 
|  | 50 | // On alloc: | 
| Ryan Prichard | db6edcc | 2019-04-01 16:16:05 -0700 | [diff] [blame] | 51 | // If size is > 1k allocator proxies malloc call directly to mmap. | 
|  | 52 | // If size <= 1k allocator uses BionicSmallObjectAllocator for the size | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 53 | // rounded up to the nearest power of two. | 
|  | 54 | // | 
|  | 55 | // On free: | 
|  | 56 | // | 
|  | 57 | // For a pointer allocated using proxy-to-mmap allocator unmaps | 
|  | 58 | // the memory. | 
|  | 59 | // | 
| Ryan Prichard | db6edcc | 2019-04-01 16:16:05 -0700 | [diff] [blame] | 60 | // For a pointer allocated using BionicSmallObjectAllocator it adds | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 61 | // the block to free_blocks_list in the corresponding page. If the number of | 
| Ryan Prichard | db6edcc | 2019-04-01 16:16:05 -0700 | [diff] [blame] | 62 | // free pages reaches 2, BionicSmallObjectAllocator munmaps one of the pages | 
|  | 63 | // keeping the other one in reserve. | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 64 |  | 
|  | 65 | // Memory management for large objects is fairly straightforward, but for small | 
|  | 66 | // objects it is more complicated.  If you are changing this code, one simple | 
|  | 67 | // way to evaluate the memory usage change is by running 'dd' and examine the | 
|  | 68 | // memory usage by 'showmap $(pidof dd)'.  'dd' is nice in that: | 
|  | 69 | //   1. It links in quite a few libraries, so you get some linker memory use. | 
|  | 70 | //   2. When run with no arguments, it sits waiting for input, so it is easy to | 
|  | 71 | //      examine its memory usage with showmap. | 
|  | 72 | //   3. Since it does nothing while waiting for input, the memory usage is | 
|  | 73 | //      determinisitic. | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 74 |  | 
|  | 75 | static const char kSignature[4] = {'L', 'M', 'A', 1}; | 
|  | 76 |  | 
|  | 77 | static const size_t kSmallObjectMaxSize = 1 << kSmallObjectMaxSizeLog2; | 
|  | 78 |  | 
|  | 79 | // This type is used for large allocations (with size >1k) | 
|  | 80 | static const uint32_t kLargeObject = 111; | 
|  | 81 |  | 
| Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 82 | // Allocated pointers must be at least 16-byte aligned.  Round up the size of | 
|  | 83 | // page_info to multiple of 16. | 
|  | 84 | static constexpr size_t kPageInfoSize = __BIONIC_ALIGN(sizeof(page_info), 16); | 
|  | 85 |  | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 86 | static inline uint16_t log2(size_t number) { | 
|  | 87 | uint16_t result = 0; | 
|  | 88 | number--; | 
|  | 89 |  | 
|  | 90 | while (number != 0) { | 
|  | 91 | result++; | 
|  | 92 | number >>= 1; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | return result; | 
|  | 96 | } | 
|  | 97 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 98 | BionicSmallObjectAllocator::BionicSmallObjectAllocator(uint32_t type, | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 99 | size_t block_size) | 
|  | 100 | : type_(type), | 
|  | 101 | block_size_(block_size), | 
| Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 102 | blocks_per_page_((PAGE_SIZE - sizeof(small_object_page_info)) / | 
|  | 103 | block_size), | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 104 | free_pages_cnt_(0), | 
|  | 105 | page_list_(nullptr) {} | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 106 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 107 | void* BionicSmallObjectAllocator::alloc() { | 
| Dimitry Ivanov | f857211 | 2016-07-13 10:24:06 -0700 | [diff] [blame] | 108 | CHECK(block_size_ != 0); | 
|  | 109 |  | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 110 | if (page_list_ == nullptr) { | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 111 | alloc_page(); | 
|  | 112 | } | 
|  | 113 |  | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 114 | // Fully allocated pages are de-managed and removed from the page list, so | 
|  | 115 | // every page from the page list must be useable.  Let's just take the first | 
|  | 116 | // one. | 
|  | 117 | small_object_page_info* page = page_list_; | 
|  | 118 | CHECK(page->free_block_list != nullptr); | 
|  | 119 |  | 
|  | 120 | small_object_block_record* const block_record = page->free_block_list; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 121 | if (block_record->free_blocks_cnt > 1) { | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 122 | small_object_block_record* next_free = | 
|  | 123 | reinterpret_cast<small_object_block_record*>( | 
|  | 124 | reinterpret_cast<uint8_t*>(block_record) + block_size_); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 125 | next_free->next = block_record->next; | 
|  | 126 | next_free->free_blocks_cnt = block_record->free_blocks_cnt - 1; | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 127 | page->free_block_list = next_free; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 128 | } else { | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 129 | page->free_block_list = block_record->next; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
| Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 132 | if (page->free_blocks_cnt == blocks_per_page_) { | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 133 | free_pages_cnt_--; | 
|  | 134 | } | 
|  | 135 |  | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 136 | page->free_blocks_cnt--; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 137 |  | 
|  | 138 | memset(block_record, 0, block_size_); | 
|  | 139 |  | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 140 | if (page->free_blocks_cnt == 0) { | 
|  | 141 | // De-manage fully allocated pages.  These pages will be managed again if | 
|  | 142 | // a block is freed. | 
|  | 143 | remove_from_page_list(page); | 
|  | 144 | } | 
|  | 145 |  | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 146 | return block_record; | 
|  | 147 | } | 
|  | 148 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 149 | void BionicSmallObjectAllocator::free_page(small_object_page_info* page) { | 
| Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 150 | CHECK(page->free_blocks_cnt == blocks_per_page_); | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 151 | if (page->prev_page) { | 
|  | 152 | page->prev_page->next_page = page->next_page; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 153 | } | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 154 | if (page->next_page) { | 
|  | 155 | page->next_page->prev_page = page->prev_page; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 156 | } | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 157 | if (page_list_ == page) { | 
|  | 158 | page_list_ = page->next_page; | 
|  | 159 | } | 
|  | 160 | munmap(page, PAGE_SIZE); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 161 | free_pages_cnt_--; | 
|  | 162 | } | 
|  | 163 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 164 | void BionicSmallObjectAllocator::free(void* ptr) { | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 165 | small_object_page_info* const page = | 
|  | 166 | reinterpret_cast<small_object_page_info*>( | 
|  | 167 | PAGE_START(reinterpret_cast<uintptr_t>(ptr))); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 168 |  | 
| Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 169 | if (reinterpret_cast<uintptr_t>(ptr) % block_size_ != 0) { | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 170 | async_safe_fatal("invalid pointer: %p (block_size=%zd)", ptr, block_size_); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 171 | } | 
|  | 172 |  | 
|  | 173 | memset(ptr, 0, block_size_); | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 174 | small_object_block_record* const block_record = | 
|  | 175 | reinterpret_cast<small_object_block_record*>(ptr); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 176 |  | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 177 | block_record->next = page->free_block_list; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 178 | block_record->free_blocks_cnt = 1; | 
|  | 179 |  | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 180 | page->free_block_list = block_record; | 
|  | 181 | page->free_blocks_cnt++; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 182 |  | 
| Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 183 | if (page->free_blocks_cnt == blocks_per_page_) { | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 184 | if (++free_pages_cnt_ > 1) { | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 185 | // if we already have a free page - unmap this one. | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 186 | free_page(page); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 187 | } | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 188 | } else if (page->free_blocks_cnt == 1) { | 
|  | 189 | // We just freed from a full page.  Add this page back to the list. | 
|  | 190 | add_to_page_list(page); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 191 | } | 
|  | 192 | } | 
|  | 193 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 194 | void BionicSmallObjectAllocator::alloc_page() { | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 195 | void* const map_ptr = mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, | 
|  | 196 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 197 | if (map_ptr == MAP_FAILED) { | 
| Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 198 | async_safe_fatal("mmap failed: %s", strerror(errno)); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 199 | } | 
|  | 200 |  | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 201 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, PAGE_SIZE, | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 202 | "bionic_alloc_small_objects"); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 203 |  | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 204 | small_object_page_info* const page = | 
|  | 205 | reinterpret_cast<small_object_page_info*>(map_ptr); | 
|  | 206 | memcpy(page->info.signature, kSignature, sizeof(kSignature)); | 
|  | 207 | page->info.type = type_; | 
|  | 208 | page->info.allocator_addr = this; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 209 |  | 
| Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 210 | page->free_blocks_cnt = blocks_per_page_; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 211 |  | 
| Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 212 | // Align the first block to block_size_. | 
|  | 213 | const uintptr_t first_block_addr = | 
|  | 214 | __BIONIC_ALIGN(reinterpret_cast<uintptr_t>(page + 1), block_size_); | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 215 | small_object_block_record* const first_block = | 
| Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 216 | reinterpret_cast<small_object_block_record*>(first_block_addr); | 
|  | 217 |  | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 218 | first_block->next = nullptr; | 
| Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 219 | first_block->free_blocks_cnt = blocks_per_page_; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 220 |  | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 221 | page->free_block_list = first_block; | 
|  | 222 |  | 
|  | 223 | add_to_page_list(page); | 
| Vic Yang | de69660 | 2018-11-27 13:34:44 -0800 | [diff] [blame] | 224 |  | 
|  | 225 | free_pages_cnt_++; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 226 | } | 
|  | 227 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 228 | void BionicSmallObjectAllocator::add_to_page_list(small_object_page_info* page) { | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 229 | page->next_page = page_list_; | 
|  | 230 | page->prev_page = nullptr; | 
|  | 231 | if (page_list_) { | 
|  | 232 | page_list_->prev_page = page; | 
|  | 233 | } | 
|  | 234 | page_list_ = page; | 
|  | 235 | } | 
|  | 236 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 237 | void BionicSmallObjectAllocator::remove_from_page_list( | 
| Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 238 | small_object_page_info* page) { | 
|  | 239 | if (page->prev_page) { | 
|  | 240 | page->prev_page->next_page = page->next_page; | 
|  | 241 | } | 
|  | 242 | if (page->next_page) { | 
|  | 243 | page->next_page->prev_page = page->prev_page; | 
|  | 244 | } | 
|  | 245 | if (page_list_ == page) { | 
|  | 246 | page_list_ = page->next_page; | 
|  | 247 | } | 
|  | 248 | page->prev_page = nullptr; | 
|  | 249 | page->next_page = nullptr; | 
|  | 250 | } | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 251 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 252 | void BionicAllocator::initialize_allocators() { | 
| Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 253 | if (allocators_ != nullptr) { | 
|  | 254 | return; | 
|  | 255 | } | 
|  | 256 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 257 | BionicSmallObjectAllocator* allocators = | 
|  | 258 | reinterpret_cast<BionicSmallObjectAllocator*>(allocators_buf_); | 
| Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 259 |  | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 260 | for (size_t i = 0; i < kSmallObjectAllocatorsCount; ++i) { | 
|  | 261 | uint32_t type = i + kSmallObjectMinSizeLog2; | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 262 | new (allocators + i) BionicSmallObjectAllocator(type, 1 << type); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 263 | } | 
| Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 264 |  | 
|  | 265 | allocators_ = allocators; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 266 | } | 
|  | 267 |  | 
| Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 268 | void* BionicAllocator::alloc_mmap(size_t align, size_t size) { | 
|  | 269 | size_t header_size = __BIONIC_ALIGN(kPageInfoSize, align); | 
|  | 270 | size_t allocated_size; | 
|  | 271 | if (__builtin_add_overflow(header_size, size, &allocated_size) || | 
|  | 272 | PAGE_END(allocated_size) < allocated_size) { | 
|  | 273 | async_safe_fatal("overflow trying to alloc %zu bytes", size); | 
|  | 274 | } | 
|  | 275 | allocated_size = PAGE_END(allocated_size); | 
| Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 276 | void* map_ptr = mmap(nullptr, allocated_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, | 
|  | 277 | -1, 0); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 278 |  | 
|  | 279 | if (map_ptr == MAP_FAILED) { | 
| Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 280 | async_safe_fatal("mmap failed: %s", strerror(errno)); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 281 | } | 
|  | 282 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 283 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, allocated_size, "bionic_alloc_lob"); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 284 |  | 
| Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 285 | void* result = static_cast<char*>(map_ptr) + header_size; | 
|  | 286 | page_info* info = get_page_info_unchecked(result); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 287 | memcpy(info->signature, kSignature, sizeof(kSignature)); | 
|  | 288 | info->type = kLargeObject; | 
|  | 289 | info->allocated_size = allocated_size; | 
|  | 290 |  | 
| Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 291 | return result; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 292 | } | 
|  | 293 |  | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 294 |  | 
| Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 295 | inline void* BionicAllocator::alloc_impl(size_t align, size_t size) { | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 296 | if (size > kSmallObjectMaxSize) { | 
| Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 297 | return alloc_mmap(align, size); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 298 | } | 
|  | 299 |  | 
|  | 300 | uint16_t log2_size = log2(size); | 
|  | 301 |  | 
|  | 302 | if (log2_size < kSmallObjectMinSizeLog2) { | 
|  | 303 | log2_size = kSmallObjectMinSizeLog2; | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | return get_small_object_allocator(log2_size)->alloc(); | 
|  | 307 | } | 
|  | 308 |  | 
| Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 309 | void* BionicAllocator::alloc(size_t size) { | 
|  | 310 | // treat alloc(0) as alloc(1) | 
|  | 311 | if (size == 0) { | 
|  | 312 | size = 1; | 
|  | 313 | } | 
|  | 314 | return alloc_impl(16, size); | 
|  | 315 | } | 
|  | 316 |  | 
|  | 317 | void* BionicAllocator::memalign(size_t align, size_t size) { | 
|  | 318 | // The Bionic allocator only supports alignment up to one page, which is good | 
|  | 319 | // enough for ELF TLS. | 
|  | 320 | align = MIN(align, PAGE_SIZE); | 
|  | 321 | align = MAX(align, 16); | 
|  | 322 | if (!powerof2(align)) { | 
|  | 323 | align = BIONIC_ROUND_UP_POWER_OF_2(align); | 
|  | 324 | } | 
|  | 325 | size = MAX(size, align); | 
|  | 326 | return alloc_impl(align, size); | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | inline page_info* BionicAllocator::get_page_info_unchecked(void* ptr) { | 
|  | 330 | uintptr_t header_page = PAGE_START(reinterpret_cast<size_t>(ptr) - kPageInfoSize); | 
|  | 331 | return reinterpret_cast<page_info*>(header_page); | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | inline page_info* BionicAllocator::get_page_info(void* ptr) { | 
|  | 335 | page_info* info = get_page_info_unchecked(ptr); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 336 | if (memcmp(info->signature, kSignature, sizeof(kSignature)) != 0) { | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 337 | async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 338 | } | 
|  | 339 |  | 
|  | 340 | return info; | 
|  | 341 | } | 
|  | 342 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 343 | void* BionicAllocator::realloc(void* ptr, size_t size) { | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 344 | if (ptr == nullptr) { | 
|  | 345 | return alloc(size); | 
|  | 346 | } | 
|  | 347 |  | 
|  | 348 | if (size == 0) { | 
|  | 349 | free(ptr); | 
|  | 350 | return nullptr; | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | page_info* info = get_page_info(ptr); | 
|  | 354 |  | 
|  | 355 | size_t old_size = 0; | 
|  | 356 |  | 
|  | 357 | if (info->type == kLargeObject) { | 
| Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 358 | old_size = info->allocated_size - (static_cast<char*>(ptr) - reinterpret_cast<char*>(info)); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 359 | } else { | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 360 | BionicSmallObjectAllocator* allocator = get_small_object_allocator(info->type); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 361 | if (allocator != info->allocator_addr) { | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 362 | async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 363 | } | 
|  | 364 |  | 
|  | 365 | old_size = allocator->get_block_size(); | 
|  | 366 | } | 
|  | 367 |  | 
|  | 368 | if (old_size < size) { | 
|  | 369 | void *result = alloc(size); | 
|  | 370 | memcpy(result, ptr, old_size); | 
|  | 371 | free(ptr); | 
|  | 372 | return result; | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | return ptr; | 
|  | 376 | } | 
|  | 377 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 378 | void BionicAllocator::free(void* ptr) { | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 379 | if (ptr == nullptr) { | 
|  | 380 | return; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | page_info* info = get_page_info(ptr); | 
|  | 384 |  | 
|  | 385 | if (info->type == kLargeObject) { | 
|  | 386 | munmap(info, info->allocated_size); | 
|  | 387 | } else { | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 388 | BionicSmallObjectAllocator* allocator = get_small_object_allocator(info->type); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 389 | if (allocator != info->allocator_addr) { | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 390 | 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] | 391 | } | 
|  | 392 |  | 
|  | 393 | allocator->free(ptr); | 
|  | 394 | } | 
|  | 395 | } | 
|  | 396 |  | 
| Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 397 | size_t BionicAllocator::get_chunk_size(void* ptr) { | 
|  | 398 | if (ptr == nullptr) return 0; | 
|  | 399 |  | 
|  | 400 | page_info* info = get_page_info_unchecked(ptr); | 
|  | 401 | if (memcmp(info->signature, kSignature, sizeof(kSignature)) != 0) { | 
|  | 402 | // Invalid pointer (mismatched signature) | 
|  | 403 | return 0; | 
|  | 404 | } | 
|  | 405 | if (info->type == kLargeObject) { | 
|  | 406 | return info->allocated_size - (static_cast<char*>(ptr) - reinterpret_cast<char*>(info)); | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | BionicSmallObjectAllocator* allocator = get_small_object_allocator(info->type); | 
|  | 410 | if (allocator != info->allocator_addr) { | 
|  | 411 | // Invalid pointer. | 
|  | 412 | return 0; | 
|  | 413 | } | 
|  | 414 | return allocator->get_block_size(); | 
|  | 415 | } | 
|  | 416 |  | 
| Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 417 | BionicSmallObjectAllocator* BionicAllocator::get_small_object_allocator(uint32_t type) { | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 418 | if (type < kSmallObjectMinSizeLog2 || type > kSmallObjectMaxSizeLog2) { | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 419 | async_safe_fatal("invalid type: %u", type); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 420 | } | 
|  | 421 |  | 
| Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 422 | initialize_allocators(); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 423 | return &allocators_[type - kSmallObjectMinSizeLog2]; | 
|  | 424 | } |