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

Side by Side Diff: base/logging.h

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
« no previous file with comments | « no previous file | base/logging_unittest.cc » ('j') | 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #ifndef BASE_LOGGING_H_ 5 #ifndef BASE_LOGGING_H_
6 #define BASE_LOGGING_H_ 6 #define BASE_LOGGING_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <cassert> 10 #include <cassert>
11 #include <cstring> 11 #include <cstring>
12 #include <sstream> 12 #include <sstream>
13 #include <string> 13 #include <string>
14 #include <type_traits> 14 #include <type_traits>
15 #include <utility> 15 #include <utility>
16 16
17 #include "base/base_export.h" 17 #include "base/base_export.h"
18 #include "base/compiler_specific.h"
18 #include "base/debug/debugger.h" 19 #include "base/debug/debugger.h"
19 #include "base/macros.h" 20 #include "base/macros.h"
20 #include "base/template_util.h" 21 #include "base/template_util.h"
21 #include "build/build_config.h" 22 #include "build/build_config.h"
22 23
23 // 24 //
24 // Optional message capabilities 25 // Optional message capabilities
25 // ----------------------------- 26 // -----------------------------
26 // Assertion failed messages and fatal errors are displayed in a dialog box 27 // Assertion failed messages and fatal errors are displayed in a dialog box
27 // before the application exits. However, running this UI creates a message 28 // before the application exits. However, running this UI creates a message
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 // 452 //
452 // We make sure CHECK et al. always evaluates their arguments, as 453 // We make sure CHECK et al. always evaluates their arguments, as
453 // doing CHECK(FunctionWithSideEffect()) is a common idiom. 454 // doing CHECK(FunctionWithSideEffect()) is a common idiom.
454 455
455 #if defined(OFFICIAL_BUILD) && defined(NDEBUG) 456 #if defined(OFFICIAL_BUILD) && defined(NDEBUG)
456 457
457 // Make all CHECK functions discard their log strings to reduce code 458 // Make all CHECK functions discard their log strings to reduce code
458 // bloat, and improve performance, for official release builds. 459 // bloat, and improve performance, for official release builds.
459 460
460 #if defined(COMPILER_GCC) || __clang__ 461 #if defined(COMPILER_GCC) || __clang__
461 #define LOGGING_CRASH() __builtin_trap() 462
462 #else 463 // This is to make sure that the crash report can pinpoint to the right source
463 #define LOGGING_CRASH() ((void)(*(volatile char*)0 = 0)) 464 // line even in presence of multiple CHECK()s in the same function. See
465 // http://crbug.com/664209 for more context.
466 // There are three things we need to guarantee here:
467 // - The trap instructions, and hence the PC value at crash time, are distinct
468 // and don't get collapsed into the same op due to compiler optimizations.
469 // - The debug line info for the trap instructions get attributed to the caller
470 // of CHECK(), to make sure that crash reports show actionable data. This in
471 // practice rules out the ability of using a inline function, at least as
472 // long as clang doesn't support attribute(artificial).
473 // - The compiler should treat the CHECK as no-return instructions, so that the
474 // trap code can be efficiently packed in the prologue of the function and
475 // doesn't interfere with the main execution flow.
476 // Code-wise:
477 // - The asm volatile statement is a cross-architecture version of
478 // mov %eax, LINE. It guarantees that the assembly is unique for each line
479 // and the multiple crash instructions generated by CHECK() don't get folded.
480 // - __builtin_trap() is the instruction that actually causes the crash,
481 // either a SIGTRAP or a SIGABRT depending on the platform.
482 // - __builtin_unreachable() enables no-return optimizations.
483 #define LOGGING_CRASH(LINE) \
484 ({ \
485 asm volatile("" : : "r"(LINE) : "memory"); \
486 __builtin_trap(); \
487 __builtin_unreachable(); \
488 })
489
490 #else // !(defined(COMPILER_GCC) || __clang__)
491 #define LOGGING_CRASH(LINE) ((void)(*(volatile char*)0 = 0))
Nico 2016/12/02 02:04:03 This doesn't help on Windows, does it. Would ((
464 #endif 492 #endif
465 493
466 // This is not calling BreakDebugger since this is called frequently, and 494 // This is not calling BreakDebugger since this is called frequently, and
467 // calling an out-of-line function instead of a noreturn inline macro prevents 495 // calling an out-of-line function instead of a noreturn inline macro prevents
468 // compiler optimizations. 496 // compiler optimizations.
469 #define CHECK(condition) \ 497 #define CHECK(condition) \
470 !(condition) ? LOGGING_CRASH() : EAT_STREAM_PARAMETERS 498 !(condition) ? LOGGING_CRASH(__LINE__) : EAT_STREAM_PARAMETERS
471 499
472 #define PCHECK(condition) CHECK(condition) 500 #define PCHECK(condition) CHECK(condition)
473 501
474 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) 502 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2))
475 503
476 #else // !(OFFICIAL_BUILD && NDEBUG) 504 #else // !(OFFICIAL_BUILD && NDEBUG)
477 505
478 #if defined(_PREFAST_) && defined(OS_WIN) 506 #if defined(_PREFAST_) && defined(OS_WIN)
479 // Use __analysis_assume to tell the VC++ static analysis engine that 507 // Use __analysis_assume to tell the VC++ static analysis engine that
480 // assert conditions are true, to suppress warnings. The LAZY_STREAM 508 // assert conditions are true, to suppress warnings. The LAZY_STREAM
(...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after
988 #elif NOTIMPLEMENTED_POLICY == 5 1016 #elif NOTIMPLEMENTED_POLICY == 5
989 #define NOTIMPLEMENTED() do {\ 1017 #define NOTIMPLEMENTED() do {\
990 static bool logged_once = false;\ 1018 static bool logged_once = false;\
991 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ 1019 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\
992 logged_once = true;\ 1020 logged_once = true;\
993 } while(0);\ 1021 } while(0);\
994 EAT_STREAM_PARAMETERS 1022 EAT_STREAM_PARAMETERS
995 #endif 1023 #endif
996 1024
997 #endif // BASE_LOGGING_H_ 1025 #endif // BASE_LOGGING_H_
OLDNEW
« no previous file with comments | « no previous file | base/logging_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698