blob: ccbdce6f0315c491d5fc3a2e8266b8e982770432 [file] [log] [blame]
Dmitriy Ivanovd597d262014-05-05 16:49:04 -07001/*
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
23#include "../linker_allocator.h"
24
25#include <unistd.h>
26
27namespace {
28
29struct 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 */
37struct test_struct_small {
38 char dummy_str[5];
39};
40
41/*
42 * 1009 byte struct (1009 is prime)
43 */
44struct test_struct_larger {
45 char dummy_str[1009];
46};
47
48static size_t kPageSize = sysconf(_SC_PAGE_SIZE);
49};
50
51TEST(linker_allocator, test_nominal) {
52 LinkerAllocator<test_struct_nominal> allocator;
53 allocator.init();
54
55 test_struct_nominal* ptr1 = allocator.alloc();
56 ASSERT_TRUE(ptr1 != nullptr);
57 test_struct_nominal* ptr2 = allocator.alloc();
58 ASSERT_TRUE(ptr2 != nullptr);
59 // they should be next to each other.
60 ASSERT_EQ(ptr1+1, ptr2);
61
62 ptr1->value = 42;
63
64 allocator.protect_page(ptr1, PROT_READ);
65
66 allocator.free(ptr1);
67 allocator.free(ptr2);
68}
69
70TEST(linker_allocator, test_small) {
71 LinkerAllocator<test_struct_small> allocator;
72 allocator.init();
73
74 char* ptr1 = reinterpret_cast<char*>(allocator.alloc());
75 char* ptr2 = reinterpret_cast<char*>(allocator.alloc());
76
77 ASSERT_TRUE(ptr1 != nullptr);
78 ASSERT_TRUE(ptr2 != nullptr);
79 ASSERT_EQ(ptr1+2*sizeof(void*), ptr2);
80}
81
82TEST(linker_allocator, test_larger) {
83 LinkerAllocator<test_struct_larger> allocator;
84 allocator.init();
85
86 test_struct_larger* ptr1 = allocator.alloc();
87 test_struct_larger* ptr2 = allocator.alloc();
88
89 ASSERT_TRUE(ptr1 != nullptr);
90 ASSERT_TRUE(ptr2 != nullptr);
91
92 ASSERT_EQ(ptr1+1, ptr2);
93
94 allocator.protect_page(ptr2, PROT_READ);
95
96 // lets allocate until we reach next page.
97 size_t n = kPageSize/sizeof(test_struct_larger) + 1 - 2;
98
99 for (size_t i=0; i<n; ++i) {
100 ASSERT_TRUE(allocator.alloc() != nullptr);
101 }
102
103}
104
105static void protect_one_page() {
106 LinkerAllocator<test_struct_larger> allocator;
107 allocator.init();
108
109 // number of allocs to reach the end of first page
110 size_t n = kPageSize/sizeof(test_struct_larger) - 1;
111 test_struct_larger* page1_ptr = allocator.alloc();
112
113 for (size_t i=0; i<n; ++i) {
114 allocator.alloc();
115 }
116
117 test_struct_larger* page2_ptr = allocator.alloc();
118
119 allocator.protect_page(page2_ptr, PROT_READ);
120
121 // check that we still have access to page1
122 page1_ptr->dummy_str[17] = 52;
123
124 fprintf(stderr, "trying to access protected page");
125
126 // this should result in segmentation fault
127 page2_ptr->dummy_str[12] = 3;
128}
129
130static void protect_all() {
131 LinkerAllocator<test_struct_larger> allocator;
132 allocator.init();
133
134 // number of allocs to reach the end of first page
135 size_t n = kPageSize/sizeof(test_struct_larger) - 1;
136 test_struct_larger* page1_ptr = allocator.alloc();
137
138 for (size_t i=0; i<n; ++i) {
139 allocator.alloc();
140 }
141
142 test_struct_larger* page2_ptr = allocator.alloc();
143 allocator.protect_all(PROT_READ);
144 allocator.protect_all(PROT_READ | PROT_WRITE);
145 // check access
146 page2_ptr->dummy_str[23] = 27;
147 page1_ptr->dummy_str[13] = 11;
148
149 allocator.protect_all(PROT_READ);
150 fprintf(stderr, "trying to access protected page");
151
152 // this should result in segmentation fault
153 page1_ptr->dummy_str[11] = 7;
154}
155
156TEST(linker_allocator, test_protect) {
157 testing::FLAGS_gtest_death_test_style = "threadsafe";
158 ASSERT_EXIT(protect_one_page(), testing::KilledBySignal(SIGSEGV), "trying to access protected page");
159 ASSERT_EXIT(protect_all(), testing::KilledBySignal(SIGSEGV), "trying to access protected page");
160}
161