1 /*
2 * Copyright (C) 2013 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 <stdlib.h>
30 #include <string.h>
31 #include <sys/mman.h>
32 #include <sys/param.h>
33
34 #include <gtest/gtest.h>
35
36 #include "linker_block_allocator.h"
37
38 #include <unistd.h>
39
40 namespace {
41
42 struct test_struct_nominal {
43 void* pointer;
44 ssize_t value;
45 };
46
47 /*
48 * this one has size below kBlockSizeAlign
49 */
50 struct test_struct_small {
51 char str[3];
52 };
53
54 struct test_struct_max_align {
55 char str[16];
56 } __attribute__((aligned(16)));
57
58 /*
59 * 1009 byte struct (1009 is prime)
60 */
61 struct test_struct_larger {
62 char str[1009];
63 };
64
65 static size_t kPageSize = sysconf(_SC_PAGE_SIZE);
66
67 template <typename Element>
linker_allocator_test_helper()68 void linker_allocator_test_helper() {
69 LinkerTypeAllocator<Element> allocator;
70
71 Element* ptr1 = allocator.alloc();
72 ASSERT_TRUE(ptr1 != nullptr);
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));
78 ASSERT_TRUE(ptr2 != nullptr);
79
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));
83
84 allocator.free(ptr1);
85 allocator.free(ptr2);
86 }
87
88 }; // anonymous namespace
89
TEST(linker_allocator,test_nominal)90 TEST(linker_allocator, test_nominal) {
91 linker_allocator_test_helper<test_struct_nominal>();
92 }
93
TEST(linker_allocator,test_small)94 TEST(linker_allocator, test_small) {
95 linker_allocator_test_helper<test_struct_small>();
96 }
97
TEST(linker_allocator,test_max_align)98 TEST(linker_allocator, test_max_align) {
99 linker_allocator_test_helper<test_struct_max_align>();
100 }
101
TEST(linker_allocator,test_larger)102 TEST(linker_allocator, test_larger) {
103 linker_allocator_test_helper<test_struct_larger>();
104
105 LinkerTypeAllocator<test_struct_larger> allocator;
106
107 // lets allocate until we reach next page.
108 size_t n = kPageSize / sizeof(test_struct_larger) + 1;
109
110 for (size_t i=0; i<n; ++i) {
111 ASSERT_TRUE(allocator.alloc() != nullptr);
112 }
113
114 test_struct_larger* ptr_to_free = allocator.alloc();
115 ASSERT_TRUE(ptr_to_free != nullptr);
116 }
117
protect_all()118 static void protect_all() {
119 LinkerTypeAllocator<test_struct_larger> allocator;
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
133 page2_ptr->str[23] = 27;
134 page1_ptr->str[13] = 11;
135
136 allocator.protect_all(PROT_READ);
137 fprintf(stderr, "trying to access protected page");
138
139 // this should result in segmentation fault
140 page1_ptr->str[11] = 7;
141 }
142
TEST(linker_allocator,test_protect)143 TEST(linker_allocator, test_protect) {
144 testing::FLAGS_gtest_death_test_style = "threadsafe";
145 ASSERT_EXIT(protect_all(), testing::KilledBySignal(SIGSEGV), "trying to access protected page");
146 }
147