OLD | NEW |
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 Loading... |
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 #elif defined(ARCH_CPU_X86) |
| 274 crash_addr = static_cast<uintptr_t>(context->uc_mcontext.gregs[REG_EIP]); |
| 275 #elif defined(ARCH_CPU_X86_64) |
| 276 crash_addr = static_cast<uintptr_t>(context->uc_mcontext.gregs[REG_RIP]); |
| 277 #elif defined(ARCH_CPU_ARMEL) |
| 278 crash_addr = static_cast<uintptr_t>(context->uc_mcontext.arm_pc); |
| 279 #elif defined(ARCH_CPU_ARM64) |
| 280 crash_addr = static_cast<uintptr_t>(context->uc_mcontext.pc); |
| 281 #endif |
| 282 HANDLE_EINTR(write(g_child_crash_pipe, &crash_addr, sizeof(uintptr_t))); |
| 283 _exit(0); |
| 284 } |
| 285 |
| 286 // CHECK causes a direct crash (without jumping to another function) only in |
| 287 // official builds. Unfortunately, continuous test coverage on official builds |
| 288 // is lower. DO_CHECK here falls back on a home-brewed implementation in |
| 289 // non-official builds, to catch regressions earlier in the CQ. |
| 290 #if defined(OFFICIAL_BUILD) |
| 291 #define DO_CHECK CHECK |
| 292 #else |
| 293 #define DO_CHECK(cond) \ |
| 294 if (!(cond)) \ |
| 295 IMMEDIATE_CRASH() |
| 296 #endif |
| 297 |
| 298 void CrashChildMain(int death_location) { |
| 299 struct sigaction act = {}; |
| 300 act.sa_sigaction = CheckCrashTestSighandler; |
| 301 act.sa_flags = SA_SIGINFO; |
| 302 ASSERT_EQ(0, sigaction(SIGTRAP, &act, NULL)); |
| 303 ASSERT_EQ(0, sigaction(SIGBUS, &act, NULL)); |
| 304 ASSERT_EQ(0, sigaction(SIGILL, &act, NULL)); |
| 305 DO_CHECK(death_location != 1); |
| 306 DO_CHECK(death_location != 2); |
| 307 printf("\n"); |
| 308 DO_CHECK(death_location != 3); |
| 309 |
| 310 // Should never reach this point. |
| 311 const uintptr_t failed = 0; |
| 312 HANDLE_EINTR(write(g_child_crash_pipe, &failed, sizeof(uintptr_t))); |
| 313 }; |
| 314 |
| 315 void SpawnChildAndCrash(int death_location, uintptr_t* child_crash_addr) { |
| 316 int pipefd[2]; |
| 317 ASSERT_EQ(0, pipe(pipefd)); |
| 318 |
| 319 int pid = fork(); |
| 320 ASSERT_GE(pid, 0); |
| 321 |
| 322 if (pid == 0) { // child process. |
| 323 close(pipefd[0]); // Close reader (parent) end. |
| 324 g_child_crash_pipe = pipefd[1]; |
| 325 CrashChildMain(death_location); |
| 326 FAIL() << "The child process was supposed to crash. It didn't."; |
| 327 } |
| 328 |
| 329 close(pipefd[1]); // Close writer (child) end. |
| 330 DCHECK(child_crash_addr); |
| 331 int res = HANDLE_EINTR(read(pipefd[0], child_crash_addr, sizeof(uintptr_t))); |
| 332 ASSERT_EQ(static_cast<int>(sizeof(uintptr_t)), res); |
| 333 } |
| 334 |
| 335 TEST_F(LoggingTest, CheckCausesDistinctBreakpoints) { |
| 336 uintptr_t child_crash_addr_1 = 0; |
| 337 uintptr_t child_crash_addr_2 = 0; |
| 338 uintptr_t child_crash_addr_3 = 0; |
| 339 |
| 340 SpawnChildAndCrash(1, &child_crash_addr_1); |
| 341 SpawnChildAndCrash(2, &child_crash_addr_2); |
| 342 SpawnChildAndCrash(3, &child_crash_addr_3); |
| 343 |
| 344 ASSERT_NE(0u, child_crash_addr_1); |
| 345 ASSERT_NE(0u, child_crash_addr_2); |
| 346 ASSERT_NE(0u, child_crash_addr_3); |
| 347 ASSERT_NE(child_crash_addr_1, child_crash_addr_2); |
| 348 ASSERT_NE(child_crash_addr_1, child_crash_addr_3); |
| 349 ASSERT_NE(child_crash_addr_2, child_crash_addr_3); |
| 350 } |
| 351 #endif // OS_POSIX |
250 | 352 |
251 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) { | 353 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) { |
252 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 354 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
253 int debug_only_variable = 1; | 355 int debug_only_variable = 1; |
254 #endif | 356 #endif |
255 // These should avoid emitting references to |debug_only_variable| | 357 // These should avoid emitting references to |debug_only_variable| |
256 // in release mode. | 358 // in release mode. |
257 DLOG_IF(INFO, debug_only_variable) << "test"; | 359 DLOG_IF(INFO, debug_only_variable) << "test"; |
258 DLOG_ASSERT(debug_only_variable) << "test"; | 360 DLOG_ASSERT(debug_only_variable) << "test"; |
259 DPLOG_IF(INFO, debug_only_variable) << "test"; | 361 DPLOG_IF(INFO, debug_only_variable) << "test"; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 std::wstring wstr = L"Hello World"; | 501 std::wstring wstr = L"Hello World"; |
400 std::ostringstream ostr; | 502 std::ostringstream ostr; |
401 ostr << wstr; | 503 ostr << wstr; |
402 EXPECT_EQ("Hello World", ostr.str()); | 504 EXPECT_EQ("Hello World", ostr.str()); |
403 } | 505 } |
404 } // namespace nested_test | 506 } // namespace nested_test |
405 | 507 |
406 } // namespace | 508 } // namespace |
407 | 509 |
408 } // namespace logging | 510 } // namespace logging |
OLD | NEW |