Changes imported from Abseil "staging" branch:
- db061dd2b795837e6154be0991077ca5a03ec471 Release the async-signal safe demangler as an internal im... by Derek Mauro <dmauro@google.com> - 310440eb33d946df79d26bf1edf795f6a0b466b9 Use static_cast to constrain nanoseconds to int range, av... by Abseil Team <absl-team@google.com> GitOrigin-RevId: db061dd2b795837e6154be0991077ca5a03ec471 Change-Id: Ibc1bc1db355a48115451da5ce88d66a1f6e1a182
This commit is contained in:
parent
03c1513538
commit
dca2eb50f9
6 changed files with 2182 additions and 2 deletions
|
@ -72,7 +72,7 @@ int SpinLockSuggestedDelayNS(int loop) {
|
||||||
// Mean is exponential in loop for first 32 iterations, then 8ms.
|
// Mean is exponential in loop for first 32 iterations, then 8ms.
|
||||||
// The futex path multiplies this by 16, since we expect explicit wakeups
|
// The futex path multiplies this by 16, since we expect explicit wakeups
|
||||||
// almost always on that path.
|
// almost always on that path.
|
||||||
return r >> (44 - (loop >> 3));
|
return static_cast<int>(r >> (44 - (loop >> 3)));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace base_internal
|
} // namespace base_internal
|
||||||
|
|
|
@ -64,10 +64,34 @@ cc_library(
|
||||||
deps = [
|
deps = [
|
||||||
"//absl/base",
|
"//absl/base",
|
||||||
"//absl/base:dynamic_annotations",
|
"//absl/base:dynamic_annotations",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
cc_library(
|
||||||
|
name = "demangle_internal",
|
||||||
|
srcs = ["internal/demangle.cc"],
|
||||||
|
hdrs = ["internal/demangle.h"],
|
||||||
|
copts = ABSL_DEFAULT_COPTS,
|
||||||
|
deps = [
|
||||||
|
"//absl/base",
|
||||||
"//absl/base:core_headers",
|
"//absl/base:core_headers",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
cc_test(
|
||||||
|
name = "demangle_test",
|
||||||
|
srcs = ["internal/demangle_test.cc"],
|
||||||
|
copts = ABSL_TEST_COPTS,
|
||||||
|
deps = [
|
||||||
|
":demangle_internal",
|
||||||
|
":stack_consumption",
|
||||||
|
"//absl/base",
|
||||||
|
"//absl/base:core_headers",
|
||||||
|
"//absl/memory",
|
||||||
|
"@com_google_googletest//:gtest_main",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
cc_library(
|
cc_library(
|
||||||
name = "leak_check",
|
name = "leak_check",
|
||||||
srcs = select({
|
srcs = select({
|
||||||
|
|
|
@ -22,6 +22,7 @@ list(APPEND DEBUGGING_PUBLIC_HEADERS
|
||||||
|
|
||||||
list(APPEND DEBUGGING_INTERNAL_HEADERS
|
list(APPEND DEBUGGING_INTERNAL_HEADERS
|
||||||
"internal/address_is_readable.h"
|
"internal/address_is_readable.h"
|
||||||
|
"internal/demangle.h"
|
||||||
"internal/elf_mem_image.h"
|
"internal/elf_mem_image.h"
|
||||||
"internal/stacktrace_config.h"
|
"internal/stacktrace_config.h"
|
||||||
"internal/vdso_support.h"
|
"internal/vdso_support.h"
|
||||||
|
@ -31,6 +32,7 @@ list(APPEND DEBUGGING_INTERNAL_HEADERS
|
||||||
list(APPEND STACKTRACE_SRC
|
list(APPEND STACKTRACE_SRC
|
||||||
"stacktrace.cc"
|
"stacktrace.cc"
|
||||||
"internal/address_is_readable.cc"
|
"internal/address_is_readable.cc"
|
||||||
|
"internal/demangle.cc"
|
||||||
"internal/elf_mem_image.cc"
|
"internal/elf_mem_image.cc"
|
||||||
"internal/vdso_support.cc"
|
"internal/vdso_support.cc"
|
||||||
${DEBUGGING_PUBLIC_HEADERS}
|
${DEBUGGING_PUBLIC_HEADERS}
|
||||||
|
@ -79,6 +81,41 @@ absl_header_library(
|
||||||
## TESTS
|
## TESTS
|
||||||
#
|
#
|
||||||
|
|
||||||
|
list(APPEND DEBUGGING_INTERNAL_TEST_HEADERS
|
||||||
|
"internal/stack_consumption.h"
|
||||||
|
)
|
||||||
|
|
||||||
|
list(APPEND STACK_CONSUMPTION_SRC
|
||||||
|
"internal/stack_consumption.cc"
|
||||||
|
${DEBUGGING_INTERNAL_TEST_HEADERS}
|
||||||
|
)
|
||||||
|
|
||||||
|
absl_library(
|
||||||
|
TARGET
|
||||||
|
absl_stack_consumption
|
||||||
|
SOURCES
|
||||||
|
${STACK_CONSUMPTION_SRC}
|
||||||
|
)
|
||||||
|
|
||||||
|
absl_test(
|
||||||
|
TARGET
|
||||||
|
absl_stack_consumption_test
|
||||||
|
SOURCES
|
||||||
|
${STACK_CONSUMPTION_SRC}
|
||||||
|
)
|
||||||
|
|
||||||
|
list(APPEND DEMANGLE_TEST_SRC "demangle_test.cc")
|
||||||
|
|
||||||
|
absl_test(
|
||||||
|
TARGET
|
||||||
|
demangle_test
|
||||||
|
SOURCES
|
||||||
|
${DEMANGLE_TEST_SRC}
|
||||||
|
PUBLIC_LIBRARIES
|
||||||
|
absl_stacktrace absl_stack_consumption
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# test leak_check_test
|
# test leak_check_test
|
||||||
list(APPEND LEAK_CHECK_TEST_SRC "leak_check_test.cc")
|
list(APPEND LEAK_CHECK_TEST_SRC "leak_check_test.cc")
|
||||||
|
|
||||||
|
@ -90,4 +127,3 @@ absl_test(
|
||||||
PUBLIC_LIBRARIES
|
PUBLIC_LIBRARIES
|
||||||
absl_leak_check
|
absl_leak_check
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
1862
absl/debugging/internal/demangle.cc
Normal file
1862
absl/debugging/internal/demangle.cc
Normal file
File diff suppressed because it is too large
Load diff
67
absl/debugging/internal/demangle.h
Normal file
67
absl/debugging/internal/demangle.h
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
// Copyright 2018 The Abseil Authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// An async-signal-safe and thread-safe demangler for Itanium C++ ABI
|
||||||
|
// (aka G++ V3 ABI).
|
||||||
|
//
|
||||||
|
// The demangler is implemented to be used in async signal handlers to
|
||||||
|
// symbolize stack traces. We cannot use libstdc++'s
|
||||||
|
// abi::__cxa_demangle() in such signal handlers since it's not async
|
||||||
|
// signal safe (it uses malloc() internally).
|
||||||
|
//
|
||||||
|
// Note that this demangler doesn't support full demangling. More
|
||||||
|
// specifically, it doesn't print types of function parameters and
|
||||||
|
// types of template arguments. It just skips them. However, it's
|
||||||
|
// still very useful to extract basic information such as class,
|
||||||
|
// function, constructor, destructor, and operator names.
|
||||||
|
//
|
||||||
|
// See the implementation note in demangle.cc if you are interested.
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
//
|
||||||
|
// | Mangled Name | The Demangler | abi::__cxa_demangle()
|
||||||
|
// |---------------|---------------|-----------------------
|
||||||
|
// | _Z1fv | f() | f()
|
||||||
|
// | _Z1fi | f() | f(int)
|
||||||
|
// | _Z3foo3bar | foo() | foo(bar)
|
||||||
|
// | _Z1fIiEvi | f<>() | void f<int>(int)
|
||||||
|
// | _ZN1N1fE | N::f | N::f
|
||||||
|
// | _ZN3Foo3BarEv | Foo::Bar() | Foo::Bar()
|
||||||
|
// | _Zrm1XS_" | operator%() | operator%(X, X)
|
||||||
|
// | _ZN3FooC1Ev | Foo::Foo() | Foo::Foo()
|
||||||
|
// | _Z1fSs | f() | f(std::basic_string<char,
|
||||||
|
// | | | std::char_traits<char>,
|
||||||
|
// | | | std::allocator<char> >)
|
||||||
|
//
|
||||||
|
// See the unit test for more examples.
|
||||||
|
//
|
||||||
|
// Note: we might want to write demanglers for ABIs other than Itanium
|
||||||
|
// C++ ABI in the future.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef ABSL_DEBUGGING_INTERNAL_DEMANGLE_H_
|
||||||
|
#define ABSL_DEBUGGING_INTERNAL_DEMANGLE_H_
|
||||||
|
|
||||||
|
namespace absl {
|
||||||
|
namespace debugging_internal {
|
||||||
|
|
||||||
|
// Demangle `mangled`. On success, return true and write the
|
||||||
|
// demangled symbol name to `out`. Otherwise, return false.
|
||||||
|
// `out` is modified even if demangling is unsuccessful.
|
||||||
|
bool Demangle(const char *mangled, char *out, int out_size);
|
||||||
|
|
||||||
|
} // namespace debugging_internal
|
||||||
|
} // namespace absl
|
||||||
|
|
||||||
|
#endif // ABSL_DEBUGGING_INTERNAL_DEMANGLE_H_
|
191
absl/debugging/internal/demangle_test.cc
Normal file
191
absl/debugging/internal/demangle_test.cc
Normal file
|
@ -0,0 +1,191 @@
|
||||||
|
// Copyright 2018 The Abseil Authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include "absl/debugging/internal/demangle.h"
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "absl/base/internal/raw_logging.h"
|
||||||
|
#include "absl/debugging/internal/stack_consumption.h"
|
||||||
|
#include "absl/memory/memory.h"
|
||||||
|
|
||||||
|
namespace absl {
|
||||||
|
namespace debugging_internal {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
// A wrapper function for Demangle() to make the unit test simple.
|
||||||
|
static const char *DemangleIt(const char * const mangled) {
|
||||||
|
static char demangled[4096];
|
||||||
|
if (Demangle(mangled, demangled, sizeof(demangled))) {
|
||||||
|
return demangled;
|
||||||
|
} else {
|
||||||
|
return mangled;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test corner cases of bounary conditions.
|
||||||
|
TEST(Demangle, CornerCases) {
|
||||||
|
char tmp[10];
|
||||||
|
EXPECT_TRUE(Demangle("_Z6foobarv", tmp, sizeof(tmp)));
|
||||||
|
// sizeof("foobar()") == 9
|
||||||
|
EXPECT_STREQ("foobar()", tmp);
|
||||||
|
EXPECT_TRUE(Demangle("_Z6foobarv", tmp, 9));
|
||||||
|
EXPECT_STREQ("foobar()", tmp);
|
||||||
|
EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 8)); // Not enough.
|
||||||
|
EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 1));
|
||||||
|
EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 0));
|
||||||
|
EXPECT_FALSE(Demangle("_Z6foobarv", nullptr, 0)); // Should not cause SEGV.
|
||||||
|
EXPECT_FALSE(Demangle("_Z1000000", tmp, 9));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test handling of functions suffixed with .clone.N, which is used
|
||||||
|
// by GCC 4.5.x (and our locally-modified version of GCC 4.4.x), and
|
||||||
|
// .constprop.N and .isra.N, which are used by GCC 4.6.x. These
|
||||||
|
// suffixes are used to indicate functions which have been cloned
|
||||||
|
// during optimization. We ignore these suffixes.
|
||||||
|
TEST(Demangle, Clones) {
|
||||||
|
char tmp[20];
|
||||||
|
EXPECT_TRUE(Demangle("_ZL3Foov", tmp, sizeof(tmp)));
|
||||||
|
EXPECT_STREQ("Foo()", tmp);
|
||||||
|
EXPECT_TRUE(Demangle("_ZL3Foov.clone.3", tmp, sizeof(tmp)));
|
||||||
|
EXPECT_STREQ("Foo()", tmp);
|
||||||
|
EXPECT_TRUE(Demangle("_ZL3Foov.constprop.80", tmp, sizeof(tmp)));
|
||||||
|
EXPECT_STREQ("Foo()", tmp);
|
||||||
|
EXPECT_TRUE(Demangle("_ZL3Foov.isra.18", tmp, sizeof(tmp)));
|
||||||
|
EXPECT_STREQ("Foo()", tmp);
|
||||||
|
EXPECT_TRUE(Demangle("_ZL3Foov.isra.2.constprop.18", tmp, sizeof(tmp)));
|
||||||
|
EXPECT_STREQ("Foo()", tmp);
|
||||||
|
// Invalid (truncated), should not demangle.
|
||||||
|
EXPECT_FALSE(Demangle("_ZL3Foov.clo", tmp, sizeof(tmp)));
|
||||||
|
// Invalid (.clone. not followed by number), should not demangle.
|
||||||
|
EXPECT_FALSE(Demangle("_ZL3Foov.clone.", tmp, sizeof(tmp)));
|
||||||
|
// Invalid (.clone. followed by non-number), should not demangle.
|
||||||
|
EXPECT_FALSE(Demangle("_ZL3Foov.clone.foo", tmp, sizeof(tmp)));
|
||||||
|
// Invalid (.constprop. not followed by number), should not demangle.
|
||||||
|
EXPECT_FALSE(Demangle("_ZL3Foov.isra.2.constprop.", tmp, sizeof(tmp)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that verify that Demangle footprint is within some limit.
|
||||||
|
// They are not to be run under sanitizers as the sanitizers increase
|
||||||
|
// stack consumption by about 4x.
|
||||||
|
#if defined(ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION) && \
|
||||||
|
!ADDRESS_SANITIZER && !MEMORY_SANITIZER && !THREAD_SANITIZER
|
||||||
|
|
||||||
|
static const char *g_mangled;
|
||||||
|
static char g_demangle_buffer[4096];
|
||||||
|
static char *g_demangle_result;
|
||||||
|
|
||||||
|
static void DemangleSignalHandler(int signo) {
|
||||||
|
if (Demangle(g_mangled, g_demangle_buffer, sizeof(g_demangle_buffer))) {
|
||||||
|
g_demangle_result = g_demangle_buffer;
|
||||||
|
} else {
|
||||||
|
g_demangle_result = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call Demangle and figure out the stack footprint of this call.
|
||||||
|
static const char *DemangleStackConsumption(const char *mangled,
|
||||||
|
int *stack_consumed) {
|
||||||
|
g_mangled = mangled;
|
||||||
|
*stack_consumed = GetSignalHandlerStackConsumption(DemangleSignalHandler);
|
||||||
|
ABSL_RAW_LOG(INFO, "Stack consumption of Demangle: %d", *stack_consumed);
|
||||||
|
return g_demangle_result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Demangle stack consumption should be within 8kB for simple mangled names
|
||||||
|
// with some level of nesting. With alternate signal stack we have 64K,
|
||||||
|
// but some signal handlers run on thread stack, and could have arbitrarily
|
||||||
|
// little space left (so we don't want to make this number too large).
|
||||||
|
const int kStackConsumptionUpperLimit = 8192;
|
||||||
|
|
||||||
|
// Returns a mangled name nested to the given depth.
|
||||||
|
static std::string NestedMangledName(int depth) {
|
||||||
|
std::string mangled_name = "_Z1a";
|
||||||
|
if (depth > 0) {
|
||||||
|
mangled_name += "IXL";
|
||||||
|
mangled_name += NestedMangledName(depth - 1);
|
||||||
|
mangled_name += "EEE";
|
||||||
|
}
|
||||||
|
return mangled_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Demangle, DemangleStackConsumption) {
|
||||||
|
// Measure stack consumption of Demangle for nested mangled names of varying
|
||||||
|
// depth. Since Demangle is implemented as a recursive descent parser,
|
||||||
|
// stack consumption will grow as the nesting depth increases. By measuring
|
||||||
|
// the stack consumption for increasing depths, we can see the growing
|
||||||
|
// impact of any stack-saving changes made to the code for Demangle.
|
||||||
|
int stack_consumed = 0;
|
||||||
|
|
||||||
|
const char *demangled =
|
||||||
|
DemangleStackConsumption("_Z6foobarv", &stack_consumed);
|
||||||
|
EXPECT_STREQ("foobar()", demangled);
|
||||||
|
EXPECT_GT(stack_consumed, 0);
|
||||||
|
EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
|
||||||
|
|
||||||
|
const std::string nested_mangled_name0 = NestedMangledName(0);
|
||||||
|
demangled = DemangleStackConsumption(nested_mangled_name0.c_str(),
|
||||||
|
&stack_consumed);
|
||||||
|
EXPECT_STREQ("a", demangled);
|
||||||
|
EXPECT_GT(stack_consumed, 0);
|
||||||
|
EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
|
||||||
|
|
||||||
|
const std::string nested_mangled_name1 = NestedMangledName(1);
|
||||||
|
demangled = DemangleStackConsumption(nested_mangled_name1.c_str(),
|
||||||
|
&stack_consumed);
|
||||||
|
EXPECT_STREQ("a<>", demangled);
|
||||||
|
EXPECT_GT(stack_consumed, 0);
|
||||||
|
EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
|
||||||
|
|
||||||
|
const std::string nested_mangled_name2 = NestedMangledName(2);
|
||||||
|
demangled = DemangleStackConsumption(nested_mangled_name2.c_str(),
|
||||||
|
&stack_consumed);
|
||||||
|
EXPECT_STREQ("a<>", demangled);
|
||||||
|
EXPECT_GT(stack_consumed, 0);
|
||||||
|
EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
|
||||||
|
|
||||||
|
const std::string nested_mangled_name3 = NestedMangledName(3);
|
||||||
|
demangled = DemangleStackConsumption(nested_mangled_name3.c_str(),
|
||||||
|
&stack_consumed);
|
||||||
|
EXPECT_STREQ("a<>", demangled);
|
||||||
|
EXPECT_GT(stack_consumed, 0);
|
||||||
|
EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // Stack consumption tests
|
||||||
|
|
||||||
|
static void TestOnInput(const char* input) {
|
||||||
|
static const int kOutSize = 1048576;
|
||||||
|
auto out = absl::make_unique<char[]>(kOutSize);
|
||||||
|
Demangle(input, out.get(), kOutSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DemangleRegression, NegativeLength) {
|
||||||
|
TestOnInput("_ZZn4");
|
||||||
|
}
|
||||||
|
TEST(DemangleRegression, DeeplyNestedArrayType) {
|
||||||
|
const int depth = 100000;
|
||||||
|
std::string data = "_ZStI";
|
||||||
|
data.reserve(data.size() + 3 * depth + 1);
|
||||||
|
for (int i = 0; i < depth; i++) {
|
||||||
|
data += "A1_";
|
||||||
|
}
|
||||||
|
TestOnInput(data.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace debugging_internal
|
||||||
|
} // namespace absl
|
Loading…
Reference in a new issue