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

Unified Diff: base/logging_unittest.cc

Issue 2502953003: base: make CHECK macros trap at distinct addresses in official builds (Closed)
Patch Set: use static_cast + primitive type 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
« no previous file with comments | « 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 9fe718c8799a4e74f170091ca148033373c79161..e6450b09ffedac97d01d9cb24441e980eb676b38 100644
--- a/base/logging_unittest.cc
+++ b/base/logging_unittest.cc
@@ -9,6 +9,16 @@
#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
+
+#if defined(OS_LINUX) || defined(OS_ANDROID)
+#include <ucontext.h>
+#endif
+
#if defined(OS_WIN)
#include <excpt.h>
#include <windows.h>
@@ -246,7 +256,102 @@ TEST_F(LoggingTest, CheckCausesDistinctBreakpoints) {
EXPECT_NE(addr1, addr3);
EXPECT_NE(addr2, addr3);
}
-#endif // OFFICIAL_BUILD && OS_WIN
+
+#elif defined(OS_POSIX) && \
+ (defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY))
+
+int g_child_crash_pipe;
+
+void CheckCrashTestSighandler(int, siginfo_t* info, void* context_ptr) {
+ // Conversely to what clearly stated in "man 2 sigaction", some Linux kernels
+ // do NOT populate the |info->si_addr| in the case of a SIGTRAP. Hence we
+ // need the arch-specific boilerplate below, which is inspired by breakpad.
+ // At the same time, on OSX, ucontext.h is deprecated but si_addr works fine.
+ uintptr_t crash_addr = 0;
+#if defined(OS_MACOSX)
+ crash_addr = reinterpret_cast<uintptr_t>(info->si_addr);
+#else // OS_POSIX && !OS_MACOSX
+ struct ucontext* context = reinterpret_cast<struct ucontext*>(context_ptr);
+#if defined(ARCH_CPU_X86)
+ crash_addr = static_cast<uintptr_t>(context->uc_mcontext.gregs[REG_EIP]);
+#elif defined(ARCH_CPU_X86_64)
+ crash_addr = static_cast<uintptr_t>(context->uc_mcontext.gregs[REG_RIP]);
+#elif defined(ARCH_CPU_ARMEL)
+ crash_addr = static_cast<uintptr_t>(context->uc_mcontext.arm_pc);
+#elif defined(ARCH_CPU_ARM64)
+ crash_addr = static_cast<uintptr_t>(context->uc_mcontext.pc);
+#endif // ARCH_*
+#endif // OS_POSIX && !OS_MACOSX
+ 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 CrashChildMain(int death_location) {
+ struct sigaction act = {};
+ act.sa_sigaction = CheckCrashTestSighandler;
+ act.sa_flags = SA_SIGINFO;
+ ASSERT_EQ(0, sigaction(SIGTRAP, &act, NULL));
+ ASSERT_EQ(0, sigaction(SIGBUS, &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 SpawnChildAndCrash(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];
+ CrashChildMain(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, CheckCausesDistinctBreakpoints) {
+ uintptr_t child_crash_addr_1 = 0;
+ uintptr_t child_crash_addr_2 = 0;
+ uintptr_t child_crash_addr_3 = 0;
+
+ SpawnChildAndCrash(1, &child_crash_addr_1);
+ SpawnChildAndCrash(2, &child_crash_addr_2);
+ SpawnChildAndCrash(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 // OS_POSIX
TEST_F(LoggingTest, DebugLoggingReleaseBehavior) {
#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
« no previous file with comments | « base/logging.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698