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
3 // found in the LICENSE file.
5 #include "base/process/memory.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
17 // Kill the process. This is important for security, since WebKit doesn't
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.
25 // HeapSetInformation function pointer.
26 typedef BOOL (WINAPI
* HeapSetFn
)(HANDLE
, HEAP_INFORMATION_CLASS
, PVOID
, SIZE_T
);
30 bool EnableLowFragmentationHeap() {
31 HMODULE kernel32
= GetModuleHandle(L
"kernel32.dll");
32 HeapSetFn heap_set
= reinterpret_cast<HeapSetFn
>(GetProcAddress(
34 "HeapSetInformation"));
36 // On Windows 2000, the function is not exported. This is not a reason to
41 unsigned number_heaps
= GetProcessHeaps(0, NULL
);
45 // Gives us some extra space in the array in case a thread is creating heaps
46 // at the same time we're querying them.
47 static const int MARGIN
= 8;
48 scoped_ptr
<HANDLE
[]> heaps(new HANDLE
[number_heaps
+ MARGIN
]);
49 number_heaps
= GetProcessHeaps(number_heaps
+ MARGIN
, heaps
.get());
53 for (unsigned i
= 0; i
< number_heaps
; ++i
) {
55 // Don't bother with the result code. It may fails on heaps that have the
56 // HEAP_NO_SERIALIZE flag. This is expected and not a problem at all.
58 HeapCompatibilityInformation
,
65 void EnableTerminationOnHeapCorruption() {
66 // Ignore the result code. Supported on XP SP3 and Vista.
67 HeapSetInformation(NULL
, HeapEnableTerminationOnCorruption
, NULL
, 0);
70 void EnableTerminationOnOutOfMemory() {
71 std::set_new_handler(&OnNoMemory
);
74 HMODULE
GetModuleFromAddress(void* address
) {
75 HMODULE instance
= NULL
;
76 if (!::GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
|
77 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
,
78 static_cast<char*>(address
),
85 // 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
87 // 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
89 // magic, much like what is done for libcrt in order to override the allocation
91 bool UncheckedMalloc(size_t size
, void** result
) {
92 *result
= malloc(size
);
93 return *result
!= NULL
;