• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * All rights reserved.
4  *
5  * 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.
14  *
15  * 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.
27  */
28 
29 #include "linker_block_allocator.h"
30 
31 #include <inttypes.h>
32 #include <string.h>
33 #include <sys/mman.h>
34 #include <sys/prctl.h>
35 #include <unistd.h>
36 
37 #include "linker_debug.h"
38 
39 static constexpr size_t kAllocateSize = PAGE_SIZE * 100;
40 static_assert(kAllocateSize % PAGE_SIZE == 0, "Invalid kAllocateSize.");
41 
42 // the multiplier should be power of 2
round_up(size_t size,size_t multiplier)43 static constexpr size_t round_up(size_t size, size_t multiplier) {
44   return (size + (multiplier - 1)) & ~(multiplier-1);
45 }
46 
47 struct LinkerBlockAllocatorPage {
48   LinkerBlockAllocatorPage* next;
49   uint8_t bytes[kAllocateSize - 16] __attribute__((aligned(16)));
50 };
51 
52 struct FreeBlockInfo {
53   void* next_block;
54   size_t num_free_blocks;
55 };
56 
LinkerBlockAllocator(size_t block_size)57 LinkerBlockAllocator::LinkerBlockAllocator(size_t block_size)
58   : block_size_(
59       round_up(block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size, 16)),
60     page_list_(nullptr),
61     free_block_list_(nullptr),
62     allocated_(0)
63 {}
64 
alloc()65 void* LinkerBlockAllocator::alloc() {
66   if (free_block_list_ == nullptr) {
67     create_new_page();
68   }
69 
70   FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(free_block_list_);
71   if (block_info->num_free_blocks > 1) {
72     FreeBlockInfo* next_block_info = reinterpret_cast<FreeBlockInfo*>(
73       reinterpret_cast<char*>(free_block_list_) + block_size_);
74     next_block_info->next_block = block_info->next_block;
75     next_block_info->num_free_blocks = block_info->num_free_blocks - 1;
76     free_block_list_ = next_block_info;
77   } else {
78     free_block_list_ = block_info->next_block;
79   }
80 
81   memset(block_info, 0, block_size_);
82 
83   ++allocated_;
84 
85   return block_info;
86 }
87 
free(void * block)88 void LinkerBlockAllocator::free(void* block) {
89   if (block == nullptr) {
90     return;
91   }
92 
93   LinkerBlockAllocatorPage* page = find_page(block);
94   CHECK(page != nullptr);
95 
96   ssize_t offset = reinterpret_cast<uint8_t*>(block) - page->bytes;
97   CHECK((offset % block_size_) == 0);
98 
99   memset(block, 0, block_size_);
100 
101   FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(block);
102 
103   block_info->next_block = free_block_list_;
104   block_info->num_free_blocks = 1;
105 
106   free_block_list_ = block_info;
107 
108   --allocated_;
109 }
110 
protect_all(int prot)111 void LinkerBlockAllocator::protect_all(int prot) {
112   for (LinkerBlockAllocatorPage* page = page_list_; page != nullptr; page = page->next) {
113     if (mprotect(page, kAllocateSize, prot) == -1) {
114       async_safe_fatal("mprotect(%p, %zu, %d) failed: %m", page, kAllocateSize, prot);
115     }
116   }
117 }
118 
create_new_page()119 void LinkerBlockAllocator::create_new_page() {
120   static_assert(sizeof(LinkerBlockAllocatorPage) == kAllocateSize,
121                 "Invalid sizeof(LinkerBlockAllocatorPage)");
122 
123   LinkerBlockAllocatorPage* page = reinterpret_cast<LinkerBlockAllocatorPage*>(
124       mmap(nullptr, kAllocateSize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0));
125   CHECK(page != MAP_FAILED);
126 
127   prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, page, kAllocateSize, "linker_alloc");
128 
129   FreeBlockInfo* first_block = reinterpret_cast<FreeBlockInfo*>(page->bytes);
130   first_block->next_block = free_block_list_;
131   first_block->num_free_blocks = sizeof(page->bytes) / block_size_;
132 
133   free_block_list_ = first_block;
134 
135   page->next = page_list_;
136   page_list_ = page;
137 }
138 
find_page(void * block)139 LinkerBlockAllocatorPage* LinkerBlockAllocator::find_page(void* block) {
140   CHECK(block != nullptr);
141 
142   LinkerBlockAllocatorPage* page = page_list_;
143   while (page != nullptr) {
144     const uint8_t* page_ptr = reinterpret_cast<const uint8_t*>(page);
145     if (block >= (page_ptr + sizeof(page->next)) && block < (page_ptr + kAllocateSize)) {
146       return page;
147     }
148 
149     page = page->next;
150   }
151 
152   async_safe_fatal("couldn't find page for %p", block);
153 }
154 
purge()155 void LinkerBlockAllocator::purge() {
156   if (allocated_) {
157     return;
158   }
159 
160   LinkerBlockAllocatorPage* page = page_list_;
161   while (page) {
162     LinkerBlockAllocatorPage* next = page->next;
163     munmap(page, kAllocateSize);
164     page = next;
165   }
166   page_list_ = nullptr;
167   free_block_list_ = nullptr;
168 }
169