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 <psapi.h> | 8 #include <psapi.h> |
8 | 9 |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 void OnNoMemory() { | 17 #pragma warning(push) |
17 // Kill the process. This is important for security, since WebKit doesn't | 18 #pragma warning(disable: 4702) |
18 // NULL-check many memory allocations. If a malloc fails, returns NULL, and | 19 |
19 // the buffer is then used, it provides a handy mapping of memory starting at | 20 int OnNoMemory(size_t) { |
20 // address 0 for an attacker to utilize. | 21 // Kill the process. This is important for security since most of code |
| 22 // does not check the result of memory allocation. |
21 __debugbreak(); | 23 __debugbreak(); |
22 _exit(1); | 24 _exit(1); |
| 25 return 0; |
23 } | 26 } |
24 | 27 |
| 28 #pragma warning(pop) |
| 29 |
25 // HeapSetInformation function pointer. | 30 // HeapSetInformation function pointer. |
26 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); | 31 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); |
27 | 32 |
28 } // namespace | 33 } // namespace |
29 | 34 |
30 bool EnableLowFragmentationHeap() { | 35 bool EnableLowFragmentationHeap() { |
31 HMODULE kernel32 = GetModuleHandle(L"kernel32.dll"); | 36 HMODULE kernel32 = GetModuleHandle(L"kernel32.dll"); |
32 HeapSetFn heap_set = reinterpret_cast<HeapSetFn>(GetProcAddress( | 37 HeapSetFn heap_set = reinterpret_cast<HeapSetFn>(GetProcAddress( |
33 kernel32, | 38 kernel32, |
34 "HeapSetInformation")); | 39 "HeapSetInformation")); |
(...skipping 26 matching lines...) Expand all Loading... |
61 } | 66 } |
62 return true; | 67 return true; |
63 } | 68 } |
64 | 69 |
65 void EnableTerminationOnHeapCorruption() { | 70 void EnableTerminationOnHeapCorruption() { |
66 // Ignore the result code. Supported on XP SP3 and Vista. | 71 // Ignore the result code. Supported on XP SP3 and Vista. |
67 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); | 72 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); |
68 } | 73 } |
69 | 74 |
70 void EnableTerminationOnOutOfMemory() { | 75 void EnableTerminationOnOutOfMemory() { |
71 std::set_new_handler(&OnNoMemory); | 76 _set_new_handler(&OnNoMemory); |
| 77 _set_new_mode(1); |
72 } | 78 } |
73 | 79 |
74 HMODULE GetModuleFromAddress(void* address) { | 80 HMODULE GetModuleFromAddress(void* address) { |
75 HMODULE instance = NULL; | 81 HMODULE instance = NULL; |
76 if (!::GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | | 82 if (!::GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
77 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, | 83 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
78 static_cast<char*>(address), | 84 static_cast<char*>(address), |
79 &instance)) { | 85 &instance)) { |
80 NOTREACHED(); | 86 NOTREACHED(); |
81 } | 87 } |
82 return instance; | 88 return instance; |
83 } | 89 } |
84 | 90 |
85 // TODO(b.kelemen): implement it with the required semantics. On Linux this is | 91 // TODO(b.kelemen): implement it with the required semantics. On Linux this is |
86 // implemented with a weak symbol that is overridden by tcmalloc. This is | 92 // implemented with a weak symbol that is overridden by tcmalloc. This is |
87 // neccessary because base cannot have a direct dependency on tcmalloc. Since | 93 // neccessary because base cannot have a direct dependency on tcmalloc. Since |
88 // weak symbols are not supported on Windows this will involve some build time | 94 // weak symbols are not supported on Windows this will involve some build time |
89 // magic, much like what is done for libcrt in order to override the allocation | 95 // magic, much like what is done for libcrt in order to override the allocation |
90 // functions. | 96 // functions. |
91 bool UncheckedMalloc(size_t size, void** result) { | 97 bool UncheckedMalloc(size_t size, void** result) { |
92 *result = malloc(size); | 98 *result = malloc(size); |
93 return *result != NULL; | 99 return *result != NULL; |
94 } | 100 } |
95 | 101 |
96 } // namespace base | 102 } // namespace base |
OLD | NEW |