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