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 // Test the GNU abi_tag extension.
TEST(Demangle,AbiTags)106 TEST(Demangle, AbiTags) {
107 char tmp[80];
108
109 // Mangled name generated via:
110 // struct [[gnu::abi_tag("abc")]] A{};
111 // A a;
112 EXPECT_TRUE(Demangle("_Z1aB3abc", tmp, sizeof(tmp)));
113 EXPECT_STREQ("a[abi:abc]", tmp);
114
115 // Mangled name generated via:
116 // struct B {
117 // B [[gnu::abi_tag("xyz")]] (){};
118 // };
119 // B b;
120 EXPECT_TRUE(Demangle("_ZN1BC2B3xyzEv", tmp, sizeof(tmp)));
121 EXPECT_STREQ("B::B[abi:xyz]()", tmp);
122
123 // Mangled name generated via:
124 // [[gnu::abi_tag("foo", "bar")]] void C() {}
125 EXPECT_TRUE(Demangle("_Z1CB3barB3foov", tmp, sizeof(tmp)));
126 EXPECT_STREQ("C[abi:bar][abi:foo]()", tmp);
127 }
128
129 // Tests that verify that Demangle footprint is within some limit.
130 // They are not to be run under sanitizers as the sanitizers increase
131 // stack consumption by about 4x.
132 #if defined(ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION) && \
133 !defined(ABSL_HAVE_ADDRESS_SANITIZER) && \
134 !defined(ABSL_HAVE_MEMORY_SANITIZER) && \
135 !defined(ABSL_HAVE_THREAD_SANITIZER)
136
137 static const char *g_mangled;
138 static char g_demangle_buffer[4096];
139 static char *g_demangle_result;
140
DemangleSignalHandler(int signo)141 static void DemangleSignalHandler(int signo) {
142 if (Demangle(g_mangled, g_demangle_buffer, sizeof(g_demangle_buffer))) {
143 g_demangle_result = g_demangle_buffer;
144 } else {
145 g_demangle_result = nullptr;
146 }
147 }
148
149 // Call Demangle and figure out the stack footprint of this call.
DemangleStackConsumption(const char * mangled,int * stack_consumed)150 static const char *DemangleStackConsumption(const char *mangled,
151 int *stack_consumed) {
152 g_mangled = mangled;
153 *stack_consumed = GetSignalHandlerStackConsumption(DemangleSignalHandler);
154 ABSL_RAW_LOG(INFO, "Stack consumption of Demangle: %d", *stack_consumed);
155 return g_demangle_result;
156 }
157
158 // Demangle stack consumption should be within 8kB for simple mangled names
159 // with some level of nesting. With alternate signal stack we have 64K,
160 // but some signal handlers run on thread stack, and could have arbitrarily
161 // little space left (so we don't want to make this number too large).
162 const int kStackConsumptionUpperLimit = 8192;
163
164 // Returns a mangled name nested to the given depth.
NestedMangledName(int depth)165 static std::string NestedMangledName(int depth) {
166 std::string mangled_name = "_Z1a";
167 if (depth > 0) {
168 mangled_name += "IXL";
169 mangled_name += NestedMangledName(depth - 1);
170 mangled_name += "EEE";
171 }
172 return mangled_name;
173 }
174
TEST(Demangle,DemangleStackConsumption)175 TEST(Demangle, DemangleStackConsumption) {
176 // Measure stack consumption of Demangle for nested mangled names of varying
177 // depth. Since Demangle is implemented as a recursive descent parser,
178 // stack consumption will grow as the nesting depth increases. By measuring
179 // the stack consumption for increasing depths, we can see the growing
180 // impact of any stack-saving changes made to the code for Demangle.
181 int stack_consumed = 0;
182
183 const char *demangled =
184 DemangleStackConsumption("_Z6foobarv", &stack_consumed);
185 EXPECT_STREQ("foobar()", demangled);
186 EXPECT_GT(stack_consumed, 0);
187 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
188
189 const std::string nested_mangled_name0 = NestedMangledName(0);
190 demangled = DemangleStackConsumption(nested_mangled_name0.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_name1 = NestedMangledName(1);
197 demangled = DemangleStackConsumption(nested_mangled_name1.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_name2 = NestedMangledName(2);
204 demangled = DemangleStackConsumption(nested_mangled_name2.c_str(),
205 &stack_consumed);
206 EXPECT_STREQ("a<>", demangled);
207 EXPECT_GT(stack_consumed, 0);
208 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
209
210 const std::string nested_mangled_name3 = NestedMangledName(3);
211 demangled = DemangleStackConsumption(nested_mangled_name3.c_str(),
212 &stack_consumed);
213 EXPECT_STREQ("a<>", demangled);
214 EXPECT_GT(stack_consumed, 0);
215 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
216 }
217
218 #endif // Stack consumption tests
219
TestOnInput(const char * input)220 static void TestOnInput(const char* input) {
221 static const int kOutSize = 1048576;
222 auto out = absl::make_unique<char[]>(kOutSize);
223 Demangle(input, out.get(), kOutSize);
224 }
225
TEST(DemangleRegression,NegativeLength)226 TEST(DemangleRegression, NegativeLength) {
227 TestOnInput("_ZZn4");
228 }
229
TEST(DemangleRegression,DeeplyNestedArrayType)230 TEST(DemangleRegression, DeeplyNestedArrayType) {
231 const int depth = 100000;
232 std::string data = "_ZStI";
233 data.reserve(data.size() + 3 * depth + 1);
234 for (int i = 0; i < depth; i++) {
235 data += "A1_";
236 }
237 TestOnInput(data.c_str());
238 }
239
240 } // namespace
241 } // namespace debugging_internal
242 ABSL_NAMESPACE_END
243 } // namespace absl
244