Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 3 | * All rights reserved. |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 4 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 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. |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 14 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 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. |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 29 | #pragma once |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 30 | |
| 31 | #include <stdlib.h> |
| 32 | #include <limits.h> |
Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 33 | |
| 34 | #include <android-base/macros.h> |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 35 | |
Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame^] | 36 | static constexpr size_t kBlockSizeAlign = sizeof(void*); |
| 37 | static constexpr size_t kBlockSizeMin = sizeof(void*) * 2; |
| 38 | |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 39 | struct LinkerBlockAllocatorPage; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 40 | |
| 41 | /* |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 42 | * This class is a non-template version of the LinkerTypeAllocator |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 43 | * It keeps code inside .cpp file by keeping the interface |
| 44 | * template-free. |
| 45 | * |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 46 | * Please use LinkerTypeAllocator<type> where possible (everywhere). |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 47 | */ |
| 48 | class LinkerBlockAllocator { |
| 49 | public: |
Dmitriy Ivanov | 4151ea7 | 2014-07-24 15:33:25 -0700 | [diff] [blame] | 50 | explicit LinkerBlockAllocator(size_t block_size); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 51 | |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 52 | void* alloc(); |
| 53 | void free(void* block); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 54 | void protect_all(int prot); |
| 55 | |
Vic Yang | bb7e123 | 2019-01-29 20:23:16 -0800 | [diff] [blame] | 56 | // Purge all pages if all previously allocated blocks have been freed. |
| 57 | void purge(); |
| 58 | |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 59 | private: |
| 60 | void create_new_page(); |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 61 | LinkerBlockAllocatorPage* find_page(void* block); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 62 | |
| 63 | size_t block_size_; |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 64 | LinkerBlockAllocatorPage* page_list_; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 65 | void* free_block_list_; |
Vic Yang | bb7e123 | 2019-01-29 20:23:16 -0800 | [diff] [blame] | 66 | size_t allocated_; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 67 | |
| 68 | DISALLOW_COPY_AND_ASSIGN(LinkerBlockAllocator); |
| 69 | }; |
| 70 | |
| 71 | /* |
| 72 | * A simple allocator for the dynamic linker. An allocator allocates instances |
| 73 | * of a single fixed-size type. Allocations are backed by page-sized private |
| 74 | * anonymous mmaps. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 75 | * |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 76 | * The differences between this allocator and BionicAllocator are: |
| 77 | * 1. This allocator manages space more efficiently. BionicAllocator operates in |
| 78 | * power-of-two sized blocks up to 1k, when this implementation splits the |
| 79 | * page to aligned size of structure; For example for structures with size |
| 80 | * 513 this allocator will use 516 (520 for lp64) bytes of data where |
| 81 | * generalized implementation is going to use 1024 sized blocks. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 82 | * |
Vic Yang | 7bb60fc | 2019-01-28 23:04:40 +0000 | [diff] [blame] | 83 | * 2. This allocator does not munmap allocated memory, where BionicAllocator does. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 84 | * |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 85 | * 3. This allocator provides mprotect services to the user, where BionicAllocator |
| 86 | * always treats its memory as READ|WRITE. |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 87 | */ |
| 88 | template<typename T> |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 89 | class LinkerTypeAllocator { |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 90 | public: |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 91 | LinkerTypeAllocator() : block_allocator_(sizeof(T)) {} |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 92 | T* alloc() { return reinterpret_cast<T*>(block_allocator_.alloc()); } |
| 93 | void free(T* t) { block_allocator_.free(t); } |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 94 | void protect_all(int prot) { block_allocator_.protect_all(prot); } |
| 95 | private: |
| 96 | LinkerBlockAllocator block_allocator_; |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 97 | DISALLOW_COPY_AND_ASSIGN(LinkerTypeAllocator); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 98 | }; |