Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 1 | // Copyright 2018 The Abseil Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "absl/debugging/symbolize.h" |
| 16 | |
| 17 | #ifndef _WIN32 |
| 18 | #include <fcntl.h> |
| 19 | #include <sys/mman.h> |
| 20 | #endif |
| 21 | |
| 22 | #include <cstring> |
| 23 | #include <iostream> |
| 24 | #include <memory> |
| 25 | |
| 26 | #include "gmock/gmock.h" |
| 27 | #include "gtest/gtest.h" |
| 28 | #include "absl/base/attributes.h" |
| 29 | #include "absl/base/casts.h" |
| 30 | #include "absl/base/internal/per_thread_tls.h" |
| 31 | #include "absl/base/internal/raw_logging.h" |
| 32 | #include "absl/base/optimization.h" |
| 33 | #include "absl/debugging/internal/stack_consumption.h" |
| 34 | #include "absl/memory/memory.h" |
| 35 | |
| 36 | using testing::Contains; |
| 37 | |
| 38 | // Functions to symbolize. Use C linkage to avoid mangled names. |
| 39 | extern "C" { |
| 40 | void nonstatic_func() { ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); } |
| 41 | static void static_func() { ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); } |
| 42 | } // extern "C" |
| 43 | |
| 44 | struct Foo { |
| 45 | static void func(int x); |
| 46 | }; |
| 47 | |
| 48 | // A C++ method that should have a mangled name. |
| 49 | void ABSL_ATTRIBUTE_NOINLINE Foo::func(int) { |
| 50 | ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); |
| 51 | } |
| 52 | |
| 53 | // Create functions that will remain in different text sections in the |
| 54 | // final binary when linker option "-z,keep-text-section-prefix" is used. |
| 55 | int ABSL_ATTRIBUTE_SECTION_VARIABLE(.text.unlikely) unlikely_func() { |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | int ABSL_ATTRIBUTE_SECTION_VARIABLE(.text.hot) hot_func() { |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int ABSL_ATTRIBUTE_SECTION_VARIABLE(.text.startup) startup_func() { |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | int ABSL_ATTRIBUTE_SECTION_VARIABLE(.text.exit) exit_func() { |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int /*ABSL_ATTRIBUTE_SECTION_VARIABLE(.text)*/ regular_func() { |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | // Thread-local data may confuse the symbolizer, ensure that it does not. |
| 76 | // Variable sizes and order are important. |
| 77 | #if ABSL_PER_THREAD_TLS |
| 78 | static ABSL_PER_THREAD_TLS_KEYWORD char symbolize_test_thread_small[1]; |
| 79 | static ABSL_PER_THREAD_TLS_KEYWORD char |
| 80 | symbolize_test_thread_big[2 * 1024 * 1024]; |
| 81 | #endif |
| 82 | |
| 83 | // Used below to hopefully inhibit some compiler/linker optimizations |
| 84 | // that may remove kHpageTextPadding, kPadding0, and kPadding1 from |
| 85 | // the binary. |
| 86 | static volatile bool volatile_bool = false; |
| 87 | |
| 88 | // Force the binary to be large enough that a THP .text remap will succeed. |
| 89 | static constexpr size_t kHpageSize = 1 << 21; |
| 90 | const char kHpageTextPadding[kHpageSize * 4] ABSL_ATTRIBUTE_SECTION_VARIABLE( |
| 91 | .text) = ""; |
| 92 | |
| 93 | static char try_symbolize_buffer[4096]; |
| 94 | |
| 95 | // A wrapper function for absl::Symbolize() to make the unit test simple. The |
| 96 | // limit must be < sizeof(try_symbolize_buffer). Returns null if |
| 97 | // absl::Symbolize() returns false, otherwise returns try_symbolize_buffer with |
| 98 | // the result of absl::Symbolize(). |
| 99 | static const char *TrySymbolizeWithLimit(void *pc, int limit) { |
| 100 | ABSL_RAW_CHECK(limit <= sizeof(try_symbolize_buffer), |
| 101 | "try_symbolize_buffer is too small"); |
| 102 | |
| 103 | // Use the heap to facilitate heap and buffer sanitizer tools. |
| 104 | auto heap_buffer = absl::make_unique<char[]>(sizeof(try_symbolize_buffer)); |
| 105 | bool found = absl::Symbolize(pc, heap_buffer.get(), limit); |
| 106 | if (found) { |
| 107 | ABSL_RAW_CHECK(strnlen(heap_buffer.get(), limit) < limit, |
| 108 | "absl::Symbolize() did not properly terminate the string"); |
| 109 | strncpy(try_symbolize_buffer, heap_buffer.get(), |
| 110 | sizeof(try_symbolize_buffer) - 1); |
| 111 | try_symbolize_buffer[sizeof(try_symbolize_buffer) - 1] = '\0'; |
| 112 | } |
| 113 | |
| 114 | return found ? try_symbolize_buffer : nullptr; |
| 115 | } |
| 116 | |
| 117 | // A wrapper for TrySymbolizeWithLimit(), with a large limit. |
| 118 | static const char *TrySymbolize(void *pc) { |
| 119 | return TrySymbolizeWithLimit(pc, sizeof(try_symbolize_buffer)); |
| 120 | } |
| 121 | |
| 122 | #ifdef ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE |
| 123 | |
| 124 | TEST(Symbolize, Cached) { |
| 125 | // Compilers should give us pointers to them. |
| 126 | EXPECT_STREQ("nonstatic_func", TrySymbolize((void *)(&nonstatic_func))); |
| 127 | |
| 128 | // The name of an internal linkage symbol is not specified; allow either a |
| 129 | // mangled or an unmangled name here. |
| 130 | const char *static_func_symbol = TrySymbolize((void *)(&static_func)); |
| 131 | EXPECT_TRUE(strcmp("static_func", static_func_symbol) == 0 || |
| 132 | strcmp("static_func()", static_func_symbol) == 0); |
| 133 | |
| 134 | EXPECT_TRUE(nullptr == TrySymbolize(nullptr)); |
| 135 | } |
| 136 | |
| 137 | TEST(Symbolize, Truncation) { |
| 138 | constexpr char kNonStaticFunc[] = "nonstatic_func"; |
| 139 | EXPECT_STREQ("nonstatic_func", |
| 140 | TrySymbolizeWithLimit((void *)(&nonstatic_func), |
| 141 | strlen(kNonStaticFunc) + 1)); |
| 142 | EXPECT_STREQ("nonstatic_...", |
| 143 | TrySymbolizeWithLimit((void *)(&nonstatic_func), |
| 144 | strlen(kNonStaticFunc) + 0)); |
| 145 | EXPECT_STREQ("nonstatic...", |
| 146 | TrySymbolizeWithLimit((void *)(&nonstatic_func), |
| 147 | strlen(kNonStaticFunc) - 1)); |
| 148 | EXPECT_STREQ("n...", TrySymbolizeWithLimit((void *)(&nonstatic_func), 5)); |
| 149 | EXPECT_STREQ("...", TrySymbolizeWithLimit((void *)(&nonstatic_func), 4)); |
| 150 | EXPECT_STREQ("..", TrySymbolizeWithLimit((void *)(&nonstatic_func), 3)); |
| 151 | EXPECT_STREQ(".", TrySymbolizeWithLimit((void *)(&nonstatic_func), 2)); |
| 152 | EXPECT_STREQ("", TrySymbolizeWithLimit((void *)(&nonstatic_func), 1)); |
| 153 | EXPECT_EQ(nullptr, TrySymbolizeWithLimit((void *)(&nonstatic_func), 0)); |
| 154 | } |
| 155 | |
| 156 | TEST(Symbolize, SymbolizeWithDemangling) { |
| 157 | Foo::func(100); |
| 158 | EXPECT_STREQ("Foo::func()", TrySymbolize((void *)(&Foo::func))); |
| 159 | } |
| 160 | |
| 161 | TEST(Symbolize, SymbolizeSplitTextSections) { |
| 162 | EXPECT_STREQ("unlikely_func()", TrySymbolize((void *)(&unlikely_func))); |
| 163 | EXPECT_STREQ("hot_func()", TrySymbolize((void *)(&hot_func))); |
| 164 | EXPECT_STREQ("startup_func()", TrySymbolize((void *)(&startup_func))); |
| 165 | EXPECT_STREQ("exit_func()", TrySymbolize((void *)(&exit_func))); |
| 166 | EXPECT_STREQ("regular_func()", TrySymbolize((void *)(®ular_func))); |
| 167 | } |
| 168 | |
| 169 | // Tests that verify that Symbolize stack footprint is within some limit. |
| 170 | #ifdef ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION |
| 171 | |
| 172 | static void *g_pc_to_symbolize; |
| 173 | static char g_symbolize_buffer[4096]; |
| 174 | static char *g_symbolize_result; |
| 175 | |
| 176 | static void SymbolizeSignalHandler(int signo) { |
| 177 | if (absl::Symbolize(g_pc_to_symbolize, g_symbolize_buffer, |
| 178 | sizeof(g_symbolize_buffer))) { |
| 179 | g_symbolize_result = g_symbolize_buffer; |
| 180 | } else { |
| 181 | g_symbolize_result = nullptr; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Call Symbolize and figure out the stack footprint of this call. |
| 186 | static const char *SymbolizeStackConsumption(void *pc, int *stack_consumed) { |
| 187 | g_pc_to_symbolize = pc; |
| 188 | *stack_consumed = absl::debugging_internal::GetSignalHandlerStackConsumption( |
| 189 | SymbolizeSignalHandler); |
| 190 | return g_symbolize_result; |
| 191 | } |
| 192 | |
| 193 | static int GetStackConsumptionUpperLimit() { |
| 194 | // Symbolize stack consumption should be within 2kB. |
| 195 | int stack_consumption_upper_limit = 2048; |
| 196 | #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \ |
| 197 | defined(THREAD_SANITIZER) |
| 198 | // Account for sanitizer instrumentation requiring additional stack space. |
| 199 | stack_consumption_upper_limit *= 5; |
| 200 | #endif |
| 201 | return stack_consumption_upper_limit; |
| 202 | } |
| 203 | |
| 204 | TEST(Symbolize, SymbolizeStackConsumption) { |
| 205 | int stack_consumed = 0; |
| 206 | |
| 207 | const char *symbol = |
| 208 | SymbolizeStackConsumption((void *)(&nonstatic_func), &stack_consumed); |
| 209 | EXPECT_STREQ("nonstatic_func", symbol); |
| 210 | EXPECT_GT(stack_consumed, 0); |
| 211 | EXPECT_LT(stack_consumed, GetStackConsumptionUpperLimit()); |
| 212 | |
| 213 | // The name of an internal linkage symbol is not specified; allow either a |
| 214 | // mangled or an unmangled name here. |
| 215 | symbol = SymbolizeStackConsumption((void *)(&static_func), &stack_consumed); |
| 216 | EXPECT_TRUE(strcmp("static_func", symbol) == 0 || |
| 217 | strcmp("static_func()", symbol) == 0); |
| 218 | EXPECT_GT(stack_consumed, 0); |
| 219 | EXPECT_LT(stack_consumed, GetStackConsumptionUpperLimit()); |
| 220 | } |
| 221 | |
| 222 | TEST(Symbolize, SymbolizeWithDemanglingStackConsumption) { |
| 223 | Foo::func(100); |
| 224 | int stack_consumed = 0; |
| 225 | |
| 226 | const char *symbol = |
| 227 | SymbolizeStackConsumption((void *)(&Foo::func), &stack_consumed); |
| 228 | |
| 229 | EXPECT_STREQ("Foo::func()", symbol); |
| 230 | EXPECT_GT(stack_consumed, 0); |
| 231 | EXPECT_LT(stack_consumed, GetStackConsumptionUpperLimit()); |
| 232 | } |
| 233 | |
| 234 | #endif // ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION |
| 235 | |
| 236 | // Use a 64K page size for PPC. |
| 237 | const size_t kPageSize = 64 << 10; |
| 238 | // We place a read-only symbols into the .text section and verify that we can |
| 239 | // symbolize them and other symbols after remapping them. |
| 240 | const char kPadding0[kPageSize * 4] ABSL_ATTRIBUTE_SECTION_VARIABLE(.text) = |
| 241 | ""; |
| 242 | const char kPadding1[kPageSize * 4] ABSL_ATTRIBUTE_SECTION_VARIABLE(.text) = |
| 243 | ""; |
| 244 | |
| 245 | static int FilterElfHeader(struct dl_phdr_info *info, size_t size, void *data) { |
| 246 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 247 | if (info->dlpi_phdr[i].p_type == PT_LOAD && |
| 248 | info->dlpi_phdr[i].p_flags == (PF_R | PF_X)) { |
| 249 | const void *const vaddr = |
| 250 | absl::bit_cast<void *>(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr); |
| 251 | const auto segsize = info->dlpi_phdr[i].p_memsz; |
| 252 | |
| 253 | const char *self_exe; |
| 254 | if (info->dlpi_name != nullptr && info->dlpi_name[0] != '\0') { |
| 255 | self_exe = info->dlpi_name; |
| 256 | } else { |
| 257 | self_exe = "/proc/self/exe"; |
| 258 | } |
| 259 | |
| 260 | absl::debugging_internal::RegisterFileMappingHint( |
| 261 | vaddr, reinterpret_cast<const char *>(vaddr) + segsize, |
| 262 | info->dlpi_phdr[i].p_offset, self_exe); |
| 263 | |
| 264 | return 1; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | TEST(Symbolize, SymbolizeWithMultipleMaps) { |
| 272 | // Force kPadding0 and kPadding1 to be linked in. |
| 273 | if (volatile_bool) { |
| 274 | ABSL_RAW_LOG(INFO, "%s", kPadding0); |
| 275 | ABSL_RAW_LOG(INFO, "%s", kPadding1); |
| 276 | } |
| 277 | |
| 278 | // Verify we can symbolize everything. |
| 279 | char buf[512]; |
| 280 | memset(buf, 0, sizeof(buf)); |
| 281 | absl::Symbolize(kPadding0, buf, sizeof(buf)); |
| 282 | EXPECT_STREQ("kPadding0", buf); |
| 283 | |
| 284 | memset(buf, 0, sizeof(buf)); |
| 285 | absl::Symbolize(kPadding1, buf, sizeof(buf)); |
| 286 | EXPECT_STREQ("kPadding1", buf); |
| 287 | |
| 288 | // Specify a hint for the executable segment. |
| 289 | dl_iterate_phdr(FilterElfHeader, nullptr); |
| 290 | |
| 291 | // Reload at least one page out of kPadding0, kPadding1 |
| 292 | const char *ptrs[] = {kPadding0, kPadding1}; |
| 293 | |
| 294 | for (const char *ptr : ptrs) { |
| 295 | const int kMapFlags = MAP_ANONYMOUS | MAP_PRIVATE; |
| 296 | void *addr = mmap(nullptr, kPageSize, PROT_READ, kMapFlags, 0, 0); |
| 297 | ASSERT_NE(addr, MAP_FAILED); |
| 298 | |
| 299 | // kPadding[0-1] is full of zeroes, so we can remap anywhere within it, but |
| 300 | // we ensure there is at least a full page of padding. |
| 301 | void *remapped = reinterpret_cast<void *>( |
| 302 | reinterpret_cast<uintptr_t>(ptr + kPageSize) & ~(kPageSize - 1ULL)); |
| 303 | |
| 304 | const int kMremapFlags = (MREMAP_MAYMOVE | MREMAP_FIXED); |
| 305 | void *ret = mremap(addr, kPageSize, kPageSize, kMremapFlags, remapped); |
| 306 | ASSERT_NE(ret, MAP_FAILED); |
| 307 | } |
| 308 | |
| 309 | // Invalidate the symbolization cache so we are forced to rely on the hint. |
| 310 | absl::Symbolize(nullptr, buf, sizeof(buf)); |
| 311 | |
| 312 | // Verify we can still symbolize. |
| 313 | const char *expected[] = {"kPadding0", "kPadding1"}; |
| 314 | const size_t offsets[] = {0, kPageSize, 2 * kPageSize, 3 * kPageSize}; |
| 315 | |
| 316 | for (int i = 0; i < 2; i++) { |
| 317 | for (size_t offset : offsets) { |
| 318 | memset(buf, 0, sizeof(buf)); |
| 319 | absl::Symbolize(ptrs[i] + offset, buf, sizeof(buf)); |
| 320 | EXPECT_STREQ(expected[i], buf); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // Appends string(*args->arg) to args->symbol_buf. |
| 326 | static void DummySymbolDecorator( |
| 327 | const absl::debugging_internal::SymbolDecoratorArgs *args) { |
| 328 | std::string *message = static_cast<std::string *>(args->arg); |
| 329 | strncat(args->symbol_buf, message->c_str(), |
| 330 | args->symbol_buf_size - strlen(args->symbol_buf) - 1); |
| 331 | } |
| 332 | |
| 333 | TEST(Symbolize, InstallAndRemoveSymbolDecorators) { |
| 334 | int ticket_a; |
| 335 | std::string a_message("a"); |
| 336 | EXPECT_GE(ticket_a = absl::debugging_internal::InstallSymbolDecorator( |
| 337 | DummySymbolDecorator, &a_message), |
| 338 | 0); |
| 339 | |
| 340 | int ticket_b; |
| 341 | std::string b_message("b"); |
| 342 | EXPECT_GE(ticket_b = absl::debugging_internal::InstallSymbolDecorator( |
| 343 | DummySymbolDecorator, &b_message), |
| 344 | 0); |
| 345 | |
| 346 | int ticket_c; |
| 347 | std::string c_message("c"); |
| 348 | EXPECT_GE(ticket_c = absl::debugging_internal::InstallSymbolDecorator( |
| 349 | DummySymbolDecorator, &c_message), |
| 350 | 0); |
| 351 | |
| 352 | char *address = reinterpret_cast<char *>(1); |
| 353 | EXPECT_STREQ("abc", TrySymbolize(address++)); |
| 354 | |
| 355 | EXPECT_TRUE(absl::debugging_internal::RemoveSymbolDecorator(ticket_b)); |
| 356 | |
| 357 | EXPECT_STREQ("ac", TrySymbolize(address++)); |
| 358 | |
| 359 | // Cleanup: remove all remaining decorators so other stack traces don't |
| 360 | // get mystery "ac" decoration. |
| 361 | EXPECT_TRUE(absl::debugging_internal::RemoveSymbolDecorator(ticket_a)); |
| 362 | EXPECT_TRUE(absl::debugging_internal::RemoveSymbolDecorator(ticket_c)); |
| 363 | } |
| 364 | |
| 365 | // Some versions of Clang with optimizations enabled seem to be able |
| 366 | // to optimize away the .data section if no variables live in the |
| 367 | // section. This variable should get placed in the .data section, and |
| 368 | // the test below checks for the existence of a .data section. |
| 369 | static int in_data_section = 1; |
| 370 | |
| 371 | TEST(Symbolize, ForEachSection) { |
| 372 | int fd = TEMP_FAILURE_RETRY(open("/proc/self/exe", O_RDONLY)); |
| 373 | ASSERT_NE(fd, -1); |
| 374 | |
| 375 | std::vector<std::string> sections; |
| 376 | ASSERT_TRUE(absl::debugging_internal::ForEachSection( |
| 377 | fd, [§ions](const std::string &name, const ElfW(Shdr) &) { |
| 378 | sections.push_back(name); |
| 379 | return true; |
| 380 | })); |
| 381 | |
| 382 | // Check for the presence of common section names. |
| 383 | EXPECT_THAT(sections, Contains(".text")); |
| 384 | EXPECT_THAT(sections, Contains(".rodata")); |
| 385 | EXPECT_THAT(sections, Contains(".bss")); |
| 386 | ++in_data_section; |
| 387 | EXPECT_THAT(sections, Contains(".data")); |
| 388 | |
| 389 | close(fd); |
| 390 | } |
| 391 | |
| 392 | // x86 specific tests. Uses some inline assembler. |
| 393 | extern "C" { |
| 394 | inline void *ABSL_ATTRIBUTE_ALWAYS_INLINE inline_func() { |
| 395 | void *pc = nullptr; |
| 396 | #if defined(__i386__) |
| 397 | __asm__ __volatile__("call 1f;\n 1: pop %[PC]" : [ PC ] "=r"(pc)); |
| 398 | #elif defined(__x86_64__) |
| 399 | __asm__ __volatile__("leaq 0(%%rip),%[PC];\n" : [ PC ] "=r"(pc)); |
| 400 | #endif |
| 401 | return pc; |
| 402 | } |
| 403 | |
| 404 | void *ABSL_ATTRIBUTE_NOINLINE non_inline_func() { |
| 405 | void *pc = nullptr; |
| 406 | #if defined(__i386__) |
| 407 | __asm__ __volatile__("call 1f;\n 1: pop %[PC]" : [ PC ] "=r"(pc)); |
| 408 | #elif defined(__x86_64__) |
| 409 | __asm__ __volatile__("leaq 0(%%rip),%[PC];\n" : [ PC ] "=r"(pc)); |
| 410 | #endif |
| 411 | return pc; |
| 412 | } |
| 413 | |
| 414 | void ABSL_ATTRIBUTE_NOINLINE TestWithPCInsideNonInlineFunction() { |
| 415 | #if defined(ABSL_HAVE_ATTRIBUTE_NOINLINE) && \ |
| 416 | (defined(__i386__) || defined(__x86_64__)) |
| 417 | void *pc = non_inline_func(); |
| 418 | const char *symbol = TrySymbolize(pc); |
| 419 | ABSL_RAW_CHECK(symbol != nullptr, "TestWithPCInsideNonInlineFunction failed"); |
| 420 | ABSL_RAW_CHECK(strcmp(symbol, "non_inline_func") == 0, |
| 421 | "TestWithPCInsideNonInlineFunction failed"); |
| 422 | std::cout << "TestWithPCInsideNonInlineFunction passed" << std::endl; |
| 423 | #endif |
| 424 | } |
| 425 | |
| 426 | void ABSL_ATTRIBUTE_NOINLINE TestWithPCInsideInlineFunction() { |
| 427 | #if defined(ABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE) && \ |
| 428 | (defined(__i386__) || defined(__x86_64__)) |
| 429 | void *pc = inline_func(); // Must be inlined. |
| 430 | const char *symbol = TrySymbolize(pc); |
| 431 | ABSL_RAW_CHECK(symbol != nullptr, "TestWithPCInsideInlineFunction failed"); |
| 432 | ABSL_RAW_CHECK(strcmp(symbol, __FUNCTION__) == 0, |
| 433 | "TestWithPCInsideInlineFunction failed"); |
| 434 | std::cout << "TestWithPCInsideInlineFunction passed" << std::endl; |
| 435 | #endif |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // Test with a return address. |
| 440 | void ABSL_ATTRIBUTE_NOINLINE TestWithReturnAddress() { |
| 441 | #if defined(ABSL_HAVE_ATTRIBUTE_NOINLINE) |
| 442 | void *return_address = __builtin_return_address(0); |
| 443 | const char *symbol = TrySymbolize(return_address); |
| 444 | ABSL_RAW_CHECK(symbol != nullptr, "TestWithReturnAddress failed"); |
| 445 | ABSL_RAW_CHECK(strcmp(symbol, "main") == 0, "TestWithReturnAddress failed"); |
| 446 | std::cout << "TestWithReturnAddress passed" << std::endl; |
| 447 | #endif |
| 448 | } |
| 449 | |
| 450 | #elif defined(_WIN32) && defined(_DEBUG) |
| 451 | |
| 452 | TEST(Symbolize, Basics) { |
| 453 | EXPECT_STREQ("nonstatic_func", TrySymbolize((void *)(&nonstatic_func))); |
| 454 | |
| 455 | // The name of an internal linkage symbol is not specified; allow either a |
| 456 | // mangled or an unmangled name here. |
| 457 | const char* static_func_symbol = TrySymbolize((void *)(&static_func)); |
| 458 | ASSERT_TRUE(static_func_symbol != nullptr); |
| 459 | EXPECT_TRUE(strstr(static_func_symbol, "static_func") != nullptr); |
| 460 | |
| 461 | EXPECT_TRUE(nullptr == TrySymbolize(nullptr)); |
| 462 | } |
| 463 | |
| 464 | TEST(Symbolize, Truncation) { |
| 465 | constexpr char kNonStaticFunc[] = "nonstatic_func"; |
| 466 | EXPECT_STREQ("nonstatic_func", |
| 467 | TrySymbolizeWithLimit((void *)(&nonstatic_func), |
| 468 | strlen(kNonStaticFunc) + 1)); |
| 469 | EXPECT_STREQ("nonstatic_...", |
| 470 | TrySymbolizeWithLimit((void *)(&nonstatic_func), |
| 471 | strlen(kNonStaticFunc) + 0)); |
| 472 | EXPECT_STREQ("nonstatic...", |
| 473 | TrySymbolizeWithLimit((void *)(&nonstatic_func), |
| 474 | strlen(kNonStaticFunc) - 1)); |
| 475 | EXPECT_STREQ("n...", TrySymbolizeWithLimit((void *)(&nonstatic_func), 5)); |
| 476 | EXPECT_STREQ("...", TrySymbolizeWithLimit((void *)(&nonstatic_func), 4)); |
| 477 | EXPECT_STREQ("..", TrySymbolizeWithLimit((void *)(&nonstatic_func), 3)); |
| 478 | EXPECT_STREQ(".", TrySymbolizeWithLimit((void *)(&nonstatic_func), 2)); |
| 479 | EXPECT_STREQ("", TrySymbolizeWithLimit((void *)(&nonstatic_func), 1)); |
| 480 | EXPECT_EQ(nullptr, TrySymbolizeWithLimit((void *)(&nonstatic_func), 0)); |
| 481 | } |
| 482 | |
| 483 | TEST(Symbolize, SymbolizeWithDemangling) { |
| 484 | const char* result = TrySymbolize((void *)(&Foo::func)); |
| 485 | ASSERT_TRUE(result != nullptr); |
| 486 | EXPECT_TRUE(strstr(result, "Foo::func") != nullptr) << result; |
| 487 | } |
| 488 | |
| 489 | #else // Symbolizer unimplemented |
| 490 | |
| 491 | TEST(Symbolize, Unimplemented) { |
| 492 | char buf[64]; |
| 493 | EXPECT_FALSE(absl::Symbolize((void *)(&nonstatic_func), buf, sizeof(buf))); |
| 494 | EXPECT_FALSE(absl::Symbolize((void *)(&static_func), buf, sizeof(buf))); |
| 495 | EXPECT_FALSE(absl::Symbolize((void *)(&Foo::func), buf, sizeof(buf))); |
| 496 | } |
| 497 | |
| 498 | #endif |
| 499 | |
| 500 | int main(int argc, char **argv) { |
| 501 | // Make sure kHpageTextPadding is linked into the binary. |
| 502 | if (volatile_bool) { |
| 503 | ABSL_RAW_LOG(INFO, "%s", kHpageTextPadding); |
| 504 | } |
| 505 | |
| 506 | #if ABSL_PER_THREAD_TLS |
| 507 | // Touch the per-thread variables. |
| 508 | symbolize_test_thread_small[0] = 0; |
| 509 | symbolize_test_thread_big[0] = 0; |
| 510 | #endif |
| 511 | |
| 512 | absl::InitializeSymbolizer(argv[0]); |
| 513 | testing::InitGoogleTest(&argc, argv); |
| 514 | |
| 515 | #ifdef ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE |
| 516 | TestWithPCInsideInlineFunction(); |
| 517 | TestWithPCInsideNonInlineFunction(); |
| 518 | TestWithReturnAddress(); |
| 519 | #endif |
| 520 | |
| 521 | return RUN_ALL_TESTS(); |
| 522 | } |