| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/process/memory.h" |
| 6 | |
| avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 9 | #include <new> |
| 10 | |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
| [email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 13 | #include "base/logging.h" |
| 14 | #include "base/process/internal_linux.h" |
| 15 | #include "base/strings/string_number_conversions.h" |
| Cheng-Yu Lee | acd58b3a | 2018-08-11 05:32:37 | [diff] [blame] | 16 | #include "base/threading/thread_restrictions.h" |
| avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 17 | #include "build/build_config.h" |
| Arthur Sonzogni | fd39d61 | 2024-06-26 08:16:23 | [diff] [blame] | 18 | #include "partition_alloc/buildflags.h" |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 19 | |
| Etienne Dechamps | 4dacf2b | 2024-12-19 16:31:05 | [diff] [blame] | 20 | #if PA_BUILDFLAG(USE_ALLOCATOR_SHIM) |
| 21 | #include "partition_alloc/shim/allocator_shim.h" // nogncheck |
| 22 | #elif !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) && defined(LIBC_GLIBC) |
| Takashi Sakamoto | 76682f3 | 2023-11-14 06:21:36 | [diff] [blame] | 23 | extern "C" { |
| 24 | void* __libc_malloc(size_t); |
| 25 | void __libc_free(void*); |
| Etienne Dechamps | 8e1287b | 2025-09-17 14:59:22 | [diff] [blame] | 26 | void* __libc_calloc(size_t, size_t); |
| Takashi Sakamoto | 76682f3 | 2023-11-14 06:21:36 | [diff] [blame] | 27 | } |
| 28 | #endif |
| 29 | |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 30 | namespace base { |
| 31 | |
| 32 | namespace { |
| 33 | |
| Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 34 | void ReleaseReservationOrTerminate() { |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 35 | if (internal::ReleaseAddressSpaceReservation()) { |
| Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 36 | return; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 37 | } |
| Torne (Richard Coles) | f6e6c27 | 2021-01-26 16:58:40 | [diff] [blame] | 38 | TerminateBecauseOutOfMemory(0); |
| Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 39 | } |
| 40 | |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 41 | } // namespace |
| 42 | |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 43 | void EnableTerminationOnHeapCorruption() { |
| 44 | // On Linux, there nothing to do AFAIK. |
| 45 | } |
| 46 | |
| 47 | void EnableTerminationOnOutOfMemory() { |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 48 | // Set the new-out of memory handler. |
| Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 49 | std::set_new_handler(&ReleaseReservationOrTerminate); |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 50 | // If we're using glibc's allocator, the above functions will override |
| 51 | // malloc and friends and make them die on out of memory. |
| primiano | 4e68ed2 | 2016-03-09 20:13:44 | [diff] [blame] | 52 | |
| Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 53 | #if PA_BUILDFLAG(USE_ALLOCATOR_SHIM) |
| Yuki Shiino | 2ff8131 | 2022-09-05 13:11:55 | [diff] [blame] | 54 | allocator_shim::SetCallNewHandlerOnMallocFailure(true); |
| primiano | f7b03f4 | 2016-01-26 00:00:23 | [diff] [blame] | 55 | #endif |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 56 | } |
| 57 | |
| Cheng-Yu Lee | acd58b3a | 2018-08-11 05:32:37 | [diff] [blame] | 58 | // ScopedAllowBlocking() has private constructor and it can only be used in |
| 59 | // friend classes/functions. Declaring a class is easier in this situation to |
| 60 | // avoid adding more dependency to thread_restrictions.h because of the |
| 61 | // parameter used in AdjustOOMScore(). Specifically, ProcessId is a typedef |
| 62 | // and we'll need to include another header file in thread_restrictions.h |
| 63 | // without the class. |
| 64 | class AdjustOOMScoreHelper { |
| 65 | public: |
| Peter Boström | 511258be | 2021-11-03 01:18:46 | [diff] [blame] | 66 | AdjustOOMScoreHelper() = delete; |
| 67 | AdjustOOMScoreHelper(const AdjustOOMScoreHelper&) = delete; |
| 68 | AdjustOOMScoreHelper& operator=(const AdjustOOMScoreHelper&) = delete; |
| Tomasz Tylenda | 15aa982 | 2021-11-02 12:30:23 | [diff] [blame] | 69 | |
| Peter Boström | 511258be | 2021-11-03 01:18:46 | [diff] [blame] | 70 | static bool AdjustOOMScore(ProcessId process, int score); |
| Cheng-Yu Lee | acd58b3a | 2018-08-11 05:32:37 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | // static. |
| 74 | bool AdjustOOMScoreHelper::AdjustOOMScore(ProcessId process, int score) { |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 75 | if (score < 0 || score > kMaxOomScore) { |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 76 | return false; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 77 | } |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 78 | |
| 79 | FilePath oom_path(internal::GetProcPidDir(process)); |
| 80 | |
| Cheng-Yu Lee | acd58b3a | 2018-08-11 05:32:37 | [diff] [blame] | 81 | // Temporarily allowing blocking since oom paths are pseudo-filesystem paths. |
| 82 | base::ScopedAllowBlocking allow_blocking; |
| 83 | |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 84 | // Attempt to write the newer oom_score_adj file first. |
| 85 | FilePath oom_file = oom_path.AppendASCII("oom_score_adj"); |
| [email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 86 | if (PathExists(oom_file)) { |
| Raul Tambre | a9c1364 | 2019-03-25 13:34:42 | [diff] [blame] | 87 | std::string score_str = NumberToString(score); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 88 | DVLOG(1) << "Adjusting oom_score_adj of " << process << " to " << score_str; |
| danakj | 580718646 | 2024-06-06 20:10:37 | [diff] [blame] | 89 | return WriteFile(oom_file, as_byte_span(score_str)); |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // If the oom_score_adj file doesn't exist, then we write the old |
| 93 | // style file and translate the oom_adj score to the range 0-15. |
| 94 | oom_file = oom_path.AppendASCII("oom_adj"); |
| [email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 95 | if (PathExists(oom_file)) { |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 96 | // Max score for the old oom_adj range. Used for conversion of new |
| 97 | // values to old values. |
| 98 | const int kMaxOldOomScore = 15; |
| 99 | |
| 100 | int converted_score = score * kMaxOldOomScore / kMaxOomScore; |
| Raul Tambre | a9c1364 | 2019-03-25 13:34:42 | [diff] [blame] | 101 | std::string score_str = NumberToString(converted_score); |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 102 | DVLOG(1) << "Adjusting oom_adj of " << process << " to " << score_str; |
| danakj | 580718646 | 2024-06-06 20:10:37 | [diff] [blame] | 103 | return WriteFile(oom_file, as_byte_span(score_str)); |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return false; |
| 107 | } |
| 108 | |
| Cheng-Yu Lee | acd58b3a | 2018-08-11 05:32:37 | [diff] [blame] | 109 | // NOTE: This is not the only version of this function in the source: |
| 110 | // the setuid sandbox (in process_util_linux.c, in the sandbox source) |
| 111 | // also has its own C version. |
| 112 | bool AdjustOOMScore(ProcessId process, int score) { |
| 113 | return AdjustOOMScoreHelper::AdjustOOMScore(process, score); |
| 114 | } |
| 115 | |
| Aldo Culquicondor | 189a4b5 | 2025-09-12 16:22:03 | [diff] [blame] | 116 | bool UncheckedCalloc(size_t num_items, size_t size, void** result) { |
| 117 | #if PA_BUILDFLAG(USE_ALLOCATOR_SHIM) |
| 118 | *result = allocator_shim::UncheckedCalloc(num_items, size); |
| 119 | #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) || !defined(LIBC_GLIBC) |
| 120 | *result = calloc(num_items, size); |
| 121 | #elif defined(LIBC_GLIBC) |
| 122 | *result = __libc_calloc(num_items, size); |
| 123 | #endif |
| 124 | return *result != nullptr; |
| 125 | } |
| 126 | |
| [email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 127 | bool UncheckedMalloc(size_t size, void** result) { |
| Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 128 | #if PA_BUILDFLAG(USE_ALLOCATOR_SHIM) |
| Yuki Shiino | 2ff8131 | 2022-09-05 13:11:55 | [diff] [blame] | 129 | *result = allocator_shim::UncheckedAlloc(size); |
| Benoit Lize | f4e1472 | 2022-01-12 10:56:52 | [diff] [blame] | 130 | #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) || !defined(LIBC_GLIBC) |
| [email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 131 | *result = malloc(size); |
| Benoit Lize | f4e1472 | 2022-01-12 10:56:52 | [diff] [blame] | 132 | #elif defined(LIBC_GLIBC) |
| [email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 133 | *result = __libc_malloc(size); |
| [email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 134 | #endif |
| Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 135 | return *result != nullptr; |
| [email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 136 | } |
| 137 | |
| Benoit Lize | 69ecd9f72 | 2021-12-13 13:49:05 | [diff] [blame] | 138 | void UncheckedFree(void* ptr) { |
| Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 139 | #if PA_BUILDFLAG(USE_ALLOCATOR_SHIM) |
| Yuki Shiino | 2ff8131 | 2022-09-05 13:11:55 | [diff] [blame] | 140 | allocator_shim::UncheckedFree(ptr); |
| Benoit Lize | f4e1472 | 2022-01-12 10:56:52 | [diff] [blame] | 141 | #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) || !defined(LIBC_GLIBC) |
| Benoit Lize | 69ecd9f72 | 2021-12-13 13:49:05 | [diff] [blame] | 142 | free(ptr); |
| Benoit Lize | f4e1472 | 2022-01-12 10:56:52 | [diff] [blame] | 143 | #elif defined(LIBC_GLIBC) |
| Benoit Lize | 69ecd9f72 | 2021-12-13 13:49:05 | [diff] [blame] | 144 | __libc_free(ptr); |
| Benoit Lize | 69ecd9f72 | 2021-12-13 13:49:05 | [diff] [blame] | 145 | #endif |
| 146 | } |
| 147 | |
| [email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 148 | } // namespace base |