| 6 |
| 7 | #include <stddef.h> |
| 8 |
| 9 | #include <new> |
| 10 |
| 11 | #include "base/allocator/allocator_shim.h" |
| 12 | #include "base/allocator/buildflags.h" |
| 13 | #include "base/files/file_path.h" |
| 14 | #include "base/files/file_util.h" |
| 15 | #include "base/logging.h" |
| 16 | #include "base/process/internal_linux.h" |
| 17 | #include "base/strings/string_number_conversions.h" |
| 18 | #include "base/threading/thread_restrictions.h" |
| 19 | #include "build/build_config.h" |
| 20 |
| 21 | #if BUILDFLAG(USE_TCMALLOC) |
| 22 | #include "third_party/tcmalloc/chromium/src/config.h" |
| 23 | #include "third_party/tcmalloc/chromium/src/gperftools/tcmalloc.h" |
| 24 | #endif |
| 25 |
| 26 | namespace base { |
| 27 |
| 28 | size_t g_oom_size = 0U; |
| 29 |
| 30 | namespace { |
| 31 |
| 32 | void OnNoMemorySize(size_t size) { |
| 33 | g_oom_size = size; |
| 34 |
| 35 | if (size != 0) |
| 36 | LOG(FATAL) << "Out of memory, size = " << size; |
| 37 | LOG(FATAL) << "Out of memory."; |
| 38 | } |
| 39 |
| 40 | // NOINLINE as base::`anonymous namespace`::OnNoMemory() is recognized by the |
| 41 | // crash server. |
| 42 | NOINLINE void OnNoMemory() { |
| 43 | OnNoMemorySize(0); |
| 44 | } |
| 45 |
| 46 | void ReleaseReservationOrTerminate() { |
| 47 | if (internal::ReleaseAddressSpaceReservation()) |
| 48 | return; |
| 49 | OnNoMemory(); |
| 50 | } |
| 51 |
| 52 | } // namespace |
| 53 |
| 54 | void EnableTerminationOnHeapCorruption() { |
| 55 | // On Linux, there nothing to do AFAIK. |
| 56 | } |
| 57 |
| 58 | void EnableTerminationOnOutOfMemory() { |
| 59 | // Set the new-out of memory handler. |
| 60 | std::set_new_handler(&ReleaseReservationOrTerminate); |
| 61 | // If we're using glibc's allocator, the above functions will override |
| 62 | // malloc and friends and make them die on out of memory. |
| 63 |
| 64 | #if BUILDFLAG(USE_ALLOCATOR_SHIM) |
| 65 | allocator::SetCallNewHandlerOnMallocFailure(true); |
| 66 | #elif defined(USE_TCMALLOC) |
| 67 | // For tcmalloc, we need to tell it to behave like new. |
| 68 | tc_set_new_mode(1); |
| 69 | #endif |
| 70 | } |
| 71 |
| 72 | // ScopedAllowBlocking() has private constructor and it can only be used in |
| 73 | // friend classes/functions. Declaring a class is easier in this situation to |
| 74 | // avoid adding more dependency to thread_restrictions.h because of the |
| 75 | // parameter used in AdjustOOMScore(). Specifically, ProcessId is a typedef |
| 76 | // and we'll need to include another header file in thread_restrictions.h |
| 77 | // without the class. |
| 78 | class AdjustOOMScoreHelper { |
| 79 | public: |
| 80 | static bool AdjustOOMScore(ProcessId process, int score); |
| 81 |
| 82 | private: |
| 83 | DISALLOW_IMPLICIT_CONSTRUCTORS(AdjustOOMScoreHelper); |
| 84 | }; |
| 85 |
| 86 | // static. |
| 87 | bool AdjustOOMScoreHelper::AdjustOOMScore(ProcessId process, int score) { |
| 88 | if (score < 0 || score > kMaxOomScore) |
| 89 | return false; |
| 90 |
| 91 | FilePath oom_path(internal::GetProcPidDir(process)); |
| 92 |
| 93 | // Temporarily allowing blocking since oom paths are pseudo-filesystem paths. |
| 94 | base::ScopedAllowBlocking allow_blocking; |
| 95 |
| 96 | // Attempt to write the newer oom_score_adj file first. |
| 97 | FilePath oom_file = oom_path.AppendASCII("oom_score_adj"); |
| 98 | if (PathExists(oom_file)) { |
| 99 | std::string score_str = NumberToString(score); |
| 100 | DVLOG(1) << "Adjusting oom_score_adj of " << process << " to " |
| 101 | << score_str; |
| 102 | int score_len = static_cast<int>(score_str.length()); |
| 103 | return (score_len == WriteFile(oom_file, score_str.c_str(), score_len)); |
| 104 | } |
| 105 |
| 106 | // If the oom_score_adj file doesn't exist, then we write the old |
| 107 | // style file and translate the oom_adj score to the range 0-15. |
| 108 | oom_file = oom_path.AppendASCII("oom_adj"); |
| 109 | if (PathExists(oom_file)) { |
| 110 | // Max score for the old oom_adj range. Used for conversion of new |
| 111 | // values to old values. |
| 112 | const int kMaxOldOomScore = 15; |
| 113 |
| 114 | int converted_score = score * kMaxOldOomScore / kMaxOomScore; |
| 115 | std::string score_str = NumberToString(converted_score); |
| 116 | DVLOG(1) << "Adjusting oom_adj of " << process << " to " << score_str; |
| 117 | int score_len = static_cast<int>(score_str.length()); |
| 118 | return (score_len == WriteFile(oom_file, score_str.c_str(), score_len)); |
| 119 | } |
| 120 |
| 121 | return false; |
| 122 | } |
| 123 |
| 124 | // NOTE: This is not the only version of this function in the source: |
| 125 | // the setuid sandbox (in process_util_linux.c, in the sandbox source) |
| 126 | // also has its own C version. |
| 127 | bool AdjustOOMScore(ProcessId process, int score) { |
| 128 | return AdjustOOMScoreHelper::AdjustOOMScore(process, score); |
| 129 | } |
| 130 |
| 131 | bool UncheckedMalloc(size_t size, void** result) { |
| 132 | #if BUILDFLAG(USE_ALLOCATOR_SHIM) |
| 133 | *result = allocator::UncheckedAlloc(size); |
| 134 | #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) || \ |
| 135 | (!defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)) |
| 136 | *result = malloc(size); |
| 137 | #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) |
| 138 | *result = __libc_malloc(size); |
| 139 | #elif defined(USE_TCMALLOC) |
| 140 | *result = tc_malloc_skip_new_handler(size); |
| 141 | #endif |
| 142 | return *result != nullptr; |
| 143 | } |
| 144 |
| 145 | } // namespace base |
| 146 |