| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 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 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 35 | #include <unwindstack/Memory.h> | 
|  | 36 |  | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 37 | #include "MemoryFake.h" | 
|  | 38 | #include "Symbols.h" | 
|  | 39 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 40 | namespace unwindstack { | 
|  | 41 |  | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 42 | template <typename TypeParam> | 
|  | 43 | class SymbolsTest : public ::testing::Test { | 
|  | 44 | protected: | 
|  | 45 | void SetUp() override { memory_.Clear(); } | 
|  | 46 |  | 
|  | 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 | }; | 
| Christopher Ferris | 7e21eba | 2019-06-20 16:16:42 -0700 | [diff] [blame] | 58 | TYPED_TEST_SUITE_P(SymbolsTest); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 59 |  | 
|  | 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; | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 73 | ASSERT_TRUE(symbols.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 74 | ASSERT_EQ("fake_function", name); | 
|  | 75 | ASSERT_EQ(0U, func_offset); | 
|  | 76 |  | 
|  | 77 | name.clear(); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(symbols.GetName<TypeParam>(0x500f, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 79 | ASSERT_EQ("fake_function", name); | 
|  | 80 | ASSERT_EQ(0xfU, func_offset); | 
|  | 81 |  | 
|  | 82 | // Check one before and one after the function. | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 83 | ASSERT_FALSE(symbols.GetName<TypeParam>(0x4fff, &this->memory_, &name, &func_offset)); | 
|  | 84 | ASSERT_FALSE(symbols.GetName<TypeParam>(0x5010, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
|  | 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; | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 101 | ASSERT_TRUE(symbols.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 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(); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 110 | ASSERT_FALSE(symbols.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 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(); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 118 | ASSERT_FALSE(symbols.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 119 | } | 
|  | 120 |  | 
|  | 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; | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 147 | ASSERT_TRUE(symbols.GetName<TypeParam>(0x3005, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 148 | ASSERT_EQ("function_two", name); | 
|  | 149 | ASSERT_EQ(1U, func_offset); | 
|  | 150 |  | 
|  | 151 | name.clear(); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 152 | ASSERT_TRUE(symbols.GetName<TypeParam>(0x5004, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 153 | ASSERT_EQ("function_one", name); | 
|  | 154 | ASSERT_EQ(4U, func_offset); | 
|  | 155 |  | 
|  | 156 | name.clear(); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 157 | ASSERT_TRUE(symbols.GetName<TypeParam>(0xa011, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 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(); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 164 | ASSERT_TRUE(symbols.GetName<TypeParam>(0x5008, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 165 | ASSERT_EQ("function_one", name); | 
|  | 166 | ASSERT_EQ(8U, func_offset); | 
|  | 167 |  | 
|  | 168 | name.clear(); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 169 | ASSERT_TRUE(symbols.GetName<TypeParam>(0x3008, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 170 | ASSERT_EQ("function_two", name); | 
|  | 171 | ASSERT_EQ(4U, func_offset); | 
|  | 172 |  | 
|  | 173 | name.clear(); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 174 | ASSERT_TRUE(symbols.GetName<TypeParam>(0xa01a, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 175 | ASSERT_EQ("function_three", name); | 
|  | 176 | ASSERT_EQ(0xaU, func_offset); | 
|  | 177 | } | 
|  | 178 |  | 
|  | 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); | 
| David Srbecky | af41960 | 2019-03-26 14:38:28 +0000 | [diff] [blame] | 188 | this->memory_.SetMemoryBlock(offset, entry_size, 0); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 189 | this->memory_.SetMemory(offset, &sym, sizeof(sym)); | 
|  | 190 | fake_name = "function_one"; | 
|  | 191 | this->memory_.SetMemory(0x2040, fake_name.c_str(), fake_name.size() + 1); | 
|  | 192 | offset += entry_size; | 
|  | 193 |  | 
|  | 194 | this->InitSym(&sym, 0x3004, 0x200, 0x100); | 
| David Srbecky | af41960 | 2019-03-26 14:38:28 +0000 | [diff] [blame] | 195 | this->memory_.SetMemoryBlock(offset, entry_size, 0); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 196 | this->memory_.SetMemory(offset, &sym, sizeof(sym)); | 
|  | 197 | fake_name = "function_two"; | 
|  | 198 | this->memory_.SetMemory(0x2100, fake_name.c_str(), fake_name.size() + 1); | 
|  | 199 | offset += entry_size; | 
|  | 200 |  | 
|  | 201 | this->InitSym(&sym, 0xa010, 0x20, 0x230); | 
| David Srbecky | af41960 | 2019-03-26 14:38:28 +0000 | [diff] [blame] | 202 | this->memory_.SetMemoryBlock(offset, entry_size, 0); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 203 | this->memory_.SetMemory(offset, &sym, sizeof(sym)); | 
|  | 204 | fake_name = "function_three"; | 
|  | 205 | this->memory_.SetMemory(0x2230, fake_name.c_str(), fake_name.size() + 1); | 
|  | 206 |  | 
|  | 207 | std::string name; | 
|  | 208 | uint64_t func_offset; | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 209 | ASSERT_TRUE(symbols.GetName<TypeParam>(0x3005, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 210 | ASSERT_EQ("function_two", name); | 
|  | 211 | ASSERT_EQ(1U, func_offset); | 
|  | 212 |  | 
|  | 213 | name.clear(); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 214 | ASSERT_TRUE(symbols.GetName<TypeParam>(0x5004, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 215 | ASSERT_EQ("function_one", name); | 
|  | 216 | ASSERT_EQ(4U, func_offset); | 
|  | 217 |  | 
|  | 218 | name.clear(); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 219 | ASSERT_TRUE(symbols.GetName<TypeParam>(0xa011, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 220 | ASSERT_EQ("function_three", name); | 
|  | 221 | ASSERT_EQ(1U, func_offset); | 
|  | 222 | } | 
|  | 223 |  | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 224 | TYPED_TEST_P(SymbolsTest, symtab_value_out_of_bounds) { | 
|  | 225 | Symbols symbols_end_at_100(0x1000, sizeof(TypeParam) * 2, sizeof(TypeParam), 0x2000, 0x100); | 
|  | 226 | Symbols symbols_end_at_200(0x1000, sizeof(TypeParam) * 2, sizeof(TypeParam), 0x2000, 0x200); | 
|  | 227 |  | 
|  | 228 | TypeParam sym; | 
|  | 229 | uint64_t offset = 0x1000; | 
|  | 230 |  | 
|  | 231 | this->InitSym(&sym, 0x5000, 0x10, 0xfb); | 
|  | 232 | this->memory_.SetMemory(offset, &sym, sizeof(sym)); | 
|  | 233 | offset += sizeof(sym); | 
|  | 234 |  | 
|  | 235 | this->InitSym(&sym, 0x3000, 0x10, 0x100); | 
|  | 236 | this->memory_.SetMemory(offset, &sym, sizeof(sym)); | 
|  | 237 |  | 
|  | 238 | // Put the name across the end of the tab. | 
|  | 239 | std::string fake_name("fake_function"); | 
|  | 240 | this->memory_.SetMemory(0x20fb, fake_name.c_str(), fake_name.size() + 1); | 
|  | 241 |  | 
|  | 242 | std::string name; | 
|  | 243 | uint64_t func_offset; | 
|  | 244 | // Verify that we can get the function name properly for both entries. | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 245 | ASSERT_TRUE(symbols_end_at_200.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 246 | ASSERT_EQ("fake_function", name); | 
|  | 247 | ASSERT_EQ(0U, func_offset); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 248 | ASSERT_TRUE(symbols_end_at_200.GetName<TypeParam>(0x3000, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 249 | ASSERT_EQ("function", name); | 
|  | 250 | ASSERT_EQ(0U, func_offset); | 
|  | 251 |  | 
|  | 252 | // Now use the symbol table that ends at 0x100. | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 253 | ASSERT_FALSE(symbols_end_at_100.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset)); | 
|  | 254 | ASSERT_FALSE(symbols_end_at_100.GetName<TypeParam>(0x3000, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 255 | } | 
|  | 256 |  | 
|  | 257 | // Verify the entire func table is cached. | 
|  | 258 | TYPED_TEST_P(SymbolsTest, symtab_read_cached) { | 
|  | 259 | Symbols symbols(0x1000, 3 * sizeof(TypeParam), sizeof(TypeParam), 0xa000, 0x1000); | 
|  | 260 |  | 
|  | 261 | TypeParam sym; | 
|  | 262 | uint64_t offset = 0x1000; | 
|  | 263 |  | 
|  | 264 | // Make sure that these entries are not in ascending order. | 
|  | 265 | this->InitSym(&sym, 0x5000, 0x10, 0x100); | 
|  | 266 | this->memory_.SetMemory(offset, &sym, sizeof(sym)); | 
|  | 267 | offset += sizeof(sym); | 
|  | 268 |  | 
|  | 269 | this->InitSym(&sym, 0x2000, 0x300, 0x200); | 
|  | 270 | this->memory_.SetMemory(offset, &sym, sizeof(sym)); | 
|  | 271 | offset += sizeof(sym); | 
|  | 272 |  | 
|  | 273 | this->InitSym(&sym, 0x1000, 0x100, 0x300); | 
|  | 274 | this->memory_.SetMemory(offset, &sym, sizeof(sym)); | 
|  | 275 | offset += sizeof(sym); | 
|  | 276 |  | 
|  | 277 | // Do call that should cache all of the entries (except the string data). | 
|  | 278 | std::string name; | 
|  | 279 | uint64_t func_offset; | 
| David Srbecky | af41960 | 2019-03-26 14:38:28 +0000 | [diff] [blame] | 280 | ASSERT_FALSE(symbols.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset)); | 
|  | 281 | ASSERT_FALSE(symbols.GetName<TypeParam>(0x2000, &this->memory_, &name, &func_offset)); | 
|  | 282 | ASSERT_FALSE(symbols.GetName<TypeParam>(0x1000, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 283 | this->memory_.Clear(); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 284 | ASSERT_FALSE(symbols.GetName<TypeParam>(0x6000, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 285 |  | 
|  | 286 | // Clear the memory and only put the symbol data string data in memory. | 
|  | 287 | this->memory_.Clear(); | 
|  | 288 |  | 
|  | 289 | std::string fake_name; | 
|  | 290 | fake_name = "first_entry"; | 
|  | 291 | this->memory_.SetMemory(0xa100, fake_name.c_str(), fake_name.size() + 1); | 
|  | 292 | fake_name = "second_entry"; | 
|  | 293 | this->memory_.SetMemory(0xa200, fake_name.c_str(), fake_name.size() + 1); | 
|  | 294 | fake_name = "third_entry"; | 
|  | 295 | this->memory_.SetMemory(0xa300, fake_name.c_str(), fake_name.size() + 1); | 
|  | 296 |  | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 297 | ASSERT_TRUE(symbols.GetName<TypeParam>(0x5001, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 298 | ASSERT_EQ("first_entry", name); | 
|  | 299 | ASSERT_EQ(1U, func_offset); | 
|  | 300 |  | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 301 | ASSERT_TRUE(symbols.GetName<TypeParam>(0x2002, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 302 | ASSERT_EQ("second_entry", name); | 
|  | 303 | ASSERT_EQ(2U, func_offset); | 
|  | 304 |  | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 305 | ASSERT_TRUE(symbols.GetName<TypeParam>(0x1003, &this->memory_, &name, &func_offset)); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 306 | ASSERT_EQ("third_entry", name); | 
|  | 307 | ASSERT_EQ(3U, func_offset); | 
|  | 308 | } | 
|  | 309 |  | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 310 | TYPED_TEST_P(SymbolsTest, get_global) { | 
|  | 311 | uint64_t start_offset = 0x1000; | 
|  | 312 | uint64_t str_offset = 0xa000; | 
|  | 313 | Symbols symbols(start_offset, 4 * sizeof(TypeParam), sizeof(TypeParam), str_offset, 0x1000); | 
|  | 314 |  | 
|  | 315 | TypeParam sym; | 
|  | 316 | memset(&sym, 0, sizeof(sym)); | 
|  | 317 | sym.st_shndx = SHN_COMMON; | 
|  | 318 | sym.st_info = STT_OBJECT | (STB_GLOBAL << 4); | 
|  | 319 | sym.st_name = 0x100; | 
|  | 320 | this->memory_.SetMemory(start_offset, &sym, sizeof(sym)); | 
|  | 321 | this->memory_.SetMemory(str_offset + 0x100, "global_0"); | 
|  | 322 |  | 
|  | 323 | start_offset += sizeof(sym); | 
|  | 324 | memset(&sym, 0, sizeof(sym)); | 
|  | 325 | sym.st_shndx = SHN_COMMON; | 
|  | 326 | sym.st_info = STT_FUNC; | 
|  | 327 | sym.st_name = 0x200; | 
|  | 328 | sym.st_value = 0x10000; | 
|  | 329 | sym.st_size = 0x100; | 
|  | 330 | this->memory_.SetMemory(start_offset, &sym, sizeof(sym)); | 
|  | 331 | this->memory_.SetMemory(str_offset + 0x200, "function_0"); | 
|  | 332 |  | 
|  | 333 | start_offset += sizeof(sym); | 
|  | 334 | memset(&sym, 0, sizeof(sym)); | 
|  | 335 | sym.st_shndx = SHN_COMMON; | 
|  | 336 | sym.st_info = STT_OBJECT | (STB_GLOBAL << 4); | 
|  | 337 | sym.st_name = 0x300; | 
|  | 338 | this->memory_.SetMemory(start_offset, &sym, sizeof(sym)); | 
|  | 339 | this->memory_.SetMemory(str_offset + 0x300, "global_1"); | 
|  | 340 |  | 
|  | 341 | start_offset += sizeof(sym); | 
|  | 342 | memset(&sym, 0, sizeof(sym)); | 
|  | 343 | sym.st_shndx = SHN_COMMON; | 
|  | 344 | sym.st_info = STT_FUNC; | 
|  | 345 | sym.st_name = 0x400; | 
|  | 346 | sym.st_value = 0x12000; | 
|  | 347 | sym.st_size = 0x100; | 
|  | 348 | this->memory_.SetMemory(start_offset, &sym, sizeof(sym)); | 
|  | 349 | this->memory_.SetMemory(str_offset + 0x400, "function_1"); | 
|  | 350 |  | 
|  | 351 | uint64_t offset; | 
|  | 352 | EXPECT_TRUE(symbols.GetGlobal<TypeParam>(&this->memory_, "global_0", &offset)); | 
|  | 353 | EXPECT_TRUE(symbols.GetGlobal<TypeParam>(&this->memory_, "global_1", &offset)); | 
|  | 354 | EXPECT_TRUE(symbols.GetGlobal<TypeParam>(&this->memory_, "global_0", &offset)); | 
|  | 355 | EXPECT_TRUE(symbols.GetGlobal<TypeParam>(&this->memory_, "global_1", &offset)); | 
|  | 356 |  | 
|  | 357 | EXPECT_FALSE(symbols.GetGlobal<TypeParam>(&this->memory_, "function_0", &offset)); | 
|  | 358 | EXPECT_FALSE(symbols.GetGlobal<TypeParam>(&this->memory_, "function_1", &offset)); | 
|  | 359 |  | 
|  | 360 | std::string name; | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 361 | EXPECT_TRUE(symbols.GetName<TypeParam>(0x10002, &this->memory_, &name, &offset)); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 362 | EXPECT_EQ("function_0", name); | 
|  | 363 | EXPECT_EQ(2U, offset); | 
|  | 364 |  | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 365 | EXPECT_TRUE(symbols.GetName<TypeParam>(0x12004, &this->memory_, &name, &offset)); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 366 | EXPECT_EQ("function_1", name); | 
|  | 367 | EXPECT_EQ(4U, offset); | 
|  | 368 | } | 
|  | 369 |  | 
| Christopher Ferris | 7e21eba | 2019-06-20 16:16:42 -0700 | [diff] [blame] | 370 | REGISTER_TYPED_TEST_SUITE_P(SymbolsTest, function_bounds_check, no_symbol, multiple_entries, | 
|  | 371 | multiple_entries_nonstandard_size, symtab_value_out_of_bounds, | 
|  | 372 | symtab_read_cached, get_global); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 373 |  | 
|  | 374 | typedef ::testing::Types<Elf32_Sym, Elf64_Sym> SymbolsTestTypes; | 
| Haibo Huang | 0c01bb6 | 2019-12-11 12:46:20 -0800 | [diff] [blame] | 375 | INSTANTIATE_TYPED_TEST_SUITE_P(Libunwindstack, SymbolsTest, SymbolsTestTypes); | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 376 |  | 
|  | 377 | }  // namespace unwindstack |