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 "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "absl/base/config.h"
23 #include "absl/debugging/internal/stack_consumption.h"
24 #include "absl/log/log.h"
25 #include "absl/memory/memory.h"
26
27 namespace absl {
28 ABSL_NAMESPACE_BEGIN
29 namespace debugging_internal {
30 namespace {
31
32 using ::testing::ContainsRegex;
33
34 // Test corner cases of boundary conditions.
TEST(Demangle,CornerCases)35 TEST(Demangle, CornerCases) {
36 char tmp[10];
37 EXPECT_TRUE(Demangle("_Z6foobarv", tmp, sizeof(tmp)));
38 // sizeof("foobar()") == 9
39 EXPECT_STREQ("foobar()", tmp);
40 EXPECT_TRUE(Demangle("_Z6foobarv", tmp, 9));
41 EXPECT_STREQ("foobar()", tmp);
42 EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 8)); // Not enough.
43 EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 1));
44 EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 0));
45 EXPECT_FALSE(Demangle("_Z6foobarv", nullptr, 0)); // Should not cause SEGV.
46 EXPECT_FALSE(Demangle("_Z1000000", tmp, 9));
47 }
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.
TEST(Demangle,Clones)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
98 // Test the GNU abi_tag extension.
TEST(Demangle,AbiTags)99 TEST(Demangle, AbiTags) {
100 char tmp[80];
101
102 // Mangled name generated via:
103 // struct [[gnu::abi_tag("abc")]] A{};
104 // A a;
105 EXPECT_TRUE(Demangle("_Z1aB3abc", tmp, sizeof(tmp)));
106 EXPECT_STREQ("a[abi:abc]", tmp);
107
108 // Mangled name generated via:
109 // struct B {
110 // B [[gnu::abi_tag("xyz")]] (){};
111 // };
112 // B b;
113 EXPECT_TRUE(Demangle("_ZN1BC2B3xyzEv", tmp, sizeof(tmp)));
114 EXPECT_STREQ("B::B[abi:xyz]()", tmp);
115
116 // Mangled name generated via:
117 // [[gnu::abi_tag("foo", "bar")]] void C() {}
118 EXPECT_TRUE(Demangle("_Z1CB3barB3foov", tmp, sizeof(tmp)));
119 EXPECT_STREQ("C[abi:bar][abi:foo]()", tmp);
120 }
121
122 // Tests that verify that Demangle footprint is within some limit.
123 // They are not to be run under sanitizers as the sanitizers increase
124 // stack consumption by about 4x.
125 #if defined(ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION) && \
126 !defined(ABSL_HAVE_ADDRESS_SANITIZER) && \
127 !defined(ABSL_HAVE_MEMORY_SANITIZER) && \
128 !defined(ABSL_HAVE_THREAD_SANITIZER)
129
130 static const char *g_mangled;
131 static char g_demangle_buffer[4096];
132 static char *g_demangle_result;
133
DemangleSignalHandler(int signo)134 static void DemangleSignalHandler(int signo) {
135 if (Demangle(g_mangled, g_demangle_buffer, sizeof(g_demangle_buffer))) {
136 g_demangle_result = g_demangle_buffer;
137 } else {
138 g_demangle_result = nullptr;
139 }
140 }
141
142 // Call Demangle and figure out the stack footprint of this call.
DemangleStackConsumption(const char * mangled,int * stack_consumed)143 static const char *DemangleStackConsumption(const char *mangled,
144 int *stack_consumed) {
145 g_mangled = mangled;
146 *stack_consumed = GetSignalHandlerStackConsumption(DemangleSignalHandler);
147 LOG(INFO) << "Stack consumption of Demangle: " << *stack_consumed;
148 return g_demangle_result;
149 }
150
151 // Demangle stack consumption should be within 8kB for simple mangled names
152 // with some level of nesting. With alternate signal stack we have 64K,
153 // but some signal handlers run on thread stack, and could have arbitrarily
154 // little space left (so we don't want to make this number too large).
155 const int kStackConsumptionUpperLimit = 8192;
156
157 // Returns a mangled name nested to the given depth.
NestedMangledName(int depth)158 static std::string NestedMangledName(int depth) {
159 std::string mangled_name = "_Z1a";
160 if (depth > 0) {
161 mangled_name += "IXL";
162 mangled_name += NestedMangledName(depth - 1);
163 mangled_name += "EEE";
164 }
165 return mangled_name;
166 }
167
TEST(Demangle,DemangleStackConsumption)168 TEST(Demangle, DemangleStackConsumption) {
169 // Measure stack consumption of Demangle for nested mangled names of varying
170 // depth. Since Demangle is implemented as a recursive descent parser,
171 // stack consumption will grow as the nesting depth increases. By measuring
172 // the stack consumption for increasing depths, we can see the growing
173 // impact of any stack-saving changes made to the code for Demangle.
174 int stack_consumed = 0;
175
176 const char *demangled =
177 DemangleStackConsumption("_Z6foobarv", &stack_consumed);
178 EXPECT_STREQ("foobar()", demangled);
179 EXPECT_GT(stack_consumed, 0);
180 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
181
182 const std::string nested_mangled_name0 = NestedMangledName(0);
183 demangled = DemangleStackConsumption(nested_mangled_name0.c_str(),
184 &stack_consumed);
185 EXPECT_STREQ("a", demangled);
186 EXPECT_GT(stack_consumed, 0);
187 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
188
189 const std::string nested_mangled_name1 = NestedMangledName(1);
190 demangled = DemangleStackConsumption(nested_mangled_name1.c_str(),
191 &stack_consumed);
192 EXPECT_STREQ("a<>", demangled);
193 EXPECT_GT(stack_consumed, 0);
194 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
195
196 const std::string nested_mangled_name2 = NestedMangledName(2);
197 demangled = DemangleStackConsumption(nested_mangled_name2.c_str(),
198 &stack_consumed);
199 EXPECT_STREQ("a<>", demangled);
200 EXPECT_GT(stack_consumed, 0);
201 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
202
203 const std::string nested_mangled_name3 = NestedMangledName(3);
204 demangled = DemangleStackConsumption(nested_mangled_name3.c_str(),
205 &stack_consumed);
206 EXPECT_STREQ("a<>", demangled);
207 EXPECT_GT(stack_consumed, 0);
208 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
209 }
210
211 #endif // Stack consumption tests
212
TestOnInput(const char * input)213 static void TestOnInput(const char* input) {
214 static const int kOutSize = 1048576;
215 auto out = absl::make_unique<char[]>(kOutSize);
216 Demangle(input, out.get(), kOutSize);
217 }
218
TEST(DemangleRegression,NegativeLength)219 TEST(DemangleRegression, NegativeLength) {
220 TestOnInput("_ZZn4");
221 }
222
TEST(DemangleRegression,DeeplyNestedArrayType)223 TEST(DemangleRegression, DeeplyNestedArrayType) {
224 const int depth = 100000;
225 std::string data = "_ZStI";
226 data.reserve(data.size() + 3 * depth + 1);
227 for (int i = 0; i < depth; i++) {
228 data += "A1_";
229 }
230 TestOnInput(data.c_str());
231 }
232
233 struct Base {
234 virtual ~Base() = default;
235 };
236
237 struct Derived : public Base {};
238
TEST(DemangleStringTest,SupportsSymbolNameReturnedByTypeId)239 TEST(DemangleStringTest, SupportsSymbolNameReturnedByTypeId) {
240 EXPECT_EQ(DemangleString(typeid(int).name()), "int");
241 // We want to test that `DemangleString` can demangle the symbol names
242 // returned by `typeid`, but without hard-coding the actual demangled values
243 // (because they are platform-specific).
244 EXPECT_THAT(
245 DemangleString(typeid(Base).name()),
246 ContainsRegex("absl.*debugging_internal.*anonymous namespace.*::Base"));
247 EXPECT_THAT(DemangleString(typeid(Derived).name()),
248 ContainsRegex(
249 "absl.*debugging_internal.*anonymous namespace.*::Derived"));
250 }
251
252 } // namespace
253 } // namespace debugging_internal
254 ABSL_NAMESPACE_END
255 } // namespace absl
256