20 #include "gtest/gtest.h" 26 namespace debugging_internal {
30 static const char *DemangleIt(
const char *
const mangled) {
31 static char demangled[4096];
32 if (
Demangle(mangled, demangled,
sizeof(demangled))) {
42 EXPECT_TRUE(
Demangle(
"_Z6foobarv", tmp,
sizeof(tmp)));
44 EXPECT_STREQ(
"foobar()", tmp);
45 EXPECT_TRUE(
Demangle(
"_Z6foobarv", tmp, 9));
46 EXPECT_STREQ(
"foobar()", tmp);
47 EXPECT_FALSE(
Demangle(
"_Z6foobarv", tmp, 8));
48 EXPECT_FALSE(
Demangle(
"_Z6foobarv", tmp, 1));
49 EXPECT_FALSE(
Demangle(
"_Z6foobarv", tmp, 0));
50 EXPECT_FALSE(
Demangle(
"_Z6foobarv",
nullptr, 0));
51 EXPECT_FALSE(
Demangle(
"_Z1000000", tmp, 9));
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);
72 EXPECT_FALSE(
Demangle(
"_ZL3Foov.clo", tmp,
sizeof(tmp)));
74 EXPECT_FALSE(
Demangle(
"_ZL3Foov.clone.", tmp,
sizeof(tmp)));
76 EXPECT_FALSE(
Demangle(
"_ZL3Foov.clone.foo", tmp,
sizeof(tmp)));
78 EXPECT_FALSE(
Demangle(
"_ZL3Foov.isra.2.constprop.", tmp,
sizeof(tmp)));
84 #if defined(ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION) && \ 85 !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && \ 86 !defined(THREAD_SANITIZER) 88 static const char *g_mangled;
89 static char g_demangle_buffer[4096];
90 static char *g_demangle_result;
92 static void DemangleSignalHandler(
int signo) {
93 if (
Demangle(g_mangled, g_demangle_buffer,
sizeof(g_demangle_buffer))) {
94 g_demangle_result = g_demangle_buffer;
96 g_demangle_result =
nullptr;
101 static const char *DemangleStackConsumption(
const char *mangled,
102 int *stack_consumed) {
104 *stack_consumed = GetSignalHandlerStackConsumption(DemangleSignalHandler);
105 ABSL_RAW_LOG(INFO,
"Stack consumption of Demangle: %d", *stack_consumed);
106 return g_demangle_result;
113 const int kStackConsumptionUpperLimit = 8192;
116 static std::string NestedMangledName(
int depth) {
117 std::string mangled_name =
"_Z1a";
119 mangled_name +=
"IXL";
120 mangled_name += NestedMangledName(depth - 1);
121 mangled_name +=
"EEE";
132 int stack_consumed = 0;
134 const char *demangled =
135 DemangleStackConsumption(
"_Z6foobarv", &stack_consumed);
136 EXPECT_STREQ(
"foobar()", demangled);
137 EXPECT_GT(stack_consumed, 0);
138 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
140 const std::string nested_mangled_name0 = NestedMangledName(0);
141 demangled = DemangleStackConsumption(nested_mangled_name0.c_str(),
143 EXPECT_STREQ(
"a", demangled);
144 EXPECT_GT(stack_consumed, 0);
145 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
147 const std::string nested_mangled_name1 = NestedMangledName(1);
148 demangled = DemangleStackConsumption(nested_mangled_name1.c_str(),
150 EXPECT_STREQ(
"a<>", demangled);
151 EXPECT_GT(stack_consumed, 0);
152 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
154 const std::string nested_mangled_name2 = NestedMangledName(2);
155 demangled = DemangleStackConsumption(nested_mangled_name2.c_str(),
157 EXPECT_STREQ(
"a<>", demangled);
158 EXPECT_GT(stack_consumed, 0);
159 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
161 const std::string nested_mangled_name3 = NestedMangledName(3);
162 demangled = DemangleStackConsumption(nested_mangled_name3.c_str(),
164 EXPECT_STREQ(
"a<>", demangled);
165 EXPECT_GT(stack_consumed, 0);
166 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
169 #endif // Stack consumption tests 171 static void TestOnInput(
const char* input) {
172 static const int kOutSize = 1048576;
173 auto out = absl::make_unique<char[]>(kOutSize);
177 TEST(DemangleRegression, NegativeLength) {
178 TestOnInput(
"_ZZn4");
181 TEST(DemangleRegression, DeeplyNestedArrayType) {
182 const int depth = 100000;
183 std::string
data =
"_ZStI";
184 data.reserve(data.size() + 3 * depth + 1);
185 for (
int i = 0;
i < depth;
i++) {
188 TestOnInput(data.c_str());
#define ABSL_RAW_LOG(severity,...)
TEST(NotificationTest, SanityTest)
bool Demangle(const char *mangled, char *out, int out_size)
static char data[kDataSize]