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