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/process/process_handle.h" 12 #include "build/build_config.h" 13 #include "partition_alloc/oom.h" 14 15 namespace base { 16 17 // Enables 'terminate on heap corruption' flag. Helps protect against heap 18 // overflow. Has no effect if the OS doesn't provide the necessary facility. 19 BASE_EXPORT void EnableTerminationOnHeapCorruption(); 20 21 // Turns on process termination if memory runs out. 22 BASE_EXPORT void EnableTerminationOnOutOfMemory(); 23 24 // The function has been moved to partition_alloc:: namespace. The base:: alias 25 // has been provided to avoid changing too many callers. 26 using partition_alloc::TerminateBecauseOutOfMemory; 27 28 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \ 29 BUILDFLAG(IS_AIX) 30 // The maximum allowed value for the OOM score. 31 const 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. 40 BASE_EXPORT bool AdjustOOMScore(ProcessId process, int score); 41 #endif 42 43 namespace 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). 46 bool ReleaseAddressSpaceReservation(); 47 } // namespace internal 48 49 #if BUILDFLAG(IS_WIN) 50 namespace win { 51 52 using partition_alloc::win::kOomExceptionCode; 53 54 } // namespace win 55 #endif 56 57 // Special allocator functions for callers that want to check for OOM. 58 // These will not abort if the allocation fails even if 59 // EnableTerminationOnOutOfMemory has been called. 60 // This can be useful for huge and/or unpredictable size memory allocations. 61 // Please only use this if you really handle the case when the allocation 62 // fails. Doing otherwise would risk security. 63 // These functions may still crash on OOM when running under memory tools, 64 // specifically ASan and other sanitizers. 65 // Return value tells whether the allocation succeeded. If it fails |result| is 66 // set to NULL, otherwise it holds the memory address. 67 // 68 // Note: You *must* use UncheckedFree() to free() the memory allocated, not 69 // regular free(). This also means that this a pointer allocated below cannot be 70 // passed to realloc(). 71 [[nodiscard]] BASE_EXPORT bool UncheckedMalloc(size_t size, void** result); 72 [[nodiscard]] BASE_EXPORT bool UncheckedCalloc(size_t num_items, 73 size_t size, 74 void** result); 75 76 // *Must* be used to free memory allocated with base::UncheckedMalloc() and 77 // base::UncheckedCalloc(). 78 // TODO(crbug.com/1279371): Enforce it, when all callers are converted. 79 BASE_EXPORT void UncheckedFree(void* ptr); 80 81 // Function object which invokes 'UncheckedFree' on its parameter, which should 82 // be a pointer resulting from UncheckedMalloc or UncheckedCalloc. Can be used 83 // to store such pointers in std::unique_ptr: 84 // 85 // int* foo_ptr = nullptr; 86 // if (UncheckedMalloc(sizeof(*foo_ptr), reinterpret_cast<void**>(&foo_ptr))) { 87 // std::unique_ptr<int, base::UncheckedFreeDeleter> unique_foo_ptr(foo_ptr); 88 // ... 89 // } 90 struct UncheckedFreeDeleter { operatorUncheckedFreeDeleter91 inline void operator()(void* ptr) const { UncheckedFree(ptr); } 92 }; 93 94 } // namespace base 95 96 #endif // BASE_PROCESS_MEMORY_H_ 97