• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors
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 
8 #include <stddef.h>
9 
10 #include "base/base_export.h"
11 #include "base/check.h"
12 #include "base/process/process_handle.h"
13 #include "build/build_config.h"
14 #include "partition_alloc/buildflags.h"
15 
16 #if PA_BUILDFLAG(USE_PARTITION_ALLOC)
17 #include "partition_alloc/oom.h"  // nogncheck
18 #endif
19 
20 namespace base {
21 
22 // Enables 'terminate on heap corruption' flag. Helps protect against heap
23 // overflow. Has no effect if the OS doesn't provide the necessary facility.
24 BASE_EXPORT void EnableTerminationOnHeapCorruption();
25 
26 // Turns on process termination if memory runs out.
27 BASE_EXPORT void EnableTerminationOnOutOfMemory();
28 
29 #if PA_BUILDFLAG(USE_PARTITION_ALLOC)
30 using partition_alloc::TerminateBecauseOutOfMemory;
31 #else
TerminateBecauseOutOfMemory(size_t)32 inline void TerminateBecauseOutOfMemory(size_t) {
33   logging::RawCheckFailure("Out of memory");
34 }
35 #endif
36 
37 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
38     BUILDFLAG(IS_AIX)
39 // The maximum allowed value for the OOM score.
40 const 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.
49 BASE_EXPORT bool AdjustOOMScore(ProcessId process, int score);
50 #endif
51 
52 namespace 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).
55 bool ReleaseAddressSpaceReservation();
56 }  // namespace internal
57 
58 #if BUILDFLAG(IS_WIN)
59 namespace win {
60 
61 using partition_alloc::win::kOomExceptionCode;
62 
63 }  // namespace win
64 #endif
65 
66 // 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.
72 // These functions may still crash on OOM when running under memory tools,
73 // specifically ASan and other sanitizers.
74 // Return value tells whether the allocation succeeded. If it fails |result| is
75 // set to NULL, otherwise it holds the memory address.
76 //
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().
80 [[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);
84 
85 // *Must* be used to free memory allocated with base::UncheckedMalloc() and
86 // base::UncheckedCalloc().
87 // TODO(crbug.com/40208525): Enforce it, when all callers are converted.
88 BASE_EXPORT void UncheckedFree(void* ptr);
89 
90 // 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 // }
99 struct UncheckedFreeDeleter {
operatorUncheckedFreeDeleter100   inline void operator()(void* ptr) const { UncheckedFree(ptr); }
101 };
102 
103 }  // namespace base
104 
105 #endif  // BASE_PROCESS_MEMORY_H_
106