| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2014 The Android Open Source Project | 
| Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 3 | * All rights reserved. | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -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 | d597d26 | 2014-05-05 16:49:04 -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 | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 27 | */ | 
| Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 28 |  | 
| Dmitriy Ivanov | c9ce70d | 2015-03-10 15:30:26 -0700 | [diff] [blame] | 29 | #include "linker_block_allocator.h" | 
| Elliott Hughes | 3205cdd | 2020-06-10 14:48:06 -0700 | [diff] [blame] | 30 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 31 | #include <inttypes.h> | 
| Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 32 | #include <string.h> | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 33 | #include <sys/mman.h> | 
| Elliott Hughes | 99d5465 | 2018-08-22 10:36:23 -0700 | [diff] [blame] | 34 | #include <sys/prctl.h> | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 35 | #include <unistd.h> | 
|  | 36 |  | 
| Elliott Hughes | 3205cdd | 2020-06-10 14:48:06 -0700 | [diff] [blame] | 37 | #include "linker_debug.h" | 
|  | 38 |  | 
| Vic Yang | dac328a | 2019-01-23 14:21:58 -0800 | [diff] [blame] | 39 | static constexpr size_t kAllocateSize = PAGE_SIZE * 100; | 
|  | 40 | static_assert(kAllocateSize % PAGE_SIZE == 0, "Invalid kAllocateSize."); | 
|  | 41 |  | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 42 | // the multiplier should be power of 2 | 
|  | 43 | static constexpr size_t round_up(size_t size, size_t multiplier) { | 
|  | 44 | return (size + (multiplier - 1)) & ~(multiplier-1); | 
|  | 45 | } | 
|  | 46 |  | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 47 | struct LinkerBlockAllocatorPage { | 
|  | 48 | LinkerBlockAllocatorPage* next; | 
| Vic Yang | dac328a | 2019-01-23 14:21:58 -0800 | [diff] [blame] | 49 | uint8_t bytes[kAllocateSize - 16] __attribute__((aligned(16))); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 50 | }; | 
|  | 51 |  | 
|  | 52 | struct FreeBlockInfo { | 
|  | 53 | void* next_block; | 
|  | 54 | size_t num_free_blocks; | 
|  | 55 | }; | 
|  | 56 |  | 
| Dmitriy Ivanov | 4151ea7 | 2014-07-24 15:33:25 -0700 | [diff] [blame] | 57 | LinkerBlockAllocator::LinkerBlockAllocator(size_t block_size) | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 58 | : block_size_( | 
|  | 59 | round_up(block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size, 16)), | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 60 | page_list_(nullptr), | 
| Vic Yang | bb7e123 | 2019-01-29 20:23:16 -0800 | [diff] [blame] | 61 | free_block_list_(nullptr), | 
|  | 62 | allocated_(0) | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 63 | {} | 
|  | 64 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 65 | void* LinkerBlockAllocator::alloc() { | 
|  | 66 | if (free_block_list_ == nullptr) { | 
|  | 67 | create_new_page(); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
|  | 70 | FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(free_block_list_); | 
|  | 71 | if (block_info->num_free_blocks > 1) { | 
|  | 72 | FreeBlockInfo* next_block_info = reinterpret_cast<FreeBlockInfo*>( | 
|  | 73 | reinterpret_cast<char*>(free_block_list_) + block_size_); | 
|  | 74 | next_block_info->next_block = block_info->next_block; | 
|  | 75 | next_block_info->num_free_blocks = block_info->num_free_blocks - 1; | 
|  | 76 | free_block_list_ = next_block_info; | 
|  | 77 | } else { | 
|  | 78 | free_block_list_ = block_info->next_block; | 
|  | 79 | } | 
|  | 80 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 81 | memset(block_info, 0, block_size_); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 82 |  | 
| Vic Yang | bb7e123 | 2019-01-29 20:23:16 -0800 | [diff] [blame] | 83 | ++allocated_; | 
|  | 84 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 85 | return block_info; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | void LinkerBlockAllocator::free(void* block) { | 
|  | 89 | if (block == nullptr) { | 
|  | 90 | return; | 
|  | 91 | } | 
|  | 92 |  | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 93 | LinkerBlockAllocatorPage* page = find_page(block); | 
| Elliott Hughes | 3205cdd | 2020-06-10 14:48:06 -0700 | [diff] [blame] | 94 | CHECK(page != nullptr); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 95 |  | 
|  | 96 | ssize_t offset = reinterpret_cast<uint8_t*>(block) - page->bytes; | 
| Elliott Hughes | 3205cdd | 2020-06-10 14:48:06 -0700 | [diff] [blame] | 97 | CHECK((offset % block_size_) == 0); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 98 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 99 | memset(block, 0, block_size_); | 
|  | 100 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 101 | FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(block); | 
|  | 102 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 103 | block_info->next_block = free_block_list_; | 
|  | 104 | block_info->num_free_blocks = 1; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 105 |  | 
|  | 106 | free_block_list_ = block_info; | 
| Vic Yang | bb7e123 | 2019-01-29 20:23:16 -0800 | [diff] [blame] | 107 |  | 
|  | 108 | --allocated_; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
|  | 111 | void LinkerBlockAllocator::protect_all(int prot) { | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 112 | for (LinkerBlockAllocatorPage* page = page_list_; page != nullptr; page = page->next) { | 
| Vic Yang | dac328a | 2019-01-23 14:21:58 -0800 | [diff] [blame] | 113 | if (mprotect(page, kAllocateSize, prot) == -1) { | 
| Elliott Hughes | 3205cdd | 2020-06-10 14:48:06 -0700 | [diff] [blame] | 114 | async_safe_fatal("mprotect(%p, %zu, %d) failed: %m", page, kAllocateSize, prot); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 115 | } | 
|  | 116 | } | 
|  | 117 | } | 
|  | 118 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 119 | void LinkerBlockAllocator::create_new_page() { | 
| Vic Yang | dac328a | 2019-01-23 14:21:58 -0800 | [diff] [blame] | 120 | static_assert(sizeof(LinkerBlockAllocatorPage) == kAllocateSize, | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 121 | "Invalid sizeof(LinkerBlockAllocatorPage)"); | 
|  | 122 |  | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 123 | LinkerBlockAllocatorPage* page = reinterpret_cast<LinkerBlockAllocatorPage*>( | 
| Vic Yang | dac328a | 2019-01-23 14:21:58 -0800 | [diff] [blame] | 124 | mmap(nullptr, kAllocateSize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)); | 
| Elliott Hughes | 3205cdd | 2020-06-10 14:48:06 -0700 | [diff] [blame] | 125 | CHECK(page != MAP_FAILED); | 
| Dmitriy Ivanov | 51a22a1 | 2014-08-08 16:57:15 -0700 | [diff] [blame] | 126 |  | 
| Vic Yang | dac328a | 2019-01-23 14:21:58 -0800 | [diff] [blame] | 127 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, page, kAllocateSize, "linker_alloc"); | 
| Dmitriy Ivanov | 51a22a1 | 2014-08-08 16:57:15 -0700 | [diff] [blame] | 128 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 129 | FreeBlockInfo* first_block = reinterpret_cast<FreeBlockInfo*>(page->bytes); | 
|  | 130 | first_block->next_block = free_block_list_; | 
| Ryan Prichard | 92b3e1b | 2019-03-11 17:27:52 -0700 | [diff] [blame] | 131 | first_block->num_free_blocks = sizeof(page->bytes) / block_size_; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 132 |  | 
|  | 133 | free_block_list_ = first_block; | 
|  | 134 |  | 
|  | 135 | page->next = page_list_; | 
|  | 136 | page_list_ = page; | 
|  | 137 | } | 
|  | 138 |  | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 139 | LinkerBlockAllocatorPage* LinkerBlockAllocator::find_page(void* block) { | 
| Elliott Hughes | 3205cdd | 2020-06-10 14:48:06 -0700 | [diff] [blame] | 140 | CHECK(block != nullptr); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 141 |  | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 142 | LinkerBlockAllocatorPage* page = page_list_; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 143 | while (page != nullptr) { | 
| Dmitriy Ivanov | 1079406 | 2014-05-14 12:52:57 -0700 | [diff] [blame] | 144 | const uint8_t* page_ptr = reinterpret_cast<const uint8_t*>(page); | 
| Vic Yang | dac328a | 2019-01-23 14:21:58 -0800 | [diff] [blame] | 145 | if (block >= (page_ptr + sizeof(page->next)) && block < (page_ptr + kAllocateSize)) { | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 146 | return page; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | page = page->next; | 
|  | 150 | } | 
|  | 151 |  | 
| Elliott Hughes | 3205cdd | 2020-06-10 14:48:06 -0700 | [diff] [blame] | 152 | async_safe_fatal("couldn't find page for %p", block); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 153 | } | 
| Vic Yang | bb7e123 | 2019-01-29 20:23:16 -0800 | [diff] [blame] | 154 |  | 
|  | 155 | void LinkerBlockAllocator::purge() { | 
|  | 156 | if (allocated_) { | 
|  | 157 | return; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | LinkerBlockAllocatorPage* page = page_list_; | 
|  | 161 | while (page) { | 
|  | 162 | LinkerBlockAllocatorPage* next = page->next; | 
|  | 163 | munmap(page, kAllocateSize); | 
|  | 164 | page = next; | 
|  | 165 | } | 
|  | 166 | page_list_ = nullptr; | 
|  | 167 | free_block_list_ = nullptr; | 
|  | 168 | } |