blob: 5d11d4a1560b1275eb4c8360eb4ceb7a2eee9d1b [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
[email protected]bac984102013-06-28 17:40:242// 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"
Etienne Dechamps4dacf2b2024-12-19 16:31:0511#include "base/check.h"
[email protected]dd4b51262013-07-25 21:38:2312#include "base/process/process_handle.h"
[email protected]bac984102013-06-28 17:40:2413#include "build/build_config.h"
Etienne Dechamps4dacf2b2024-12-19 16:31:0514#include "partition_alloc/buildflags.h"
15
16#if PA_BUILDFLAG(USE_PARTITION_ALLOC)
17#include "partition_alloc/oom.h" // nogncheck
18#endif
[email protected]bac984102013-06-28 17:40:2419
[email protected]bac984102013-06-28 17:40:2420namespace base {
21
[email protected]bac984102013-06-28 17:40:2422// Enables 'terminate on heap corruption' flag. Helps protect against heap
23// overflow. Has no effect if the OS doesn't provide the necessary facility.
24BASE_EXPORT void EnableTerminationOnHeapCorruption();
25
26// Turns on process termination if memory runs out.
27BASE_EXPORT void EnableTerminationOnOutOfMemory();
28
Etienne Dechamps4dacf2b2024-12-19 16:31:0529#if PA_BUILDFLAG(USE_PARTITION_ALLOC)
Bartek Nowierskia9ac92732022-05-25 17:44:3330using partition_alloc::TerminateBecauseOutOfMemory;
Etienne Dechamps4dacf2b2024-12-19 16:31:0531#else
32inline void TerminateBecauseOutOfMemory(size_t) {
33 logging::RawCheckFailure("Out of memory");
34}
35#endif
Torne (Richard Coles)f6e6c272021-01-26 16:58:4036
Xiaohan Wang37e81612022-01-15 18:27:0037#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
38 BUILDFLAG(IS_AIX)
[email protected]bac984102013-06-28 17:40:2439// The maximum allowed value for the OOM score.
40const int kMaxOomScore = 1000;
41
42// This adjusts /proc/<pid>/oom_score_adj so the Linux OOM killer will
43// prefer to kill certain process types over others. The range for the
44// adjustment is [-1000, 1000], with [0, 1000] being user accessible.
45// If the Linux system doesn't support the newer oom_score_adj range
46// of [0, 1000], then we revert to using the older oom_adj, and
47// translate the given value into [0, 15]. Some aliasing of values
48// may occur in that case, of course.
49BASE_EXPORT bool AdjustOOMScore(ProcessId process, int score);
50#endif
51
Benoît Lizé70f64a02020-01-15 00:33:1352namespace internal {
53// Returns true if address-space was released. Some configurations reserve part
54// of the process address-space for special allocations (e.g. WASM).
55bool ReleaseAddressSpaceReservation();
56} // namespace internal
57
Xiaohan Wang37e81612022-01-15 18:27:0058#if BUILDFLAG(IS_WIN)
wfh8ca194a2016-07-20 02:06:5459namespace win {
60
Bartek Nowierskia9ac92732022-05-25 17:44:3361using partition_alloc::win::kOomExceptionCode;
wfh8ca194a2016-07-20 02:06:5462
63} // namespace win
64#endif
65
[email protected]29159eb2014-03-21 22:07:0366// Special allocator functions for callers that want to check for OOM.
67// These will not abort if the allocation fails even if
68// EnableTerminationOnOutOfMemory has been called.
69// This can be useful for huge and/or unpredictable size memory allocations.
70// Please only use this if you really handle the case when the allocation
71// fails. Doing otherwise would risk security.
[email protected]e24b74f2014-03-29 17:30:4072// These functions may still crash on OOM when running under memory tools,
73// specifically ASan and other sanitizers.
[email protected]29159eb2014-03-21 22:07:0374// Return value tells whether the allocation succeeded. If it fails |result| is
75// set to NULL, otherwise it holds the memory address.
Benoit Lize69ecd9f722021-12-13 13:49:0576//
77// Note: You *must* use UncheckedFree() to free() the memory allocated, not
78// regular free(). This also means that this a pointer allocated below cannot be
79// passed to realloc().
Daniel Cheng4455c9842022-01-13 23:26:3780[[nodiscard]] BASE_EXPORT bool UncheckedMalloc(size_t size, void** result);
81[[nodiscard]] BASE_EXPORT bool UncheckedCalloc(size_t num_items,
82 size_t size,
83 void** result);
[email protected]29159eb2014-03-21 22:07:0384
Benoit Lize69ecd9f722021-12-13 13:49:0585// *Must* be used to free memory allocated with base::UncheckedMalloc() and
86// base::UncheckedCalloc().
Alison Gale47d1537d2024-04-19 21:31:4687// TODO(crbug.com/40208525): Enforce it, when all callers are converted.
Benoit Lize69ecd9f722021-12-13 13:49:0588BASE_EXPORT void UncheckedFree(void* ptr);
89
Matt Wolenetzdcb32012022-12-15 18:49:3490// Function object which invokes 'UncheckedFree' on its parameter, which should
91// be a pointer resulting from UncheckedMalloc or UncheckedCalloc. Can be used
92// to store such pointers in std::unique_ptr:
93//
94// int* foo_ptr = nullptr;
95// if (UncheckedMalloc(sizeof(*foo_ptr), reinterpret_cast<void**>(&foo_ptr))) {
96// std::unique_ptr<int, base::UncheckedFreeDeleter> unique_foo_ptr(foo_ptr);
97// ...
98// }
99struct UncheckedFreeDeleter {
100 inline void operator()(void* ptr) const { UncheckedFree(ptr); }
101};
102
[email protected]bac984102013-06-28 17:40:24103} // namespace base
104
105#endif // BASE_PROCESS_MEMORY_H_