blob: 0bed7359d8b4acb652dbd47e20819271efcebb34 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// 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"
Austin Schuhb4691e92020-12-31 12:37:18 -080021#include "absl/base/config.h"
Austin Schuh36244a12019-09-21 17:52:38 -070022#include "absl/base/internal/raw_logging.h"
23#include "absl/debugging/internal/stack_consumption.h"
24#include "absl/memory/memory.h"
25
26namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080027ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070028namespace debugging_internal {
29namespace {
30
31// A wrapper function for Demangle() to make the unit test simple.
32static 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.
42TEST(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.
61TEST(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 // Invalid (truncated), should not demangle.
74 EXPECT_FALSE(Demangle("_ZL3Foov.clo", tmp, sizeof(tmp)));
75 // Invalid (.clone. not followed by number), should not demangle.
76 EXPECT_FALSE(Demangle("_ZL3Foov.clone.", tmp, sizeof(tmp)));
77 // Invalid (.clone. followed by non-number), should not demangle.
78 EXPECT_FALSE(Demangle("_ZL3Foov.clone.foo", tmp, sizeof(tmp)));
79 // Invalid (.constprop. not followed by number), should not demangle.
80 EXPECT_FALSE(Demangle("_ZL3Foov.isra.2.constprop.", tmp, sizeof(tmp)));
81}
82
83// Tests that verify that Demangle footprint is within some limit.
84// They are not to be run under sanitizers as the sanitizers increase
85// stack consumption by about 4x.
Austin Schuhb4691e92020-12-31 12:37:18 -080086#if defined(ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION) && \
87 !defined(ABSL_HAVE_ADDRESS_SANITIZER) && \
88 !defined(ABSL_HAVE_MEMORY_SANITIZER) && \
89 !defined(ABSL_HAVE_THREAD_SANITIZER)
Austin Schuh36244a12019-09-21 17:52:38 -070090
91static const char *g_mangled;
92static char g_demangle_buffer[4096];
93static char *g_demangle_result;
94
95static void DemangleSignalHandler(int signo) {
96 if (Demangle(g_mangled, g_demangle_buffer, sizeof(g_demangle_buffer))) {
97 g_demangle_result = g_demangle_buffer;
98 } else {
99 g_demangle_result = nullptr;
100 }
101}
102
103// Call Demangle and figure out the stack footprint of this call.
104static const char *DemangleStackConsumption(const char *mangled,
105 int *stack_consumed) {
106 g_mangled = mangled;
107 *stack_consumed = GetSignalHandlerStackConsumption(DemangleSignalHandler);
108 ABSL_RAW_LOG(INFO, "Stack consumption of Demangle: %d", *stack_consumed);
109 return g_demangle_result;
110}
111
112// Demangle stack consumption should be within 8kB for simple mangled names
113// with some level of nesting. With alternate signal stack we have 64K,
114// but some signal handlers run on thread stack, and could have arbitrarily
115// little space left (so we don't want to make this number too large).
116const int kStackConsumptionUpperLimit = 8192;
117
118// Returns a mangled name nested to the given depth.
119static std::string NestedMangledName(int depth) {
120 std::string mangled_name = "_Z1a";
121 if (depth > 0) {
122 mangled_name += "IXL";
123 mangled_name += NestedMangledName(depth - 1);
124 mangled_name += "EEE";
125 }
126 return mangled_name;
127}
128
129TEST(Demangle, DemangleStackConsumption) {
130 // Measure stack consumption of Demangle for nested mangled names of varying
131 // depth. Since Demangle is implemented as a recursive descent parser,
132 // stack consumption will grow as the nesting depth increases. By measuring
133 // the stack consumption for increasing depths, we can see the growing
134 // impact of any stack-saving changes made to the code for Demangle.
135 int stack_consumed = 0;
136
137 const char *demangled =
138 DemangleStackConsumption("_Z6foobarv", &stack_consumed);
139 EXPECT_STREQ("foobar()", demangled);
140 EXPECT_GT(stack_consumed, 0);
141 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
142
143 const std::string nested_mangled_name0 = NestedMangledName(0);
144 demangled = DemangleStackConsumption(nested_mangled_name0.c_str(),
145 &stack_consumed);
146 EXPECT_STREQ("a", demangled);
147 EXPECT_GT(stack_consumed, 0);
148 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
149
150 const std::string nested_mangled_name1 = NestedMangledName(1);
151 demangled = DemangleStackConsumption(nested_mangled_name1.c_str(),
152 &stack_consumed);
153 EXPECT_STREQ("a<>", demangled);
154 EXPECT_GT(stack_consumed, 0);
155 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
156
157 const std::string nested_mangled_name2 = NestedMangledName(2);
158 demangled = DemangleStackConsumption(nested_mangled_name2.c_str(),
159 &stack_consumed);
160 EXPECT_STREQ("a<>", demangled);
161 EXPECT_GT(stack_consumed, 0);
162 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
163
164 const std::string nested_mangled_name3 = NestedMangledName(3);
165 demangled = DemangleStackConsumption(nested_mangled_name3.c_str(),
166 &stack_consumed);
167 EXPECT_STREQ("a<>", demangled);
168 EXPECT_GT(stack_consumed, 0);
169 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
170}
171
172#endif // Stack consumption tests
173
174static void TestOnInput(const char* input) {
175 static const int kOutSize = 1048576;
176 auto out = absl::make_unique<char[]>(kOutSize);
177 Demangle(input, out.get(), kOutSize);
178}
179
180TEST(DemangleRegression, NegativeLength) {
181 TestOnInput("_ZZn4");
182}
183
184TEST(DemangleRegression, DeeplyNestedArrayType) {
185 const int depth = 100000;
186 std::string data = "_ZStI";
187 data.reserve(data.size() + 3 * depth + 1);
188 for (int i = 0; i < depth; i++) {
189 data += "A1_";
190 }
191 TestOnInput(data.c_str());
192}
193
194} // namespace
195} // namespace debugging_internal
Austin Schuhb4691e92020-12-31 12:37:18 -0800196ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -0700197} // namespace absl