blob: 34df9a5d8441ce93718aeead6554bc348c29eabc [file] [log] [blame]
Dmitriy Ivanovd597d262014-05-05 16:49:04 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08003 * All rights reserved.
Dmitriy Ivanovd597d262014-05-05 16:49:04 -07004 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08005 * 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 Ivanovd597d262014-05-05 16:49:04 -070014 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -080015 * 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 Ivanovd597d262014-05-05 16:49:04 -070027 */
Elliott Hughes05fc1d72015-01-28 18:02:33 -080028
Dmitriy Ivanovc9ce70d2015-03-10 15:30:26 -070029#include "linker_block_allocator.h"
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070030#include <inttypes.h>
Elliott Hughes05fc1d72015-01-28 18:02:33 -080031#include <string.h>
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070032#include <sys/mman.h>
Elliott Hughes99d54652018-08-22 10:36:23 -070033#include <sys/prctl.h>
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070034#include <unistd.h>
35
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -080036// the multiplier should be power of 2
37static constexpr size_t round_up(size_t size, size_t multiplier) {
38 return (size + (multiplier - 1)) & ~(multiplier-1);
39}
40
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070041struct LinkerBlockAllocatorPage {
42 LinkerBlockAllocatorPage* next;
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -080043 uint8_t bytes[PAGE_SIZE - 16] __attribute__((aligned(16)));
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070044};
45
46struct FreeBlockInfo {
47 void* next_block;
48 size_t num_free_blocks;
49};
50
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -070051LinkerBlockAllocator::LinkerBlockAllocator(size_t block_size)
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -080052 : block_size_(
53 round_up(block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size, 16)),
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070054 page_list_(nullptr),
Vic Yangfb78a4a2019-01-23 12:19:41 -080055 free_block_list_(nullptr),
56 allocated_(0)
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070057{}
58
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070059void* LinkerBlockAllocator::alloc() {
60 if (free_block_list_ == nullptr) {
61 create_new_page();
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070062 }
63
64 FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(free_block_list_);
65 if (block_info->num_free_blocks > 1) {
66 FreeBlockInfo* next_block_info = reinterpret_cast<FreeBlockInfo*>(
67 reinterpret_cast<char*>(free_block_list_) + block_size_);
68 next_block_info->next_block = block_info->next_block;
69 next_block_info->num_free_blocks = block_info->num_free_blocks - 1;
70 free_block_list_ = next_block_info;
71 } else {
72 free_block_list_ = block_info->next_block;
73 }
74
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070075 memset(block_info, 0, block_size_);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070076
Vic Yangfb78a4a2019-01-23 12:19:41 -080077 ++allocated_;
78
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070079 return block_info;
80}
81
82void LinkerBlockAllocator::free(void* block) {
83 if (block == nullptr) {
84 return;
85 }
86
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070087 LinkerBlockAllocatorPage* page = find_page(block);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070088
89 if (page == nullptr) {
90 abort();
91 }
92
93 ssize_t offset = reinterpret_cast<uint8_t*>(block) - page->bytes;
94
95 if (offset % block_size_ != 0) {
96 abort();
97 }
98
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070099 memset(block, 0, block_size_);
100
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700101 FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(block);
102
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700103 block_info->next_block = free_block_list_;
104 block_info->num_free_blocks = 1;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700105
106 free_block_list_ = block_info;
Vic Yangfb78a4a2019-01-23 12:19:41 -0800107
108 --allocated_;
109 if (allocated_ == 0) {
110 free_all_pages();
111 }
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700112}
113
114void LinkerBlockAllocator::protect_all(int prot) {
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700115 for (LinkerBlockAllocatorPage* page = page_list_; page != nullptr; page = page->next) {
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700116 if (mprotect(page, PAGE_SIZE, prot) == -1) {
117 abort();
118 }
119 }
120}
121
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700122void LinkerBlockAllocator::create_new_page() {
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -0800123 static_assert(sizeof(LinkerBlockAllocatorPage) == PAGE_SIZE,
124 "Invalid sizeof(LinkerBlockAllocatorPage)");
125
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700126 LinkerBlockAllocatorPage* page = reinterpret_cast<LinkerBlockAllocatorPage*>(
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700127 mmap(nullptr, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0));
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700128
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700129 if (page == MAP_FAILED) {
130 abort(); // oom
131 }
Dmitriy Ivanov51a22a12014-08-08 16:57:15 -0700132
133 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, page, PAGE_SIZE, "linker_alloc");
134
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700135 FreeBlockInfo* first_block = reinterpret_cast<FreeBlockInfo*>(page->bytes);
136 first_block->next_block = free_block_list_;
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700137 first_block->num_free_blocks = (PAGE_SIZE - sizeof(LinkerBlockAllocatorPage*))/block_size_;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700138
139 free_block_list_ = first_block;
140
141 page->next = page_list_;
142 page_list_ = page;
143}
144
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700145LinkerBlockAllocatorPage* LinkerBlockAllocator::find_page(void* block) {
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700146 if (block == nullptr) {
147 abort();
148 }
149
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700150 LinkerBlockAllocatorPage* page = page_list_;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700151 while (page != nullptr) {
Dmitriy Ivanov10794062014-05-14 12:52:57 -0700152 const uint8_t* page_ptr = reinterpret_cast<const uint8_t*>(page);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700153 if (block >= (page_ptr + sizeof(page->next)) && block < (page_ptr + PAGE_SIZE)) {
154 return page;
155 }
156
157 page = page->next;
158 }
159
160 abort();
161}
Vic Yangfb78a4a2019-01-23 12:19:41 -0800162
163void LinkerBlockAllocator::free_all_pages() {
164 if (allocated_) {
165 abort();
166 }
167
168 LinkerBlockAllocatorPage* page = page_list_;
169 while (page) {
170 LinkerBlockAllocatorPage* next = page->next;
171 munmap(page, PAGE_SIZE);
172 page = next;
173 }
174 page_list_ = nullptr;
175 free_block_list_ = nullptr;
176}