• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_NOTREACHED_H_
6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_NOTREACHED_H_
7 
8 #include "base/allocator/partition_allocator/logging_buildflags.h"
9 #include "base/allocator/partition_allocator/partition_alloc_base/compiler_specific.h"
10 #include "base/allocator/partition_allocator/partition_alloc_base/debug/debugging_buildflags.h"
11 #include "base/allocator/partition_allocator/partition_alloc_buildflags.h"
12 #include "base/allocator/partition_allocator/partition_alloc_check.h"
13 
14 // When PartitionAlloc is used as the default allocator, we cannot use the
15 // regular (D)CHECK() macros, as they allocate internally. (c.f. //
16 // base/allocator/partition_allocator/partition_alloc_check.h)
17 // So PA_NOTREACHED() uses PA_DCHECK() instead of DCHECK().
18 
19 #if BUILDFLAG(PA_ENABLE_LOG_ERROR_NOT_REACHED)
20 #define PA_NOTREACHED()                                                    \
21   true ? ::partition_alloc::internal::logging::RawError(                   \
22              __FILE__ "(" PA_STRINGIFY(__LINE__) ") PA_NOTREACHED() hit.") \
23        : PA_EAT_CHECK_STREAM_PARAMS()
24 
25 #elif BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && defined(OFFICIAL_BUILD) && \
26     defined(NDEBUG) && BUILDFLAG(PA_DCHECK_IS_ON)
27 
28 // PA_DCHECK(condition) is PA_CHECK(condition) if BUILDFLAG(PA_DCHECK_IS_ON).
29 // When BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC), OFFICIAL_BUILD,
30 // NDEBUG are defined, PA_CHECK(false) is IMMEDIATE_CRASH(). Since
31 // IMMEDIATE_CRASH() hints __builtin_unreachable() to the compiler, the
32 // following code causes compile failure:
33 //   switch(...) {
34 //     ...
35 //     case X:
36 //       PA_DCHECK(false);
37 //       [[fallthrough]]; // The compiler knows "not reached".
38 //     case Y:
39 //     ...
40 // So define PA_NOTREACHED() by using async-signal-safe RawCheck().
41 #define PA_NOTREACHED()                                               \
42   PA_UNLIKELY(true)                                                   \
43   ? ::partition_alloc::internal::logging::RawCheck(                   \
44         __FILE__ "(" PA_STRINGIFY(__LINE__) ") PA_NOTREACHED() hit.") \
45   : PA_EAT_CHECK_STREAM_PARAMS()
46 
47 #else
48 
49 // PA_CHECK() uses RawCheck() for error reporting. So "PA_DCHECK(false);
50 // [[fallthrough]];" doesn't cause compile failure.
51 #define PA_NOTREACHED() PA_DCHECK(false)
52 
53 #endif
54 
55 #endif  // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_NOTREACHED_H_
56