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