Commit 28f5b890 by Abseil Team Committed by Derek Mauro

- 81cdce434ff1bd8fa54c832a11dda59af46e79cc Adds a failure signal handler to…

  - 81cdce434ff1bd8fa54c832a11dda59af46e79cc Adds a failure signal handler to Abseil. by Derek Mauro <dmauro@google.com>
  - 40a973dd1b159e7455dd5fc06ac2d3f494d72c3e Remove test fixture requirement for ExceptionSafetyTester... by Abseil Team <absl-team@google.com>

GitOrigin-RevId: 81cdce434ff1bd8fa54c832a11dda59af46e79cc
Change-Id: Ia9fca98e38f229b68f7ec45600dee1bbd5dcff33
parent ea0e750e
...@@ -37,8 +37,6 @@ ...@@ -37,8 +37,6 @@
namespace absl { namespace absl {
struct ConstructorTracker;
// A configuration enum for Throwing*. Operations whose flags are set will // A configuration enum for Throwing*. Operations whose flags are set will
// throw, everything else won't. This isn't meant to be exhaustive, more flags // throw, everything else won't. This isn't meant to be exhaustive, more flags
// can always be made in the future. // can always be made in the future.
...@@ -105,6 +103,8 @@ void MaybeThrow(absl::string_view msg, bool throw_bad_alloc = false); ...@@ -105,6 +103,8 @@ void MaybeThrow(absl::string_view msg, bool throw_bad_alloc = false);
testing::AssertionResult FailureMessage(const TestException& e, testing::AssertionResult FailureMessage(const TestException& e,
int countdown) noexcept; int countdown) noexcept;
class ConstructorTracker;
class TrackedObject { class TrackedObject {
public: public:
TrackedObject(const TrackedObject&) = delete; TrackedObject(const TrackedObject&) = delete;
...@@ -112,26 +112,56 @@ class TrackedObject { ...@@ -112,26 +112,56 @@ class TrackedObject {
protected: protected:
explicit TrackedObject(const char* child_ctor) { explicit TrackedObject(const char* child_ctor) {
if (!GetAllocs().emplace(this, child_ctor).second) { if (!GetInstanceMap().emplace(this, child_ctor).second) {
ADD_FAILURE() << "Object at address " << static_cast<void*>(this) ADD_FAILURE() << "Object at address " << static_cast<void*>(this)
<< " re-constructed in ctor " << child_ctor; << " re-constructed in ctor " << child_ctor;
} }
} }
static std::unordered_map<TrackedObject*, absl::string_view>& GetAllocs() {
static auto* m =
new std::unordered_map<TrackedObject*, absl::string_view>();
return *m;
}
~TrackedObject() noexcept { ~TrackedObject() noexcept {
if (GetAllocs().erase(this) == 0) { if (GetInstanceMap().erase(this) == 0) {
ADD_FAILURE() << "Object at address " << static_cast<void*>(this) ADD_FAILURE() << "Object at address " << static_cast<void*>(this)
<< " destroyed improperly"; << " destroyed improperly";
} }
} }
friend struct ::absl::ConstructorTracker; private:
using InstanceMap = std::unordered_map<TrackedObject*, absl::string_view>;
static InstanceMap& GetInstanceMap() {
static auto* instance_map = new InstanceMap();
return *instance_map;
}
friend class ConstructorTracker;
};
// Inspects the constructions and destructions of anything inheriting from
// TrackedObject. This allows us to safely "leak" TrackedObjects, as
// ConstructorTracker will destroy everything left over in its destructor.
class ConstructorTracker {
public:
explicit ConstructorTracker(int c)
: init_count_(c), init_instances_(TrackedObject::GetInstanceMap()) {}
~ConstructorTracker() {
auto& cur_instances = TrackedObject::GetInstanceMap();
for (auto it = cur_instances.begin(); it != cur_instances.end();) {
if (init_instances_.count(it->first) == 0) {
ADD_FAILURE() << "Object at address " << static_cast<void*>(it->first)
<< " constructed from " << it->second
<< " where the exception countdown was set to "
<< init_count_ << " was not destroyed";
// Erasing an item inside an unordered_map invalidates the existing
// iterator. A new one is returned for iteration to continue.
it = cur_instances.erase(it);
} else {
++it;
}
}
}
private:
int init_count_;
TrackedObject::InstanceMap init_instances_;
}; };
template <typename Factory, typename Operation, typename Invariant> template <typename Factory, typename Operation, typename Invariant>
...@@ -707,37 +737,21 @@ class ThrowingAllocator : private exceptions_internal::TrackedObject { ...@@ -707,37 +737,21 @@ class ThrowingAllocator : private exceptions_internal::TrackedObject {
template <typename T, NoThrow Throws> template <typename T, NoThrow Throws>
int ThrowingAllocator<T, Throws>::next_id_ = 0; int ThrowingAllocator<T, Throws>::next_id_ = 0;
// Inspects the constructions and destructions of anything inheriting from
// TrackedObject. Place this as a member variable in a test fixture to ensure
// that every ThrowingValue was constructed and destroyed correctly. This also
// allows us to safely "leak" TrackedObjects, as ConstructorTracker will destroy
// everything left over in its destructor.
struct ConstructorTracker {
ConstructorTracker() = default;
~ConstructorTracker() {
auto& allocs = exceptions_internal::TrackedObject::GetAllocs();
for (const auto& kv : allocs) {
ADD_FAILURE() << "Object at address " << static_cast<void*>(kv.first)
<< " constructed from " << kv.second << " not destroyed";
}
allocs.clear();
}
};
// Tests for resource leaks by attempting to construct a T using args repeatedly // Tests for resource leaks by attempting to construct a T using args repeatedly
// until successful, using the countdown method. Side effects can then be // until successful, using the countdown method. Side effects can then be
// tested for resource leaks. If a ConstructorTracker is present in the test // tested for resource leaks.
// fixture, then this will also test that memory resources are not leaked as
// long as T allocates TrackedObjects.
template <typename T, typename... Args> template <typename T, typename... Args>
T TestThrowingCtor(Args&&... args) { void TestThrowingCtor(Args&&... args) {
struct Cleanup { struct Cleanup {
~Cleanup() { exceptions_internal::UnsetCountdown(); } ~Cleanup() { exceptions_internal::UnsetCountdown(); }
} c; } c;
for (int count = 0;; ++count) { for (int count = 0;; ++count) {
exceptions_internal::ConstructorTracker ct(count);
exceptions_internal::SetCountdown(count); exceptions_internal::SetCountdown(count);
try { try {
return T(std::forward<Args>(args)...); T temp(std::forward<Args>(args)...);
static_cast<void>(temp);
break;
} catch (const exceptions_internal::TestException&) { } catch (const exceptions_internal::TestException&) {
} }
} }
...@@ -934,6 +948,8 @@ class ExceptionSafetyTester { ...@@ -934,6 +948,8 @@ class ExceptionSafetyTester {
// Starting from 0 and counting upwards until one of the exit conditions is // Starting from 0 and counting upwards until one of the exit conditions is
// hit... // hit...
for (int count = 0;; ++count) { for (int count = 0;; ++count) {
exceptions_internal::ConstructorTracker ct(count);
// Run the full exception safety test algorithm for the current countdown // Run the full exception safety test algorithm for the current countdown
auto reduced_res = auto reduced_res =
TestAllInvariantsAtCountdown(factory_, selected_operation, count, TestAllInvariantsAtCountdown(factory_, selected_operation, count,
......
...@@ -95,6 +95,38 @@ cc_library( ...@@ -95,6 +95,38 @@ cc_library(
) )
cc_library( cc_library(
name = "failure_signal_handler",
srcs = ["failure_signal_handler.cc"],
hdrs = ["failure_signal_handler.h"],
copts = ABSL_DEFAULT_COPTS,
deps = [
":examine_stack",
":stacktrace",
"//absl/base",
"//absl/base:config",
"//absl/base:core_headers",
],
)
cc_test(
name = "failure_signal_handler_test",
srcs = ["failure_signal_handler_test.cc"],
copts = ABSL_TEST_COPTS,
linkopts = select({
"//absl:windows": [],
"//conditions:default": ["-pthread"],
}),
deps = [
":failure_signal_handler",
":stacktrace",
":symbolize",
"//absl/base",
"//absl/strings",
"@com_google_googletest//:gtest",
],
)
cc_library(
name = "debugging_internal", name = "debugging_internal",
srcs = [ srcs = [
"internal/address_is_readable.cc", "internal/address_is_readable.cc",
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
# #
list(APPEND DEBUGGING_PUBLIC_HEADERS list(APPEND DEBUGGING_PUBLIC_HEADERS
"failure_signal_handler.h"
"leak_check.h" "leak_check.h"
"stacktrace.h" "stacktrace.h"
"symbolize.h" "symbolize.h"
...@@ -51,6 +52,11 @@ list(APPEND SYMBOLIZE_SRC ...@@ -51,6 +52,11 @@ list(APPEND SYMBOLIZE_SRC
${DEBUGGING_INTERNAL_HEADERS} ${DEBUGGING_INTERNAL_HEADERS}
) )
list(APPEND FAILURE_SIGNAL_HANDLER_SRC
"failure_signal_handler.cc"
${DEBUGGING_PUBLIC_HEADERS}
)
list(APPEND EXAMINE_STACK_SRC list(APPEND EXAMINE_STACK_SRC
"internal/examine_stack.cc" "internal/examine_stack.cc"
${DEBUGGING_PUBLIC_HEADERS} ${DEBUGGING_PUBLIC_HEADERS}
...@@ -75,6 +81,17 @@ absl_library( ...@@ -75,6 +81,17 @@ absl_library(
symbolize symbolize
) )
absl_library(
TARGET
absl_failure_signal_handler
SOURCES
${FAILURE_SIGNAL_HANDLER_SRC}
PUBLIC_LIBRARIES
absl_base absl_synchronization
EXPORT_NAME
failure_signal_handler
)
# Internal-only. Projects external to Abseil should not depend # Internal-only. Projects external to Abseil should not depend
# directly on this library. # directly on this library.
absl_library( absl_library(
...@@ -163,6 +180,17 @@ absl_test( ...@@ -163,6 +180,17 @@ absl_test(
absl_symbolize absl_stack_consumption absl_symbolize absl_stack_consumption
) )
list(APPEND FAILURE_SIGNAL_HANDLER_TEST_SRC "failure_signal_handler_test.cc")
absl_test(
TARGET
failure_signal_handler_test
SOURCES
${FAILURE_SIGNAL_HANDLER_TEST_SRC}
PUBLIC_LIBRARIES
absl_examine_stack absl_stacktrace absl_symbolize
)
# 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")
......
//
// 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.
//
// This module allows the programmer to install a signal handler that
// dumps useful debugging information (like a stacktrace) on program
// failure. To use this functionality, call
// absl::InstallFailureSignalHandler() very early in your program,
// usually in the first few lines of main():
//
// int main(int argc, char** argv) {
// absl::InitializeSymbolizer(argv[0]);
// absl::FailureSignalHandlerOptions options;
// absl::InstallFailureSignalHandler(options);
// DoSomethingInteresting();
// return 0;
// }
#ifndef ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_
#define ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_
namespace absl {
// Options struct for absl::InstallFailureSignalHandler().
struct FailureSignalHandlerOptions {
// If true, try to symbolize the stacktrace emitted on failure.
bool symbolize_stacktrace = true;
// If true, try to run signal handlers on an alternate stack (if
// supported on the given platform). This is useful in the case
// where the program crashes due to a stack overflow. By running on
// a alternate stack, the signal handler might be able to run even
// when the normal stack space has been exausted. The downside of
// using an alternate stack is that extra memory for the alternate
// stack needs to be pre-allocated.
bool use_alternate_stack = true;
// If positive, FailureSignalHandler() sets an alarm to be delivered
// to the program after this many seconds, which will immediately
// abort the program. This is useful in the potential case where
// FailureSignalHandler() itself is hung or deadlocked.
int alarm_on_failure_secs = 3;
// If false, after absl::FailureSignalHandler() runs, the signal is
// raised to the default handler for that signal (which normally
// terminates the program).
//
// If true, after absl::FailureSignalHandler() runs, it will call
// the previously registered signal handler for the signal that was
// received (if one was registered). This can be used to chain
// signal handlers.
//
// IMPORTANT: If true, the chained fatal signal handlers must not
// try to recover from the fatal signal. Instead, they should
// terminate the program via some mechanism, like raising the
// default handler for the signal, or by calling _exit().
// absl::FailureSignalHandler() may put parts of the Abseil
// library into a state that cannot be recovered from.
bool call_previous_handler = false;
// If not null, this function may be called with a std::string argument
// containing failure data. This function is used as a hook to write
// the failure data to a secondary location, for instance, to a log
// file. This function may also be called with a null data
// argument. This is a hint that this is a good time to flush any
// buffered data before the program may be terminated. Consider
// flushing any buffered data in all calls to this function.
//
// Since this function runs in a signal handler, it should be
// async-signal-safe if possible.
// See http://man7.org/linux/man-pages/man7/signal-safety.7.html
void (*writerfn)(const char*) = nullptr;
};
// Installs a signal handler for the common failure signals SIGSEGV,
// SIGILL, SIGFPE, SIGABRT, SIGTERM, SIGBUG, and SIGTRAP (if they
// exist on the given platform). The signal handler dumps program
// failure data in a unspecified format to stderr. The data dumped by
// the signal handler includes information that may be useful in
// debugging the failure. This may include the program counter, a
// stacktrace, and register information on some systems. Do not rely
// on the exact format of the output; it is subject to change.
void InstallFailureSignalHandler(const FailureSignalHandlerOptions& options);
namespace debugging_internal {
const char* FailureSignalToString(int signo);
} // namespace debugging_internal
} // namespace absl
#endif // ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_
//
// 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/failure_signal_handler.h"
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include "gtest/gtest.h"
#include "absl/base/internal/raw_logging.h"
#include "absl/debugging/stacktrace.h"
#include "absl/debugging/symbolize.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
namespace {
#if GTEST_HAS_DEATH_TEST
// For the parameterized death tests. GetParam() returns the signal number.
using FailureSignalHandlerDeathTest = ::testing::TestWithParam<int>;
// This function runs in a fork()ed process on most systems.
void InstallHandlerAndRaise(int signo) {
absl::InstallFailureSignalHandler(absl::FailureSignalHandlerOptions());
raise(signo);
}
TEST_P(FailureSignalHandlerDeathTest, AbslFailureSignal) {
const int signo = GetParam();
std::string exit_regex = absl::StrCat(
"\\*\\*\\* ", absl::debugging_internal::FailureSignalToString(signo),
" received at time=");
#ifndef _WIN32
EXPECT_EXIT(InstallHandlerAndRaise(signo), testing::KilledBySignal(signo),
exit_regex);
#else
// Windows doesn't have testing::KilledBySignal().
EXPECT_DEATH(InstallHandlerAndRaise(signo), exit_regex);
#endif
}
ABSL_CONST_INIT FILE* error_file = nullptr;
void WriteToErrorFile(const char* msg) {
if (msg != nullptr) {
ABSL_RAW_CHECK(fwrite(msg, strlen(msg), 1, error_file) == 1,
"fwrite() failed");
}
ABSL_RAW_CHECK(fflush(error_file) == 0, "fflush() failed");
}
std::string GetTmpDir() {
// TEST_TMPDIR is set by Bazel. Try the others when not running under Bazel.
static const char* const kTmpEnvVars[] = {"TEST_TMPDIR", "TMPDIR", "TEMP",
"TEMPDIR", "TMP"};
for (const char* const var : kTmpEnvVars) {
const char* tmp_dir = std::getenv(var);
if (tmp_dir != nullptr) {
return tmp_dir;
}
}
// Try something reasonable.
return "/tmp";
}
// This function runs in a fork()ed process on most systems.
void InstallHandlerWithWriteToFileAndRaise(const char* file, int signo) {
error_file = fopen(file, "w");
ABSL_RAW_CHECK(error_file != nullptr, "Failed create error_file");
absl::FailureSignalHandlerOptions options;
options.writerfn = WriteToErrorFile;
absl::InstallFailureSignalHandler(options);
raise(signo);
}
TEST_P(FailureSignalHandlerDeathTest, AbslFatalSignalsWithWriterFn) {
const int signo = GetParam();
std::string tmp_dir = GetTmpDir();
std::string file = absl::StrCat(tmp_dir, "/signo_", signo);
std::string exit_regex = absl::StrCat(
"\\*\\*\\* ", absl::debugging_internal::FailureSignalToString(signo),
" received at time=");
#ifndef _WIN32
EXPECT_EXIT(InstallHandlerWithWriteToFileAndRaise(file.c_str(), signo),
testing::KilledBySignal(signo), exit_regex);
#else
// Windows doesn't have testing::KilledBySignal().
EXPECT_DEATH(InstallHandlerWithWriteToFileAndRaise(file.c_str(), signo),
exit_regex);
#endif
// Open the file in this process and check its contents.
std::fstream error_output(file);
ASSERT_TRUE(error_output.is_open()) << file;
std::string error_line;
std::getline(error_output, error_line);
EXPECT_TRUE(absl::StartsWith(
error_line,
absl::StrCat("*** ",
absl::debugging_internal::FailureSignalToString(signo),
" received at ")));
if (absl::debugging_internal::StackTraceWorksForTest()) {
std::getline(error_output, error_line);
EXPECT_TRUE(absl::StartsWith(error_line, "PC: "));
}
}
constexpr int kFailureSignals[] = {
SIGSEGV, SIGILL, SIGFPE, SIGABRT, SIGTERM,
#ifndef _WIN32
SIGBUS, SIGTRAP,
#endif
};
INSTANTIATE_TEST_CASE_P(AbslDeathTest, FailureSignalHandlerDeathTest,
::testing::ValuesIn(kFailureSignals));
#endif // GTEST_HAS_DEATH_TEST
} // namespace
int main(int argc, char** argv) {
absl::InitializeSymbolizer(argv[0]);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
...@@ -30,11 +30,6 @@ using ThrowingThrowerVec = std::vector<Thrower, ThrowingAlloc>; ...@@ -30,11 +30,6 @@ using ThrowingThrowerVec = std::vector<Thrower, ThrowingAlloc>;
namespace { namespace {
class AnyExceptionSafety : public ::testing::Test {
private:
absl::ConstructorTracker inspector_;
};
testing::AssertionResult AnyInvariants(absl::any* a) { testing::AssertionResult AnyInvariants(absl::any* a) {
using testing::AssertionFailure; using testing::AssertionFailure;
using testing::AssertionSuccess; using testing::AssertionSuccess;
...@@ -84,22 +79,24 @@ testing::AssertionResult AnyIsEmpty(absl::any* a) { ...@@ -84,22 +79,24 @@ testing::AssertionResult AnyIsEmpty(absl::any* a) {
<< absl::any_cast<Thrower>(*a).Get(); << absl::any_cast<Thrower>(*a).Get();
} }
TEST_F(AnyExceptionSafety, Ctors) { TEST(AnyExceptionSafety, Ctors) {
Thrower val(1); Thrower val(1);
auto with_val = absl::TestThrowingCtor<absl::any>(val); absl::TestThrowingCtor<absl::any>(val);
auto copy = absl::TestThrowingCtor<absl::any>(with_val);
auto in_place = Thrower copy(val);
absl::TestThrowingCtor<absl::any>(absl::in_place_type_t<Thrower>(), 1); absl::TestThrowingCtor<absl::any>(copy);
auto in_place_list = absl::TestThrowingCtor<absl::any>(
absl::in_place_type_t<ThrowerVec>(), ThrowerList{val}); absl::TestThrowingCtor<absl::any>(absl::in_place_type_t<Thrower>(), 1);
auto in_place_list_again =
absl::TestThrowingCtor<absl::any, absl::TestThrowingCtor<absl::any>(absl::in_place_type_t<ThrowerVec>(),
absl::in_place_type_t<ThrowingThrowerVec>, ThrowerList{val});
ThrowerList, ThrowingAlloc>(
absl::in_place_type_t<ThrowingThrowerVec>(), {val}, ThrowingAlloc()); absl::TestThrowingCtor<absl::any, absl::in_place_type_t<ThrowingThrowerVec>,
ThrowerList, ThrowingAlloc>(
absl::in_place_type_t<ThrowingThrowerVec>(), {val}, ThrowingAlloc());
} }
TEST_F(AnyExceptionSafety, Assignment) { TEST(AnyExceptionSafety, Assignment) {
auto original = auto original =
absl::any(absl::in_place_type_t<Thrower>(), 1, absl::no_throw_ctor); absl::any(absl::in_place_type_t<Thrower>(), 1, absl::no_throw_ctor);
auto any_is_strong = [original](absl::any* ap) { auto any_is_strong = [original](absl::any* ap) {
...@@ -139,7 +136,7 @@ TEST_F(AnyExceptionSafety, Assignment) { ...@@ -139,7 +136,7 @@ TEST_F(AnyExceptionSafety, Assignment) {
} }
// libstdc++ std::any fails this test // libstdc++ std::any fails this test
#if !defined(ABSL_HAVE_STD_ANY) #if !defined(ABSL_HAVE_STD_ANY)
TEST_F(AnyExceptionSafety, Emplace) { TEST(AnyExceptionSafety, Emplace) {
auto initial_val = auto initial_val =
absl::any{absl::in_place_type_t<Thrower>(), 1, absl::no_throw_ctor}; absl::any{absl::in_place_type_t<Thrower>(), 1, absl::no_throw_ctor};
auto one_tester = absl::MakeExceptionSafetyTester() auto one_tester = absl::MakeExceptionSafetyTester()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment