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