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