partition_alloc_notreached.h 2.5 KB

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