| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2013 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 |  | 
|  | 29 | #include <stdlib.h> | 
|  | 30 | #include <string.h> | 
|  | 31 | #include <sys/mman.h> | 
| Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame] | 32 | #include <sys/param.h> | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 33 |  | 
|  | 34 | #include <gtest/gtest.h> | 
|  | 35 |  | 
| Elliott Hughes | 15a2b7b | 2019-02-15 13:48:38 -0800 | [diff] [blame] | 36 | #include "linker_block_allocator.h" | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | #include <unistd.h> | 
|  | 39 |  | 
|  | 40 | namespace { | 
|  | 41 |  | 
|  | 42 | struct test_struct_nominal { | 
|  | 43 | void* pointer; | 
|  | 44 | ssize_t value; | 
|  | 45 | }; | 
|  | 46 |  | 
|  | 47 | /* | 
| Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame] | 48 | * this one has size below kBlockSizeAlign | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 49 | */ | 
|  | 50 | struct test_struct_small { | 
| Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame] | 51 | char str[3]; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 52 | }; | 
|  | 53 |  | 
| Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame] | 54 | struct test_struct_max_align { | 
|  | 55 | char str[16]; | 
|  | 56 | } __attribute__((aligned(16))); | 
|  | 57 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 58 | /* | 
|  | 59 | * 1009 byte struct (1009 is prime) | 
|  | 60 | */ | 
|  | 61 | struct test_struct_larger { | 
| Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 62 | char str[1009]; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 63 | }; | 
|  | 64 |  | 
|  | 65 | static size_t kPageSize = sysconf(_SC_PAGE_SIZE); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 66 |  | 
| Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame] | 67 | template <typename Element> | 
|  | 68 | void linker_allocator_test_helper() { | 
|  | 69 | LinkerTypeAllocator<Element> allocator; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 70 |  | 
| Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame] | 71 | Element* ptr1 = allocator.alloc(); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 72 | ASSERT_TRUE(ptr1 != nullptr); | 
| Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame] | 73 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % kBlockSizeAlign); | 
|  | 74 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % alignof(Element)); | 
|  | 75 | Element* ptr2 = allocator.alloc(); | 
|  | 76 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % kBlockSizeAlign); | 
|  | 77 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % alignof(Element)); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(ptr2 != nullptr); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 79 |  | 
| Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame] | 80 | // they should be next to each other. | 
|  | 81 | size_t dist = __BIONIC_ALIGN(MAX(sizeof(Element), kBlockSizeMin), kBlockSizeAlign); | 
|  | 82 | ASSERT_EQ(reinterpret_cast<uint8_t*>(ptr1) + dist, reinterpret_cast<uint8_t*>(ptr2)); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 83 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 84 | allocator.free(ptr1); | 
|  | 85 | allocator.free(ptr2); | 
|  | 86 | } | 
|  | 87 |  | 
| Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame] | 88 | };  // anonymous namespace | 
|  | 89 |  | 
|  | 90 | TEST(linker_allocator, test_nominal) { | 
|  | 91 | linker_allocator_test_helper<test_struct_nominal>(); | 
|  | 92 | } | 
|  | 93 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 94 | TEST(linker_allocator, test_small) { | 
| Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame] | 95 | linker_allocator_test_helper<test_struct_small>(); | 
|  | 96 | } | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 97 |  | 
| Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame] | 98 | TEST(linker_allocator, test_max_align) { | 
|  | 99 | linker_allocator_test_helper<test_struct_max_align>(); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
|  | 102 | TEST(linker_allocator, test_larger) { | 
| Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame] | 103 | linker_allocator_test_helper<test_struct_larger>(); | 
|  | 104 |  | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 105 | LinkerTypeAllocator<test_struct_larger> allocator; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 106 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 107 | // lets allocate until we reach next page. | 
| Eric Miao | 08cf949 | 2021-11-18 18:48:47 +0000 | [diff] [blame] | 108 | size_t n = kPageSize / sizeof(test_struct_larger) + 1; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 109 |  | 
|  | 110 | for (size_t i=0; i<n; ++i) { | 
|  | 111 | ASSERT_TRUE(allocator.alloc() != nullptr); | 
|  | 112 | } | 
|  | 113 |  | 
| Dmitriy Ivanov | 1079406 | 2014-05-14 12:52:57 -0700 | [diff] [blame] | 114 | test_struct_larger* ptr_to_free = allocator.alloc(); | 
|  | 115 | ASSERT_TRUE(ptr_to_free != nullptr); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 118 | static void protect_all() { | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 119 | LinkerTypeAllocator<test_struct_larger> allocator; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 120 |  | 
|  | 121 | // number of allocs to reach the end of first page | 
|  | 122 | size_t n = kPageSize/sizeof(test_struct_larger) - 1; | 
|  | 123 | test_struct_larger* page1_ptr = allocator.alloc(); | 
|  | 124 |  | 
|  | 125 | for (size_t i=0; i<n; ++i) { | 
|  | 126 | allocator.alloc(); | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | test_struct_larger* page2_ptr = allocator.alloc(); | 
|  | 130 | allocator.protect_all(PROT_READ); | 
|  | 131 | allocator.protect_all(PROT_READ | PROT_WRITE); | 
|  | 132 | // check access | 
| Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 133 | page2_ptr->str[23] = 27; | 
|  | 134 | page1_ptr->str[13] = 11; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 135 |  | 
|  | 136 | allocator.protect_all(PROT_READ); | 
|  | 137 | fprintf(stderr, "trying to access protected page"); | 
|  | 138 |  | 
|  | 139 | // this should result in segmentation fault | 
| Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 140 | page1_ptr->str[11] = 7; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 141 | } | 
|  | 142 |  | 
|  | 143 | TEST(linker_allocator, test_protect) { | 
|  | 144 | testing::FLAGS_gtest_death_test_style = "threadsafe"; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 145 | ASSERT_EXIT(protect_all(), testing::KilledBySignal(SIGSEGV), "trying to access protected page"); | 
|  | 146 | } |