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