Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3108)

Unified Diff: base/logging_unittest.cc

Issue 2502953003: base: make CHECK macros trap at distinct addresses in official builds (Closed)
Patch Set: fix comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« base/logging.h ('K') | « base/logging.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/logging_unittest.cc
diff --git a/base/logging_unittest.cc b/base/logging_unittest.cc
index f41cce2f43b390fa9603a14f3f689fe87d0dcb33..ac38d4ca379d447736ca280b07c5a1aa62aa1b3e 100644
--- a/base/logging_unittest.cc
+++ b/base/logging_unittest.cc
@@ -9,6 +9,12 @@
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
+#if defined(OS_POSIX)
+#include <signal.h>
+#include <unistd.h>
+#include "base/posix/eintr_wrapper.h"
+#endif // OS_POSIX
+
namespace logging {
namespace {
@@ -345,6 +351,85 @@ namespace nested_test {
}
} // namespace nested_test
+// This test checks that the crash address of CHECK() is distinct even when
+// multiple CHECK()s in the same function. http://crbug.com/664209 .
+#if defined(OS_POSIX)
+
+int g_child_crash_pipe;
+
+void CHECKNotAmbiguous_Sighandler(int, siginfo_t* info, void*) {
+ const uintptr_t crash_addr = reinterpret_cast<uintptr_t>(info->si_addr);
+ HANDLE_EINTR(write(g_child_crash_pipe, &crash_addr, sizeof(uintptr_t)));
+ _exit(0);
+}
+
+// CHECK causes a direct crash (without jumping to another function) only in
+// official builds. Unfortunately, continuous test coverage on official builds
+// is lower. DO_CHECK here falls back on a home-brewed implementation in
+// non-official builds, to catch regressions earlier in the CQ.
+#if defined(OFFICIAL_BUILD)
+#define DO_CHECK CHECK
+#else
+#define DO_CHECK(cond) \
+ if (!(cond)) \
+ IMMEDIATE_CRASH()
+#endif
+
+void CHECKNotAmbiguous_ChildMain(int death_location) {
+ struct sigaction act = {};
+ act.sa_sigaction = CHECKNotAmbiguous_Sighandler;
+ act.sa_flags = SA_SIGINFO;
+ ASSERT_EQ(0, sigaction(SIGTRAP, &act, NULL));
+ ASSERT_EQ(0, sigaction(SIGILL, &act, NULL));
+ DO_CHECK(death_location != 1);
+ DO_CHECK(death_location != 2);
+ printf("\n");
+ DO_CHECK(death_location != 3);
+
+ // Should never reach this point.
+ const uintptr_t failed = 0;
+ HANDLE_EINTR(write(g_child_crash_pipe, &failed, sizeof(uintptr_t)));
+};
+
+void CHECKNotAmbiguous_DoCrashChild(int death_location,
+ uintptr_t* child_crash_addr) {
+ int pipefd[2];
+ ASSERT_EQ(0, pipe(pipefd));
+
+ int pid = fork();
+ ASSERT_GE(pid, 0);
+
+ if (pid == 0) { // child process.
+ close(pipefd[0]); // Close reader (parent) end.
+ g_child_crash_pipe = pipefd[1];
+ CHECKNotAmbiguous_ChildMain(death_location);
+ FAIL() << "The child process was supposed to crash. It didn't.";
+ }
+
+ close(pipefd[1]); // Close writer (child) end.
+ DCHECK(child_crash_addr);
+ int res = HANDLE_EINTR(read(pipefd[0], child_crash_addr, sizeof(uintptr_t)));
+ ASSERT_EQ(static_cast<int>(sizeof(uintptr_t)), res);
+}
+
+TEST_F(LoggingTest, CHECKNotAmbiguous) {
+ uintptr_t child_crash_addr_1 = 0;
+ uintptr_t child_crash_addr_2 = 0;
+ uintptr_t child_crash_addr_3 = 0;
+
+ CHECKNotAmbiguous_DoCrashChild(1, &child_crash_addr_1);
+ CHECKNotAmbiguous_DoCrashChild(2, &child_crash_addr_2);
+ CHECKNotAmbiguous_DoCrashChild(3, &child_crash_addr_3);
+
+ ASSERT_NE(0u, child_crash_addr_1);
+ ASSERT_NE(0u, child_crash_addr_2);
+ ASSERT_NE(0u, child_crash_addr_3);
+ ASSERT_NE(child_crash_addr_1, child_crash_addr_2);
+ ASSERT_NE(child_crash_addr_1, child_crash_addr_3);
+ ASSERT_NE(child_crash_addr_2, child_crash_addr_3);
+}
+#endif // defined(OFFICIAL_BUILD) && defined(OS_POSIX)
+
} // namespace
} // namespace logging
« base/logging.h ('K') | « base/logging.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698