1 /*
2 * Copyright (C) 2016 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 <elf.h>
18 #include <errno.h>
19 #include <signal.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/mman.h>
24 #include <sys/ptrace.h>
25 #include <sys/types.h>
26 #include <time.h>
27 #include <unistd.h>
28
29 #include <vector>
30
31 #include <android-base/file.h>
32 #include <android-base/test_utils.h>
33 #include <gtest/gtest.h>
34
35 #include <unwindstack/Memory.h>
36
37 #include "MemoryFake.h"
38 #include "Symbols.h"
39
40 namespace unwindstack {
41
42 template <typename TypeParam>
43 class SymbolsTest : public ::testing::Test {
44 protected:
SetUp()45 void SetUp() override { memory_.Clear(); }
46
InitSym(TypeParam * sym,uint32_t st_value,uint32_t st_size,uint32_t st_name)47 void InitSym(TypeParam* sym, uint32_t st_value, uint32_t st_size, uint32_t st_name) {
48 memset(sym, 0, sizeof(*sym));
49 sym->st_info = STT_FUNC;
50 sym->st_value = st_value;
51 sym->st_size = st_size;
52 sym->st_name = st_name;
53 sym->st_shndx = SHN_COMMON;
54 }
55
56 MemoryFake memory_;
57 };
58 TYPED_TEST_CASE_P(SymbolsTest);
59
TYPED_TEST_P(SymbolsTest,function_bounds_check)60 TYPED_TEST_P(SymbolsTest, function_bounds_check) {
61 Symbols symbols(0x1000, sizeof(TypeParam), sizeof(TypeParam), 0x2000, 0x100);
62
63 TypeParam sym;
64 this->InitSym(&sym, 0x5000, 0x10, 0x40);
65 uint64_t offset = 0x1000;
66 this->memory_.SetMemory(offset, &sym, sizeof(sym));
67
68 std::string fake_name("fake_function");
69 this->memory_.SetMemory(0x2040, fake_name.c_str(), fake_name.size() + 1);
70
71 std::string name;
72 uint64_t func_offset;
73 ASSERT_TRUE(symbols.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset));
74 ASSERT_EQ("fake_function", name);
75 ASSERT_EQ(0U, func_offset);
76
77 name.clear();
78 ASSERT_TRUE(symbols.GetName<TypeParam>(0x500f, &this->memory_, &name, &func_offset));
79 ASSERT_EQ("fake_function", name);
80 ASSERT_EQ(0xfU, func_offset);
81
82 // Check one before and one after the function.
83 ASSERT_FALSE(symbols.GetName<TypeParam>(0x4fff, &this->memory_, &name, &func_offset));
84 ASSERT_FALSE(symbols.GetName<TypeParam>(0x5010, &this->memory_, &name, &func_offset));
85 }
86
TYPED_TEST_P(SymbolsTest,no_symbol)87 TYPED_TEST_P(SymbolsTest, no_symbol) {
88 Symbols symbols(0x1000, sizeof(TypeParam), sizeof(TypeParam), 0x2000, 0x100);
89
90 TypeParam sym;
91 this->InitSym(&sym, 0x5000, 0x10, 0x40);
92 uint64_t offset = 0x1000;
93 this->memory_.SetMemory(offset, &sym, sizeof(sym));
94
95 std::string fake_name("fake_function");
96 this->memory_.SetMemory(0x2040, fake_name.c_str(), fake_name.size() + 1);
97
98 // First verify that we can get the name.
99 std::string name;
100 uint64_t func_offset;
101 ASSERT_TRUE(symbols.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset));
102 ASSERT_EQ("fake_function", name);
103 ASSERT_EQ(0U, func_offset);
104
105 // Now modify the info field so it's no longer a function.
106 sym.st_info = 0;
107 this->memory_.SetMemory(offset, &sym, sizeof(sym));
108 // Clear the cache to force the symbol data to be re-read.
109 symbols.ClearCache();
110 ASSERT_FALSE(symbols.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset));
111
112 // Set the function back, and set the shndx to UNDEF.
113 sym.st_info = STT_FUNC;
114 sym.st_shndx = SHN_UNDEF;
115 this->memory_.SetMemory(offset, &sym, sizeof(sym));
116 // Clear the cache to force the symbol data to be re-read.
117 symbols.ClearCache();
118 ASSERT_FALSE(symbols.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset));
119 }
120
TYPED_TEST_P(SymbolsTest,multiple_entries)121 TYPED_TEST_P(SymbolsTest, multiple_entries) {
122 Symbols symbols(0x1000, sizeof(TypeParam) * 3, sizeof(TypeParam), 0x2000, 0x500);
123
124 TypeParam sym;
125 uint64_t offset = 0x1000;
126 std::string fake_name;
127
128 this->InitSym(&sym, 0x5000, 0x10, 0x40);
129 this->memory_.SetMemory(offset, &sym, sizeof(sym));
130 fake_name = "function_one";
131 this->memory_.SetMemory(0x2040, fake_name.c_str(), fake_name.size() + 1);
132 offset += sizeof(sym);
133
134 this->InitSym(&sym, 0x3004, 0x200, 0x100);
135 this->memory_.SetMemory(offset, &sym, sizeof(sym));
136 fake_name = "function_two";
137 this->memory_.SetMemory(0x2100, fake_name.c_str(), fake_name.size() + 1);
138 offset += sizeof(sym);
139
140 this->InitSym(&sym, 0xa010, 0x20, 0x230);
141 this->memory_.SetMemory(offset, &sym, sizeof(sym));
142 fake_name = "function_three";
143 this->memory_.SetMemory(0x2230, fake_name.c_str(), fake_name.size() + 1);
144
145 std::string name;
146 uint64_t func_offset;
147 ASSERT_TRUE(symbols.GetName<TypeParam>(0x3005, &this->memory_, &name, &func_offset));
148 ASSERT_EQ("function_two", name);
149 ASSERT_EQ(1U, func_offset);
150
151 name.clear();
152 ASSERT_TRUE(symbols.GetName<TypeParam>(0x5004, &this->memory_, &name, &func_offset));
153 ASSERT_EQ("function_one", name);
154 ASSERT_EQ(4U, func_offset);
155
156 name.clear();
157 ASSERT_TRUE(symbols.GetName<TypeParam>(0xa011, &this->memory_, &name, &func_offset));
158 ASSERT_EQ("function_three", name);
159 ASSERT_EQ(1U, func_offset);
160
161 // Reget some of the others to verify getting one function name doesn't
162 // affect any of the next calls.
163 name.clear();
164 ASSERT_TRUE(symbols.GetName<TypeParam>(0x5008, &this->memory_, &name, &func_offset));
165 ASSERT_EQ("function_one", name);
166 ASSERT_EQ(8U, func_offset);
167
168 name.clear();
169 ASSERT_TRUE(symbols.GetName<TypeParam>(0x3008, &this->memory_, &name, &func_offset));
170 ASSERT_EQ("function_two", name);
171 ASSERT_EQ(4U, func_offset);
172
173 name.clear();
174 ASSERT_TRUE(symbols.GetName<TypeParam>(0xa01a, &this->memory_, &name, &func_offset));
175 ASSERT_EQ("function_three", name);
176 ASSERT_EQ(0xaU, func_offset);
177 }
178
TYPED_TEST_P(SymbolsTest,multiple_entries_nonstandard_size)179 TYPED_TEST_P(SymbolsTest, multiple_entries_nonstandard_size) {
180 uint64_t entry_size = sizeof(TypeParam) + 5;
181 Symbols symbols(0x1000, entry_size * 3, entry_size, 0x2000, 0x500);
182
183 TypeParam sym;
184 uint64_t offset = 0x1000;
185 std::string fake_name;
186
187 this->InitSym(&sym, 0x5000, 0x10, 0x40);
188 this->memory_.SetMemory(offset, &sym, sizeof(sym));
189 fake_name = "function_one";
190 this->memory_.SetMemory(0x2040, fake_name.c_str(), fake_name.size() + 1);
191 offset += entry_size;
192
193 this->InitSym(&sym, 0x3004, 0x200, 0x100);
194 this->memory_.SetMemory(offset, &sym, sizeof(sym));
195 fake_name = "function_two";
196 this->memory_.SetMemory(0x2100, fake_name.c_str(), fake_name.size() + 1);
197 offset += entry_size;
198
199 this->InitSym(&sym, 0xa010, 0x20, 0x230);
200 this->memory_.SetMemory(offset, &sym, sizeof(sym));
201 fake_name = "function_three";
202 this->memory_.SetMemory(0x2230, fake_name.c_str(), fake_name.size() + 1);
203
204 std::string name;
205 uint64_t func_offset;
206 ASSERT_TRUE(symbols.GetName<TypeParam>(0x3005, &this->memory_, &name, &func_offset));
207 ASSERT_EQ("function_two", name);
208 ASSERT_EQ(1U, func_offset);
209
210 name.clear();
211 ASSERT_TRUE(symbols.GetName<TypeParam>(0x5004, &this->memory_, &name, &func_offset));
212 ASSERT_EQ("function_one", name);
213 ASSERT_EQ(4U, func_offset);
214
215 name.clear();
216 ASSERT_TRUE(symbols.GetName<TypeParam>(0xa011, &this->memory_, &name, &func_offset));
217 ASSERT_EQ("function_three", name);
218 ASSERT_EQ(1U, func_offset);
219 }
220
TYPED_TEST_P(SymbolsTest,symtab_value_out_of_bounds)221 TYPED_TEST_P(SymbolsTest, symtab_value_out_of_bounds) {
222 Symbols symbols_end_at_100(0x1000, sizeof(TypeParam) * 2, sizeof(TypeParam), 0x2000, 0x100);
223 Symbols symbols_end_at_200(0x1000, sizeof(TypeParam) * 2, sizeof(TypeParam), 0x2000, 0x200);
224
225 TypeParam sym;
226 uint64_t offset = 0x1000;
227
228 this->InitSym(&sym, 0x5000, 0x10, 0xfb);
229 this->memory_.SetMemory(offset, &sym, sizeof(sym));
230 offset += sizeof(sym);
231
232 this->InitSym(&sym, 0x3000, 0x10, 0x100);
233 this->memory_.SetMemory(offset, &sym, sizeof(sym));
234
235 // Put the name across the end of the tab.
236 std::string fake_name("fake_function");
237 this->memory_.SetMemory(0x20fb, fake_name.c_str(), fake_name.size() + 1);
238
239 std::string name;
240 uint64_t func_offset;
241 // Verify that we can get the function name properly for both entries.
242 ASSERT_TRUE(symbols_end_at_200.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset));
243 ASSERT_EQ("fake_function", name);
244 ASSERT_EQ(0U, func_offset);
245 ASSERT_TRUE(symbols_end_at_200.GetName<TypeParam>(0x3000, &this->memory_, &name, &func_offset));
246 ASSERT_EQ("function", name);
247 ASSERT_EQ(0U, func_offset);
248
249 // Now use the symbol table that ends at 0x100.
250 ASSERT_FALSE(symbols_end_at_100.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset));
251 ASSERT_FALSE(symbols_end_at_100.GetName<TypeParam>(0x3000, &this->memory_, &name, &func_offset));
252 }
253
254 // Verify the entire func table is cached.
TYPED_TEST_P(SymbolsTest,symtab_read_cached)255 TYPED_TEST_P(SymbolsTest, symtab_read_cached) {
256 Symbols symbols(0x1000, 3 * sizeof(TypeParam), sizeof(TypeParam), 0xa000, 0x1000);
257
258 TypeParam sym;
259 uint64_t offset = 0x1000;
260
261 // Make sure that these entries are not in ascending order.
262 this->InitSym(&sym, 0x5000, 0x10, 0x100);
263 this->memory_.SetMemory(offset, &sym, sizeof(sym));
264 offset += sizeof(sym);
265
266 this->InitSym(&sym, 0x2000, 0x300, 0x200);
267 this->memory_.SetMemory(offset, &sym, sizeof(sym));
268 offset += sizeof(sym);
269
270 this->InitSym(&sym, 0x1000, 0x100, 0x300);
271 this->memory_.SetMemory(offset, &sym, sizeof(sym));
272 offset += sizeof(sym);
273
274 // Do call that should cache all of the entries (except the string data).
275 std::string name;
276 uint64_t func_offset;
277 ASSERT_FALSE(symbols.GetName<TypeParam>(0x6000, &this->memory_, &name, &func_offset));
278 this->memory_.Clear();
279 ASSERT_FALSE(symbols.GetName<TypeParam>(0x6000, &this->memory_, &name, &func_offset));
280
281 // Clear the memory and only put the symbol data string data in memory.
282 this->memory_.Clear();
283
284 std::string fake_name;
285 fake_name = "first_entry";
286 this->memory_.SetMemory(0xa100, fake_name.c_str(), fake_name.size() + 1);
287 fake_name = "second_entry";
288 this->memory_.SetMemory(0xa200, fake_name.c_str(), fake_name.size() + 1);
289 fake_name = "third_entry";
290 this->memory_.SetMemory(0xa300, fake_name.c_str(), fake_name.size() + 1);
291
292 ASSERT_TRUE(symbols.GetName<TypeParam>(0x5001, &this->memory_, &name, &func_offset));
293 ASSERT_EQ("first_entry", name);
294 ASSERT_EQ(1U, func_offset);
295
296 ASSERT_TRUE(symbols.GetName<TypeParam>(0x2002, &this->memory_, &name, &func_offset));
297 ASSERT_EQ("second_entry", name);
298 ASSERT_EQ(2U, func_offset);
299
300 ASSERT_TRUE(symbols.GetName<TypeParam>(0x1003, &this->memory_, &name, &func_offset));
301 ASSERT_EQ("third_entry", name);
302 ASSERT_EQ(3U, func_offset);
303 }
304
TYPED_TEST_P(SymbolsTest,get_global)305 TYPED_TEST_P(SymbolsTest, get_global) {
306 uint64_t start_offset = 0x1000;
307 uint64_t str_offset = 0xa000;
308 Symbols symbols(start_offset, 4 * sizeof(TypeParam), sizeof(TypeParam), str_offset, 0x1000);
309
310 TypeParam sym;
311 memset(&sym, 0, sizeof(sym));
312 sym.st_shndx = SHN_COMMON;
313 sym.st_info = STT_OBJECT | (STB_GLOBAL << 4);
314 sym.st_name = 0x100;
315 this->memory_.SetMemory(start_offset, &sym, sizeof(sym));
316 this->memory_.SetMemory(str_offset + 0x100, "global_0");
317
318 start_offset += sizeof(sym);
319 memset(&sym, 0, sizeof(sym));
320 sym.st_shndx = SHN_COMMON;
321 sym.st_info = STT_FUNC;
322 sym.st_name = 0x200;
323 sym.st_value = 0x10000;
324 sym.st_size = 0x100;
325 this->memory_.SetMemory(start_offset, &sym, sizeof(sym));
326 this->memory_.SetMemory(str_offset + 0x200, "function_0");
327
328 start_offset += sizeof(sym);
329 memset(&sym, 0, sizeof(sym));
330 sym.st_shndx = SHN_COMMON;
331 sym.st_info = STT_OBJECT | (STB_GLOBAL << 4);
332 sym.st_name = 0x300;
333 this->memory_.SetMemory(start_offset, &sym, sizeof(sym));
334 this->memory_.SetMemory(str_offset + 0x300, "global_1");
335
336 start_offset += sizeof(sym);
337 memset(&sym, 0, sizeof(sym));
338 sym.st_shndx = SHN_COMMON;
339 sym.st_info = STT_FUNC;
340 sym.st_name = 0x400;
341 sym.st_value = 0x12000;
342 sym.st_size = 0x100;
343 this->memory_.SetMemory(start_offset, &sym, sizeof(sym));
344 this->memory_.SetMemory(str_offset + 0x400, "function_1");
345
346 uint64_t offset;
347 EXPECT_TRUE(symbols.GetGlobal<TypeParam>(&this->memory_, "global_0", &offset));
348 EXPECT_TRUE(symbols.GetGlobal<TypeParam>(&this->memory_, "global_1", &offset));
349 EXPECT_TRUE(symbols.GetGlobal<TypeParam>(&this->memory_, "global_0", &offset));
350 EXPECT_TRUE(symbols.GetGlobal<TypeParam>(&this->memory_, "global_1", &offset));
351
352 EXPECT_FALSE(symbols.GetGlobal<TypeParam>(&this->memory_, "function_0", &offset));
353 EXPECT_FALSE(symbols.GetGlobal<TypeParam>(&this->memory_, "function_1", &offset));
354
355 std::string name;
356 EXPECT_TRUE(symbols.GetName<TypeParam>(0x10002, &this->memory_, &name, &offset));
357 EXPECT_EQ("function_0", name);
358 EXPECT_EQ(2U, offset);
359
360 EXPECT_TRUE(symbols.GetName<TypeParam>(0x12004, &this->memory_, &name, &offset));
361 EXPECT_EQ("function_1", name);
362 EXPECT_EQ(4U, offset);
363 }
364
365 REGISTER_TYPED_TEST_CASE_P(SymbolsTest, function_bounds_check, no_symbol, multiple_entries,
366 multiple_entries_nonstandard_size, symtab_value_out_of_bounds,
367 symtab_read_cached, get_global);
368
369 typedef ::testing::Types<Elf32_Sym, Elf64_Sym> SymbolsTestTypes;
370 INSTANTIATE_TYPED_TEST_CASE_P(, SymbolsTest, SymbolsTestTypes);
371
372 } // namespace unwindstack
373