symbolize_test.cc
Go to the documentation of this file.
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 
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"
32 #include "absl/base/optimization.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" {
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.
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];
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));
111  }
112 
113  return found ? try_symbolize_buffer : nullptr;
114 }
115 
116 // A wrapper for TrySymbolizeWithLimit(), with a large limit.
117 static const char *TrySymbolize(void *pc) {
118  return TrySymbolizeWithLimit(pc, sizeof(try_symbolize_buffer));
119 }
120 
121 #ifdef ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE
122 
123 TEST(Symbolize, Cached) {
124  // Compilers should give us pointers to them.
125  EXPECT_STREQ("nonstatic_func", TrySymbolize((void *)(&nonstatic_func)));
126 
127  // The name of an internal linkage symbol is not specified; allow either a
128  // mangled or an unmangled name here.
129  const char *static_func_symbol = TrySymbolize((void *)(&static_func));
130  EXPECT_TRUE(strcmp("static_func", static_func_symbol) == 0 ||
131  strcmp("static_func()", static_func_symbol) == 0);
132 
133  EXPECT_TRUE(nullptr == TrySymbolize(nullptr));
134 }
135 
136 TEST(Symbolize, Truncation) {
137  constexpr char kNonStaticFunc[] = "nonstatic_func";
138  EXPECT_STREQ("nonstatic_func",
140  strlen(kNonStaticFunc) + 1));
141  EXPECT_STREQ("nonstatic_...",
143  strlen(kNonStaticFunc) + 0));
144  EXPECT_STREQ("nonstatic...",
146  strlen(kNonStaticFunc) - 1));
147  EXPECT_STREQ("n...", TrySymbolizeWithLimit((void *)(&nonstatic_func), 5));
148  EXPECT_STREQ("...", TrySymbolizeWithLimit((void *)(&nonstatic_func), 4));
149  EXPECT_STREQ("..", TrySymbolizeWithLimit((void *)(&nonstatic_func), 3));
150  EXPECT_STREQ(".", TrySymbolizeWithLimit((void *)(&nonstatic_func), 2));
151  EXPECT_STREQ("", TrySymbolizeWithLimit((void *)(&nonstatic_func), 1));
152  EXPECT_EQ(nullptr, TrySymbolizeWithLimit((void *)(&nonstatic_func), 0));
153 }
154 
155 TEST(Symbolize, SymbolizeWithDemangling) {
156  Foo::func(100);
157  EXPECT_STREQ("Foo::func()", TrySymbolize((void *)(&Foo::func)));
158 }
159 
160 TEST(Symbolize, SymbolizeSplitTextSections) {
161  EXPECT_STREQ("unlikely_func()", TrySymbolize((void *)(&unlikely_func)));
162  EXPECT_STREQ("hot_func()", TrySymbolize((void *)(&hot_func)));
163  EXPECT_STREQ("startup_func()", TrySymbolize((void *)(&startup_func)));
164  EXPECT_STREQ("exit_func()", TrySymbolize((void *)(&exit_func)));
165  EXPECT_STREQ("regular_func()", TrySymbolize((void *)(&regular_func)));
166 }
167 
168 // Tests that verify that Symbolize stack footprint is within some limit.
169 #ifdef ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION
170 
171 static void *g_pc_to_symbolize;
172 static char g_symbolize_buffer[4096];
173 static char *g_symbolize_result;
174 
175 static void SymbolizeSignalHandler(int signo) {
176  if (absl::Symbolize(g_pc_to_symbolize, g_symbolize_buffer,
177  sizeof(g_symbolize_buffer))) {
178  g_symbolize_result = g_symbolize_buffer;
179  } else {
180  g_symbolize_result = nullptr;
181  }
182 }
183 
184 // Call Symbolize and figure out the stack footprint of this call.
185 static const char *SymbolizeStackConsumption(void *pc, int *stack_consumed) {
186  g_pc_to_symbolize = pc;
187  *stack_consumed = absl::debugging_internal::GetSignalHandlerStackConsumption(
188  SymbolizeSignalHandler);
189  return g_symbolize_result;
190 }
191 
192 static int GetStackConsumptionUpperLimit() {
193  // Symbolize stack consumption should be within 2kB.
194  int stack_consumption_upper_limit = 2048;
195 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
196  defined(THREAD_SANITIZER)
197  // Account for sanitizer instrumentation requiring additional stack space.
198  stack_consumption_upper_limit *= 5;
199 #endif
200  return stack_consumption_upper_limit;
201 }
202 
203 TEST(Symbolize, SymbolizeStackConsumption) {
204  int stack_consumed = 0;
205 
206  const char *symbol =
207  SymbolizeStackConsumption((void *)(&nonstatic_func), &stack_consumed);
208  EXPECT_STREQ("nonstatic_func", symbol);
209  EXPECT_GT(stack_consumed, 0);
210  EXPECT_LT(stack_consumed, GetStackConsumptionUpperLimit());
211 
212  // The name of an internal linkage symbol is not specified; allow either a
213  // mangled or an unmangled name here.
214  symbol = SymbolizeStackConsumption((void *)(&static_func), &stack_consumed);
215  EXPECT_TRUE(strcmp("static_func", symbol) == 0 ||
216  strcmp("static_func()", symbol) == 0);
217  EXPECT_GT(stack_consumed, 0);
218  EXPECT_LT(stack_consumed, GetStackConsumptionUpperLimit());
219 }
220 
221 TEST(Symbolize, SymbolizeWithDemanglingStackConsumption) {
222  Foo::func(100);
223  int stack_consumed = 0;
224 
225  const char *symbol =
226  SymbolizeStackConsumption((void *)(&Foo::func), &stack_consumed);
227 
228  EXPECT_STREQ("Foo::func()", symbol);
229  EXPECT_GT(stack_consumed, 0);
230  EXPECT_LT(stack_consumed, GetStackConsumptionUpperLimit());
231 }
232 
233 #endif // ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION
234 
235 // Use a 64K page size for PPC.
236 const size_t kPageSize = 64 << 10;
237 // We place a read-only symbols into the .text section and verify that we can
238 // symbolize them and other symbols after remapping them.
239 const char kPadding0[kPageSize * 4] ABSL_ATTRIBUTE_SECTION_VARIABLE(.text) =
240  "";
241 const char kPadding1[kPageSize * 4] ABSL_ATTRIBUTE_SECTION_VARIABLE(.text) =
242  "";
243 
244 static int FilterElfHeader(struct dl_phdr_info *info, size_t size, void *data) {
245  for (int i = 0; i < info->dlpi_phnum; i++) {
246  if (info->dlpi_phdr[i].p_type == PT_LOAD &&
247  info->dlpi_phdr[i].p_flags == (PF_R | PF_X)) {
248  const void *const vaddr =
249  absl::bit_cast<void *>(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
250  const auto segsize = info->dlpi_phdr[i].p_memsz;
251 
252  const char *self_exe;
253  if (info->dlpi_name != nullptr && info->dlpi_name[0] != '\0') {
254  self_exe = info->dlpi_name;
255  } else {
256  self_exe = "/proc/self/exe";
257  }
258 
260  vaddr, reinterpret_cast<const char *>(vaddr) + segsize,
261  info->dlpi_phdr[i].p_offset, self_exe);
262 
263  return 1;
264  }
265  }
266 
267  return 1;
268 }
269 
270 TEST(Symbolize, SymbolizeWithMultipleMaps) {
271  // Force kPadding0 and kPadding1 to be linked in.
272  if (volatile_bool) {
273  ABSL_RAW_LOG(INFO, "%s", kPadding0);
274  ABSL_RAW_LOG(INFO, "%s", kPadding1);
275  }
276 
277  // Verify we can symbolize everything.
278  char buf[512];
279  memset(buf, 0, sizeof(buf));
280  absl::Symbolize(kPadding0, buf, sizeof(buf));
281  EXPECT_STREQ("kPadding0", buf);
282 
283  memset(buf, 0, sizeof(buf));
284  absl::Symbolize(kPadding1, buf, sizeof(buf));
285  EXPECT_STREQ("kPadding1", buf);
286 
287  // Specify a hint for the executable segment.
288  dl_iterate_phdr(FilterElfHeader, nullptr);
289 
290  // Reload at least one page out of kPadding0, kPadding1
291  const char *ptrs[] = {kPadding0, kPadding1};
292 
293  for (const char *ptr : ptrs) {
294  const int kMapFlags = MAP_ANONYMOUS | MAP_PRIVATE;
295  void *addr = mmap(nullptr, kPageSize, PROT_READ, kMapFlags, 0, 0);
296  ASSERT_NE(addr, MAP_FAILED);
297 
298  // kPadding[0-1] is full of zeroes, so we can remap anywhere within it, but
299  // we ensure there is at least a full page of padding.
300  void *remapped = reinterpret_cast<void *>(
301  reinterpret_cast<uintptr_t>(ptr + kPageSize) & ~(kPageSize - 1ULL));
302 
303  const int kMremapFlags = (MREMAP_MAYMOVE | MREMAP_FIXED);
304  void *ret = mremap(addr, kPageSize, kPageSize, kMremapFlags, remapped);
305  ASSERT_NE(ret, MAP_FAILED);
306  }
307 
308  // Invalidate the symbolization cache so we are forced to rely on the hint.
309  absl::Symbolize(nullptr, buf, sizeof(buf));
310 
311  // Verify we can still symbolize.
312  const char *expected[] = {"kPadding0", "kPadding1"};
313  const size_t offsets[] = {0, kPageSize, 2 * kPageSize, 3 * kPageSize};
314 
315  for (int i = 0; i < 2; i++) {
316  for (size_t offset : offsets) {
317  memset(buf, 0, sizeof(buf));
318  absl::Symbolize(ptrs[i] + offset, buf, sizeof(buf));
319  EXPECT_STREQ(expected[i], buf);
320  }
321  }
322 }
323 
324 // Appends string(*args->arg) to args->symbol_buf.
325 static void DummySymbolDecorator(
327  std::string *message = static_cast<std::string *>(args->arg);
328  strncat(args->symbol_buf, message->c_str(),
329  args->symbol_buf_size - strlen(args->symbol_buf) - 1);
330 }
331 
332 TEST(Symbolize, InstallAndRemoveSymbolDecorators) {
333  int ticket_a;
334  std::string a_message("a");
336  DummySymbolDecorator, &a_message),
337  0);
338 
339  int ticket_b;
340  std::string b_message("b");
342  DummySymbolDecorator, &b_message),
343  0);
344 
345  int ticket_c;
346  std::string c_message("c");
348  DummySymbolDecorator, &c_message),
349  0);
350 
351  char *address = reinterpret_cast<char *>(1);
352  EXPECT_STREQ("abc", TrySymbolize(address++));
353 
355 
356  EXPECT_STREQ("ac", TrySymbolize(address++));
357 
358  // Cleanup: remove all remaining decorators so other stack traces don't
359  // get mystery "ac" decoration.
362 }
363 
364 // Some versions of Clang with optimizations enabled seem to be able
365 // to optimize away the .data section if no variables live in the
366 // section. This variable should get placed in the .data section, and
367 // the test below checks for the existence of a .data section.
368 static int in_data_section = 1;
369 
370 TEST(Symbolize, ForEachSection) {
371  int fd = TEMP_FAILURE_RETRY(open("/proc/self/exe", O_RDONLY));
372  ASSERT_NE(fd, -1);
373 
374  std::vector<std::string> sections;
375  ASSERT_TRUE(absl::debugging_internal::ForEachSection(
376  fd, [&sections](const std::string &name, const ElfW(Shdr) &) {
377  sections.push_back(name);
378  return true;
379  }));
380 
381  // Check for the presence of common section names.
382  EXPECT_THAT(sections, Contains(".text"));
383  EXPECT_THAT(sections, Contains(".rodata"));
384  EXPECT_THAT(sections, Contains(".bss"));
385  ++in_data_section;
386  EXPECT_THAT(sections, Contains(".data"));
387 
388  close(fd);
389 }
390 
391 // x86 specific tests. Uses some inline assembler.
392 extern "C" {
393 inline void *ABSL_ATTRIBUTE_ALWAYS_INLINE inline_func() {
394  void *pc = nullptr;
395 #if defined(__i386__)
396  __asm__ __volatile__("call 1f;\n 1: pop %[PC]" : [ PC ] "=r"(pc));
397 #elif defined(__x86_64__)
398  __asm__ __volatile__("leaq 0(%%rip),%[PC];\n" : [ PC ] "=r"(pc));
399 #endif
400  return pc;
401 }
402 
403 void *ABSL_ATTRIBUTE_NOINLINE non_inline_func() {
404  void *pc = nullptr;
405 #if defined(__i386__)
406  __asm__ __volatile__("call 1f;\n 1: pop %[PC]" : [ PC ] "=r"(pc));
407 #elif defined(__x86_64__)
408  __asm__ __volatile__("leaq 0(%%rip),%[PC];\n" : [ PC ] "=r"(pc));
409 #endif
410  return pc;
411 }
412 
413 void ABSL_ATTRIBUTE_NOINLINE TestWithPCInsideNonInlineFunction() {
414 #if defined(ABSL_HAVE_ATTRIBUTE_NOINLINE) && \
415  (defined(__i386__) || defined(__x86_64__))
416  void *pc = non_inline_func();
417  const char *symbol = TrySymbolize(pc);
418  ABSL_RAW_CHECK(symbol != nullptr, "TestWithPCInsideNonInlineFunction failed");
419  ABSL_RAW_CHECK(strcmp(symbol, "non_inline_func") == 0,
420  "TestWithPCInsideNonInlineFunction failed");
421  std::cout << "TestWithPCInsideNonInlineFunction passed" << std::endl;
422 #endif
423 }
424 
425 void ABSL_ATTRIBUTE_NOINLINE TestWithPCInsideInlineFunction() {
426 #if defined(ABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE) && \
427  (defined(__i386__) || defined(__x86_64__))
428  void *pc = inline_func(); // Must be inlined.
429  const char *symbol = TrySymbolize(pc);
430  ABSL_RAW_CHECK(symbol != nullptr, "TestWithPCInsideInlineFunction failed");
431  ABSL_RAW_CHECK(strcmp(symbol, __FUNCTION__) == 0,
432  "TestWithPCInsideInlineFunction failed");
433  std::cout << "TestWithPCInsideInlineFunction passed" << std::endl;
434 #endif
435 }
436 }
437 
438 // Test with a return address.
439 void ABSL_ATTRIBUTE_NOINLINE TestWithReturnAddress() {
440 #if defined(ABSL_HAVE_ATTRIBUTE_NOINLINE)
441  void *return_address = __builtin_return_address(0);
442  const char *symbol = TrySymbolize(return_address);
443  ABSL_RAW_CHECK(symbol != nullptr, "TestWithReturnAddress failed");
444  ABSL_RAW_CHECK(strcmp(symbol, "main") == 0, "TestWithReturnAddress failed");
445  std::cout << "TestWithReturnAddress passed" << std::endl;
446 #endif
447 }
448 
449 #elif defined(_WIN32) && defined(_DEBUG)
450 
451 TEST(Symbolize, Basics) {
452  EXPECT_STREQ("nonstatic_func", TrySymbolize((void *)(&nonstatic_func)));
453 
454  // The name of an internal linkage symbol is not specified; allow either a
455  // mangled or an unmangled name here.
456  const char* static_func_symbol = TrySymbolize((void *)(&static_func));
457  ASSERT_TRUE(static_func_symbol != nullptr);
458  EXPECT_TRUE(strstr(static_func_symbol, "static_func") != nullptr);
459 
460  EXPECT_TRUE(nullptr == TrySymbolize(nullptr));
461 }
462 
463 TEST(Symbolize, Truncation) {
464  constexpr char kNonStaticFunc[] = "nonstatic_func";
465  EXPECT_STREQ("nonstatic_func",
467  strlen(kNonStaticFunc) + 1));
468  EXPECT_STREQ("nonstatic_...",
470  strlen(kNonStaticFunc) + 0));
471  EXPECT_STREQ("nonstatic...",
473  strlen(kNonStaticFunc) - 1));
474  EXPECT_STREQ("n...", TrySymbolizeWithLimit((void *)(&nonstatic_func), 5));
475  EXPECT_STREQ("...", TrySymbolizeWithLimit((void *)(&nonstatic_func), 4));
476  EXPECT_STREQ("..", TrySymbolizeWithLimit((void *)(&nonstatic_func), 3));
477  EXPECT_STREQ(".", TrySymbolizeWithLimit((void *)(&nonstatic_func), 2));
478  EXPECT_STREQ("", TrySymbolizeWithLimit((void *)(&nonstatic_func), 1));
479  EXPECT_EQ(nullptr, TrySymbolizeWithLimit((void *)(&nonstatic_func), 0));
480 }
481 
482 TEST(Symbolize, SymbolizeWithDemangling) {
483  const char* result = TrySymbolize((void *)(&Foo::func));
484  ASSERT_TRUE(result != nullptr);
485  EXPECT_TRUE(strstr(result, "Foo::func") != nullptr) << result;
486 }
487 
488 #else // Symbolizer unimplemented
489 
490 TEST(Symbolize, Unimplemented) {
491  char buf[64];
492  EXPECT_FALSE(absl::Symbolize((void *)(&nonstatic_func), buf, sizeof(buf)));
493  EXPECT_FALSE(absl::Symbolize((void *)(&static_func), buf, sizeof(buf)));
494  EXPECT_FALSE(absl::Symbolize((void *)(&Foo::func), buf, sizeof(buf)));
495 }
496 
497 #endif
498 
499 int main(int argc, char **argv) {
500  // Make sure kHpageTextPadding is linked into the binary.
501  if (volatile_bool) {
502  ABSL_RAW_LOG(INFO, "%s", kHpageTextPadding);
503  }
504 
505 #if ABSL_PER_THREAD_TLS
506  // Touch the per-thread variables.
507  symbolize_test_thread_small[0] = 0;
508  symbolize_test_thread_big[0] = 0;
509 #endif
510 
512  testing::InitGoogleTest(&argc, argv);
513 
514 #ifdef ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE
515  TestWithPCInsideInlineFunction();
516  TestWithPCInsideNonInlineFunction();
517  TestWithReturnAddress();
518 #endif
519 
520  return RUN_ALL_TESTS();
521 }
#define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION()
Definition: optimization.h:53
int InstallSymbolDecorator(SymbolDecorator decorator, void *arg)
static const char * TrySymbolize(void *pc)
static constexpr size_t kHpageSize
static volatile bool volatile_bool
#define ABSL_RAW_LOG(severity,...)
Definition: raw_logging.h:42
void InitializeSymbolizer(const char *argv0)
#define ABSL_ATTRIBUTE_ALWAYS_INLINE
Definition: attributes.h:129
static const char * TrySymbolizeWithLimit(void *pc, int limit)
int regular_func()
static void func(int x)
Dest bit_cast(const Source &source)
Definition: casts.h:154
char buf[N]
static char try_symbolize_buffer[4096]
#define ABSL_RAW_CHECK(condition, message)
Definition: raw_logging.h:57
int main(int argc, char **argv)
absl::disjunction< std::is_same< T, Ts >... > Contains
Definition: layout.h:250
char * ptr
bool Symbolize(const void *pc, char *out, int out_size)
static char data[kDataSize]
Definition: city_test.cc:31
bool RegisterFileMappingHint(const void *start, const void *end, uint64_t offset, const char *filename)
char name[1]
Definition: mutex.cc:296
uintptr_t size
#define ABSL_ATTRIBUTE_NOINLINE
Definition: attributes.h:136
static void static_func()
void nonstatic_func()
TEST(Symbolize, Unimplemented)
#define ABSL_PER_THREAD_TLS_KEYWORD
int ABSL_ATTRIBUTE_SECTION_VARIABLE(.text.unlikely) unlikely_func()
bool RemoveSymbolDecorator(int ticket)


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:58