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