| 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 | |
| 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 return _crtheap != nullptr; |
| 43 } | 48 } |
| 44 | 49 |
| 45 void* win_heap_malloc(size_t size) { | 50 void* win_heap_malloc(size_t size) { |
| 46 if (size < kMaxWindowsAllocation) | 51 if (size < kMaxWindowsAllocation) |
| 47 return HeapAlloc(get_heap_handle(), 0, size); | 52 return HeapAlloc(_crtheap, 0, size); |
| 48 return nullptr; | 53 return NULL; |
| 49 } | 54 } |
| 50 | 55 |
| 51 void win_heap_free(void* size) { | 56 void win_heap_free(void* size) { |
| 52 HeapFree(get_heap_handle(), 0, size); | 57 HeapFree(_crtheap, 0, size); |
| 53 } | 58 } |
| 54 | 59 |
| 55 void* win_heap_realloc(void* ptr, size_t size) { | 60 void* win_heap_realloc(void* ptr, size_t size) { |
| 56 if (!ptr) | 61 if (!ptr) |
| 57 return win_heap_malloc(size); | 62 return win_heap_malloc(size); |
| 58 if (!size) { | 63 if (!size) { |
| 59 win_heap_free(ptr); | 64 win_heap_free(ptr); |
| 60 return nullptr; | 65 return NULL; |
| 61 } | 66 } |
| 62 if (size < kMaxWindowsAllocation) | 67 if (size < kMaxWindowsAllocation) |
| 63 return HeapReAlloc(get_heap_handle(), 0, ptr, size); | 68 return HeapReAlloc(_crtheap, 0, ptr, size); |
| 64 return nullptr; | 69 return NULL; |
| 70 } |
| 71 |
| 72 void win_heap_term() { |
| 73 _crtheap = NULL; |
| 65 } | 74 } |
| 66 | 75 |
| 67 // Call the new handler, if one has been set. | 76 // Call the new handler, if one has been set. |
| 68 // Returns true on successfully calling the handler, false otherwise. | 77 // Returns true on successfully calling the handler, false otherwise. |
| 69 inline bool call_new_handler(bool nothrow, size_t size) { | 78 inline bool call_new_handler(bool nothrow, size_t size) { |
| 70 // Get the current new handler. | 79 // Get the current new handler. |
| 71 _PNH nh = _query_new_handler(); | 80 _PNH nh = _query_new_handler(); |
| 72 #if defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | 81 #if defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS |
| 73 if (!nh) | 82 if (!nh) |
| 74 return false; | 83 return false; |
| 75 // Since exceptions are disabled, we don't really know if new_handler | 84 // Since exceptions are disabled, we don't really know if new_handler |
| 76 // failed. Assume it will abort if it fails. | 85 // failed. Assume it will abort if it fails. |
| 77 return nh(size) ? true : false; | 86 return nh(size); |
| 78 #else | 87 #else |
| 79 #error "Exceptions in allocator shim are not supported!" | 88 #error "Exceptions in allocator shim are not supported!" |
| 80 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | 89 #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; |
| 81 } | 105 } |
| 82 | 106 |
| 83 } // namespace | 107 } // namespace |
| 84 | 108 |
| 85 extern "C" { | 109 // new.cpp |
| 110 void* operator new(size_t size) { |
| 111 return generic_cpp_alloc(size, false); |
| 112 } |
| 86 | 113 |
| 87 // Symbol to allow weak linkage to win_heap_malloc from memory_win.cc. | 114 // delete.cpp |
| 88 void* (*malloc_unchecked)(size_t) = &win_heap_malloc; | 115 void operator delete(void* p) throw() { |
| 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 } |
| 89 | 138 |
| 90 // This function behaves similarly to MSVC's _set_new_mode. | 139 // This function behaves similarly to MSVC's _set_new_mode. |
| 91 // If flag is 0 (default), calls to malloc will behave normally. | 140 // 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, | 141 // If flag is 1, calls to malloc will behave like calls to new, |
| 93 // and the std_new_handler will be invoked on failure. | 142 // and the std_new_handler will be invoked on failure. |
| 94 // Returns the previous mode. | 143 // Returns the previous mode. |
| 95 // | 144 // new_mode.cpp |
| 96 // Replaces _set_new_mode in ucrt\heap\new_mode.cpp | 145 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; | 146 int old_mode = new_mode; |
| 102 new_mode = flag; | 147 new_mode = flag; |
| 103 return old_mode; | 148 return old_mode; |
| 104 } | 149 } |
| 105 | 150 |
| 106 // Replaces _query_new_mode in ucrt\heap\new_mode.cpp | 151 // new_mode.cpp |
| 107 int _query_new_mode() { | 152 int _query_new_mode() { |
| 108 return new_mode; | 153 return new_mode; |
| 109 } | 154 } |
| 110 | 155 |
| 111 // Replaces malloc in ucrt\heap\malloc.cpp | 156 extern "C" { |
| 112 __declspec(restrict) void* malloc(size_t size) { | 157 // malloc.c |
| 158 void* malloc(size_t size) { |
| 113 void* ptr; | 159 void* ptr; |
| 114 for (;;) { | 160 for (;;) { |
| 115 ptr = win_heap_malloc(size); | 161 ptr = win_heap_malloc(size); |
| 116 if (ptr) | 162 if (ptr) |
| 117 return ptr; | 163 return ptr; |
| 118 | 164 |
| 119 if (!new_mode || !call_new_handler(true, size)) | 165 if (!new_mode || !call_new_handler(true, size)) |
| 120 break; | 166 break; |
| 121 } | 167 } |
| 122 return ptr; | 168 return ptr; |
| 123 } | 169 } |
| 124 | 170 |
| 125 // Replaces free in ucrt\heap\free.cpp | 171 // Symbol to allow weak linkage to win_heap_malloc from memory_win.cc. |
| 172 void* (*malloc_unchecked)(size_t) = &win_heap_malloc; |
| 173 |
| 174 // free.c |
| 126 void free(void* p) { | 175 void free(void* p) { |
| 127 win_heap_free(p); | 176 win_heap_free(p); |
| 128 return; | 177 return; |
| 129 } | 178 } |
| 130 | 179 |
| 131 // Replaces realloc in ucrt\heap\realloc.cpp | 180 // realloc.c |
| 132 __declspec(restrict) void* realloc(void* ptr, size_t size) { | 181 void* realloc(void* ptr, size_t size) { |
| 133 // Webkit is brittle for allocators that return NULL for malloc(0). The | 182 // 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 | 183 // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure |
| 135 // to call malloc for this case. | 184 // to call malloc for this case. |
| 136 if (!ptr) | 185 if (!ptr) |
| 137 return malloc(size); | 186 return malloc(size); |
| 138 | 187 |
| 139 void* new_ptr; | 188 void* new_ptr; |
| 140 for (;;) { | 189 for (;;) { |
| 141 new_ptr = win_heap_realloc(ptr, size); | 190 new_ptr = win_heap_realloc(ptr, size); |
| 142 | 191 |
| 143 // Subtle warning: NULL return does not alwas indicate out-of-memory. If | 192 // 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 | 193 // the requested new size is zero, realloc should free the ptr and return |
| 145 // NULL. | 194 // NULL. |
| 146 if (new_ptr || !size) | 195 if (new_ptr || !size) |
| 147 return new_ptr; | 196 return new_ptr; |
| 148 if (!new_mode || !call_new_handler(true, size)) | 197 if (!new_mode || !call_new_handler(true, size)) |
| 149 break; | 198 break; |
| 150 } | 199 } |
| 151 return new_ptr; | 200 return new_ptr; |
| 152 } | 201 } |
| 153 | 202 |
| 154 // Replaces calloc in ucrt\heap\calloc.cpp | 203 // heapinit.c |
| 155 __declspec(restrict) void* calloc(size_t n, size_t elem_size) { | 204 intptr_t _get_heap_handle() { |
| 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) { |
| 156 // Overflow check. | 221 // Overflow check. |
| 157 const size_t size = n * elem_size; | 222 const size_t size = n * elem_size; |
| 158 if (elem_size != 0 && size / elem_size != n) | 223 if (elem_size != 0 && size / elem_size != n) |
| 159 return nullptr; | 224 return NULL; |
| 160 | 225 |
| 161 void* result = malloc(size); | 226 void* result = malloc(size); |
| 162 if (result) { | 227 if (result != NULL) { |
| 163 memset(result, 0, size); | 228 memset(result, 0, size); |
| 164 } | 229 } |
| 165 return result; | 230 return result; |
| 166 } | 231 } |
| 167 | 232 |
| 233 // recalloc.c |
| 234 void* _recalloc(void* p, size_t n, size_t elem_size) { |
| 235 if (!p) |
| 236 return calloc(n, elem_size); |
| 237 |
| 238 // This API is a bit odd. |
| 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 |
| 168 } // extern C | 319 } // extern C |
| 169 | |
| 170 #endif // defined(ALLOCATOR_SHIM) | |
| OLD | NEW |