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