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. |
| 6 // This #if is needed as gyp can't have different compile |
| 7 // targets between Debug and Release. |
| 8 // TODO(wfh): Remove this once gyp is dead. |
| 9 #if defined(ALLOCATOR_SHIM) |
| 10 |
5 #include <limits.h> | 11 #include <limits.h> |
6 #include <malloc.h> | 12 #include <malloc.h> |
7 #include <new.h> | 13 #include <new.h> |
8 #include <windows.h> | 14 #include <windows.h> |
9 #include <stddef.h> | 15 #include <stddef.h> |
10 | 16 |
| 17 #include "allocator_shim_win.h" |
| 18 |
11 // This shim make it possible to perform additional checks on allocations | 19 // This shim make it possible to perform additional checks on allocations |
12 // before passing them to the Heap functions. | 20 // before passing them to the Heap functions. |
13 | 21 |
14 // Heap functions are stripped from libcmt.lib using the prep_libc.py | 22 // Override heap functions to perform additional checks: |
15 // for each object file stripped, we re-implement them here to allow us to | |
16 // perform additional checks: | |
17 // 1. Enforcing the maximum size that can be allocated to 2Gb. | 23 // 1. Enforcing the maximum size that can be allocated to 2Gb. |
18 // 2. Calling new_handler if malloc fails. | 24 // 2. Calling new_handler if malloc fails |
19 | 25 |
20 extern "C" { | 26 // See definitions of original functions in ucrt\corecrt_malloc.h in SDK |
21 // We set this to 1 because part of the CRT uses a check of _crtheap != 0 | 27 // include directory. |
22 // to test whether the CRT has been initialized. Once we've ripped out | |
23 // the allocators from libcmt, we need to provide this definition so that | |
24 // the rest of the CRT is still usable. | |
25 // heapinit.c | |
26 void* _crtheap = reinterpret_cast<void*>(1); | |
27 } | |
28 | 28 |
29 namespace base { | 29 namespace base { |
30 namespace allocator { | 30 namespace allocator { |
31 bool g_is_win_shim_layer_initialized = false; | 31 bool g_is_win_shim_layer_initialized = false; |
32 } // namespace allocator | 32 } // namespace allocator |
33 } // namespace base | 33 } // namespace base |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 const size_t kWindowsPageSize = 4096; | 37 const size_t kWindowsPageSize = 4096; |
38 const size_t kMaxWindowsAllocation = INT_MAX - kWindowsPageSize; | 38 const size_t kMaxWindowsAllocation = INT_MAX - kWindowsPageSize; |
39 int new_mode = 0; | 39 int new_mode = 0; |
40 | 40 |
41 // VS2013 crt uses the process heap as its heap, so we do the same here. | 41 inline HANDLE get_heap_handle() { |
42 // See heapinit.c in VS CRT sources. | 42 return reinterpret_cast<HANDLE>(_get_heap_handle()); |
43 bool win_heap_init() { | |
44 // Set the _crtheap global here. THis allows us to offload most of the | |
45 // memory management to the CRT, except the functions we need to shim. | |
46 _crtheap = GetProcessHeap(); | |
47 return _crtheap != nullptr; | |
48 } | 43 } |
49 | 44 |
50 void* win_heap_malloc(size_t size) { | 45 void* win_heap_malloc(size_t size) { |
51 if (size < kMaxWindowsAllocation) | 46 if (size < kMaxWindowsAllocation) |
52 return HeapAlloc(_crtheap, 0, size); | 47 return HeapAlloc(get_heap_handle(), 0, size); |
53 return NULL; | 48 return nullptr; |
54 } | 49 } |
55 | 50 |
56 void win_heap_free(void* size) { | 51 void win_heap_free(void* size) { |
57 HeapFree(_crtheap, 0, size); | 52 HeapFree(get_heap_handle(), 0, size); |
58 } | 53 } |
59 | 54 |
60 void* win_heap_realloc(void* ptr, size_t size) { | 55 void* win_heap_realloc(void* ptr, size_t size) { |
61 if (!ptr) | 56 if (!ptr) |
62 return win_heap_malloc(size); | 57 return win_heap_malloc(size); |
63 if (!size) { | 58 if (!size) { |
64 win_heap_free(ptr); | 59 win_heap_free(ptr); |
65 return NULL; | 60 return nullptr; |
66 } | 61 } |
67 if (size < kMaxWindowsAllocation) | 62 if (size < kMaxWindowsAllocation) |
68 return HeapReAlloc(_crtheap, 0, ptr, size); | 63 return HeapReAlloc(get_heap_handle(), 0, ptr, size); |
69 return NULL; | 64 return nullptr; |
70 } | |
71 | |
72 void win_heap_term() { | |
73 _crtheap = NULL; | |
74 } | 65 } |
75 | 66 |
76 // Call the new handler, if one has been set. | 67 // Call the new handler, if one has been set. |
77 // Returns true on successfully calling the handler, false otherwise. | 68 // Returns true on successfully calling the handler, false otherwise. |
78 inline bool call_new_handler(bool nothrow, size_t size) { | 69 inline bool call_new_handler(bool nothrow, size_t size) { |
79 // Get the current new handler. | 70 // Get the current new handler. |
80 _PNH nh = _query_new_handler(); | 71 _PNH nh = _query_new_handler(); |
81 #if defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | 72 #if defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS |
82 if (!nh) | 73 if (!nh) |
83 return false; | 74 return false; |
84 // Since exceptions are disabled, we don't really know if new_handler | 75 // Since exceptions are disabled, we don't really know if new_handler |
85 // failed. Assume it will abort if it fails. | 76 // failed. Assume it will abort if it fails. |
86 return nh(size); | 77 return nh(size) ? true : false; |
87 #else | 78 #else |
88 #error "Exceptions in allocator shim are not supported!" | 79 #error "Exceptions in allocator shim are not supported!" |
89 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | 80 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS |
90 return false; | |
91 } | |
92 | |
93 // Implement a C++ style allocation, which always calls the new_handler | |
94 // on failure. | |
95 inline void* generic_cpp_alloc(size_t size, bool nothrow) { | |
96 void* ptr; | |
97 for (;;) { | |
98 ptr = malloc(size); | |
99 if (ptr) | |
100 return ptr; | |
101 if (!call_new_handler(nothrow, size)) | |
102 break; | |
103 } | |
104 return ptr; | |
105 } | 81 } |
106 | 82 |
107 } // namespace | 83 } // namespace |
108 | 84 |
109 // new.cpp | 85 extern "C" { |
110 void* operator new(size_t size) { | |
111 return generic_cpp_alloc(size, false); | |
112 } | |
113 | 86 |
114 // delete.cpp | 87 // Symbol to allow weak linkage to win_heap_malloc from memory_win.cc. |
115 void operator delete(void* p) throw() { | 88 void* (*malloc_unchecked)(size_t) = &win_heap_malloc; |
116 free(p); | |
117 } | |
118 | |
119 // new2.cpp | |
120 void* operator new[](size_t size) { | |
121 return generic_cpp_alloc(size, false); | |
122 } | |
123 | |
124 // delete2.cpp | |
125 void operator delete[](void* p) throw() { | |
126 free(p); | |
127 } | |
128 | |
129 // newopnt.cpp | |
130 void* operator new(size_t size, const std::nothrow_t& nt) { | |
131 return generic_cpp_alloc(size, true); | |
132 } | |
133 | |
134 // newaopnt.cpp | |
135 void* operator new[](size_t size, const std::nothrow_t& nt) { | |
136 return generic_cpp_alloc(size, true); | |
137 } | |
138 | 89 |
139 // This function behaves similarly to MSVC's _set_new_mode. | 90 // This function behaves similarly to MSVC's _set_new_mode. |
140 // If flag is 0 (default), calls to malloc will behave normally. | 91 // If flag is 0 (default), calls to malloc will behave normally. |
141 // If flag is 1, calls to malloc will behave like calls to new, | 92 // If flag is 1, calls to malloc will behave like calls to new, |
142 // and the std_new_handler will be invoked on failure. | 93 // and the std_new_handler will be invoked on failure. |
143 // Returns the previous mode. | 94 // Returns the previous mode. |
144 // new_mode.cpp | 95 // |
145 int _set_new_mode(int flag) throw() { | 96 // Replaces _set_new_mode in ucrt\heap\new_mode.cpp |
| 97 int _set_new_mode(int flag) { |
| 98 // 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. |
| 100 base::allocator::g_is_win_shim_layer_initialized = true; |
146 int old_mode = new_mode; | 101 int old_mode = new_mode; |
147 new_mode = flag; | 102 new_mode = flag; |
148 return old_mode; | 103 return old_mode; |
149 } | 104 } |
150 | 105 |
151 // new_mode.cpp | 106 // Replaces _query_new_mode in ucrt\heap\new_mode.cpp |
152 int _query_new_mode() { | 107 int _query_new_mode() { |
153 return new_mode; | 108 return new_mode; |
154 } | 109 } |
155 | 110 |
156 extern "C" { | 111 // Replaces malloc in ucrt\heap\malloc.cpp |
157 // malloc.c | 112 __declspec(restrict) void* malloc(size_t size) { |
158 void* malloc(size_t size) { | |
159 void* ptr; | 113 void* ptr; |
160 for (;;) { | 114 for (;;) { |
161 ptr = win_heap_malloc(size); | 115 ptr = win_heap_malloc(size); |
162 if (ptr) | 116 if (ptr) |
163 return ptr; | 117 return ptr; |
164 | 118 |
165 if (!new_mode || !call_new_handler(true, size)) | 119 if (!new_mode || !call_new_handler(true, size)) |
166 break; | 120 break; |
167 } | 121 } |
168 return ptr; | 122 return ptr; |
169 } | 123 } |
170 | 124 |
171 // Symbol to allow weak linkage to win_heap_malloc from memory_win.cc. | 125 // Replaces free in ucrt\heap\free.cpp |
172 void* (*malloc_unchecked)(size_t) = &win_heap_malloc; | |
173 | |
174 // free.c | |
175 void free(void* p) { | 126 void free(void* p) { |
176 win_heap_free(p); | 127 win_heap_free(p); |
177 return; | 128 return; |
178 } | 129 } |
179 | 130 |
180 // realloc.c | 131 // Replaces realloc in ucrt\heap\realloc.cpp |
181 void* realloc(void* ptr, size_t size) { | 132 __declspec(restrict) void* realloc(void* ptr, size_t size) { |
182 // Webkit is brittle for allocators that return NULL for malloc(0). The | 133 // Webkit is brittle for allocators that return NULL for malloc(0). The |
183 // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure | 134 // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure |
184 // to call malloc for this case. | 135 // to call malloc for this case. |
185 if (!ptr) | 136 if (!ptr) |
186 return malloc(size); | 137 return malloc(size); |
187 | 138 |
188 void* new_ptr; | 139 void* new_ptr; |
189 for (;;) { | 140 for (;;) { |
190 new_ptr = win_heap_realloc(ptr, size); | 141 new_ptr = win_heap_realloc(ptr, size); |
191 | 142 |
192 // Subtle warning: NULL return does not alwas indicate out-of-memory. If | 143 // Subtle warning: NULL return does not alwas indicate out-of-memory. If |
193 // the requested new size is zero, realloc should free the ptr and return | 144 // the requested new size is zero, realloc should free the ptr and return |
194 // NULL. | 145 // NULL. |
195 if (new_ptr || !size) | 146 if (new_ptr || !size) |
196 return new_ptr; | 147 return new_ptr; |
197 if (!new_mode || !call_new_handler(true, size)) | 148 if (!new_mode || !call_new_handler(true, size)) |
198 break; | 149 break; |
199 } | 150 } |
200 return new_ptr; | 151 return new_ptr; |
201 } | 152 } |
202 | 153 |
203 // heapinit.c | 154 // Replaces calloc in ucrt\heap\calloc.cpp |
204 intptr_t _get_heap_handle() { | 155 __declspec(restrict) void* calloc(size_t n, size_t elem_size) { |
205 return reinterpret_cast<intptr_t>(_crtheap); | |
206 } | |
207 | |
208 // heapinit.c | |
209 int _heap_init() { | |
210 base::allocator::g_is_win_shim_layer_initialized = true; | |
211 return win_heap_init() ? 1 : 0; | |
212 } | |
213 | |
214 // heapinit.c | |
215 void _heap_term() { | |
216 win_heap_term(); | |
217 } | |
218 | |
219 // calloc.c | |
220 void* calloc(size_t n, size_t elem_size) { | |
221 // Overflow check. | 156 // Overflow check. |
222 const size_t size = n * elem_size; | 157 const size_t size = n * elem_size; |
223 if (elem_size != 0 && size / elem_size != n) | 158 if (elem_size != 0 && size / elem_size != n) |
224 return NULL; | 159 return nullptr; |
225 | 160 |
226 void* result = malloc(size); | 161 void* result = malloc(size); |
227 if (result != NULL) { | 162 if (result) { |
228 memset(result, 0, size); | 163 memset(result, 0, size); |
229 } | 164 } |
230 return result; | 165 return result; |
231 } | 166 } |
232 | 167 |
233 // recalloc.c | 168 } // extern C |
234 void* _recalloc(void* p, size_t n, size_t elem_size) { | |
235 if (!p) | |
236 return calloc(n, elem_size); | |
237 | 169 |
238 // This API is a bit odd. | 170 #endif // defined(ALLOCATOR_SHIM) |
239 // Note: recalloc only guarantees zeroed memory when p is NULL. | |
240 // Generally, calls to malloc() have padding. So a request | |
241 // to malloc N bytes actually malloc's N+x bytes. Later, if | |
242 // that buffer is passed to recalloc, we don't know what N | |
243 // was anymore. We only know what N+x is. As such, there is | |
244 // no way to know what to zero out. | |
245 const size_t size = n * elem_size; | |
246 if (elem_size != 0 && size / elem_size != n) | |
247 return NULL; | |
248 return realloc(p, size); | |
249 } | |
250 | |
251 // calloc_impl.c | |
252 void* _calloc_impl(size_t n, size_t size) { | |
253 return calloc(n, size); | |
254 } | |
255 | |
256 #ifndef NDEBUG | |
257 #undef malloc | |
258 #undef free | |
259 #undef calloc | |
260 | |
261 static int error_handler(int reportType) { | |
262 switch (reportType) { | |
263 case 0: // _CRT_WARN | |
264 __debugbreak(); | |
265 return 0; | |
266 | |
267 case 1: // _CRT_ERROR | |
268 __debugbreak(); | |
269 return 0; | |
270 | |
271 case 2: // _CRT_ASSERT | |
272 __debugbreak(); | |
273 return 0; | |
274 } | |
275 char* p = NULL; | |
276 *p = '\0'; | |
277 return 0; | |
278 } | |
279 | |
280 int _CrtDbgReport(int reportType, | |
281 const char*, | |
282 int, | |
283 const char*, | |
284 const char*, | |
285 ...) { | |
286 return error_handler(reportType); | |
287 } | |
288 | |
289 int _CrtDbgReportW(int reportType, | |
290 const wchar_t*, | |
291 int, | |
292 const wchar_t*, | |
293 const wchar_t*, | |
294 ...) { | |
295 return error_handler(reportType); | |
296 } | |
297 | |
298 int _CrtSetReportMode(int, int) { | |
299 return 0; | |
300 } | |
301 | |
302 void* _malloc_dbg(size_t size, int, const char*, int) { | |
303 return malloc(size); | |
304 } | |
305 | |
306 void* _realloc_dbg(void* ptr, size_t size, int, const char*, int) { | |
307 return realloc(ptr, size); | |
308 } | |
309 | |
310 void _free_dbg(void* ptr, int) { | |
311 free(ptr); | |
312 } | |
313 | |
314 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) { | |
315 return calloc(n, size); | |
316 } | |
317 #endif // NDEBUG | |
318 | |
319 } // extern C | |
OLD | NEW |