demangle_test.cc
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/debugging/internal/demangle.h"
00016 
00017 #include <cstdlib>
00018 #include <string>
00019 
00020 #include "gtest/gtest.h"
00021 #include "absl/base/internal/raw_logging.h"
00022 #include "absl/debugging/internal/stack_consumption.h"
00023 #include "absl/memory/memory.h"
00024 
00025 namespace absl {
00026 namespace debugging_internal {
00027 namespace {
00028 
00029 // A wrapper function for Demangle() to make the unit test simple.
00030 static const char *DemangleIt(const char * const mangled) {
00031   static char demangled[4096];
00032   if (Demangle(mangled, demangled, sizeof(demangled))) {
00033     return demangled;
00034   } else {
00035     return mangled;
00036   }
00037 }
00038 
00039 // Test corner cases of bounary conditions.
00040 TEST(Demangle, CornerCases) {
00041   char tmp[10];
00042   EXPECT_TRUE(Demangle("_Z6foobarv", tmp, sizeof(tmp)));
00043   // sizeof("foobar()") == 9
00044   EXPECT_STREQ("foobar()", tmp);
00045   EXPECT_TRUE(Demangle("_Z6foobarv", tmp, 9));
00046   EXPECT_STREQ("foobar()", tmp);
00047   EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 8));  // Not enough.
00048   EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 1));
00049   EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 0));
00050   EXPECT_FALSE(Demangle("_Z6foobarv", nullptr, 0));  // Should not cause SEGV.
00051   EXPECT_FALSE(Demangle("_Z1000000", tmp, 9));
00052 }
00053 
00054 // Test handling of functions suffixed with .clone.N, which is used
00055 // by GCC 4.5.x (and our locally-modified version of GCC 4.4.x), and
00056 // .constprop.N and .isra.N, which are used by GCC 4.6.x.  These
00057 // suffixes are used to indicate functions which have been cloned
00058 // during optimization.  We ignore these suffixes.
00059 TEST(Demangle, Clones) {
00060   char tmp[20];
00061   EXPECT_TRUE(Demangle("_ZL3Foov", tmp, sizeof(tmp)));
00062   EXPECT_STREQ("Foo()", tmp);
00063   EXPECT_TRUE(Demangle("_ZL3Foov.clone.3", tmp, sizeof(tmp)));
00064   EXPECT_STREQ("Foo()", tmp);
00065   EXPECT_TRUE(Demangle("_ZL3Foov.constprop.80", tmp, sizeof(tmp)));
00066   EXPECT_STREQ("Foo()", tmp);
00067   EXPECT_TRUE(Demangle("_ZL3Foov.isra.18", tmp, sizeof(tmp)));
00068   EXPECT_STREQ("Foo()", tmp);
00069   EXPECT_TRUE(Demangle("_ZL3Foov.isra.2.constprop.18", tmp, sizeof(tmp)));
00070   EXPECT_STREQ("Foo()", tmp);
00071   // Invalid (truncated), should not demangle.
00072   EXPECT_FALSE(Demangle("_ZL3Foov.clo", tmp, sizeof(tmp)));
00073   // Invalid (.clone. not followed by number), should not demangle.
00074   EXPECT_FALSE(Demangle("_ZL3Foov.clone.", tmp, sizeof(tmp)));
00075   // Invalid (.clone. followed by non-number), should not demangle.
00076   EXPECT_FALSE(Demangle("_ZL3Foov.clone.foo", tmp, sizeof(tmp)));
00077   // Invalid (.constprop. not followed by number), should not demangle.
00078   EXPECT_FALSE(Demangle("_ZL3Foov.isra.2.constprop.", tmp, sizeof(tmp)));
00079 }
00080 
00081 // Tests that verify that Demangle footprint is within some limit.
00082 // They are not to be run under sanitizers as the sanitizers increase
00083 // stack consumption by about 4x.
00084 #if defined(ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION) &&   \
00085     !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && \
00086     !defined(THREAD_SANITIZER)
00087 
00088 static const char *g_mangled;
00089 static char g_demangle_buffer[4096];
00090 static char *g_demangle_result;
00091 
00092 static void DemangleSignalHandler(int signo) {
00093   if (Demangle(g_mangled, g_demangle_buffer, sizeof(g_demangle_buffer))) {
00094     g_demangle_result = g_demangle_buffer;
00095   } else {
00096     g_demangle_result = nullptr;
00097   }
00098 }
00099 
00100 // Call Demangle and figure out the stack footprint of this call.
00101 static const char *DemangleStackConsumption(const char *mangled,
00102                                             int *stack_consumed) {
00103   g_mangled = mangled;
00104   *stack_consumed = GetSignalHandlerStackConsumption(DemangleSignalHandler);
00105   ABSL_RAW_LOG(INFO, "Stack consumption of Demangle: %d", *stack_consumed);
00106   return g_demangle_result;
00107 }
00108 
00109 // Demangle stack consumption should be within 8kB for simple mangled names
00110 // with some level of nesting. With alternate signal stack we have 64K,
00111 // but some signal handlers run on thread stack, and could have arbitrarily
00112 // little space left (so we don't want to make this number too large).
00113 const int kStackConsumptionUpperLimit = 8192;
00114 
00115 // Returns a mangled name nested to the given depth.
00116 static std::string NestedMangledName(int depth) {
00117   std::string mangled_name = "_Z1a";
00118   if (depth > 0) {
00119     mangled_name += "IXL";
00120     mangled_name += NestedMangledName(depth - 1);
00121     mangled_name += "EEE";
00122   }
00123   return mangled_name;
00124 }
00125 
00126 TEST(Demangle, DemangleStackConsumption) {
00127   // Measure stack consumption of Demangle for nested mangled names of varying
00128   // depth.  Since Demangle is implemented as a recursive descent parser,
00129   // stack consumption will grow as the nesting depth increases.  By measuring
00130   // the stack consumption for increasing depths, we can see the growing
00131   // impact of any stack-saving changes made to the code for Demangle.
00132   int stack_consumed = 0;
00133 
00134   const char *demangled =
00135       DemangleStackConsumption("_Z6foobarv", &stack_consumed);
00136   EXPECT_STREQ("foobar()", demangled);
00137   EXPECT_GT(stack_consumed, 0);
00138   EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
00139 
00140   const std::string nested_mangled_name0 = NestedMangledName(0);
00141   demangled = DemangleStackConsumption(nested_mangled_name0.c_str(),
00142                                        &stack_consumed);
00143   EXPECT_STREQ("a", demangled);
00144   EXPECT_GT(stack_consumed, 0);
00145   EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
00146 
00147   const std::string nested_mangled_name1 = NestedMangledName(1);
00148   demangled = DemangleStackConsumption(nested_mangled_name1.c_str(),
00149                                        &stack_consumed);
00150   EXPECT_STREQ("a<>", demangled);
00151   EXPECT_GT(stack_consumed, 0);
00152   EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
00153 
00154   const std::string nested_mangled_name2 = NestedMangledName(2);
00155   demangled = DemangleStackConsumption(nested_mangled_name2.c_str(),
00156                                        &stack_consumed);
00157   EXPECT_STREQ("a<>", demangled);
00158   EXPECT_GT(stack_consumed, 0);
00159   EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
00160 
00161   const std::string nested_mangled_name3 = NestedMangledName(3);
00162   demangled = DemangleStackConsumption(nested_mangled_name3.c_str(),
00163                                        &stack_consumed);
00164   EXPECT_STREQ("a<>", demangled);
00165   EXPECT_GT(stack_consumed, 0);
00166   EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
00167 }
00168 
00169 #endif  // Stack consumption tests
00170 
00171 static void TestOnInput(const char* input) {
00172   static const int kOutSize = 1048576;
00173   auto out = absl::make_unique<char[]>(kOutSize);
00174   Demangle(input, out.get(), kOutSize);
00175 }
00176 
00177 TEST(DemangleRegression, NegativeLength) {
00178   TestOnInput("_ZZn4");
00179 }
00180 
00181 TEST(DemangleRegression, DeeplyNestedArrayType) {
00182   const int depth = 100000;
00183   std::string data = "_ZStI";
00184   data.reserve(data.size() + 3 * depth + 1);
00185   for (int i = 0; i < depth; i++) {
00186     data += "A1_";
00187   }
00188   TestOnInput(data.c_str());
00189 }
00190 
00191 }  // namespace
00192 }  // namespace debugging_internal
00193 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14