OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // The allocator shim is only enabled in Release Static builds. | 5 // The allocator shim is only enabled in Release Static builds. |
6 // This #if is needed as gyp can't have different compile | 6 // This #if is needed as gyp can't have different compile |
7 // targets between Debug and Release. | 7 // targets between Debug and Release. |
8 // TODO(wfh): Remove this once gyp is dead. | 8 // TODO(wfh): Remove this once gyp is dead. |
| 9 // TODO(siggi): Remove this file once the generic shim sticks. |
9 #if defined(ALLOCATOR_SHIM) | 10 #if defined(ALLOCATOR_SHIM) |
10 | 11 |
11 #include <limits.h> | 12 #include <limits.h> |
12 #include <malloc.h> | 13 #include <malloc.h> |
13 #include <new.h> | 14 #include <new.h> |
14 #include <windows.h> | 15 #include <windows.h> |
15 #include <stddef.h> | 16 #include <stddef.h> |
16 | 17 |
17 #include "allocator_shim_win.h" | 18 #include "base/allocator/allocator_shim_win.h" |
| 19 #include "base/allocator/winheap_stubs_win.h" |
18 | 20 |
19 // This shim make it possible to perform additional checks on allocations | 21 // This shim make it possible to perform additional checks on allocations |
20 // before passing them to the Heap functions. | 22 // before passing them to the Heap functions. |
21 | 23 |
22 // Override heap functions to perform additional checks: | 24 // Override heap functions to perform additional checks: |
23 // 1. Enforcing the maximum size that can be allocated to 2Gb. | 25 // 1. Enforcing the maximum size that can be allocated to 2Gb. |
24 // 2. Calling new_handler if malloc fails | 26 // 2. Calling new_handler if malloc fails |
25 | 27 |
26 // See definitions of original functions in ucrt\corecrt_malloc.h in SDK | 28 // See definitions of original functions in ucrt\corecrt_malloc.h in SDK |
27 // include directory. | 29 // include directory. |
28 | 30 |
29 namespace base { | |
30 namespace allocator { | |
31 bool g_is_win_shim_layer_initialized = false; | |
32 } // namespace allocator | |
33 } // namespace base | |
34 | |
35 namespace { | 31 namespace { |
36 | 32 |
37 const size_t kWindowsPageSize = 4096; | |
38 const size_t kMaxWindowsAllocation = INT_MAX - kWindowsPageSize; | |
39 int new_mode = 0; | 33 int new_mode = 0; |
40 | 34 |
41 inline HANDLE get_heap_handle() { | |
42 return reinterpret_cast<HANDLE>(_get_heap_handle()); | |
43 } | |
44 | |
45 void* win_heap_malloc(size_t size) { | |
46 if (size < kMaxWindowsAllocation) | |
47 return HeapAlloc(get_heap_handle(), 0, size); | |
48 return nullptr; | |
49 } | |
50 | |
51 void win_heap_free(void* size) { | |
52 HeapFree(get_heap_handle(), 0, size); | |
53 } | |
54 | |
55 void* win_heap_realloc(void* ptr, size_t size) { | |
56 if (!ptr) | |
57 return win_heap_malloc(size); | |
58 if (!size) { | |
59 win_heap_free(ptr); | |
60 return nullptr; | |
61 } | |
62 if (size < kMaxWindowsAllocation) | |
63 return HeapReAlloc(get_heap_handle(), 0, ptr, size); | |
64 return nullptr; | |
65 } | |
66 | |
67 // Call the new handler, if one has been set. | |
68 // Returns true on successfully calling the handler, false otherwise. | |
69 inline bool call_new_handler(bool nothrow, size_t size) { | |
70 // Get the current new handler. | |
71 _PNH nh = _query_new_handler(); | |
72 #if defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | |
73 if (!nh) | |
74 return false; | |
75 // Since exceptions are disabled, we don't really know if new_handler | |
76 // failed. Assume it will abort if it fails. | |
77 return nh(size) ? true : false; | |
78 #else | |
79 #error "Exceptions in allocator shim are not supported!" | |
80 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | |
81 } | |
82 | |
83 } // namespace | 35 } // namespace |
84 | 36 |
85 extern "C" { | 37 extern "C" { |
86 | 38 |
87 // Symbol to allow weak linkage to win_heap_malloc from memory_win.cc. | 39 // Symbol to allow weak linkage to WinHeapMalloc from allocator_impl_win.cc. |
88 void* (*malloc_unchecked)(size_t) = &win_heap_malloc; | 40 void* (*malloc_unchecked)(size_t) = &base::allocator::WinHeapMalloc; |
89 | 41 |
90 // This function behaves similarly to MSVC's _set_new_mode. | 42 // This function behaves similarly to MSVC's _set_new_mode. |
91 // If flag is 0 (default), calls to malloc will behave normally. | 43 // If flag is 0 (default), calls to malloc will behave normally. |
92 // If flag is 1, calls to malloc will behave like calls to new, | 44 // If flag is 1, calls to malloc will behave like calls to new, |
93 // and the std_new_handler will be invoked on failure. | 45 // and the std_new_handler will be invoked on failure. |
94 // Returns the previous mode. | 46 // Returns the previous mode. |
95 // | 47 // |
96 // Replaces _set_new_mode in ucrt\heap\new_mode.cpp | 48 // Replaces _set_new_mode in ucrt\heap\new_mode.cpp |
97 int _set_new_mode(int flag) { | 49 int _set_new_mode(int flag) { |
98 // The MS CRT calls this function early on in startup, so this serves as a low | 50 // The MS CRT calls this function early on in startup, so this serves as a low |
99 // overhead proof that the allocator shim is in place for this process. | 51 // overhead proof that the allocator shim is in place for this process. |
100 base::allocator::g_is_win_shim_layer_initialized = true; | 52 base::allocator::g_is_win_shim_layer_initialized = true; |
101 int old_mode = new_mode; | 53 int old_mode = new_mode; |
102 new_mode = flag; | 54 new_mode = flag; |
| 55 |
103 return old_mode; | 56 return old_mode; |
104 } | 57 } |
105 | 58 |
106 // Replaces _query_new_mode in ucrt\heap\new_mode.cpp | 59 // Replaces _query_new_mode in ucrt\heap\new_mode.cpp |
107 int _query_new_mode() { | 60 int _query_new_mode() { |
108 return new_mode; | 61 return new_mode; |
109 } | 62 } |
110 | 63 |
111 // Replaces malloc in ucrt\heap\malloc.cpp | 64 // Replaces malloc in ucrt\heap\malloc.cpp |
112 __declspec(restrict) void* malloc(size_t size) { | 65 __declspec(restrict) void* malloc(size_t size) { |
113 void* ptr; | 66 void* ptr; |
114 for (;;) { | 67 for (;;) { |
115 ptr = win_heap_malloc(size); | 68 ptr = base::allocator::WinHeapMalloc(size); |
116 if (ptr) | 69 if (ptr) |
117 return ptr; | 70 return ptr; |
118 | 71 |
119 if (!new_mode || !call_new_handler(true, size)) | 72 if (!new_mode || base::allocator::WinCallNewHandler(size)) |
120 break; | 73 break; |
121 } | 74 } |
122 return ptr; | 75 return ptr; |
123 } | 76 } |
124 | 77 |
125 // Replaces free in ucrt\heap\free.cpp | 78 // Replaces free in ucrt\heap\free.cpp |
126 void free(void* p) { | 79 void free(void* p) { |
127 win_heap_free(p); | 80 base::allocator::WinHeapFree(p); |
128 return; | 81 return; |
129 } | 82 } |
130 | 83 |
131 // Replaces realloc in ucrt\heap\realloc.cpp | 84 // Replaces realloc in ucrt\heap\realloc.cpp |
132 __declspec(restrict) void* realloc(void* ptr, size_t size) { | 85 __declspec(restrict) void* realloc(void* ptr, size_t size) { |
133 // Webkit is brittle for allocators that return NULL for malloc(0). The | 86 // Webkit is brittle for allocators that return NULL for malloc(0). The |
134 // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure | 87 // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure |
135 // to call malloc for this case. | 88 // to call malloc for this case. |
136 if (!ptr) | 89 if (!ptr) |
137 return malloc(size); | 90 return malloc(size); |
138 | 91 |
139 void* new_ptr; | 92 void* new_ptr; |
140 for (;;) { | 93 for (;;) { |
141 new_ptr = win_heap_realloc(ptr, size); | 94 new_ptr = base::allocator::WinHeapRealloc(ptr, size); |
142 | 95 |
143 // Subtle warning: NULL return does not alwas indicate out-of-memory. If | 96 // Subtle warning: NULL return does not alwas indicate out-of-memory. If |
144 // the requested new size is zero, realloc should free the ptr and return | 97 // the requested new size is zero, realloc should free the ptr and return |
145 // NULL. | 98 // NULL. |
146 if (new_ptr || !size) | 99 if (new_ptr || !size) |
147 return new_ptr; | 100 return new_ptr; |
148 if (!new_mode || !call_new_handler(true, size)) | 101 if (!new_mode || !base::allocator::WinCallNewHandler(size)) |
149 break; | 102 break; |
150 } | 103 } |
151 return new_ptr; | 104 return new_ptr; |
152 } | 105 } |
153 | 106 |
154 // Replaces calloc in ucrt\heap\calloc.cpp | 107 // Replaces calloc in ucrt\heap\calloc.cpp |
155 __declspec(restrict) void* calloc(size_t n, size_t elem_size) { | 108 __declspec(restrict) void* calloc(size_t n, size_t elem_size) { |
156 // Overflow check. | 109 // Overflow check. |
157 const size_t size = n * elem_size; | 110 const size_t size = n * elem_size; |
158 if (elem_size != 0 && size / elem_size != n) | 111 if (elem_size != 0 && size / elem_size != n) |
159 return nullptr; | 112 return nullptr; |
160 | 113 |
161 void* result = malloc(size); | 114 void* result = malloc(size); |
162 if (result) { | 115 if (result) { |
163 memset(result, 0, size); | 116 memset(result, 0, size); |
164 } | 117 } |
165 return result; | 118 return result; |
166 } | 119 } |
167 | 120 |
168 } // extern C | 121 } // extern C |
169 | 122 |
170 #endif // defined(ALLOCATOR_SHIM) | 123 #endif // defined(ALLOCATOR_SHIM) |
OLD | NEW |