From bf0e00374f06791e5d35c98a2b31b57774ffe5f6 Mon Sep 17 00:00:00 2001 From: "ivanpe@chromium.org" Date: Tue, 1 Jul 2014 17:50:05 +0000 Subject: Cleanup: hide undefined behavior from the compiler better. Submitting this on behalf of Paul Pluzhnikov. R=mark@chromium.org Review URL: https://breakpad.appspot.com/6674002 git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@1342 4c0a9323-5329-0410-9bdc-e9ce6186880e --- .../linux/handler/exception_handler_unittest.cc | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/client/linux/handler/exception_handler_unittest.cc b/src/client/linux/handler/exception_handler_unittest.cc index 976f9f41..fc81f61f 100644 --- a/src/client/linux/handler/exception_handler_unittest.cc +++ b/src/client/linux/handler/exception_handler_unittest.cc @@ -193,6 +193,20 @@ static bool DoneCallback(const MinidumpDescriptor& descriptor, #ifndef ADDRESS_SANITIZER +// This is a replacement for "*reinterpret_cast(NULL) = 0;" +// It is needed because GCC is allowed to assume that the program will +// not execute any undefined behavior (UB) operation. Further, when GCC +// observes that UB statement is reached, it can assume that all statements +// leading to the UB one are never executed either, and can completely +// optimize them out. In the case of ExceptionHandlerTest::ExternalDumper, +// GCC-4.9 optimized out the entire set up of ExceptionHandler, causing +// test failure. +volatile int *p_null; // external linkage, so GCC can't tell that it + // remains NULL. Volatile just for a good measure. +static void DoNullPointerDereference() { + *p_null = 1; +} + void ChildCrash(bool use_fd) { AutoTempDir temp_dir; int fds[2] = {0}; @@ -219,7 +233,7 @@ void ChildCrash(bool use_fd) { true, -1)); } // Crash with the exception handler in scope. - *reinterpret_cast(NULL) = 0; + DoNullPointerDereference(); } } if (!use_fd) @@ -295,7 +309,7 @@ static void CrashWithCallbacks(ExceptionHandler::FilterCallback filter, ExceptionHandler handler( MinidumpDescriptor(path), filter, done, NULL, true, -1); // Crash with the exception handler in scope. - *reinterpret_cast(NULL) = 0; + DoNullPointerDereference(); } TEST(ExceptionHandlerTest, RedeliveryOnFilterCallbackFalse) { @@ -386,7 +400,7 @@ TEST(ExceptionHandlerTest, RedeliveryOnBadSignalHandlerFlag) { reinterpret_cast(SIG_ERR)); // Crash with the exception handler in scope. - *reinterpret_cast(NULL) = 0; + DoNullPointerDereference(); } // SIGKILL means Breakpad's signal handler didn't crash. ASSERT_NO_FATAL_FAILURE(WaitForProcessToTerminate(child, SIGKILL)); @@ -922,7 +936,7 @@ TEST(ExceptionHandlerTest, ExternalDumper) { ExceptionHandler handler(MinidumpDescriptor("/tmp1"), NULL, NULL, reinterpret_cast(fds[1]), true, -1); handler.set_crash_handler(CrashHandler); - *reinterpret_cast(NULL) = 0; + DoNullPointerDereference(); } close(fds[1]); struct msghdr msg = {0}; -- cgit v1.2.1