2014-05-06 01:49:04 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
2017-02-16 00:31:13 +01:00
|
|
|
* All rights reserved.
|
2014-05-06 01:49:04 +02:00
|
|
|
*
|
2017-02-16 00:31:13 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2014-05-06 01:49:04 +02:00
|
|
|
*
|
2017-02-16 00:31:13 +01:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2014-05-06 01:49:04 +02:00
|
|
|
*/
|
2015-01-29 03:02:33 +01:00
|
|
|
|
2015-03-10 23:30:26 +01:00
|
|
|
#include "linker_block_allocator.h"
|
2020-06-10 23:48:06 +02:00
|
|
|
|
2014-05-06 01:49:04 +02:00
|
|
|
#include <inttypes.h>
|
2015-01-29 03:02:33 +01:00
|
|
|
#include <string.h>
|
2014-05-06 01:49:04 +02:00
|
|
|
#include <sys/mman.h>
|
2021-11-18 19:48:47 +01:00
|
|
|
#include <sys/param.h>
|
2018-08-22 19:36:23 +02:00
|
|
|
#include <sys/prctl.h>
|
2014-05-06 01:49:04 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-06-10 23:48:06 +02:00
|
|
|
#include "linker_debug.h"
|
|
|
|
|
2023-08-03 23:13:41 +02:00
|
|
|
static constexpr size_t kMaxPageSize = 65536;
|
|
|
|
static constexpr size_t kAllocateSize = kMaxPageSize * 6;
|
|
|
|
static_assert(kAllocateSize % kMaxPageSize == 0, "Invalid kAllocateSize.");
|
2019-01-23 23:21:58 +01:00
|
|
|
|
2015-03-10 23:43:50 +01:00
|
|
|
struct LinkerBlockAllocatorPage {
|
|
|
|
LinkerBlockAllocatorPage* next;
|
2019-01-23 23:21:58 +01:00
|
|
|
uint8_t bytes[kAllocateSize - 16] __attribute__((aligned(16)));
|
2014-05-06 01:49:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FreeBlockInfo {
|
|
|
|
void* next_block;
|
|
|
|
size_t num_free_blocks;
|
|
|
|
};
|
|
|
|
|
2021-11-18 19:48:47 +01:00
|
|
|
static_assert(kBlockSizeAlign >= alignof(FreeBlockInfo));
|
|
|
|
static_assert(kBlockSizeMin == sizeof(FreeBlockInfo));
|
|
|
|
|
2014-07-25 00:33:25 +02:00
|
|
|
LinkerBlockAllocator::LinkerBlockAllocator(size_t block_size)
|
2021-11-18 19:48:47 +01:00
|
|
|
: block_size_(__BIONIC_ALIGN(MAX(block_size, kBlockSizeMin), kBlockSizeAlign)),
|
|
|
|
page_list_(nullptr),
|
|
|
|
free_block_list_(nullptr),
|
|
|
|
allocated_(0) {}
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
void* LinkerBlockAllocator::alloc() {
|
|
|
|
if (free_block_list_ == nullptr) {
|
|
|
|
create_new_page();
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(free_block_list_);
|
|
|
|
if (block_info->num_free_blocks > 1) {
|
|
|
|
FreeBlockInfo* next_block_info = reinterpret_cast<FreeBlockInfo*>(
|
|
|
|
reinterpret_cast<char*>(free_block_list_) + block_size_);
|
|
|
|
next_block_info->next_block = block_info->next_block;
|
|
|
|
next_block_info->num_free_blocks = block_info->num_free_blocks - 1;
|
|
|
|
free_block_list_ = next_block_info;
|
|
|
|
} else {
|
|
|
|
free_block_list_ = block_info->next_block;
|
|
|
|
}
|
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
memset(block_info, 0, block_size_);
|
2014-05-06 01:49:04 +02:00
|
|
|
|
2019-01-30 05:23:16 +01:00
|
|
|
++allocated_;
|
|
|
|
|
2014-05-06 01:49:04 +02:00
|
|
|
return block_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkerBlockAllocator::free(void* block) {
|
|
|
|
if (block == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-10 23:43:50 +01:00
|
|
|
LinkerBlockAllocatorPage* page = find_page(block);
|
2020-06-10 23:48:06 +02:00
|
|
|
CHECK(page != nullptr);
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
ssize_t offset = reinterpret_cast<uint8_t*>(block) - page->bytes;
|
2020-06-10 23:48:06 +02:00
|
|
|
CHECK((offset % block_size_) == 0);
|
2014-05-06 01:49:04 +02:00
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
memset(block, 0, block_size_);
|
|
|
|
|
2014-05-06 01:49:04 +02:00
|
|
|
FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(block);
|
|
|
|
|
|
|
|
block_info->next_block = free_block_list_;
|
|
|
|
block_info->num_free_blocks = 1;
|
|
|
|
|
|
|
|
free_block_list_ = block_info;
|
2019-01-30 05:23:16 +01:00
|
|
|
|
|
|
|
--allocated_;
|
2014-05-06 01:49:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinkerBlockAllocator::protect_all(int prot) {
|
2015-03-10 23:43:50 +01:00
|
|
|
for (LinkerBlockAllocatorPage* page = page_list_; page != nullptr; page = page->next) {
|
2019-01-23 23:21:58 +01:00
|
|
|
if (mprotect(page, kAllocateSize, prot) == -1) {
|
2020-06-10 23:48:06 +02:00
|
|
|
async_safe_fatal("mprotect(%p, %zu, %d) failed: %m", page, kAllocateSize, prot);
|
2014-05-06 01:49:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkerBlockAllocator::create_new_page() {
|
2019-01-23 23:21:58 +01:00
|
|
|
static_assert(sizeof(LinkerBlockAllocatorPage) == kAllocateSize,
|
2016-01-21 19:55:40 +01:00
|
|
|
"Invalid sizeof(LinkerBlockAllocatorPage)");
|
|
|
|
|
2015-03-10 23:43:50 +01:00
|
|
|
LinkerBlockAllocatorPage* page = reinterpret_cast<LinkerBlockAllocatorPage*>(
|
2019-01-23 23:21:58 +01:00
|
|
|
mmap(nullptr, kAllocateSize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0));
|
2020-06-10 23:48:06 +02:00
|
|
|
CHECK(page != MAP_FAILED);
|
2014-08-09 01:57:15 +02:00
|
|
|
|
2019-01-23 23:21:58 +01:00
|
|
|
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, page, kAllocateSize, "linker_alloc");
|
2014-08-09 01:57:15 +02:00
|
|
|
|
2014-05-06 01:49:04 +02:00
|
|
|
FreeBlockInfo* first_block = reinterpret_cast<FreeBlockInfo*>(page->bytes);
|
|
|
|
first_block->next_block = free_block_list_;
|
2019-03-12 01:27:52 +01:00
|
|
|
first_block->num_free_blocks = sizeof(page->bytes) / block_size_;
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
free_block_list_ = first_block;
|
|
|
|
|
|
|
|
page->next = page_list_;
|
|
|
|
page_list_ = page;
|
|
|
|
}
|
|
|
|
|
2015-03-10 23:43:50 +01:00
|
|
|
LinkerBlockAllocatorPage* LinkerBlockAllocator::find_page(void* block) {
|
2020-06-10 23:48:06 +02:00
|
|
|
CHECK(block != nullptr);
|
2014-05-06 01:49:04 +02:00
|
|
|
|
2015-03-10 23:43:50 +01:00
|
|
|
LinkerBlockAllocatorPage* page = page_list_;
|
2014-05-06 01:49:04 +02:00
|
|
|
while (page != nullptr) {
|
2014-05-14 21:52:57 +02:00
|
|
|
const uint8_t* page_ptr = reinterpret_cast<const uint8_t*>(page);
|
2019-01-23 23:21:58 +01:00
|
|
|
if (block >= (page_ptr + sizeof(page->next)) && block < (page_ptr + kAllocateSize)) {
|
2014-05-06 01:49:04 +02:00
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
page = page->next;
|
|
|
|
}
|
|
|
|
|
2020-06-10 23:48:06 +02:00
|
|
|
async_safe_fatal("couldn't find page for %p", block);
|
2014-05-06 01:49:04 +02:00
|
|
|
}
|
2019-01-30 05:23:16 +01:00
|
|
|
|
|
|
|
void LinkerBlockAllocator::purge() {
|
|
|
|
if (allocated_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkerBlockAllocatorPage* page = page_list_;
|
|
|
|
while (page) {
|
|
|
|
LinkerBlockAllocatorPage* next = page->next;
|
|
|
|
munmap(page, kAllocateSize);
|
|
|
|
page = next;
|
|
|
|
}
|
|
|
|
page_list_ = nullptr;
|
|
|
|
free_block_list_ = nullptr;
|
|
|
|
}
|