blob: b5c964084870a927d8adb60e8692f4b6fa5819d7 [file] [log] [blame]
[email protected]bac984102013-06-28 17:40:241// 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
avibeced7c2015-12-24 06:47:597#include <stddef.h>
8
[email protected]bac984102013-06-28 17:40:249#include <new>
10
[email protected]bac984102013-06-28 17:40:2411#include "base/files/file_path.h"
[email protected]e3177dd52014-08-13 20:22:1412#include "base/files/file_util.h"
[email protected]bac984102013-06-28 17:40:2413#include "base/logging.h"
14#include "base/process/internal_linux.h"
15#include "base/strings/string_number_conversions.h"
avibeced7c2015-12-24 06:47:5916#include "build/build_config.h"
[email protected]bac984102013-06-28 17:40:2417
[email protected]29159eb2014-03-21 22:07:0318#if defined(USE_TCMALLOC)
primianof7b03f42016-01-26 00:00:2319#include "third_party/tcmalloc/chromium/src/gperftools/tcmalloc.h"
[email protected]29159eb2014-03-21 22:07:0320#endif
21
[email protected]bac984102013-06-28 17:40:2422namespace base {
23
[email protected]992a60652013-07-15 18:29:3524size_t g_oom_size = 0U;
[email protected]992a60652013-07-15 18:29:3525
[email protected]bac984102013-06-28 17:40:2426namespace {
27
[email protected]1d51882f2013-11-12 01:59:0228#if !defined(OS_ANDROID)
[email protected]bac984102013-06-28 17:40:2429void OnNoMemorySize(size_t size) {
[email protected]bac984102013-06-28 17:40:2430 g_oom_size = size;
[email protected]bac984102013-06-28 17:40:2431
32 if (size != 0)
33 LOG(FATAL) << "Out of memory, size = " << size;
34 LOG(FATAL) << "Out of memory.";
35}
36
37void OnNoMemory() {
38 OnNoMemorySize(0);
39}
[email protected]1d51882f2013-11-12 01:59:0240#endif // !defined(OS_ANDROID)
[email protected]bac984102013-06-28 17:40:2441
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
49extern "C" {
50void* __libc_malloc(size_t size);
51void* __libc_realloc(void* ptr, size_t size);
52void* __libc_calloc(size_t nmemb, size_t size);
53void* __libc_valloc(size_t size);
[email protected]6bad17e2014-03-04 04:54:2654#if PVALLOC_AVAILABLE == 1
[email protected]bac984102013-06-28 17:40:2455void* __libc_pvalloc(size_t size);
[email protected]6bad17e2014-03-04 04:54:2656#endif
[email protected]bac984102013-06-28 17:40:2457void* __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
109DIE_ON_OOM_1(malloc)
110DIE_ON_OOM_1(valloc)
[email protected]6bad17e2014-03-04 04:54:26111#if PVALLOC_AVAILABLE == 1
[email protected]bac984102013-06-28 17:40:24112DIE_ON_OOM_1(pvalloc)
[email protected]6bad17e2014-03-04 04:54:26113#endif
[email protected]bac984102013-06-28 17:40:24114
115DIE_ON_OOM_2(calloc, size_t)
116DIE_ON_OOM_2(realloc, void*)
117DIE_ON_OOM_2(memalign, size_t)
118
119// posix_memalign has a unique signature and doesn't have a __libc_ variant.
120int posix_memalign(void** ptr, size_t alignment, size_t size)
121 __attribute__ ((visibility("default")));
122
123int 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
139void EnableTerminationOnHeapCorruption() {
140 // On Linux, there nothing to do AFAIK.
141}
142
143void 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
primianof7b03f42016-01-26 00:00:23153#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]bac984102013-06-28 17:40:24157}
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.
162bool 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]7567484142013-07-11 17:36:07170 if (PathExists(oom_file)) {
[email protected]bac984102013-06-28 17:40:24171 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]e5c2a22e2014-03-06 20:42:30175 return (score_len == WriteFile(oom_file, score_str.c_str(), score_len));
[email protected]bac984102013-06-28 17:40:24176 }
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]7567484142013-07-11 17:36:07181 if (PathExists(oom_file)) {
[email protected]bac984102013-06-28 17:40:24182 // 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]e5c2a22e2014-03-06 20:42:30190 return (score_len == WriteFile(oom_file, score_str.c_str(), score_len));
[email protected]bac984102013-06-28 17:40:24191 }
192
193 return false;
194}
195
[email protected]29159eb2014-03-21 22:07:03196bool UncheckedMalloc(size_t size, void** result) {
[email protected]e24b74f2014-03-29 17:30:40197#if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) || \
[email protected]29159eb2014-03-21 22:07:03198 (!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)
primianof7b03f42016-01-26 00:00:23203 *result = tc_malloc_skip_new_handler(size);
[email protected]29159eb2014-03-21 22:07:03204#endif
205 return *result != NULL;
206}
207
[email protected]bac984102013-06-28 17:40:24208} // namespace base