1 // Copyright 2016 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.
4
5 // This header defines symbols to override the same functions in the Visual C++
6 // CRT implementation.
7
8 #ifdef BASE_ALLOCATOR_ALLOCATOR_SHIM_OVERRIDE_UCRT_SYMBOLS_WIN_H_
9 #error This header is meant to be included only once by allocator_shim.cc
10 #endif
11 #define BASE_ALLOCATOR_ALLOCATOR_SHIM_OVERRIDE_UCRT_SYMBOLS_WIN_H_
12
13 #include <malloc.h>
14
15 #include <windows.h>
16
17 extern "C" {
18
19 void* (*malloc_unchecked)(size_t) = &base::allocator::UncheckedAlloc;
20
21 namespace {
22
23 int win_new_mode = 0;
24
25 } // namespace
26
27 // This function behaves similarly to MSVC's _set_new_mode.
28 // If flag is 0 (default), calls to malloc will behave normally.
29 // If flag is 1, calls to malloc will behave like calls to new,
30 // and the std_new_handler will be invoked on failure.
31 // Returns the previous mode.
32 //
33 // Replaces _set_new_mode in ucrt\heap\new_mode.cpp
_set_new_mode(int flag)34 int _set_new_mode(int flag) {
35 // The MS CRT calls this function early on in startup, so this serves as a low
36 // overhead proof that the allocator shim is in place for this process.
37 base::allocator::g_is_win_shim_layer_initialized = true;
38 int old_mode = win_new_mode;
39 win_new_mode = flag;
40
41 base::allocator::SetCallNewHandlerOnMallocFailure(win_new_mode != 0);
42
43 return old_mode;
44 }
45
46 // Replaces _query_new_mode in ucrt\heap\new_mode.cpp
_query_new_mode()47 int _query_new_mode() {
48 return win_new_mode;
49 }
50
51 // These symbols override the CRT's implementation of the same functions.
malloc(size_t size)52 __declspec(restrict) void* malloc(size_t size) {
53 return ShimMalloc(size, nullptr);
54 }
55
free(void * ptr)56 void free(void* ptr) {
57 ShimFree(ptr, nullptr);
58 }
59
realloc(void * ptr,size_t size)60 __declspec(restrict) void* realloc(void* ptr, size_t size) {
61 return ShimRealloc(ptr, size, nullptr);
62 }
63
calloc(size_t n,size_t size)64 __declspec(restrict) void* calloc(size_t n, size_t size) {
65 return ShimCalloc(n, size, nullptr);
66 }
67
68 // _msize() is the Windows equivalent of malloc_size().
_msize(void * memblock)69 size_t _msize(void* memblock) {
70 return ShimGetSizeEstimate(memblock, nullptr);
71 }
72
_aligned_malloc(size_t size,size_t alignment)73 __declspec(restrict) void* _aligned_malloc(size_t size, size_t alignment) {
74 return ShimAlignedMalloc(size, alignment, nullptr);
75 }
76
_aligned_realloc(void * address,size_t size,size_t alignment)77 __declspec(restrict) void* _aligned_realloc(void* address,
78 size_t size,
79 size_t alignment) {
80 return ShimAlignedRealloc(address, size, alignment, nullptr);
81 }
82
_aligned_free(void * address)83 void _aligned_free(void* address) {
84 ShimAlignedFree(address, nullptr);
85 }
86
87 // _recalloc_base is called by CRT internally.
_recalloc_base(void * block,size_t count,size_t size)88 __declspec(restrict) void* _recalloc_base(void* block,
89 size_t count,
90 size_t size) {
91 const size_t old_block_size = (block != nullptr) ? _msize(block) : 0;
92 base::CheckedNumeric<size_t> new_block_size_checked = count;
93 new_block_size_checked *= size;
94 const size_t new_block_size = new_block_size_checked.ValueOrDie();
95
96 void* const new_block = realloc(block, new_block_size);
97
98 if (new_block != nullptr && old_block_size < new_block_size) {
99 memset(static_cast<char*>(new_block) + old_block_size, 0,
100 new_block_size - old_block_size);
101 }
102
103 return new_block;
104 }
105
_recalloc(void * block,size_t count,size_t size)106 __declspec(restrict) void* _recalloc(void* block, size_t count, size_t size) {
107 return _recalloc_base(block, count, size);
108 }
109
110 // The following uncommon _aligned_* routines are not used in Chromium and have
111 // been shimmed to immediately crash to ensure that implementations are added if
112 // uses are introduced.
_aligned_recalloc(void * address,size_t num,size_t size,size_t alignment)113 __declspec(restrict) void* _aligned_recalloc(void* address,
114 size_t num,
115 size_t size,
116 size_t alignment) {
117 CHECK(false) << "This routine has not been implemented";
118 __builtin_unreachable();
119 }
120
_aligned_msize(void * address,size_t alignment,size_t offset)121 size_t _aligned_msize(void* address, size_t alignment, size_t offset) {
122 CHECK(false) << "This routine has not been implemented";
123 __builtin_unreachable();
124 }
125
_aligned_offset_malloc(size_t size,size_t alignment,size_t offset)126 __declspec(restrict) void* _aligned_offset_malloc(size_t size,
127 size_t alignment,
128 size_t offset) {
129 CHECK(false) << "This routine has not been implemented";
130 __builtin_unreachable();
131 }
132
_aligned_offset_realloc(void * address,size_t size,size_t alignment,size_t offset)133 __declspec(restrict) void* _aligned_offset_realloc(void* address,
134 size_t size,
135 size_t alignment,
136 size_t offset) {
137 CHECK(false) << "This routine has not been implemented";
138 __builtin_unreachable();
139 }
140
_aligned_offset_recalloc(void * address,size_t num,size_t size,size_t alignment,size_t offset)141 __declspec(restrict) void* _aligned_offset_recalloc(void* address,
142 size_t num,
143 size_t size,
144 size_t alignment,
145 size_t offset) {
146 CHECK(false) << "This routine has not been implemented";
147 __builtin_unreachable();
148 }
149
150 // The symbols
151 // * __acrt_heap
152 // * __acrt_initialize_heap
153 // * __acrt_uninitialize_heap
154 // * _get_heap_handle
155 // must be overridden all or none, as they are otherwise supplied
156 // by heap_handle.obj in the ucrt.lib file.
157 HANDLE __acrt_heap = nullptr;
158
__acrt_initialize_heap()159 bool __acrt_initialize_heap() {
160 __acrt_heap = ::HeapCreate(0, 0, 0);
161 return true;
162 }
163
__acrt_uninitialize_heap()164 bool __acrt_uninitialize_heap() {
165 ::HeapDestroy(__acrt_heap);
166 __acrt_heap = nullptr;
167 return true;
168 }
169
_get_heap_handle(void)170 intptr_t _get_heap_handle(void) {
171 return reinterpret_cast<intptr_t>(__acrt_heap);
172 }
173
174 } // extern "C"
175