Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/process/memory.h" | 5 #include "base/process/memory.h" |
| 6 | 6 |
| 7 #include <new.h> | 7 #include <new.h> |
| 8 #include <psapi.h> | 8 #include <psapi.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <windows.h> | 10 #include <windows.h> |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 #endif | 25 #endif |
| 26 | 26 |
| 27 namespace base { | 27 namespace base { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 #pragma warning(push) | 31 #pragma warning(push) |
| 32 #pragma warning(disable: 4702) | 32 #pragma warning(disable: 4702) |
| 33 | 33 |
| 34 int OnNoMemory(size_t size) { | 34 int OnNoMemory(size_t size) { |
| 35 // Custom exception code chosen to indicate an out of memory error. | |
| 36 // See https://msdn.microsoft.com/en-us/library/het71c37.aspx. | |
| 37 // "To make sure that you do not define a code that conflicts with an existing | |
| 38 // exception code" ... "The resulting error code should therefore have the | |
| 39 // highest four bits set to hexadecimal E." | |
| 40 // 0xe0000008 was chosen arbitrarily as 0x00000008 is ERROR_NOT_ENOUGH_MEMORY. | |
| 41 const DWORD kOomExceptionCode = 0xe0000008; | |
|
Mark Mentovai
2016/07/21 21:15:11
Now you’ve got to update the comment in Breakpad t
| |
| 42 | |
| 35 // Kill the process. This is important for security since most of code | 43 // Kill the process. This is important for security since most of code |
| 36 // does not check the result of memory allocation. | 44 // does not check the result of memory allocation. |
| 37 // https://msdn.microsoft.com/en-us/library/het71c37.aspx | 45 // https://msdn.microsoft.com/en-us/library/het71c37.aspx |
| 38 ::RaiseException(win::kOomExceptionCode, EXCEPTION_NONCONTINUABLE, 0, | 46 ::RaiseException(kOomExceptionCode, EXCEPTION_NONCONTINUABLE, 0, nullptr); |
| 39 nullptr); | |
| 40 // Safety check, make sure process exits here. | 47 // Safety check, make sure process exits here. |
| 41 _exit(win::kOomExceptionCode); | 48 _exit(kOomExceptionCode); |
| 42 return 0; | 49 return 0; |
| 43 } | 50 } |
| 44 | 51 |
| 45 #pragma warning(pop) | 52 #pragma warning(pop) |
| 46 | 53 |
| 47 // HeapSetInformation function pointer. | 54 } // namespace |
| 48 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); | |
| 49 | 55 |
| 50 } // namespace | 56 void TerminateBecauseOutOfMemory(size_t size) { |
| 57 OnNoMemory(size); | |
| 58 } | |
| 51 | 59 |
| 52 void EnableTerminationOnHeapCorruption() { | 60 void EnableTerminationOnHeapCorruption() { |
| 53 // Ignore the result code. Supported on XP SP3 and Vista. | 61 // Ignore the result code. Supported on XP SP3 and Vista. |
| 54 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); | 62 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); |
| 55 } | 63 } |
| 56 | 64 |
| 57 void EnableTerminationOnOutOfMemory() { | 65 void EnableTerminationOnOutOfMemory() { |
| 58 _set_new_handler(&OnNoMemory); | 66 _set_new_handler(&OnNoMemory); |
| 59 _set_new_mode(1); | 67 _set_new_mode(1); |
| 60 } | 68 } |
| 61 | 69 |
| 62 // Implemented using a weak symbol. | 70 // Implemented using a weak symbol. |
| 63 bool UncheckedMalloc(size_t size, void** result) { | 71 bool UncheckedMalloc(size_t size, void** result) { |
| 64 *result = malloc_unchecked(size); | 72 *result = malloc_unchecked(size); |
| 65 return *result != NULL; | 73 return *result != NULL; |
| 66 } | 74 } |
| 67 | 75 |
| 68 } // namespace base | 76 } // namespace base |
| OLD | NEW |