symbolize_test.cc 21 KB

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