Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: base/allocator/allocator_shim_win.cc

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

Powered by Google App Engine
This is Rietveld 408576698