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

Side by Side 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 unified diff | Download patch
« base/logging.h ('K') | « 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 namespace logging { 18 namespace logging {
13 19
14 namespace { 20 namespace {
15 21
16 using ::testing::Return; 22 using ::testing::Return;
17 23
18 // Needs to be global since log assert handlers can't maintain state. 24 // Needs to be global since log assert handlers can't maintain state.
19 int log_sink_call_count = 0; 25 int log_sink_call_count = 0;
20 26
21 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) 27 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG)
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 return out << "Streamable"; 344 return out << "Streamable";
339 } 345 }
340 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { 346 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) {
341 std::wstring wstr = L"Hello World"; 347 std::wstring wstr = L"Hello World";
342 std::ostringstream ostr; 348 std::ostringstream ostr;
343 ostr << wstr; 349 ostr << wstr;
344 EXPECT_EQ("Hello World", ostr.str()); 350 EXPECT_EQ("Hello World", ostr.str());
345 } 351 }
346 } // namespace nested_test 352 } // namespace nested_test
347 353
354 // This test checks that the crash address of CHECK() is distinct even when
355 // multiple CHECK()s in the same function. http://crbug.com/664209 .
356 #if defined(OS_POSIX)
357
358 int g_child_crash_pipe;
359
360 void CHECKNotAmbiguous_Sighandler(int, siginfo_t* info, void*) {
361 const uintptr_t crash_addr = reinterpret_cast<uintptr_t>(info->si_addr);
362 HANDLE_EINTR(write(g_child_crash_pipe, &crash_addr, sizeof(uintptr_t)));
363 _exit(0);
364 }
365
366 // CHECK causes a direct crash (without jumping to another function) only in
367 // official builds. Unfortunately, continuous test coverage on official builds
368 // is lower. DO_CHECK here falls back on a home-brewed implementation in
369 // non-official builds, to catch regressions earlier in the CQ.
370 #if defined(OFFICIAL_BUILD)
371 #define DO_CHECK CHECK
372 #else
373 #define DO_CHECK(cond) \
374 if (!(cond)) \
375 IMMEDIATE_CRASH()
376 #endif
377
378 void CHECKNotAmbiguous_ChildMain(int death_location) {
379 struct sigaction act = {};
380 act.sa_sigaction = CHECKNotAmbiguous_Sighandler;
381 act.sa_flags = SA_SIGINFO;
382 ASSERT_EQ(0, sigaction(SIGTRAP, &act, NULL));
383 ASSERT_EQ(0, sigaction(SIGILL, &act, NULL));
384 DO_CHECK(death_location != 1);
385 DO_CHECK(death_location != 2);
386 printf("\n");
387 DO_CHECK(death_location != 3);
388
389 // Should never reach this point.
390 const uintptr_t failed = 0;
391 HANDLE_EINTR(write(g_child_crash_pipe, &failed, sizeof(uintptr_t)));
392 };
393
394 void CHECKNotAmbiguous_DoCrashChild(int death_location,
395 uintptr_t* child_crash_addr) {
396 int pipefd[2];
397 ASSERT_EQ(0, pipe(pipefd));
398
399 int pid = fork();
400 ASSERT_GE(pid, 0);
401
402 if (pid == 0) { // child process.
403 close(pipefd[0]); // Close reader (parent) end.
404 g_child_crash_pipe = pipefd[1];
405 CHECKNotAmbiguous_ChildMain(death_location);
406 FAIL() << "The child process was supposed to crash. It didn't.";
407 }
408
409 close(pipefd[1]); // Close writer (child) end.
410 DCHECK(child_crash_addr);
411 int res = HANDLE_EINTR(read(pipefd[0], child_crash_addr, sizeof(uintptr_t)));
412 ASSERT_EQ(static_cast<int>(sizeof(uintptr_t)), res);
413 }
414
415 TEST_F(LoggingTest, CHECKNotAmbiguous) {
416 uintptr_t child_crash_addr_1 = 0;
417 uintptr_t child_crash_addr_2 = 0;
418 uintptr_t child_crash_addr_3 = 0;
419
420 CHECKNotAmbiguous_DoCrashChild(1, &child_crash_addr_1);
421 CHECKNotAmbiguous_DoCrashChild(2, &child_crash_addr_2);
422 CHECKNotAmbiguous_DoCrashChild(3, &child_crash_addr_3);
423
424 ASSERT_NE(0u, child_crash_addr_1);
425 ASSERT_NE(0u, child_crash_addr_2);
426 ASSERT_NE(0u, child_crash_addr_3);
427 ASSERT_NE(child_crash_addr_1, child_crash_addr_2);
428 ASSERT_NE(child_crash_addr_1, child_crash_addr_3);
429 ASSERT_NE(child_crash_addr_2, child_crash_addr_3);
430 }
431 #endif // defined(OFFICIAL_BUILD) && defined(OS_POSIX)
432
348 } // namespace 433 } // namespace
349 434
350 } // namespace logging 435 } // namespace logging
OLDNEW
« 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