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

Side by Side Diff: base/logging_unittest.cc

Issue 2502953003: base: make CHECK macros trap at distinct addresses in official builds (Closed)
Patch Set: . 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 unified diff | Download patch
« no previous file with comments | « base/logging.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/compiler_specific.h" 5 #include "base/compiler_specific.h"
6 #include "base/logging.h" 6 #include "base/logging.h"
7 #include "base/macros.h" 7 #include "base/macros.h"
8 8
9 #include "testing/gmock/include/gmock/gmock.h" 9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 #if defined(OS_POSIX)
13 #include <signal.h>
14 #include <unistd.h>
15 #include "base/posix/eintr_wrapper.h"
16 #endif // OS_POSIX
17
12 #if defined(OS_WIN) 18 #if defined(OS_WIN)
13 #include <excpt.h> 19 #include <excpt.h>
14 #include <windows.h> 20 #include <windows.h>
15 #endif // OS_WIN 21 #endif // OS_WIN
16 22
17 namespace logging { 23 namespace logging {
18 24
19 namespace { 25 namespace {
20 26
21 using ::testing::Return; 27 using ::testing::Return;
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 // not access violations). 245 // not access violations).
240 EXPECT_EQ(STATUS_BREAKPOINT, code1); 246 EXPECT_EQ(STATUS_BREAKPOINT, code1);
241 EXPECT_EQ(STATUS_BREAKPOINT, code2); 247 EXPECT_EQ(STATUS_BREAKPOINT, code2);
242 EXPECT_EQ(STATUS_BREAKPOINT, code3); 248 EXPECT_EQ(STATUS_BREAKPOINT, code3);
243 249
244 // Ensure that none of the CHECKs are colocated. 250 // Ensure that none of the CHECKs are colocated.
245 EXPECT_NE(addr1, addr2); 251 EXPECT_NE(addr1, addr2);
246 EXPECT_NE(addr1, addr3); 252 EXPECT_NE(addr1, addr3);
247 EXPECT_NE(addr2, addr3); 253 EXPECT_NE(addr2, addr3);
248 } 254 }
249 #endif // OFFICIAL_BUILD && OS_WIN 255 #elif defined(OS_POSIX)
256
257 int g_child_crash_pipe;
258
259 void CheckCrashTestSighandler(int, siginfo_t* info, void*) {
260 const uintptr_t crash_addr = reinterpret_cast<uintptr_t>(info->si_addr);
261 HANDLE_EINTR(write(g_child_crash_pipe, &crash_addr, sizeof(uintptr_t)));
262 _exit(0);
263 }
264
265 // CHECK causes a direct crash (without jumping to another function) only in
266 // official builds. Unfortunately, continuous test coverage on official builds
267 // is lower. DO_CHECK here falls back on a home-brewed implementation in
268 // non-official builds, to catch regressions earlier in the CQ.
269 #if defined(OFFICIAL_BUILD)
270 #define DO_CHECK CHECK
271 #else
272 #define DO_CHECK(cond) \
273 if (!(cond)) \
274 IMMEDIATE_CRASH()
275 #endif
276
277 void CrashChildMain(int death_location) {
278 struct sigaction act = {};
279 act.sa_sigaction = CheckCrashTestSighandler;
280 act.sa_flags = SA_SIGINFO;
281 ASSERT_EQ(0, sigaction(SIGTRAP, &act, NULL));
282 ASSERT_EQ(0, sigaction(SIGILL, &act, NULL));
Torne 2017/02/17 11:06:31 Don't you need to handle SIGBUS here as well for a
Primiano Tucci (use gerrit) 2017/02/17 12:37:35 Ehm err you are definitely right. It was green bec
283 DO_CHECK(death_location != 1);
284 DO_CHECK(death_location != 2);
285 printf("\n");
286 DO_CHECK(death_location != 3);
287
288 // Should never reach this point.
289 const uintptr_t failed = 0;
290 HANDLE_EINTR(write(g_child_crash_pipe, &failed, sizeof(uintptr_t)));
291 };
292
293 void SpawnChildAndCrash(int death_location, uintptr_t* child_crash_addr) {
294 int pipefd[2];
295 ASSERT_EQ(0, pipe(pipefd));
296
297 int pid = fork();
298 ASSERT_GE(pid, 0);
299
300 if (pid == 0) { // child process.
301 close(pipefd[0]); // Close reader (parent) end.
302 g_child_crash_pipe = pipefd[1];
303 CrashChildMain(death_location);
304 FAIL() << "The child process was supposed to crash. It didn't.";
305 }
306
307 close(pipefd[1]); // Close writer (child) end.
308 DCHECK(child_crash_addr);
309 int res = HANDLE_EINTR(read(pipefd[0], child_crash_addr, sizeof(uintptr_t)));
310 ASSERT_EQ(static_cast<int>(sizeof(uintptr_t)), res);
311 }
312
313 TEST_F(LoggingTest, CheckCausesDistinctBreakpoints) {
314 uintptr_t child_crash_addr_1 = 0;
315 uintptr_t child_crash_addr_2 = 0;
316 uintptr_t child_crash_addr_3 = 0;
317
318 SpawnChildAndCrash(1, &child_crash_addr_1);
319 SpawnChildAndCrash(2, &child_crash_addr_2);
320 SpawnChildAndCrash(3, &child_crash_addr_3);
321
322 ASSERT_NE(0u, child_crash_addr_1);
323 ASSERT_NE(0u, child_crash_addr_2);
324 ASSERT_NE(0u, child_crash_addr_3);
325 ASSERT_NE(child_crash_addr_1, child_crash_addr_2);
326 ASSERT_NE(child_crash_addr_1, child_crash_addr_3);
327 ASSERT_NE(child_crash_addr_2, child_crash_addr_3);
328 }
329 #endif // OS_POSIX
250 330
251 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) { 331 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) {
252 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) 332 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
253 int debug_only_variable = 1; 333 int debug_only_variable = 1;
254 #endif 334 #endif
255 // These should avoid emitting references to |debug_only_variable| 335 // These should avoid emitting references to |debug_only_variable|
256 // in release mode. 336 // in release mode.
257 DLOG_IF(INFO, debug_only_variable) << "test"; 337 DLOG_IF(INFO, debug_only_variable) << "test";
258 DLOG_ASSERT(debug_only_variable) << "test"; 338 DLOG_ASSERT(debug_only_variable) << "test";
259 DPLOG_IF(INFO, debug_only_variable) << "test"; 339 DPLOG_IF(INFO, debug_only_variable) << "test";
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 std::wstring wstr = L"Hello World"; 479 std::wstring wstr = L"Hello World";
400 std::ostringstream ostr; 480 std::ostringstream ostr;
401 ostr << wstr; 481 ostr << wstr;
402 EXPECT_EQ("Hello World", ostr.str()); 482 EXPECT_EQ("Hello World", ostr.str());
403 } 483 }
404 } // namespace nested_test 484 } // namespace nested_test
405 485
406 } // namespace 486 } // namespace
407 487
408 } // namespace logging 488 } // namespace logging
OLDNEW
« 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