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