| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2014 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
| Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 16 |  | 
| Dmitriy Ivanov | c9ce70d | 2015-03-10 15:30:26 -0700 | [diff] [blame] | 17 | #include "linker_block_allocator.h" | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 18 | #include <inttypes.h> | 
| Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 19 | #include <string.h> | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 20 | #include <sys/mman.h> | 
|  | 21 | #include <unistd.h> | 
|  | 22 |  | 
| Dmitriy Ivanov | 51a22a1 | 2014-08-08 16:57:15 -0700 | [diff] [blame] | 23 | #include "private/bionic_prctl.h" | 
|  | 24 |  | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 25 | // the multiplier should be power of 2 | 
|  | 26 | static constexpr size_t round_up(size_t size, size_t multiplier) { | 
|  | 27 | return (size + (multiplier - 1)) & ~(multiplier-1); | 
|  | 28 | } | 
|  | 29 |  | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 30 | struct LinkerBlockAllocatorPage { | 
|  | 31 | LinkerBlockAllocatorPage* next; | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 32 | uint8_t bytes[PAGE_SIZE - 16] __attribute__((aligned(16))); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 33 | }; | 
|  | 34 |  | 
|  | 35 | struct FreeBlockInfo { | 
|  | 36 | void* next_block; | 
|  | 37 | size_t num_free_blocks; | 
|  | 38 | }; | 
|  | 39 |  | 
| Dmitriy Ivanov | 4151ea7 | 2014-07-24 15:33:25 -0700 | [diff] [blame] | 40 | LinkerBlockAllocator::LinkerBlockAllocator(size_t block_size) | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 41 | : block_size_( | 
|  | 42 | round_up(block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size, 16)), | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 43 | page_list_(nullptr), | 
|  | 44 | free_block_list_(nullptr) | 
|  | 45 | {} | 
|  | 46 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 47 | void* LinkerBlockAllocator::alloc() { | 
|  | 48 | if (free_block_list_ == nullptr) { | 
|  | 49 | create_new_page(); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 50 | } | 
|  | 51 |  | 
|  | 52 | FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(free_block_list_); | 
|  | 53 | if (block_info->num_free_blocks > 1) { | 
|  | 54 | FreeBlockInfo* next_block_info = reinterpret_cast<FreeBlockInfo*>( | 
|  | 55 | reinterpret_cast<char*>(free_block_list_) + block_size_); | 
|  | 56 | next_block_info->next_block = block_info->next_block; | 
|  | 57 | next_block_info->num_free_blocks = block_info->num_free_blocks - 1; | 
|  | 58 | free_block_list_ = next_block_info; | 
|  | 59 | } else { | 
|  | 60 | free_block_list_ = block_info->next_block; | 
|  | 61 | } | 
|  | 62 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 63 | memset(block_info, 0, block_size_); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | return block_info; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | void LinkerBlockAllocator::free(void* block) { | 
|  | 69 | if (block == nullptr) { | 
|  | 70 | return; | 
|  | 71 | } | 
|  | 72 |  | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 73 | LinkerBlockAllocatorPage* page = find_page(block); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 74 |  | 
|  | 75 | if (page == nullptr) { | 
|  | 76 | abort(); | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | ssize_t offset = reinterpret_cast<uint8_t*>(block) - page->bytes; | 
|  | 80 |  | 
|  | 81 | if (offset % block_size_ != 0) { | 
|  | 82 | abort(); | 
|  | 83 | } | 
|  | 84 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 85 | memset(block, 0, block_size_); | 
|  | 86 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 87 | FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(block); | 
|  | 88 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 89 | block_info->next_block = free_block_list_; | 
|  | 90 | block_info->num_free_blocks = 1; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 91 |  | 
|  | 92 | free_block_list_ = block_info; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | void LinkerBlockAllocator::protect_all(int prot) { | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 96 | for (LinkerBlockAllocatorPage* page = page_list_; page != nullptr; page = page->next) { | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 97 | if (mprotect(page, PAGE_SIZE, prot) == -1) { | 
|  | 98 | abort(); | 
|  | 99 | } | 
|  | 100 | } | 
|  | 101 | } | 
|  | 102 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 103 | void LinkerBlockAllocator::create_new_page() { | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 104 | static_assert(sizeof(LinkerBlockAllocatorPage) == PAGE_SIZE, | 
|  | 105 | "Invalid sizeof(LinkerBlockAllocatorPage)"); | 
|  | 106 |  | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 107 | LinkerBlockAllocatorPage* page = reinterpret_cast<LinkerBlockAllocatorPage*>( | 
|  | 108 | mmap(nullptr, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0)); | 
|  | 109 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 110 | if (page == MAP_FAILED) { | 
|  | 111 | abort(); // oom | 
|  | 112 | } | 
| Dmitriy Ivanov | 51a22a1 | 2014-08-08 16:57:15 -0700 | [diff] [blame] | 113 |  | 
|  | 114 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, page, PAGE_SIZE, "linker_alloc"); | 
|  | 115 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 116 | memset(page, 0, PAGE_SIZE); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 117 |  | 
|  | 118 | FreeBlockInfo* first_block = reinterpret_cast<FreeBlockInfo*>(page->bytes); | 
|  | 119 | first_block->next_block = free_block_list_; | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 120 | first_block->num_free_blocks = (PAGE_SIZE - sizeof(LinkerBlockAllocatorPage*))/block_size_; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 121 |  | 
|  | 122 | free_block_list_ = first_block; | 
|  | 123 |  | 
|  | 124 | page->next = page_list_; | 
|  | 125 | page_list_ = page; | 
|  | 126 | } | 
|  | 127 |  | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 128 | LinkerBlockAllocatorPage* LinkerBlockAllocator::find_page(void* block) { | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 129 | if (block == nullptr) { | 
|  | 130 | abort(); | 
|  | 131 | } | 
|  | 132 |  | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 133 | LinkerBlockAllocatorPage* page = page_list_; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 134 | while (page != nullptr) { | 
| Dmitriy Ivanov | 1079406 | 2014-05-14 12:52:57 -0700 | [diff] [blame] | 135 | const uint8_t* page_ptr = reinterpret_cast<const uint8_t*>(page); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 136 | if (block >= (page_ptr + sizeof(page->next)) && block < (page_ptr + PAGE_SIZE)) { | 
|  | 137 | return page; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | page = page->next; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | abort(); | 
|  | 144 | } |