failure_signal_handler_test.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // Copyright 2018 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "absl/debugging/failure_signal_handler.h"
  17. #include <csignal>
  18. #include <cstdio>
  19. #include <cstdlib>
  20. #include <cstring>
  21. #include <fstream>
  22. #include "gtest/gtest.h"
  23. #include "gmock/gmock.h"
  24. #include "absl/base/internal/raw_logging.h"
  25. #include "absl/debugging/stacktrace.h"
  26. #include "absl/debugging/symbolize.h"
  27. #include "absl/strings/match.h"
  28. #include "absl/strings/str_cat.h"
  29. namespace {
  30. using testing::StartsWith;
  31. #if GTEST_HAS_DEATH_TEST
  32. // For the parameterized death tests. GetParam() returns the signal number.
  33. using FailureSignalHandlerDeathTest = ::testing::TestWithParam<int>;
  34. // This function runs in a fork()ed process on most systems.
  35. void InstallHandlerAndRaise(int signo) {
  36. absl::InstallFailureSignalHandler(absl::FailureSignalHandlerOptions());
  37. raise(signo);
  38. }
  39. TEST_P(FailureSignalHandlerDeathTest, AbslFailureSignal) {
  40. const int signo = GetParam();
  41. std::string exit_regex = absl::StrCat(
  42. "\\*\\*\\* ", absl::debugging_internal::FailureSignalToString(signo),
  43. " received at time=");
  44. #ifndef _WIN32
  45. EXPECT_EXIT(InstallHandlerAndRaise(signo), testing::KilledBySignal(signo),
  46. exit_regex);
  47. #else
  48. // Windows doesn't have testing::KilledBySignal().
  49. EXPECT_DEATH_IF_SUPPORTED(InstallHandlerAndRaise(signo), exit_regex);
  50. #endif
  51. }
  52. ABSL_CONST_INIT FILE* error_file = nullptr;
  53. void WriteToErrorFile(const char* msg) {
  54. if (msg != nullptr) {
  55. ABSL_RAW_CHECK(fwrite(msg, strlen(msg), 1, error_file) == 1,
  56. "fwrite() failed");
  57. }
  58. ABSL_RAW_CHECK(fflush(error_file) == 0, "fflush() failed");
  59. }
  60. std::string GetTmpDir() {
  61. // TEST_TMPDIR is set by Bazel. Try the others when not running under Bazel.
  62. static const char* const kTmpEnvVars[] = {"TEST_TMPDIR", "TMPDIR", "TEMP",
  63. "TEMPDIR", "TMP"};
  64. for (const char* const var : kTmpEnvVars) {
  65. const char* tmp_dir = std::getenv(var);
  66. if (tmp_dir != nullptr) {
  67. return tmp_dir;
  68. }
  69. }
  70. // Try something reasonable.
  71. return "/tmp";
  72. }
  73. // This function runs in a fork()ed process on most systems.
  74. void InstallHandlerWithWriteToFileAndRaise(const char* file, int signo) {
  75. error_file = fopen(file, "w");
  76. ABSL_RAW_CHECK(error_file != nullptr, "Failed create error_file");
  77. absl::FailureSignalHandlerOptions options;
  78. options.writerfn = WriteToErrorFile;
  79. absl::InstallFailureSignalHandler(options);
  80. raise(signo);
  81. }
  82. TEST_P(FailureSignalHandlerDeathTest, AbslFatalSignalsWithWriterFn) {
  83. const int signo = GetParam();
  84. std::string tmp_dir = GetTmpDir();
  85. std::string file = absl::StrCat(tmp_dir, "/signo_", signo);
  86. std::string exit_regex = absl::StrCat(
  87. "\\*\\*\\* ", absl::debugging_internal::FailureSignalToString(signo),
  88. " received at time=");
  89. #ifndef _WIN32
  90. EXPECT_EXIT(InstallHandlerWithWriteToFileAndRaise(file.c_str(), signo),
  91. testing::KilledBySignal(signo), exit_regex);
  92. #else
  93. // Windows doesn't have testing::KilledBySignal().
  94. EXPECT_DEATH_IF_SUPPORTED(
  95. InstallHandlerWithWriteToFileAndRaise(file.c_str(), signo), exit_regex);
  96. #endif
  97. // Open the file in this process and check its contents.
  98. std::fstream error_output(file);
  99. ASSERT_TRUE(error_output.is_open()) << file;
  100. std::string error_line;
  101. std::getline(error_output, error_line);
  102. EXPECT_THAT(
  103. error_line,
  104. StartsWith(absl::StrCat(
  105. "*** ", absl::debugging_internal::FailureSignalToString(signo),
  106. " received at ")));
  107. // On platforms where it is possible to get the current CPU, the
  108. // CPU number is also logged. Check that it is present in output.
  109. #if defined(__linux__)
  110. EXPECT_THAT(error_line, testing::HasSubstr(" on cpu "));
  111. #endif
  112. if (absl::debugging_internal::StackTraceWorksForTest()) {
  113. std::getline(error_output, error_line);
  114. EXPECT_THAT(error_line, StartsWith("PC: "));
  115. }
  116. }
  117. constexpr int kFailureSignals[] = {
  118. SIGSEGV, SIGILL, SIGFPE, SIGABRT, SIGTERM,
  119. #ifndef _WIN32
  120. SIGBUS, SIGTRAP,
  121. #endif
  122. };
  123. std::string SignalParamToString(const ::testing::TestParamInfo<int>& info) {
  124. std::string result =
  125. absl::debugging_internal::FailureSignalToString(info.param);
  126. if (result.empty()) {
  127. result = absl::StrCat(info.param);
  128. }
  129. return result;
  130. }
  131. INSTANTIATE_TEST_SUITE_P(AbslDeathTest, FailureSignalHandlerDeathTest,
  132. ::testing::ValuesIn(kFailureSignals),
  133. SignalParamToString);
  134. #endif // GTEST_HAS_DEATH_TEST
  135. } // namespace
  136. int main(int argc, char** argv) {
  137. absl::InitializeSymbolizer(argv[0]);
  138. testing::InitGoogleTest(&argc, argv);
  139. return RUN_ALL_TESTS();
  140. }