| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/process_util.h" | 5 #include "base/process_util.h" |
| 6 | 6 |
| 7 #include <dirent.h> | 7 #include <dirent.h> |
| 8 #include <malloc.h> | 8 #include <malloc.h> |
| 9 #include <sys/time.h> | 9 #include <sys/time.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 return FilePath(); | 43 return FilePath(); |
| 44 } | 44 } |
| 45 return exe_name; | 45 return exe_name; |
| 46 } | 46 } |
| 47 | 47 |
| 48 int GetNumberOfThreads(ProcessHandle process) { | 48 int GetNumberOfThreads(ProcessHandle process) { |
| 49 return internal::ReadProcStatsAndGetFieldAsInt(process, | 49 return internal::ReadProcStatsAndGetFieldAsInt(process, |
| 50 internal::VM_NUMTHREADS); | 50 internal::VM_NUMTHREADS); |
| 51 } | 51 } |
| 52 | 52 |
| 53 namespace { | |
| 54 | |
| 55 void OnNoMemorySize(size_t size) { | |
| 56 #if defined(USE_LINUX_BREAKPAD) | |
| 57 g_oom_size = size; | |
| 58 #endif | |
| 59 | |
| 60 if (size != 0) | |
| 61 LOG(FATAL) << "Out of memory, size = " << size; | |
| 62 LOG(FATAL) << "Out of memory."; | |
| 63 } | |
| 64 | |
| 65 void OnNoMemory() { | |
| 66 OnNoMemorySize(0); | |
| 67 } | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 #if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && \ | |
| 72 !defined(THREAD_SANITIZER) && !defined(LEAK_SANITIZER) | |
| 73 | |
| 74 #if defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) | |
| 75 | |
| 76 extern "C" { | |
| 77 void* __libc_malloc(size_t size); | |
| 78 void* __libc_realloc(void* ptr, size_t size); | |
| 79 void* __libc_calloc(size_t nmemb, size_t size); | |
| 80 void* __libc_valloc(size_t size); | |
| 81 void* __libc_pvalloc(size_t size); | |
| 82 void* __libc_memalign(size_t alignment, size_t size); | |
| 83 | |
| 84 // Overriding the system memory allocation functions: | |
| 85 // | |
| 86 // For security reasons, we want malloc failures to be fatal. Too much code | |
| 87 // doesn't check for a NULL return value from malloc and unconditionally uses | |
| 88 // the resulting pointer. If the first offset that they try to access is | |
| 89 // attacker controlled, then the attacker can direct the code to access any | |
| 90 // part of memory. | |
| 91 // | |
| 92 // Thus, we define all the standard malloc functions here and mark them as | |
| 93 // visibility 'default'. This means that they replace the malloc functions for | |
| 94 // all Chromium code and also for all code in shared libraries. There are tests | |
| 95 // for this in process_util_unittest.cc. | |
| 96 // | |
| 97 // If we are using tcmalloc, then the problem is moot since tcmalloc handles | |
| 98 // this for us. Thus this code is in a !defined(USE_TCMALLOC) block. | |
| 99 // | |
| 100 // If we are testing the binary with AddressSanitizer, we should not | |
| 101 // redefine malloc and let AddressSanitizer do it instead. | |
| 102 // | |
| 103 // We call the real libc functions in this code by using __libc_malloc etc. | |
| 104 // Previously we tried using dlsym(RTLD_NEXT, ...) but that failed depending on | |
| 105 // the link order. Since ld.so needs calloc during symbol resolution, it | |
| 106 // defines its own versions of several of these functions in dl-minimal.c. | |
| 107 // Depending on the runtime library order, dlsym ended up giving us those | |
| 108 // functions and bad things happened. See crbug.com/31809 | |
| 109 // | |
| 110 // This means that any code which calls __libc_* gets the raw libc versions of | |
| 111 // these functions. | |
| 112 | |
| 113 #define DIE_ON_OOM_1(function_name) \ | |
| 114 void* function_name(size_t) __attribute__ ((visibility("default"))); \ | |
| 115 \ | |
| 116 void* function_name(size_t size) { \ | |
| 117 void* ret = __libc_##function_name(size); \ | |
| 118 if (ret == NULL && size != 0) \ | |
| 119 OnNoMemorySize(size); \ | |
| 120 return ret; \ | |
| 121 } | |
| 122 | |
| 123 #define DIE_ON_OOM_2(function_name, arg1_type) \ | |
| 124 void* function_name(arg1_type, size_t) \ | |
| 125 __attribute__ ((visibility("default"))); \ | |
| 126 \ | |
| 127 void* function_name(arg1_type arg1, size_t size) { \ | |
| 128 void* ret = __libc_##function_name(arg1, size); \ | |
| 129 if (ret == NULL && size != 0) \ | |
| 130 OnNoMemorySize(size); \ | |
| 131 return ret; \ | |
| 132 } | |
| 133 | |
| 134 DIE_ON_OOM_1(malloc) | |
| 135 DIE_ON_OOM_1(valloc) | |
| 136 DIE_ON_OOM_1(pvalloc) | |
| 137 | |
| 138 DIE_ON_OOM_2(calloc, size_t) | |
| 139 DIE_ON_OOM_2(realloc, void*) | |
| 140 DIE_ON_OOM_2(memalign, size_t) | |
| 141 | |
| 142 // posix_memalign has a unique signature and doesn't have a __libc_ variant. | |
| 143 int posix_memalign(void** ptr, size_t alignment, size_t size) | |
| 144 __attribute__ ((visibility("default"))); | |
| 145 | |
| 146 int posix_memalign(void** ptr, size_t alignment, size_t size) { | |
| 147 // This will use the safe version of memalign, above. | |
| 148 *ptr = memalign(alignment, size); | |
| 149 return 0; | |
| 150 } | |
| 151 | |
| 152 } // extern C | |
| 153 | |
| 154 #else | |
| 155 | |
| 156 // TODO(mostynb@opera.com): dlsym dance | |
| 157 | |
| 158 #endif // LIBC_GLIBC && !USE_TCMALLOC | |
| 159 | |
| 160 #endif // !*_SANITIZER | |
| 161 | |
| 162 void EnableTerminationOnHeapCorruption() { | |
| 163 // On Linux, there nothing to do AFAIK. | |
| 164 } | |
| 165 | |
| 166 void EnableTerminationOnOutOfMemory() { | |
| 167 #if defined(OS_ANDROID) | |
| 168 // Android doesn't support setting a new handler. | |
| 169 DLOG(WARNING) << "Not feasible."; | |
| 170 #else | |
| 171 // Set the new-out of memory handler. | |
| 172 std::set_new_handler(&OnNoMemory); | |
| 173 // If we're using glibc's allocator, the above functions will override | |
| 174 // malloc and friends and make them die on out of memory. | |
| 175 #endif | |
| 176 } | |
| 177 | |
| 178 // NOTE: This is not the only version of this function in the source: | |
| 179 // the setuid sandbox (in process_util_linux.c, in the sandbox source) | |
| 180 // also has its own C version. | |
| 181 bool AdjustOOMScore(ProcessId process, int score) { | |
| 182 if (score < 0 || score > kMaxOomScore) | |
| 183 return false; | |
| 184 | |
| 185 FilePath oom_path(internal::GetProcPidDir(process)); | |
| 186 | |
| 187 // Attempt to write the newer oom_score_adj file first. | |
| 188 FilePath oom_file = oom_path.AppendASCII("oom_score_adj"); | |
| 189 if (file_util::PathExists(oom_file)) { | |
| 190 std::string score_str = IntToString(score); | |
| 191 DVLOG(1) << "Adjusting oom_score_adj of " << process << " to " | |
| 192 << score_str; | |
| 193 int score_len = static_cast<int>(score_str.length()); | |
| 194 return (score_len == file_util::WriteFile(oom_file, | |
| 195 score_str.c_str(), | |
| 196 score_len)); | |
| 197 } | |
| 198 | |
| 199 // If the oom_score_adj file doesn't exist, then we write the old | |
| 200 // style file and translate the oom_adj score to the range 0-15. | |
| 201 oom_file = oom_path.AppendASCII("oom_adj"); | |
| 202 if (file_util::PathExists(oom_file)) { | |
| 203 // Max score for the old oom_adj range. Used for conversion of new | |
| 204 // values to old values. | |
| 205 const int kMaxOldOomScore = 15; | |
| 206 | |
| 207 int converted_score = score * kMaxOldOomScore / kMaxOomScore; | |
| 208 std::string score_str = IntToString(converted_score); | |
| 209 DVLOG(1) << "Adjusting oom_adj of " << process << " to " << score_str; | |
| 210 int score_len = static_cast<int>(score_str.length()); | |
| 211 return (score_len == file_util::WriteFile(oom_file, | |
| 212 score_str.c_str(), | |
| 213 score_len)); | |
| 214 } | |
| 215 | |
| 216 return false; | |
| 217 } | |
| 218 | |
| 219 } // namespace base | 53 } // namespace base |
| OLD | NEW |