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