xref: /aosp_15_r20/external/libchrome/base/process/memory.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_PROCESS_MEMORY_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_PROCESS_MEMORY_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/process/process_handle.h"
12*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker #ifdef PVALLOC_AVAILABLE
15*635a8641SAndroid Build Coastguard Worker // Build config explicitly tells us whether or not pvalloc is available.
16*635a8641SAndroid Build Coastguard Worker #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)
17*635a8641SAndroid Build Coastguard Worker #define PVALLOC_AVAILABLE 1
18*635a8641SAndroid Build Coastguard Worker #else
19*635a8641SAndroid Build Coastguard Worker #define PVALLOC_AVAILABLE 0
20*635a8641SAndroid Build Coastguard Worker #endif
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker namespace base {
23*635a8641SAndroid Build Coastguard Worker 
24*635a8641SAndroid Build Coastguard Worker // Enables 'terminate on heap corruption' flag. Helps protect against heap
25*635a8641SAndroid Build Coastguard Worker // overflow. Has no effect if the OS doesn't provide the necessary facility.
26*635a8641SAndroid Build Coastguard Worker BASE_EXPORT void EnableTerminationOnHeapCorruption();
27*635a8641SAndroid Build Coastguard Worker 
28*635a8641SAndroid Build Coastguard Worker // Turns on process termination if memory runs out.
29*635a8641SAndroid Build Coastguard Worker BASE_EXPORT void EnableTerminationOnOutOfMemory();
30*635a8641SAndroid Build Coastguard Worker 
31*635a8641SAndroid Build Coastguard Worker // Terminates process. Should be called only for out of memory errors.
32*635a8641SAndroid Build Coastguard Worker // Crash reporting classifies such crashes as OOM.
33*635a8641SAndroid Build Coastguard Worker BASE_EXPORT void TerminateBecauseOutOfMemory(size_t size);
34*635a8641SAndroid Build Coastguard Worker 
35*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX)
36*635a8641SAndroid Build Coastguard Worker BASE_EXPORT extern size_t g_oom_size;
37*635a8641SAndroid Build Coastguard Worker 
38*635a8641SAndroid Build Coastguard Worker // The maximum allowed value for the OOM score.
39*635a8641SAndroid Build Coastguard Worker const int kMaxOomScore = 1000;
40*635a8641SAndroid Build Coastguard Worker 
41*635a8641SAndroid Build Coastguard Worker // This adjusts /proc/<pid>/oom_score_adj so the Linux OOM killer will
42*635a8641SAndroid Build Coastguard Worker // prefer to kill certain process types over others. The range for the
43*635a8641SAndroid Build Coastguard Worker // adjustment is [-1000, 1000], with [0, 1000] being user accessible.
44*635a8641SAndroid Build Coastguard Worker // If the Linux system doesn't support the newer oom_score_adj range
45*635a8641SAndroid Build Coastguard Worker // of [0, 1000], then we revert to using the older oom_adj, and
46*635a8641SAndroid Build Coastguard Worker // translate the given value into [0, 15].  Some aliasing of values
47*635a8641SAndroid Build Coastguard Worker // may occur in that case, of course.
48*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool AdjustOOMScore(ProcessId process, int score);
49*635a8641SAndroid Build Coastguard Worker #endif
50*635a8641SAndroid Build Coastguard Worker 
51*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
52*635a8641SAndroid Build Coastguard Worker namespace win {
53*635a8641SAndroid Build Coastguard Worker 
54*635a8641SAndroid Build Coastguard Worker // Custom Windows exception code chosen to indicate an out of memory error.
55*635a8641SAndroid Build Coastguard Worker // See https://msdn.microsoft.com/en-us/library/het71c37.aspx.
56*635a8641SAndroid Build Coastguard Worker // "To make sure that you do not define a code that conflicts with an existing
57*635a8641SAndroid Build Coastguard Worker // exception code" ... "The resulting error code should therefore have the
58*635a8641SAndroid Build Coastguard Worker // highest four bits set to hexadecimal E."
59*635a8641SAndroid Build Coastguard Worker // 0xe0000008 was chosen arbitrarily, as 0x00000008 is ERROR_NOT_ENOUGH_MEMORY.
60*635a8641SAndroid Build Coastguard Worker const DWORD kOomExceptionCode = 0xe0000008;
61*635a8641SAndroid Build Coastguard Worker 
62*635a8641SAndroid Build Coastguard Worker }  // namespace win
63*635a8641SAndroid Build Coastguard Worker #endif
64*635a8641SAndroid Build Coastguard Worker 
65*635a8641SAndroid Build Coastguard Worker // Special allocator functions for callers that want to check for OOM.
66*635a8641SAndroid Build Coastguard Worker // These will not abort if the allocation fails even if
67*635a8641SAndroid Build Coastguard Worker // EnableTerminationOnOutOfMemory has been called.
68*635a8641SAndroid Build Coastguard Worker // This can be useful for huge and/or unpredictable size memory allocations.
69*635a8641SAndroid Build Coastguard Worker // Please only use this if you really handle the case when the allocation
70*635a8641SAndroid Build Coastguard Worker // fails. Doing otherwise would risk security.
71*635a8641SAndroid Build Coastguard Worker // These functions may still crash on OOM when running under memory tools,
72*635a8641SAndroid Build Coastguard Worker // specifically ASan and other sanitizers.
73*635a8641SAndroid Build Coastguard Worker // Return value tells whether the allocation succeeded. If it fails |result| is
74*635a8641SAndroid Build Coastguard Worker // set to NULL, otherwise it holds the memory address.
75*635a8641SAndroid Build Coastguard Worker BASE_EXPORT WARN_UNUSED_RESULT bool UncheckedMalloc(size_t size,
76*635a8641SAndroid Build Coastguard Worker                                                     void** result);
77*635a8641SAndroid Build Coastguard Worker BASE_EXPORT WARN_UNUSED_RESULT bool UncheckedCalloc(size_t num_items,
78*635a8641SAndroid Build Coastguard Worker                                                     size_t size,
79*635a8641SAndroid Build Coastguard Worker                                                     void** result);
80*635a8641SAndroid Build Coastguard Worker 
81*635a8641SAndroid Build Coastguard Worker }  // namespace base
82*635a8641SAndroid Build Coastguard Worker 
83*635a8641SAndroid Build Coastguard Worker #endif  // BASE_PROCESS_MEMORY_H_
84