1 /*
2 * Copyright (C) 2018 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 <stdint.h>
18 #include <sys/mman.h>
19
20 #include <thread>
21
22 #include <gtest/gtest.h>
23
24 #include <unwindstack/MapInfo.h>
25 #include <unwindstack/Maps.h>
26
27 #include "ElfFake.h"
28
29 namespace unwindstack {
30
TEST(MapInfoTest,maps_constructor_const_char)31 TEST(MapInfoTest, maps_constructor_const_char) {
32 auto prev_map = MapInfo::Create(0, 0, 0, 0, "");
33 auto map_info = MapInfo::Create(prev_map, 1, 2, 3, 4, "map");
34
35 EXPECT_EQ(prev_map.get(), map_info->prev_map().get());
36 EXPECT_EQ(1UL, map_info->start());
37 EXPECT_EQ(2UL, map_info->end());
38 EXPECT_EQ(3UL, map_info->offset());
39 EXPECT_EQ(4UL, map_info->flags());
40 EXPECT_EQ("map", map_info->name());
41 EXPECT_EQ(UINT64_MAX, map_info->load_bias());
42 EXPECT_EQ(0UL, map_info->elf_offset());
43 EXPECT_TRUE(map_info->elf().get() == nullptr);
44 }
45
TEST(MapInfoTest,maps_constructor_string)46 TEST(MapInfoTest, maps_constructor_string) {
47 std::string name("string_map");
48 auto prev_map = MapInfo::Create(0, 0, 0, 0, "");
49 auto map_info = MapInfo::Create(prev_map, 1, 2, 3, 4, name);
50
51 EXPECT_EQ(prev_map, map_info->prev_map());
52 EXPECT_EQ(1UL, map_info->start());
53 EXPECT_EQ(2UL, map_info->end());
54 EXPECT_EQ(3UL, map_info->offset());
55 EXPECT_EQ(4UL, map_info->flags());
56 EXPECT_EQ("string_map", map_info->name());
57 EXPECT_EQ(UINT64_MAX, map_info->load_bias());
58 EXPECT_EQ(0UL, map_info->elf_offset());
59 EXPECT_TRUE(map_info->elf().get() == nullptr);
60 }
61
TEST(MapInfoTest,real_map_check)62 TEST(MapInfoTest, real_map_check) {
63 auto map1 = MapInfo::Create(0, 0x1000, 0, PROT_READ, "fake.so");
64 auto map2 = MapInfo::Create(map1, 0, 0, 0, 0, "");
65 auto map3 = MapInfo::Create(map2, 0x1000, 0x2000, 0x1000, PROT_READ | PROT_EXEC, "fake.so");
66
67 EXPECT_EQ(nullptr, map1->prev_map());
68 EXPECT_EQ(nullptr, map1->GetPrevRealMap());
69 EXPECT_EQ(map2, map1->next_map());
70 EXPECT_EQ(map3, map1->GetNextRealMap());
71
72 EXPECT_EQ(map1, map2->prev_map());
73 EXPECT_EQ(nullptr, map2->GetPrevRealMap());
74 EXPECT_EQ(map3, map2->next_map());
75 EXPECT_EQ(nullptr, map2->GetNextRealMap());
76
77 EXPECT_EQ(map2, map3->prev_map());
78 EXPECT_EQ(map1, map3->GetPrevRealMap());
79 EXPECT_EQ(nullptr, map3->next_map());
80 EXPECT_EQ(nullptr, map3->GetNextRealMap());
81
82 // Verify that if the middle map is not blank, then the Get{Next,Prev}RealMap
83 // functions return nullptrs.
84 map2->set_offset(1);
85 EXPECT_EQ(nullptr, map1->GetPrevRealMap());
86 EXPECT_EQ(nullptr, map1->GetNextRealMap());
87 EXPECT_EQ(nullptr, map3->GetPrevRealMap());
88 EXPECT_EQ(nullptr, map3->GetNextRealMap());
89 map2->set_offset(0);
90 EXPECT_EQ(map3, map1->GetNextRealMap());
91
92 map2->set_flags(1);
93 EXPECT_EQ(nullptr, map1->GetPrevRealMap());
94 EXPECT_EQ(nullptr, map1->GetNextRealMap());
95 EXPECT_EQ(nullptr, map3->GetPrevRealMap());
96 EXPECT_EQ(nullptr, map3->GetNextRealMap());
97 map2->set_flags(0);
98 EXPECT_EQ(map3, map1->GetNextRealMap());
99
100 map2->set_name("something");
101 EXPECT_EQ(nullptr, map1->GetPrevRealMap());
102 EXPECT_EQ(nullptr, map1->GetNextRealMap());
103 EXPECT_EQ(nullptr, map3->GetPrevRealMap());
104 EXPECT_EQ(nullptr, map3->GetNextRealMap());
105 map2->set_name("");
106 EXPECT_EQ(map3, map1->GetNextRealMap());
107
108 // Verify if the map has the name [page size compat] it's still considered blank.
109 map2->set_name("[page size compat]");
110 EXPECT_EQ(nullptr, map1->GetPrevRealMap());
111 EXPECT_EQ(map3, map1->GetNextRealMap());
112 EXPECT_EQ(map1, map3->GetPrevRealMap());
113 EXPECT_EQ(nullptr, map3->GetNextRealMap());
114 map2->set_name("");
115 EXPECT_EQ(map3, map1->GetNextRealMap());
116
117 // Verify that if the Get{Next,Prev}RealMap names must match.
118 map1->set_name("another");
119 EXPECT_EQ(nullptr, map1->GetPrevRealMap());
120 EXPECT_EQ(nullptr, map1->GetNextRealMap());
121 EXPECT_EQ(nullptr, map3->GetPrevRealMap());
122 EXPECT_EQ(nullptr, map3->GetNextRealMap());
123 map1->set_name("fake.so");
124 EXPECT_EQ(map3, map1->GetNextRealMap());
125
126 map3->set_name("another");
127 EXPECT_EQ(nullptr, map1->GetPrevRealMap());
128 EXPECT_EQ(nullptr, map1->GetNextRealMap());
129 EXPECT_EQ(nullptr, map3->GetPrevRealMap());
130 EXPECT_EQ(nullptr, map3->GetNextRealMap());
131 map3->set_name("fake.so");
132 EXPECT_EQ(map3, map1->GetNextRealMap());
133 }
134
TEST(MapInfoTest,get_function_name)135 TEST(MapInfoTest, get_function_name) {
136 std::shared_ptr<Memory> empty;
137 ElfFake* elf = new ElfFake(empty);
138 ElfInterfaceFake* interface = new ElfInterfaceFake(empty);
139 elf->FakeSetInterface(interface);
140 interface->FakePushFunctionData(FunctionData("function", 1000));
141
142 auto map_info = MapInfo::Create(1, 2, 3, 4, "");
143 map_info->set_elf(elf);
144
145 SharedString name;
146 uint64_t offset;
147 ASSERT_TRUE(map_info->GetFunctionName(1000, &name, &offset));
148 EXPECT_EQ("function", name);
149 EXPECT_EQ(1000UL, offset);
150 }
151
TEST(MapInfoTest,multiple_thread_get_elf_fields)152 TEST(MapInfoTest, multiple_thread_get_elf_fields) {
153 auto map_info = MapInfo::Create(0, 0, 0, 0, "");
154
155 static constexpr size_t kNumConcurrentThreads = 100;
156 MapInfo::ElfFields* elf_fields[kNumConcurrentThreads];
157
158 std::atomic_bool wait;
159 wait = true;
160 // Create all of the threads and have them do the call at the same time
161 // to make it likely that a race will occur.
162 std::vector<std::thread*> threads;
163 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
164 std::thread* thread = new std::thread([i, &wait, &map_info, &elf_fields]() {
165 while (wait)
166 ;
167 elf_fields[i] = &map_info->GetElfFields();
168 });
169 threads.push_back(thread);
170 }
171
172 // Set them all going and wait for the threads to finish.
173 wait = false;
174 for (auto thread : threads) {
175 thread->join();
176 delete thread;
177 }
178
179 // Now verify that all of elf fields are exactly the same and valid.
180 MapInfo::ElfFields* expected_elf_fields = &map_info->GetElfFields();
181 ASSERT_TRUE(expected_elf_fields != nullptr);
182 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
183 EXPECT_EQ(expected_elf_fields, elf_fields[i]) << "Thread " << i << " mismatched.";
184 }
185 }
186
TEST(MapInfoTest,elf_file_not_readable)187 TEST(MapInfoTest, elf_file_not_readable) {
188 auto map_info_readable = MapInfo::Create(0, 0x1000, 0, PROT_READ, "fake.so");
189 map_info_readable->set_memory_backed_elf(true);
190 ASSERT_TRUE(map_info_readable->ElfFileNotReadable());
191
192 auto map_info_no_name = MapInfo::Create(0, 0x1000, 0, PROT_READ, "");
193 map_info_no_name->set_memory_backed_elf(true);
194 ASSERT_FALSE(map_info_no_name->ElfFileNotReadable());
195
196 auto map_info_bracket = MapInfo::Create(0, 0x2000, 0, PROT_READ, "[vdso]");
197 map_info_bracket->set_memory_backed_elf(true);
198 ASSERT_FALSE(map_info_bracket->ElfFileNotReadable());
199
200 auto map_info_memfd = MapInfo::Create(0, 0x3000, 0, PROT_READ, "/memfd:jit-cache");
201 map_info_memfd->set_memory_backed_elf(true);
202 ASSERT_FALSE(map_info_memfd->ElfFileNotReadable());
203 }
204
205 } // namespace unwindstack
206