[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 | #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" |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 16 | #include "build/build_config.h" |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 17 | |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 18 | #if defined(USE_TCMALLOC) |
primiano | f7b03f4 | 2016-01-26 00:00:23 | [diff] [blame^] | 19 | #include "third_party/tcmalloc/chromium/src/gperftools/tcmalloc.h" |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 20 | #endif |
| 21 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 22 | namespace base { |
| 23 | |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 24 | size_t g_oom_size = 0U; |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 25 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 26 | namespace { |
| 27 | |
[email protected] | 1d51882f | 2013-11-12 01:59:02 | [diff] [blame] | 28 | #if !defined(OS_ANDROID) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 29 | void OnNoMemorySize(size_t size) { |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 30 | g_oom_size = size; |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 31 | |
| 32 | if (size != 0) |
| 33 | LOG(FATAL) << "Out of memory, size = " << size; |
| 34 | LOG(FATAL) << "Out of memory."; |
| 35 | } |
| 36 | |
| 37 | void OnNoMemory() { |
| 38 | OnNoMemorySize(0); |
| 39 | } |
[email protected] | 1d51882f | 2013-11-12 01:59:02 | [diff] [blame] | 40 | #endif // !defined(OS_ANDROID) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 41 | |
| 42 | } // namespace |
| 43 | |
| 44 | #if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && \ |
| 45 | !defined(THREAD_SANITIZER) && !defined(LEAK_SANITIZER) |
| 46 | |
| 47 | #if defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) |
| 48 | |
| 49 | extern "C" { |
| 50 | void* __libc_malloc(size_t size); |
| 51 | void* __libc_realloc(void* ptr, size_t size); |
| 52 | void* __libc_calloc(size_t nmemb, size_t size); |
| 53 | void* __libc_valloc(size_t size); |
[email protected] | 6bad17e | 2014-03-04 04:54:26 | [diff] [blame] | 54 | #if PVALLOC_AVAILABLE == 1 |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 55 | void* __libc_pvalloc(size_t size); |
[email protected] | 6bad17e | 2014-03-04 04:54:26 | [diff] [blame] | 56 | #endif |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 57 | void* __libc_memalign(size_t alignment, size_t size); |
| 58 | |
| 59 | // Overriding the system memory allocation functions: |
| 60 | // |
| 61 | // For security reasons, we want malloc failures to be fatal. Too much code |
| 62 | // doesn't check for a NULL return value from malloc and unconditionally uses |
| 63 | // the resulting pointer. If the first offset that they try to access is |
| 64 | // attacker controlled, then the attacker can direct the code to access any |
| 65 | // part of memory. |
| 66 | // |
| 67 | // Thus, we define all the standard malloc functions here and mark them as |
| 68 | // visibility 'default'. This means that they replace the malloc functions for |
| 69 | // all Chromium code and also for all code in shared libraries. There are tests |
| 70 | // for this in process_util_unittest.cc. |
| 71 | // |
| 72 | // If we are using tcmalloc, then the problem is moot since tcmalloc handles |
| 73 | // this for us. Thus this code is in a !defined(USE_TCMALLOC) block. |
| 74 | // |
| 75 | // If we are testing the binary with AddressSanitizer, we should not |
| 76 | // redefine malloc and let AddressSanitizer do it instead. |
| 77 | // |
| 78 | // We call the real libc functions in this code by using __libc_malloc etc. |
| 79 | // Previously we tried using dlsym(RTLD_NEXT, ...) but that failed depending on |
| 80 | // the link order. Since ld.so needs calloc during symbol resolution, it |
| 81 | // defines its own versions of several of these functions in dl-minimal.c. |
| 82 | // Depending on the runtime library order, dlsym ended up giving us those |
| 83 | // functions and bad things happened. See crbug.com/31809 |
| 84 | // |
| 85 | // This means that any code which calls __libc_* gets the raw libc versions of |
| 86 | // these functions. |
| 87 | |
| 88 | #define DIE_ON_OOM_1(function_name) \ |
| 89 | void* function_name(size_t) __attribute__ ((visibility("default"))); \ |
| 90 | \ |
| 91 | void* function_name(size_t size) { \ |
| 92 | void* ret = __libc_##function_name(size); \ |
| 93 | if (ret == NULL && size != 0) \ |
| 94 | OnNoMemorySize(size); \ |
| 95 | return ret; \ |
| 96 | } |
| 97 | |
| 98 | #define DIE_ON_OOM_2(function_name, arg1_type) \ |
| 99 | void* function_name(arg1_type, size_t) \ |
| 100 | __attribute__ ((visibility("default"))); \ |
| 101 | \ |
| 102 | void* function_name(arg1_type arg1, size_t size) { \ |
| 103 | void* ret = __libc_##function_name(arg1, size); \ |
| 104 | if (ret == NULL && size != 0) \ |
| 105 | OnNoMemorySize(size); \ |
| 106 | return ret; \ |
| 107 | } |
| 108 | |
| 109 | DIE_ON_OOM_1(malloc) |
| 110 | DIE_ON_OOM_1(valloc) |
[email protected] | 6bad17e | 2014-03-04 04:54:26 | [diff] [blame] | 111 | #if PVALLOC_AVAILABLE == 1 |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 112 | DIE_ON_OOM_1(pvalloc) |
[email protected] | 6bad17e | 2014-03-04 04:54:26 | [diff] [blame] | 113 | #endif |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 114 | |
| 115 | DIE_ON_OOM_2(calloc, size_t) |
| 116 | DIE_ON_OOM_2(realloc, void*) |
| 117 | DIE_ON_OOM_2(memalign, size_t) |
| 118 | |
| 119 | // posix_memalign has a unique signature and doesn't have a __libc_ variant. |
| 120 | int posix_memalign(void** ptr, size_t alignment, size_t size) |
| 121 | __attribute__ ((visibility("default"))); |
| 122 | |
| 123 | int posix_memalign(void** ptr, size_t alignment, size_t size) { |
| 124 | // This will use the safe version of memalign, above. |
| 125 | *ptr = memalign(alignment, size); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | } // extern C |
| 130 | |
| 131 | #else |
| 132 | |
| 133 | // TODO([email protected]): dlsym dance |
| 134 | |
| 135 | #endif // LIBC_GLIBC && !USE_TCMALLOC |
| 136 | |
| 137 | #endif // !*_SANITIZER |
| 138 | |
| 139 | void EnableTerminationOnHeapCorruption() { |
| 140 | // On Linux, there nothing to do AFAIK. |
| 141 | } |
| 142 | |
| 143 | void EnableTerminationOnOutOfMemory() { |
| 144 | #if defined(OS_ANDROID) |
| 145 | // Android doesn't support setting a new handler. |
| 146 | DLOG(WARNING) << "Not feasible."; |
| 147 | #else |
| 148 | // Set the new-out of memory handler. |
| 149 | std::set_new_handler(&OnNoMemory); |
| 150 | // If we're using glibc's allocator, the above functions will override |
| 151 | // malloc and friends and make them die on out of memory. |
| 152 | #endif |
primiano | f7b03f4 | 2016-01-26 00:00:23 | [diff] [blame^] | 153 | #if defined(USE_TCMALLOC) |
| 154 | // For tcmalloc, we need to tell it to behave like new. |
| 155 | tc_set_new_mode(1); |
| 156 | #endif |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // NOTE: This is not the only version of this function in the source: |
| 160 | // the setuid sandbox (in process_util_linux.c, in the sandbox source) |
| 161 | // also has its own C version. |
| 162 | bool AdjustOOMScore(ProcessId process, int score) { |
| 163 | if (score < 0 || score > kMaxOomScore) |
| 164 | return false; |
| 165 | |
| 166 | FilePath oom_path(internal::GetProcPidDir(process)); |
| 167 | |
| 168 | // Attempt to write the newer oom_score_adj file first. |
| 169 | FilePath oom_file = oom_path.AppendASCII("oom_score_adj"); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 170 | if (PathExists(oom_file)) { |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 171 | std::string score_str = IntToString(score); |
| 172 | DVLOG(1) << "Adjusting oom_score_adj of " << process << " to " |
| 173 | << score_str; |
| 174 | int score_len = static_cast<int>(score_str.length()); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 175 | return (score_len == WriteFile(oom_file, score_str.c_str(), score_len)); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // If the oom_score_adj file doesn't exist, then we write the old |
| 179 | // style file and translate the oom_adj score to the range 0-15. |
| 180 | oom_file = oom_path.AppendASCII("oom_adj"); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 181 | if (PathExists(oom_file)) { |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 182 | // Max score for the old oom_adj range. Used for conversion of new |
| 183 | // values to old values. |
| 184 | const int kMaxOldOomScore = 15; |
| 185 | |
| 186 | int converted_score = score * kMaxOldOomScore / kMaxOomScore; |
| 187 | std::string score_str = IntToString(converted_score); |
| 188 | DVLOG(1) << "Adjusting oom_adj of " << process << " to " << score_str; |
| 189 | int score_len = static_cast<int>(score_str.length()); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 190 | return (score_len == WriteFile(oom_file, score_str.c_str(), score_len)); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | return false; |
| 194 | } |
| 195 | |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 196 | bool UncheckedMalloc(size_t size, void** result) { |
[email protected] | e24b74f | 2014-03-29 17:30:40 | [diff] [blame] | 197 | #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) || \ |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 198 | (!defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)) |
| 199 | *result = malloc(size); |
| 200 | #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) |
| 201 | *result = __libc_malloc(size); |
| 202 | #elif defined(USE_TCMALLOC) |
primiano | f7b03f4 | 2016-01-26 00:00:23 | [diff] [blame^] | 203 | *result = tc_malloc_skip_new_handler(size); |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 204 | #endif |
| 205 | return *result != NULL; |
| 206 | } |
| 207 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 208 | } // namespace base |