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