Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <sys/mman.h> |
| 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | |
Dmitriy Ivanov | c9ce70d | 2015-03-10 15:30:26 -0700 | [diff] [blame] | 23 | #include "../linker_block_allocator.h" |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 24 | |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | struct test_struct_nominal { |
| 30 | void* pointer; |
| 31 | ssize_t value; |
| 32 | }; |
| 33 | |
| 34 | /* |
| 35 | * this one has size below allocator cap which is 2*sizeof(void*) |
| 36 | */ |
| 37 | struct test_struct_small { |
| 38 | char dummy_str[5]; |
| 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * 1009 byte struct (1009 is prime) |
| 43 | */ |
| 44 | struct test_struct_larger { |
| 45 | char dummy_str[1009]; |
| 46 | }; |
| 47 | |
| 48 | static size_t kPageSize = sysconf(_SC_PAGE_SIZE); |
| 49 | }; |
| 50 | |
| 51 | TEST(linker_allocator, test_nominal) { |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 52 | LinkerTypeAllocator<test_struct_nominal> allocator; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 53 | |
| 54 | test_struct_nominal* ptr1 = allocator.alloc(); |
| 55 | ASSERT_TRUE(ptr1 != nullptr); |
Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame^] | 56 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % 16); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 57 | test_struct_nominal* ptr2 = allocator.alloc(); |
Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame^] | 58 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % 16); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 59 | ASSERT_TRUE(ptr2 != nullptr); |
| 60 | // they should be next to each other. |
Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame^] | 61 | 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] | 62 | |
| 63 | ptr1->value = 42; |
| 64 | |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 65 | allocator.free(ptr1); |
| 66 | allocator.free(ptr2); |
| 67 | } |
| 68 | |
| 69 | TEST(linker_allocator, test_small) { |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 70 | LinkerTypeAllocator<test_struct_small> allocator; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 71 | |
| 72 | char* ptr1 = reinterpret_cast<char*>(allocator.alloc()); |
| 73 | char* ptr2 = reinterpret_cast<char*>(allocator.alloc()); |
| 74 | |
| 75 | ASSERT_TRUE(ptr1 != nullptr); |
Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame^] | 76 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % 16); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 77 | ASSERT_TRUE(ptr2 != nullptr); |
Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame^] | 78 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % 16); |
| 79 | ASSERT_EQ(ptr1+16, ptr2); // aligned to 16 |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TEST(linker_allocator, test_larger) { |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 83 | LinkerTypeAllocator<test_struct_larger> allocator; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 84 | |
| 85 | test_struct_larger* ptr1 = allocator.alloc(); |
| 86 | test_struct_larger* ptr2 = allocator.alloc(); |
| 87 | |
| 88 | ASSERT_TRUE(ptr1 != nullptr); |
Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame^] | 89 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % 16); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 90 | ASSERT_TRUE(ptr2 != nullptr); |
Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame^] | 91 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % 16); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 92 | |
Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame^] | 93 | 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] | 94 | |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 95 | // lets allocate until we reach next page. |
| 96 | size_t n = kPageSize/sizeof(test_struct_larger) + 1 - 2; |
| 97 | |
| 98 | for (size_t i=0; i<n; ++i) { |
| 99 | ASSERT_TRUE(allocator.alloc() != nullptr); |
| 100 | } |
| 101 | |
Dmitriy Ivanov | 1079406 | 2014-05-14 12:52:57 -0700 | [diff] [blame] | 102 | test_struct_larger* ptr_to_free = allocator.alloc(); |
| 103 | ASSERT_TRUE(ptr_to_free != nullptr); |
| 104 | allocator.free(ptr1); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 107 | static void protect_all() { |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 108 | LinkerTypeAllocator<test_struct_larger> allocator; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 109 | |
| 110 | // number of allocs to reach the end of first page |
| 111 | size_t n = kPageSize/sizeof(test_struct_larger) - 1; |
| 112 | test_struct_larger* page1_ptr = allocator.alloc(); |
| 113 | |
| 114 | for (size_t i=0; i<n; ++i) { |
| 115 | allocator.alloc(); |
| 116 | } |
| 117 | |
| 118 | test_struct_larger* page2_ptr = allocator.alloc(); |
| 119 | allocator.protect_all(PROT_READ); |
| 120 | allocator.protect_all(PROT_READ | PROT_WRITE); |
| 121 | // check access |
| 122 | page2_ptr->dummy_str[23] = 27; |
| 123 | page1_ptr->dummy_str[13] = 11; |
| 124 | |
| 125 | allocator.protect_all(PROT_READ); |
| 126 | fprintf(stderr, "trying to access protected page"); |
| 127 | |
| 128 | // this should result in segmentation fault |
| 129 | page1_ptr->dummy_str[11] = 7; |
| 130 | } |
| 131 | |
| 132 | TEST(linker_allocator, test_protect) { |
| 133 | testing::FLAGS_gtest_death_test_style = "threadsafe"; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 134 | ASSERT_EXIT(protect_all(), testing::KilledBySignal(SIGSEGV), "trying to access protected page"); |
| 135 | } |
| 136 | |