blob: 3fb2caa1b64ba7ee1fe057bea695eb75eda76695 [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#ifndef BASE_PROCESS_MEMORY_H_
6#define BASE_PROCESS_MEMORY_H_
7
avibeced7c2015-12-24 06:47:598#include <stddef.h>
9
[email protected]bac984102013-06-28 17:40:2410#include "base/base_export.h"
[email protected]dd4b51262013-07-25 21:38:2311#include "base/process/process_handle.h"
[email protected]bac984102013-06-28 17:40:2412#include "build/build_config.h"
13
[email protected]bac984102013-06-28 17:40:2414namespace base {
15
[email protected]bac984102013-06-28 17:40:2416// Enables 'terminate on heap corruption' flag. Helps protect against heap
17// overflow. Has no effect if the OS doesn't provide the necessary facility.
18BASE_EXPORT void EnableTerminationOnHeapCorruption();
19
20// Turns on process termination if memory runs out.
21BASE_EXPORT void EnableTerminationOnOutOfMemory();
22
vitalybuka06c83182015-03-27 19:06:3923// Terminates process. Should be called only for out of memory errors.
24// Crash reporting classifies such crashes as OOM.
25BASE_EXPORT void TerminateBecauseOutOfMemory(size_t size);
26
rayb0088ee52017-04-26 22:35:0827#if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX)
[email protected]97425082013-07-16 03:10:0728BASE_EXPORT extern size_t g_oom_size;
29
[email protected]bac984102013-06-28 17:40:2430// The maximum allowed value for the OOM score.
31const int kMaxOomScore = 1000;
32
33// This adjusts /proc/<pid>/oom_score_adj so the Linux OOM killer will
34// prefer to kill certain process types over others. The range for the
35// adjustment is [-1000, 1000], with [0, 1000] being user accessible.
36// If the Linux system doesn't support the newer oom_score_adj range
37// of [0, 1000], then we revert to using the older oom_adj, and
38// translate the given value into [0, 15]. Some aliasing of values
39// may occur in that case, of course.
40BASE_EXPORT bool AdjustOOMScore(ProcessId process, int score);
41#endif
42
Benoît Lizé70f64a02020-01-15 00:33:1343namespace internal {
44// Returns true if address-space was released. Some configurations reserve part
45// of the process address-space for special allocations (e.g. WASM).
46bool ReleaseAddressSpaceReservation();
47} // namespace internal
48
wfh8ca194a2016-07-20 02:06:5449#if defined(OS_WIN)
50namespace win {
51
wfh3850a4d2016-07-23 00:23:5352// Custom Windows exception code chosen to indicate an out of memory error.
wfh8ca194a2016-07-20 02:06:5453// See https://msdn.microsoft.com/en-us/library/het71c37.aspx.
54// "To make sure that you do not define a code that conflicts with an existing
55// exception code" ... "The resulting error code should therefore have the
56// highest four bits set to hexadecimal E."
57// 0xe0000008 was chosen arbitrarily, as 0x00000008 is ERROR_NOT_ENOUGH_MEMORY.
58const DWORD kOomExceptionCode = 0xe0000008;
59
60} // namespace win
61#endif
62
[email protected]29159eb2014-03-21 22:07:0363// Special allocator functions for callers that want to check for OOM.
64// These will not abort if the allocation fails even if
65// EnableTerminationOnOutOfMemory has been called.
66// This can be useful for huge and/or unpredictable size memory allocations.
67// Please only use this if you really handle the case when the allocation
68// fails. Doing otherwise would risk security.
[email protected]e24b74f2014-03-29 17:30:4069// These functions may still crash on OOM when running under memory tools,
70// specifically ASan and other sanitizers.
[email protected]29159eb2014-03-21 22:07:0371// Return value tells whether the allocation succeeded. If it fails |result| is
72// set to NULL, otherwise it holds the memory address.
73BASE_EXPORT WARN_UNUSED_RESULT bool UncheckedMalloc(size_t size,
74 void** result);
75BASE_EXPORT WARN_UNUSED_RESULT bool UncheckedCalloc(size_t num_items,
76 size_t size,
77 void** result);
78
[email protected]bac984102013-06-28 17:40:2479} // namespace base
80
81#endif // BASE_PROCESS_MEMORY_H_