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

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 4 years, 1 month 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 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 return out << "Streamable"; 311 return out << "Streamable";
306 } 312 }
307 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { 313 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) {
308 std::wstring wstr = L"Hello World"; 314 std::wstring wstr = L"Hello World";
309 std::ostringstream ostr; 315 std::ostringstream ostr;
310 ostr << wstr; 316 ostr << wstr;
311 EXPECT_EQ("Hello World", ostr.str()); 317 EXPECT_EQ("Hello World", ostr.str());
312 } 318 }
313 } // namespace nested_test 319 } // namespace nested_test
314 320
321 // This test checks that, on official builds, the crash address of CHECK() is
322 // distinct in presence of multiple CHECK()s in the same function. This is a
323 // regression test for http://crbug.com/664209 .
324 #if defined(OFFICIAL_BUILD) && defined(OS_POSIX)
325
326 int g_child_crash_pipe;
327
328 void OfficialCHECKNotAmbiguous_Sighandler(int, siginfo_t* info, void*) {
329 const uintptr_t crash_addr = reinterpret_cast<uintptr_t>(info->si_addr);
330 write(g_child_crash_pipe, &crash_addr, sizeof(uintptr_t));
331 _exit(0);
332 }
333
334 void OfficialCHECKNotAmbiguous_ChildMain(int death_location) {
335 struct sigaction act = {};
336 act.sa_sigaction = OfficialCHECKNotAmbiguous_Sighandler;
337 act.sa_flags = SA_SIGINFO;
338 ASSERT_EQ(0, sigaction(SIGTRAP, &act, NULL));
339 ASSERT_EQ(0, sigaction(SIGILL, &act, NULL));
340 CHECK(death_location != 1);
341 CHECK(death_location != 2);
342 printf("\n");
343 CHECK(death_location != 3);
344
345 // Should never reach this point.
346 const uintptr_t failed = 0;
347 write(g_child_crash_pipe, &failed, sizeof(uintptr_t));
348 };
349
350 void OfficialCHECKNotAmbiguous_DoCrashChild(int death_location,
351 uintptr_t* child_crash_addr) {
352 int pipefd[2];
353 ASSERT_EQ(0, pipe(pipefd));
354
355 int pid = fork();
356 ASSERT_GE(pid, 0);
357
358 if (pid == 0) { // child process.
359 close(pipefd[0]); // Close reader (parent) end.
360 g_child_crash_pipe = pipefd[1];
361 OfficialCHECKNotAmbiguous_ChildMain(death_location);
362 FAIL() << "The child process was supposed to crash. It didn't.";
363 }
364
365 close(pipefd[1]); // Close writer (child) end.
366 DCHECK(child_crash_addr);
367 int res = HANDLE_EINTR(read(pipefd[0], child_crash_addr, sizeof(uintptr_t)));
368 ASSERT_EQ(static_cast<int>(sizeof(uintptr_t)), res);
369 }
370
371 TEST_F(LoggingTest, OfficialCHECKNotAmbiguous) {
372 uintptr_t child_crash_addr_1 = 0;
373 uintptr_t child_crash_addr_2 = 0;
374 uintptr_t child_crash_addr_3 = 0;
375
376 OfficialCHECKNotAmbiguous_DoCrashChild(1, &child_crash_addr_1);
377 OfficialCHECKNotAmbiguous_DoCrashChild(2, &child_crash_addr_2);
378 OfficialCHECKNotAmbiguous_DoCrashChild(3, &child_crash_addr_3);
379
380 ASSERT_NE(0u, child_crash_addr_1);
381 ASSERT_NE(0u, child_crash_addr_2);
382 ASSERT_NE(0u, child_crash_addr_3);
383 ASSERT_NE(child_crash_addr_1, child_crash_addr_2);
384 ASSERT_NE(child_crash_addr_1, child_crash_addr_3);
385 ASSERT_NE(child_crash_addr_2, child_crash_addr_3);
386 }
387 #endif // defined(OFFICIAL_BUILD) && defined(OS_POSIX)
388
315 } // namespace 389 } // namespace
316 390
317 } // namespace logging 391 } // 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