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 | |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame^] | 25 | struct LinkerBlockAllocatorPage { |
| 26 | LinkerBlockAllocatorPage* next; |
| 27 | uint8_t bytes[PAGE_SIZE-sizeof(LinkerBlockAllocatorPage*)]; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | struct FreeBlockInfo { |
| 31 | void* next_block; |
| 32 | size_t num_free_blocks; |
| 33 | }; |
| 34 | |
Dmitriy Ivanov | 4151ea7 | 2014-07-24 15:33:25 -0700 | [diff] [blame] | 35 | LinkerBlockAllocator::LinkerBlockAllocator(size_t block_size) |
| 36 | : block_size_(block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size), |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 37 | page_list_(nullptr), |
| 38 | free_block_list_(nullptr) |
| 39 | {} |
| 40 | |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 41 | void* LinkerBlockAllocator::alloc() { |
| 42 | if (free_block_list_ == nullptr) { |
| 43 | create_new_page(); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(free_block_list_); |
| 47 | if (block_info->num_free_blocks > 1) { |
| 48 | FreeBlockInfo* next_block_info = reinterpret_cast<FreeBlockInfo*>( |
| 49 | reinterpret_cast<char*>(free_block_list_) + block_size_); |
| 50 | next_block_info->next_block = block_info->next_block; |
| 51 | next_block_info->num_free_blocks = block_info->num_free_blocks - 1; |
| 52 | free_block_list_ = next_block_info; |
| 53 | } else { |
| 54 | free_block_list_ = block_info->next_block; |
| 55 | } |
| 56 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 57 | memset(block_info, 0, block_size_); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 58 | |
| 59 | return block_info; |
| 60 | } |
| 61 | |
| 62 | void LinkerBlockAllocator::free(void* block) { |
| 63 | if (block == nullptr) { |
| 64 | return; |
| 65 | } |
| 66 | |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame^] | 67 | LinkerBlockAllocatorPage* page = find_page(block); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 68 | |
| 69 | if (page == nullptr) { |
| 70 | abort(); |
| 71 | } |
| 72 | |
| 73 | ssize_t offset = reinterpret_cast<uint8_t*>(block) - page->bytes; |
| 74 | |
| 75 | if (offset % block_size_ != 0) { |
| 76 | abort(); |
| 77 | } |
| 78 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 79 | memset(block, 0, block_size_); |
| 80 | |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 81 | FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(block); |
| 82 | |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 83 | block_info->next_block = free_block_list_; |
| 84 | block_info->num_free_blocks = 1; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 85 | |
| 86 | free_block_list_ = block_info; |
| 87 | } |
| 88 | |
| 89 | void LinkerBlockAllocator::protect_all(int prot) { |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame^] | 90 | for (LinkerBlockAllocatorPage* page = page_list_; page != nullptr; page = page->next) { |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 91 | if (mprotect(page, PAGE_SIZE, prot) == -1) { |
| 92 | abort(); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 97 | void LinkerBlockAllocator::create_new_page() { |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame^] | 98 | LinkerBlockAllocatorPage* page = reinterpret_cast<LinkerBlockAllocatorPage*>( |
| 99 | mmap(nullptr, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0)); |
| 100 | |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 101 | if (page == MAP_FAILED) { |
| 102 | abort(); // oom |
| 103 | } |
Dmitriy Ivanov | 51a22a1 | 2014-08-08 16:57:15 -0700 | [diff] [blame] | 104 | |
| 105 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, page, PAGE_SIZE, "linker_alloc"); |
| 106 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 107 | memset(page, 0, PAGE_SIZE); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 108 | |
| 109 | FreeBlockInfo* first_block = reinterpret_cast<FreeBlockInfo*>(page->bytes); |
| 110 | first_block->next_block = free_block_list_; |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame^] | 111 | first_block->num_free_blocks = (PAGE_SIZE - sizeof(LinkerBlockAllocatorPage*))/block_size_; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 112 | |
| 113 | free_block_list_ = first_block; |
| 114 | |
| 115 | page->next = page_list_; |
| 116 | page_list_ = page; |
| 117 | } |
| 118 | |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame^] | 119 | LinkerBlockAllocatorPage* LinkerBlockAllocator::find_page(void* block) { |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 120 | if (block == nullptr) { |
| 121 | abort(); |
| 122 | } |
| 123 | |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame^] | 124 | LinkerBlockAllocatorPage* page = page_list_; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 125 | while (page != nullptr) { |
Dmitriy Ivanov | 1079406 | 2014-05-14 12:52:57 -0700 | [diff] [blame] | 126 | const uint8_t* page_ptr = reinterpret_cast<const uint8_t*>(page); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 127 | if (block >= (page_ptr + sizeof(page->next)) && block < (page_ptr + PAGE_SIZE)) { |
| 128 | return page; |
| 129 | } |
| 130 | |
| 131 | page = page->next; |
| 132 | } |
| 133 | |
| 134 | abort(); |
| 135 | } |