oom.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) 2016 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_OOM_H_
  5. #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_OOM_H_
  6. #include <cstddef>
  7. #include "base/allocator/partition_allocator/allocation_guard.h"
  8. #include "base/allocator/partition_allocator/partition_alloc_base/compiler_specific.h"
  9. #include "base/allocator/partition_allocator/partition_alloc_base/component_export.h"
  10. #include "build/build_config.h"
  11. #if BUILDFLAG(IS_WIN)
  12. #include "base/allocator/partition_allocator/partition_alloc_base/win/windows_types.h"
  13. #endif
  14. namespace partition_alloc {
  15. // Terminates process. Should be called only for out of memory errors.
  16. // |size| is the size of the failed allocation, or 0 if not known.
  17. // Crash reporting classifies such crashes as OOM.
  18. // Must be allocation-safe.
  19. PA_COMPONENT_EXPORT(PARTITION_ALLOC)
  20. void TerminateBecauseOutOfMemory(size_t size);
  21. // Records the size of the allocation that caused the current OOM crash, for
  22. // consumption by Breakpad.
  23. // TODO: this can be removed when Breakpad is no longer supported.
  24. PA_COMPONENT_EXPORT(PARTITION_ALLOC) extern size_t g_oom_size;
  25. #if BUILDFLAG(IS_WIN)
  26. namespace win {
  27. // Custom Windows exception code chosen to indicate an out of memory error.
  28. // See https://msdn.microsoft.com/en-us/library/het71c37.aspx.
  29. // "To make sure that you do not define a code that conflicts with an existing
  30. // exception code" ... "The resulting error code should therefore have the
  31. // highest four bits set to hexadecimal E."
  32. // 0xe0000008 was chosen arbitrarily, as 0x00000008 is ERROR_NOT_ENOUGH_MEMORY.
  33. const DWORD kOomExceptionCode = 0xe0000008;
  34. } // namespace win
  35. #endif
  36. namespace internal {
  37. // The crash is generated in a PA_NOINLINE function so that we can classify the
  38. // crash as an OOM solely by analyzing the stack trace. It is tagged as
  39. // PA_NOT_TAIL_CALLED to ensure that its parent function stays on the stack.
  40. [[noreturn]] PA_COMPONENT_EXPORT(PARTITION_ALLOC) void PA_NOT_TAIL_CALLED
  41. OnNoMemory(size_t size);
  42. // OOM_CRASH(size) - Specialization of IMMEDIATE_CRASH which will raise a custom
  43. // exception on Windows to signal this is OOM and not a normal assert.
  44. // OOM_CRASH(size) is called by users of PageAllocator (including
  45. // PartitionAlloc) to signify an allocation failure from the platform.
  46. #define OOM_CRASH(size) \
  47. do { \
  48. /* Raising an exception might allocate, allow that. */ \
  49. ::partition_alloc::ScopedAllowAllocations guard{}; \
  50. ::partition_alloc::internal::OnNoMemory(size); \
  51. } while (0)
  52. } // namespace internal
  53. } // namespace partition_alloc
  54. #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_OOM_H_