| 6 |
| 7 | #if defined(OS_WIN) |
| 8 | #include <windows.h> |
| 9 | #endif // defined(OS_WIN) |
| 10 |
| 11 | #include "base/debug/alias.h" |
| 12 | #include "base/logging.h" |
| 13 | #include "base/partition_alloc_buildflags.h" |
| 14 | #if BUILDFLAG(USE_PARTITION_ALLOC) |
| 15 | #include "base/allocator/partition_allocator/page_allocator.h" |
| 16 | #endif |
| 17 | #include "build/build_config.h" |
| 18 |
| 19 | namespace base { |
| 20 |
| 21 | namespace internal { |
| 22 |
| 23 | NOINLINE void OnNoMemoryInternal(size_t size) { |
| 24 | #if defined(OS_WIN) |
| 25 | // Kill the process. This is important for security since most of code |
| 26 | // does not check the result of memory allocation. |
| 27 | // https://msdn.microsoft.com/en-us/library/het71c37.aspx |
| 28 | // Pass the size of the failed request in an exception argument. |
| 29 | ULONG_PTR exception_args[] = {size}; |
| 30 | ::RaiseException(base::win::kOomExceptionCode, EXCEPTION_NONCONTINUABLE, |
| 31 | base::size(exception_args), exception_args); |
| 32 |
| 33 | // Safety check, make sure process exits here. |
| 34 | _exit(win::kOomExceptionCode); |
| 35 | #else |
| 36 | size_t tmp_size = size; |
| 37 | base::debug::Alias(&tmp_size); |
| 38 | LOG(FATAL) << "Out of memory. size=" << tmp_size; |
| 39 | #endif // defined(OS_WIN) |
| 40 | } |
| 41 |
| 42 | } // namespace internal |
| 43 |
| 44 | // Defined in memory_win.cc for Windows. |
| 45 | #if !defined(OS_WIN) |
| 46 |
| 47 | namespace { |
| 48 |
| 49 | // Breakpad server classifies base::`anonymous namespace'::OnNoMemory as |
| 50 | // out-of-memory crash. |
| 51 | NOINLINE void OnNoMemory(size_t size) { |
| 52 | internal::OnNoMemoryInternal(size); |
| 53 | } |
| 54 |
| 55 | } // namespace |
| 56 |
| 57 | void TerminateBecauseOutOfMemory(size_t size) { |
| 58 | OnNoMemory(size); |
| 59 | } |
| 60 |
| 61 | #endif // !defined(OS_WIN) |
| 62 |
| 63 | // Defined in memory_mac.mm for Mac. |
| 64 | #if !defined(OS_MACOSX) |
| 65 |
| 66 | bool UncheckedCalloc(size_t num_items, size_t size, void** result) { |
| 67 | const size_t alloc_size = num_items * size; |
| 68 |
| 69 | // Overflow check |
| 70 | if (size && ((alloc_size / size) != num_items)) { |
| 71 | *result = nullptr; |
| 72 | return false; |
| 73 | } |
| 74 |
| 75 | if (!UncheckedMalloc(alloc_size, result)) |
| 76 | return false; |
| 77 |
| 78 | memset(*result, 0, alloc_size); |
| 79 | return true; |
| 80 | } |
| 81 |
| 82 | #endif // defined(OS_MACOSX) |
| 83 |
| 84 | namespace internal { |
| 85 | bool ReleaseAddressSpaceReservation() { |
| 86 | #if BUILDFLAG(USE_PARTITION_ALLOC) |
| 87 | return ReleaseReservation(); |
| 88 | #else |
| 89 | return false; |
| 90 | #endif |
| 91 | } |
| 92 | } // namespace internal |
| 93 |
| 94 | } // namespace base |
| 95 |