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


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