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 11 matching lines...) Expand all Loading... |
22 #pragma comment(linker, "/alternatename:malloc_unchecked=malloc_default") | 22 #pragma comment(linker, "/alternatename:malloc_unchecked=malloc_default") |
23 #else | 23 #else |
24 #error Unsupported platform | 24 #error Unsupported platform |
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) // Unreachable code after the _exit. |
33 | 33 |
34 int OnNoMemory(size_t size) { | 34 NOINLINE int OnNoMemory(size_t size) { |
35 // Kill the process. This is important for security since most of code | 35 // Kill the process. This is important for security since most of code |
36 // does not check the result of memory allocation. | 36 // does not check the result of memory allocation. |
37 // https://msdn.microsoft.com/en-us/library/het71c37.aspx | 37 // https://msdn.microsoft.com/en-us/library/het71c37.aspx |
38 ::RaiseException(win::kOomExceptionCode, EXCEPTION_NONCONTINUABLE, 0, | 38 ::RaiseException(win::kOomExceptionCode, EXCEPTION_NONCONTINUABLE, 0, |
39 nullptr); | 39 nullptr); |
40 // Safety check, make sure process exits here. | 40 // Safety check, make sure process exits here. |
41 _exit(win::kOomExceptionCode); | 41 _exit(win::kOomExceptionCode); |
42 return 0; | 42 return 0; |
43 } | 43 } |
44 | 44 |
45 #pragma warning(pop) | 45 #pragma warning(pop) |
46 | 46 |
47 // HeapSetInformation function pointer. | 47 } // namespace |
48 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); | |
49 | 48 |
50 } // namespace | 49 void TerminateBecauseOutOfMemory(size_t size) { |
| 50 OnNoMemory(size); |
| 51 } |
51 | 52 |
52 void EnableTerminationOnHeapCorruption() { | 53 void EnableTerminationOnHeapCorruption() { |
53 // Ignore the result code. Supported on XP SP3 and Vista. | 54 // Ignore the result code. Supported on XP SP3 and Vista. |
54 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); | 55 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); |
55 } | 56 } |
56 | 57 |
57 void EnableTerminationOnOutOfMemory() { | 58 void EnableTerminationOnOutOfMemory() { |
58 _set_new_handler(&OnNoMemory); | 59 _set_new_handler(&OnNoMemory); |
59 _set_new_mode(1); | 60 _set_new_mode(1); |
60 } | 61 } |
61 | 62 |
62 // Implemented using a weak symbol. | 63 // Implemented using a weak symbol. |
63 bool UncheckedMalloc(size_t size, void** result) { | 64 bool UncheckedMalloc(size_t size, void** result) { |
64 *result = malloc_unchecked(size); | 65 *result = malloc_unchecked(size); |
65 return *result != NULL; | 66 return *result != NULL; |
66 } | 67 } |
67 | 68 |
68 } // namespace base | 69 } // namespace base |
OLD | NEW |