[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 1 | // Copyright (c) 2013 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 | |
| 5 | #ifndef BASE_PROCESS_MEMORY_H_ |
| 6 | #define BASE_PROCESS_MEMORY_H_ |
| 7 | |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 10 | #include "base/base_export.h" |
[email protected] | dd4b5126 | 2013-07-25 21:38:23 | [diff] [blame] | 11 | #include "base/process/process_handle.h" |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 12 | #include "build/build_config.h" |
| 13 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 14 | namespace base { |
| 15 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 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 | |
vitalybuka | 06c8318 | 2015-03-27 19:06:39 | [diff] [blame] | 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 | |
rayb | 0088ee5 | 2017-04-26 22:35:08 | [diff] [blame] | 27 | #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX) |
[email protected] | 9742508 | 2013-07-16 03:10:07 | [diff] [blame] | 28 | BASE_EXPORT extern size_t g_oom_size; |
| 29 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 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 | |
Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame^] | 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 | |
wfh | 8ca194a | 2016-07-20 02:06:54 | [diff] [blame] | 49 | #if defined(OS_WIN) |
| 50 | namespace win { |
| 51 | |
wfh | 3850a4d | 2016-07-23 00:23:53 | [diff] [blame] | 52 | // Custom Windows exception code chosen to indicate an out of memory error. |
wfh | 8ca194a | 2016-07-20 02:06:54 | [diff] [blame] | 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 | |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 63 | // Special allocator functions for callers that want to check for OOM. |
| 64 | // These will not abort if the allocation fails even if |
| 65 | // EnableTerminationOnOutOfMemory has been called. |
| 66 | // This can be useful for huge and/or unpredictable size memory allocations. |
| 67 | // Please only use this if you really handle the case when the allocation |
| 68 | // fails. Doing otherwise would risk security. |
[email protected] | e24b74f | 2014-03-29 17:30:40 | [diff] [blame] | 69 | // These functions may still crash on OOM when running under memory tools, |
| 70 | // specifically ASan and other sanitizers. |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 71 | // Return value tells whether the allocation succeeded. If it fails |result| is |
| 72 | // set to NULL, otherwise it holds the memory address. |
| 73 | BASE_EXPORT WARN_UNUSED_RESULT bool UncheckedMalloc(size_t size, |
| 74 | void** result); |
| 75 | BASE_EXPORT WARN_UNUSED_RESULT bool UncheckedCalloc(size_t num_items, |
| 76 | size_t size, |
| 77 | void** result); |
| 78 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 79 | } // namespace base |
| 80 | |
| 81 | #endif // BASE_PROCESS_MEMORY_H_ |