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 | 10 #include <windows.h> |
11 #include "base/logging.h" | |
12 #include "base/strings/safe_sprintf.h" | |
13 | 11 |
14 // malloc_unchecked is required to implement UncheckedMalloc properly. | 12 // malloc_unchecked is required to implement UncheckedMalloc properly. |
15 // It's provided by allocator_shim_win.cc but since that's not always present, | 13 // It's provided by allocator_shim_win.cc but since that's not always present, |
16 // we provide a default that falls back to regular malloc. | 14 // we provide a default that falls back to regular malloc. |
17 typedef void* (*MallocFn)(size_t); | 15 typedef void* (*MallocFn)(size_t); |
18 extern "C" void* (*const malloc_unchecked)(size_t); | 16 extern "C" void* (*const malloc_unchecked)(size_t); |
19 extern "C" void* (*const malloc_default)(size_t) = &malloc; | 17 extern "C" void* (*const malloc_default)(size_t) = &malloc; |
20 | 18 |
21 #if defined(_M_IX86) | 19 #if defined(_M_IX86) |
22 #pragma comment(linker, "/alternatename:_malloc_unchecked=_malloc_default") | 20 #pragma comment(linker, "/alternatename:_malloc_unchecked=_malloc_default") |
23 #elif defined(_M_X64) || defined(_M_ARM) | 21 #elif defined(_M_X64) || defined(_M_ARM) |
24 #pragma comment(linker, "/alternatename:malloc_unchecked=malloc_default") | 22 #pragma comment(linker, "/alternatename:malloc_unchecked=malloc_default") |
25 #else | 23 #else |
26 #error Unsupported platform | 24 #error Unsupported platform |
27 #endif | 25 #endif |
28 | 26 |
29 namespace base { | 27 namespace base { |
30 | 28 |
31 namespace { | 29 namespace { |
32 | 30 |
33 #pragma warning(push) | 31 #pragma warning(push) |
34 #pragma warning(disable: 4702) | 32 #pragma warning(disable: 4702) |
35 | 33 |
36 int OnNoMemory(size_t size) { | 34 int OnNoMemory(size_t size) { |
37 // Make no additional allocations here to avoid getting into a death spiral | |
38 // when trying to log the error message. | |
39 char buf[64]; | |
40 strings::ssize_t result = | |
41 strings::SafeSPrintf(buf, "Out of memory, size = %d\n", size); | |
42 RAW_CHECK(result != -1); | |
43 | |
44 // 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 |
45 // does not check the result of memory allocation. | 36 // does not check the result of memory allocation. |
46 RAW_LOG(FATAL, buf); | 37 // https://msdn.microsoft.com/en-us/library/het71c37.aspx |
47 | 38 ::RaiseException(win::kOomExceptionCode, EXCEPTION_NONCONTINUABLE, 0, |
| 39 nullptr); |
48 // Safety check, make sure process exits here. | 40 // Safety check, make sure process exits here. |
49 _exit(1); | 41 _exit(win::kOomExceptionCode); |
50 return 0; | 42 return 0; |
51 } | 43 } |
52 | 44 |
53 #pragma warning(pop) | 45 #pragma warning(pop) |
54 | 46 |
55 // HeapSetInformation function pointer. | 47 // HeapSetInformation function pointer. |
56 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); | 48 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); |
57 | 49 |
58 } // namespace | 50 } // namespace |
59 | 51 |
60 void EnableTerminationOnHeapCorruption() { | 52 void EnableTerminationOnHeapCorruption() { |
61 // Ignore the result code. Supported on XP SP3 and Vista. | 53 // Ignore the result code. Supported on XP SP3 and Vista. |
62 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); | 54 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); |
63 } | 55 } |
64 | 56 |
65 void EnableTerminationOnOutOfMemory() { | 57 void EnableTerminationOnOutOfMemory() { |
66 _set_new_handler(&OnNoMemory); | 58 _set_new_handler(&OnNoMemory); |
67 _set_new_mode(1); | 59 _set_new_mode(1); |
68 } | 60 } |
69 | 61 |
70 // Implemented using a weak symbol. | 62 // Implemented using a weak symbol. |
71 bool UncheckedMalloc(size_t size, void** result) { | 63 bool UncheckedMalloc(size_t size, void** result) { |
72 *result = malloc_unchecked(size); | 64 *result = malloc_unchecked(size); |
73 return *result != NULL; | 65 return *result != NULL; |
74 } | 66 } |
75 | 67 |
76 } // namespace base | 68 } // namespace base |
OLD | NEW |