demangle_test.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/internal/demangle.h"
  15. #include <cstdlib>
  16. #include <string>
  17. #include "gtest/gtest.h"
  18. #include "absl/base/config.h"
  19. #include "absl/base/internal/raw_logging.h"
  20. #include "absl/debugging/internal/stack_consumption.h"
  21. #include "absl/memory/memory.h"
  22. namespace absl {
  23. ABSL_NAMESPACE_BEGIN
  24. namespace debugging_internal {
  25. namespace {
  26. // A wrapper function for Demangle() to make the unit test simple.
  27. static const char *DemangleIt(const char * const mangled) {
  28. static char demangled[4096];
  29. if (Demangle(mangled, demangled, sizeof(demangled))) {
  30. return demangled;
  31. } else {
  32. return mangled;
  33. }
  34. }
  35. // Test corner cases of bounary conditions.
  36. TEST(Demangle, CornerCases) {
  37. char tmp[10];
  38. EXPECT_TRUE(Demangle("_Z6foobarv", tmp, sizeof(tmp)));
  39. // sizeof("foobar()") == 9
  40. EXPECT_STREQ("foobar()", tmp);
  41. EXPECT_TRUE(Demangle("_Z6foobarv", tmp, 9));
  42. EXPECT_STREQ("foobar()", tmp);
  43. EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 8)); // Not enough.
  44. EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 1));
  45. EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 0));
  46. EXPECT_FALSE(Demangle("_Z6foobarv", nullptr, 0)); // Should not cause SEGV.
  47. EXPECT_FALSE(Demangle("_Z1000000", tmp, 9));
  48. }
  49. // Test handling of functions suffixed with .clone.N, which is used
  50. // by GCC 4.5.x (and our locally-modified version of GCC 4.4.x), and
  51. // .constprop.N and .isra.N, which are used by GCC 4.6.x. These
  52. // suffixes are used to indicate functions which have been cloned
  53. // during optimization. We ignore these suffixes.
  54. TEST(Demangle, Clones) {
  55. char tmp[20];
  56. EXPECT_TRUE(Demangle("_ZL3Foov", tmp, sizeof(tmp)));
  57. EXPECT_STREQ("Foo()", tmp);
  58. EXPECT_TRUE(Demangle("_ZL3Foov.clone.3", tmp, sizeof(tmp)));
  59. EXPECT_STREQ("Foo()", tmp);
  60. EXPECT_TRUE(Demangle("_ZL3Foov.constprop.80", tmp, sizeof(tmp)));
  61. EXPECT_STREQ("Foo()", tmp);
  62. EXPECT_TRUE(Demangle("_ZL3Foov.isra.18", tmp, sizeof(tmp)));
  63. EXPECT_STREQ("Foo()", tmp);
  64. EXPECT_TRUE(Demangle("_ZL3Foov.isra.2.constprop.18", tmp, sizeof(tmp)));
  65. EXPECT_STREQ("Foo()", tmp);
  66. // Demangle suffixes produced by -funique-internal-linkage-names.
  67. EXPECT_TRUE(Demangle("_ZL3Foov.__uniq.12345", tmp, sizeof(tmp)));
  68. EXPECT_STREQ("Foo()", tmp);
  69. EXPECT_TRUE(Demangle("_ZL3Foov.__uniq.12345.isra.2.constprop.18", tmp,
  70. sizeof(tmp)));
  71. EXPECT_STREQ("Foo()", tmp);
  72. // Suffixes without the number should also demangle.
  73. EXPECT_TRUE(Demangle("_ZL3Foov.clo", tmp, sizeof(tmp)));
  74. EXPECT_STREQ("Foo()", tmp);
  75. // Suffixes with just the number should also demangle.
  76. EXPECT_TRUE(Demangle("_ZL3Foov.123", tmp, sizeof(tmp)));
  77. EXPECT_STREQ("Foo()", tmp);
  78. // (.clone. followed by non-number), should also demangle.
  79. EXPECT_TRUE(Demangle("_ZL3Foov.clone.foo", tmp, sizeof(tmp)));
  80. EXPECT_STREQ("Foo()", tmp);
  81. // (.clone. followed by multiple numbers), should also demangle.
  82. EXPECT_TRUE(Demangle("_ZL3Foov.clone.123.456", tmp, sizeof(tmp)));
  83. EXPECT_STREQ("Foo()", tmp);
  84. // (a long valid suffix), should demangle.
  85. EXPECT_TRUE(Demangle("_ZL3Foov.part.9.165493.constprop.775.31805", tmp,
  86. sizeof(tmp)));
  87. EXPECT_STREQ("Foo()", tmp);
  88. // Invalid (. without anything else), should not demangle.
  89. EXPECT_FALSE(Demangle("_ZL3Foov.", tmp, sizeof(tmp)));
  90. // Invalid (. with mix of alpha and digits), should not demangle.
  91. EXPECT_FALSE(Demangle("_ZL3Foov.abc123", tmp, sizeof(tmp)));
  92. // Invalid (.clone. not followed by number), should not demangle.
  93. EXPECT_FALSE(Demangle("_ZL3Foov.clone.", tmp, sizeof(tmp)));
  94. // Invalid (.constprop. not followed by number), should not demangle.
  95. EXPECT_FALSE(Demangle("_ZL3Foov.isra.2.constprop.", tmp, sizeof(tmp)));
  96. }
  97. // Tests that verify that Demangle footprint is within some limit.
  98. // They are not to be run under sanitizers as the sanitizers increase
  99. // stack consumption by about 4x.
  100. #if defined(ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION) && \
  101. !defined(ABSL_HAVE_ADDRESS_SANITIZER) && \
  102. !defined(ABSL_HAVE_MEMORY_SANITIZER) && \
  103. !defined(ABSL_HAVE_THREAD_SANITIZER)
  104. static const char *g_mangled;
  105. static char g_demangle_buffer[4096];
  106. static char *g_demangle_result;
  107. static void DemangleSignalHandler(int signo) {
  108. if (Demangle(g_mangled, g_demangle_buffer, sizeof(g_demangle_buffer))) {
  109. g_demangle_result = g_demangle_buffer;
  110. } else {
  111. g_demangle_result = nullptr;
  112. }
  113. }
  114. // Call Demangle and figure out the stack footprint of this call.
  115. static const char *DemangleStackConsumption(const char *mangled,
  116. int *stack_consumed) {
  117. g_mangled = mangled;
  118. *stack_consumed = GetSignalHandlerStackConsumption(DemangleSignalHandler);
  119. ABSL_RAW_LOG(INFO, "Stack consumption of Demangle: %d", *stack_consumed);
  120. return g_demangle_result;
  121. }
  122. // Demangle stack consumption should be within 8kB for simple mangled names
  123. // with some level of nesting. With alternate signal stack we have 64K,
  124. // but some signal handlers run on thread stack, and could have arbitrarily
  125. // little space left (so we don't want to make this number too large).
  126. const int kStackConsumptionUpperLimit = 8192;
  127. // Returns a mangled name nested to the given depth.
  128. static std::string NestedMangledName(int depth) {
  129. std::string mangled_name = "_Z1a";
  130. if (depth > 0) {
  131. mangled_name += "IXL";
  132. mangled_name += NestedMangledName(depth - 1);
  133. mangled_name += "EEE";
  134. }
  135. return mangled_name;
  136. }
  137. TEST(Demangle, DemangleStackConsumption) {
  138. // Measure stack consumption of Demangle for nested mangled names of varying
  139. // depth. Since Demangle is implemented as a recursive descent parser,
  140. // stack consumption will grow as the nesting depth increases. By measuring
  141. // the stack consumption for increasing depths, we can see the growing
  142. // impact of any stack-saving changes made to the code for Demangle.
  143. int stack_consumed = 0;
  144. const char *demangled =
  145. DemangleStackConsumption("_Z6foobarv", &stack_consumed);
  146. EXPECT_STREQ("foobar()", demangled);
  147. EXPECT_GT(stack_consumed, 0);
  148. EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
  149. const std::string nested_mangled_name0 = NestedMangledName(0);
  150. demangled = DemangleStackConsumption(nested_mangled_name0.c_str(),
  151. &stack_consumed);
  152. EXPECT_STREQ("a", demangled);
  153. EXPECT_GT(stack_consumed, 0);
  154. EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
  155. const std::string nested_mangled_name1 = NestedMangledName(1);
  156. demangled = DemangleStackConsumption(nested_mangled_name1.c_str(),
  157. &stack_consumed);
  158. EXPECT_STREQ("a<>", demangled);
  159. EXPECT_GT(stack_consumed, 0);
  160. EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
  161. const std::string nested_mangled_name2 = NestedMangledName(2);
  162. demangled = DemangleStackConsumption(nested_mangled_name2.c_str(),
  163. &stack_consumed);
  164. EXPECT_STREQ("a<>", demangled);
  165. EXPECT_GT(stack_consumed, 0);
  166. EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
  167. const std::string nested_mangled_name3 = NestedMangledName(3);
  168. demangled = DemangleStackConsumption(nested_mangled_name3.c_str(),
  169. &stack_consumed);
  170. EXPECT_STREQ("a<>", demangled);
  171. EXPECT_GT(stack_consumed, 0);
  172. EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
  173. }
  174. #endif // Stack consumption tests
  175. static void TestOnInput(const char* input) {
  176. static const int kOutSize = 1048576;
  177. auto out = absl::make_unique<char[]>(kOutSize);
  178. Demangle(input, out.get(), kOutSize);
  179. }
  180. TEST(DemangleRegression, NegativeLength) {
  181. TestOnInput("_ZZn4");
  182. }
  183. TEST(DemangleRegression, DeeplyNestedArrayType) {
  184. const int depth = 100000;
  185. std::string data = "_ZStI";
  186. data.reserve(data.size() + 3 * depth + 1);
  187. for (int i = 0; i < depth; i++) {
  188. data += "A1_";
  189. }
  190. TestOnInput(data.c_str());
  191. }
  192. } // namespace
  193. } // namespace debugging_internal
  194. ABSL_NAMESPACE_END
  195. } // namespace absl