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

Side by Side 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 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
18 #if defined(OS_LINUX) || defined(OS_ANDROID)
19 #include <ucontext.h>
20 #endif
21
12 #if defined(OS_WIN) 22 #if defined(OS_WIN)
13 #include <excpt.h> 23 #include <excpt.h>
14 #include <windows.h> 24 #include <windows.h>
15 #endif // OS_WIN 25 #endif // OS_WIN
16 26
17 namespace logging { 27 namespace logging {
18 28
19 namespace { 29 namespace {
20 30
21 using ::testing::Return; 31 using ::testing::Return;
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 // not access violations). 249 // not access violations).
240 EXPECT_EQ(STATUS_BREAKPOINT, code1); 250 EXPECT_EQ(STATUS_BREAKPOINT, code1);
241 EXPECT_EQ(STATUS_BREAKPOINT, code2); 251 EXPECT_EQ(STATUS_BREAKPOINT, code2);
242 EXPECT_EQ(STATUS_BREAKPOINT, code3); 252 EXPECT_EQ(STATUS_BREAKPOINT, code3);
243 253
244 // Ensure that none of the CHECKs are colocated. 254 // Ensure that none of the CHECKs are colocated.
245 EXPECT_NE(addr1, addr2); 255 EXPECT_NE(addr1, addr2);
246 EXPECT_NE(addr1, addr3); 256 EXPECT_NE(addr1, addr3);
247 EXPECT_NE(addr2, addr3); 257 EXPECT_NE(addr2, addr3);
248 } 258 }
249 #endif // OFFICIAL_BUILD && OS_WIN 259
260 #elif defined(OS_POSIX) && \
261 (defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY))
262
263 int g_child_crash_pipe;
264
265 void CheckCrashTestSighandler(int, siginfo_t* info, void* context_ptr) {
266 // Conversely to what clearly stated in "man 2 sigaction", some Linux kernels
267 // do NOT populate the |info->si_addr| in the case of a SIGTRAP. Hence we
268 // need the arch-specific boilerplate below, which is inspired by breakpad.
269 // At the same time, on OSX, ucontext.h is deprecated but si_addr works fine.
270 uintptr_t crash_addr = 0;
271 #if defined(OS_MACOSX)
272 crash_addr = reinterpret_cast<uintptr_t>(info->si_addr);
273 #else // OS_POSIX && !OS_MACOSX
274 struct ucontext* context = reinterpret_cast<struct ucontext*>(context_ptr);
275 #if defined(ARCH_CPU_X86)
276 crash_addr = static_cast<uintptr_t>(context->uc_mcontext.gregs[REG_EIP]);
277 #elif defined(ARCH_CPU_X86_64)
278 crash_addr = static_cast<uintptr_t>(context->uc_mcontext.gregs[REG_RIP]);
279 #elif defined(ARCH_CPU_ARMEL)
280 crash_addr = static_cast<uintptr_t>(context->uc_mcontext.arm_pc);
281 #elif defined(ARCH_CPU_ARM64)
282 crash_addr = static_cast<uintptr_t>(context->uc_mcontext.pc);
283 #endif // ARCH_*
284 #endif // OS_POSIX && !OS_MACOSX
285 HANDLE_EINTR(write(g_child_crash_pipe, &crash_addr, sizeof(uintptr_t)));
286 _exit(0);
287 }
288
289 // CHECK causes a direct crash (without jumping to another function) only in
290 // official builds. Unfortunately, continuous test coverage on official builds
291 // is lower. DO_CHECK here falls back on a home-brewed implementation in
292 // non-official builds, to catch regressions earlier in the CQ.
293 #if defined(OFFICIAL_BUILD)
294 #define DO_CHECK CHECK
295 #else
296 #define DO_CHECK(cond) \
297 if (!(cond)) \
298 IMMEDIATE_CRASH()
299 #endif
300
301 void CrashChildMain(int death_location) {
302 struct sigaction act = {};
303 act.sa_sigaction = CheckCrashTestSighandler;
304 act.sa_flags = SA_SIGINFO;
305 ASSERT_EQ(0, sigaction(SIGTRAP, &act, NULL));
306 ASSERT_EQ(0, sigaction(SIGBUS, &act, NULL));
307 ASSERT_EQ(0, sigaction(SIGILL, &act, NULL));
308 DO_CHECK(death_location != 1);
309 DO_CHECK(death_location != 2);
310 printf("\n");
311 DO_CHECK(death_location != 3);
312
313 // Should never reach this point.
314 const uintptr_t failed = 0;
315 HANDLE_EINTR(write(g_child_crash_pipe, &failed, sizeof(uintptr_t)));
316 };
317
318 void SpawnChildAndCrash(int death_location, uintptr_t* child_crash_addr) {
319 int pipefd[2];
320 ASSERT_EQ(0, pipe(pipefd));
321
322 int pid = fork();
323 ASSERT_GE(pid, 0);
324
325 if (pid == 0) { // child process.
326 close(pipefd[0]); // Close reader (parent) end.
327 g_child_crash_pipe = pipefd[1];
328 CrashChildMain(death_location);
329 FAIL() << "The child process was supposed to crash. It didn't.";
330 }
331
332 close(pipefd[1]); // Close writer (child) end.
333 DCHECK(child_crash_addr);
334 int res = HANDLE_EINTR(read(pipefd[0], child_crash_addr, sizeof(uintptr_t)));
335 ASSERT_EQ(static_cast<int>(sizeof(uintptr_t)), res);
336 }
337
338 TEST_F(LoggingTest, CheckCausesDistinctBreakpoints) {
339 uintptr_t child_crash_addr_1 = 0;
340 uintptr_t child_crash_addr_2 = 0;
341 uintptr_t child_crash_addr_3 = 0;
342
343 SpawnChildAndCrash(1, &child_crash_addr_1);
344 SpawnChildAndCrash(2, &child_crash_addr_2);
345 SpawnChildAndCrash(3, &child_crash_addr_3);
346
347 ASSERT_NE(0u, child_crash_addr_1);
348 ASSERT_NE(0u, child_crash_addr_2);
349 ASSERT_NE(0u, child_crash_addr_3);
350 ASSERT_NE(child_crash_addr_1, child_crash_addr_2);
351 ASSERT_NE(child_crash_addr_1, child_crash_addr_3);
352 ASSERT_NE(child_crash_addr_2, child_crash_addr_3);
353 }
354 #endif // OS_POSIX
250 355
251 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) { 356 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) {
252 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) 357 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
253 int debug_only_variable = 1; 358 int debug_only_variable = 1;
254 #endif 359 #endif
255 // These should avoid emitting references to |debug_only_variable| 360 // These should avoid emitting references to |debug_only_variable|
256 // in release mode. 361 // in release mode.
257 DLOG_IF(INFO, debug_only_variable) << "test"; 362 DLOG_IF(INFO, debug_only_variable) << "test";
258 DLOG_ASSERT(debug_only_variable) << "test"; 363 DLOG_ASSERT(debug_only_variable) << "test";
259 DPLOG_IF(INFO, debug_only_variable) << "test"; 364 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"; 504 std::wstring wstr = L"Hello World";
400 std::ostringstream ostr; 505 std::ostringstream ostr;
401 ostr << wstr; 506 ostr << wstr;
402 EXPECT_EQ("Hello World", ostr.str()); 507 EXPECT_EQ("Hello World", ostr.str());
403 } 508 }
404 } // namespace nested_test 509 } // namespace nested_test
405 510
406 } // namespace 511 } // namespace
407 512
408 } // namespace logging 513 } // 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