| 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> | 
|  | 32 |  | 
|  | 33 | #include <gtest/gtest.h> | 
|  | 34 |  | 
| Dmitriy Ivanov | c9ce70d | 2015-03-10 15:30:26 -0700 | [diff] [blame] | 35 | #include "../linker_block_allocator.h" | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 36 |  | 
|  | 37 | #include <unistd.h> | 
|  | 38 |  | 
|  | 39 | namespace { | 
|  | 40 |  | 
|  | 41 | struct test_struct_nominal { | 
|  | 42 | void* pointer; | 
|  | 43 | ssize_t value; | 
|  | 44 | }; | 
|  | 45 |  | 
|  | 46 | /* | 
|  | 47 | * this one has size below allocator cap which is 2*sizeof(void*) | 
|  | 48 | */ | 
|  | 49 | struct test_struct_small { | 
|  | 50 | char dummy_str[5]; | 
|  | 51 | }; | 
|  | 52 |  | 
|  | 53 | /* | 
|  | 54 | * 1009 byte struct (1009 is prime) | 
|  | 55 | */ | 
|  | 56 | struct test_struct_larger { | 
|  | 57 | char dummy_str[1009]; | 
|  | 58 | }; | 
|  | 59 |  | 
|  | 60 | static size_t kPageSize = sysconf(_SC_PAGE_SIZE); | 
|  | 61 | }; | 
|  | 62 |  | 
|  | 63 | TEST(linker_allocator, test_nominal) { | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 64 | LinkerTypeAllocator<test_struct_nominal> allocator; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 65 |  | 
|  | 66 | test_struct_nominal* ptr1 = allocator.alloc(); | 
|  | 67 | ASSERT_TRUE(ptr1 != nullptr); | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 68 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % 16); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 69 | test_struct_nominal* ptr2 = allocator.alloc(); | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 70 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % 16); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 71 | ASSERT_TRUE(ptr2 != nullptr); | 
|  | 72 | // they should be next to each other. | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 73 | ASSERT_EQ(reinterpret_cast<uint8_t*>(ptr1)+16, reinterpret_cast<uint8_t*>(ptr2)); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 74 |  | 
|  | 75 | ptr1->value = 42; | 
|  | 76 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 77 | allocator.free(ptr1); | 
|  | 78 | allocator.free(ptr2); | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | TEST(linker_allocator, test_small) { | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 82 | LinkerTypeAllocator<test_struct_small> allocator; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 83 |  | 
|  | 84 | char* ptr1 = reinterpret_cast<char*>(allocator.alloc()); | 
|  | 85 | char* ptr2 = reinterpret_cast<char*>(allocator.alloc()); | 
|  | 86 |  | 
|  | 87 | ASSERT_TRUE(ptr1 != nullptr); | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 88 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % 16); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 89 | ASSERT_TRUE(ptr2 != nullptr); | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 90 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % 16); | 
|  | 91 | ASSERT_EQ(ptr1+16, ptr2); // aligned to 16 | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
|  | 94 | TEST(linker_allocator, test_larger) { | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 95 | LinkerTypeAllocator<test_struct_larger> allocator; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 96 |  | 
|  | 97 | test_struct_larger* ptr1 = allocator.alloc(); | 
|  | 98 | test_struct_larger* ptr2 = allocator.alloc(); | 
|  | 99 |  | 
|  | 100 | ASSERT_TRUE(ptr1 != nullptr); | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 101 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % 16); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 102 | ASSERT_TRUE(ptr2 != nullptr); | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 103 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % 16); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 104 |  | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 105 | ASSERT_EQ(reinterpret_cast<uint8_t*>(ptr1) + 1024, reinterpret_cast<uint8_t*>(ptr2)); | 
| 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. | 
|  | 108 | size_t n = kPageSize/sizeof(test_struct_larger) + 1 - 2; | 
|  | 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); | 
|  | 116 | allocator.free(ptr1); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 119 | static void protect_all() { | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 120 | LinkerTypeAllocator<test_struct_larger> allocator; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 121 |  | 
|  | 122 | // number of allocs to reach the end of first page | 
|  | 123 | size_t n = kPageSize/sizeof(test_struct_larger) - 1; | 
|  | 124 | test_struct_larger* page1_ptr = allocator.alloc(); | 
|  | 125 |  | 
|  | 126 | for (size_t i=0; i<n; ++i) { | 
|  | 127 | allocator.alloc(); | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | test_struct_larger* page2_ptr = allocator.alloc(); | 
|  | 131 | allocator.protect_all(PROT_READ); | 
|  | 132 | allocator.protect_all(PROT_READ | PROT_WRITE); | 
|  | 133 | // check access | 
|  | 134 | page2_ptr->dummy_str[23] = 27; | 
|  | 135 | page1_ptr->dummy_str[13] = 11; | 
|  | 136 |  | 
|  | 137 | allocator.protect_all(PROT_READ); | 
|  | 138 | fprintf(stderr, "trying to access protected page"); | 
|  | 139 |  | 
|  | 140 | // this should result in segmentation fault | 
|  | 141 | page1_ptr->dummy_str[11] = 7; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | TEST(linker_allocator, test_protect) { | 
|  | 145 | testing::FLAGS_gtest_death_test_style = "threadsafe"; | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 146 | ASSERT_EXIT(protect_all(), testing::KilledBySignal(SIGSEGV), "trying to access protected page"); | 
|  | 147 | } | 
|  | 148 |  |