1 // Copyright 2016 The Chromium Authors
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 PARTITION_ALLOC_SHIM_ALLOCATOR_SHIM_OVERRIDE_UCRT_SYMBOLS_WIN_H_
9 #error This header is meant to be included only once by allocator_shim_win_static.cc or allocator_shim_win_component.cc
10 #endif
11 
12 #ifndef PARTITION_ALLOC_SHIM_ALLOCATOR_SHIM_OVERRIDE_UCRT_SYMBOLS_WIN_H_
13 #define PARTITION_ALLOC_SHIM_ALLOCATOR_SHIM_OVERRIDE_UCRT_SYMBOLS_WIN_H_
14 
15 #include "partition_alloc/partition_alloc_buildflags.h"
16 
17 #if BUILDFLAG(USE_ALLOCATOR_SHIM)
18 #include "partition_alloc/partition_alloc_base/component_export.h"
19 #include "partition_alloc/partition_alloc_check.h"
20 #include "partition_alloc/shim/allocator_shim_internals.h"
21 #include "partition_alloc/shim/shim_alloc_functions.h"
22 #include "partition_alloc/shim/winheap_stubs_win.h"
23 
24 #if defined(COMPONENT_BUILD)
25 #include <cstdlib>
26 #include <cstring>
27 #endif
28 
29 namespace allocator_shim::internal {
30 
31 size_t CheckedMultiply(size_t multiplicand, size_t multiplier);
32 
33 }  // namespace allocator_shim::internal
34 
35 // Even though most C++ allocation operators can be left alone since the
36 // interception works at a lower level, these ones should be
37 // overridden. Otherwise they redirect to malloc(), which is configured to crash
38 // with an OOM in failure cases, such as allocation requests that are too large.
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)39 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
40 SHIM_ALWAYS_EXPORT void* operator new(size_t size,
41                                       const std::nothrow_t&) noexcept {
42   return ShimCppNewNoThrow(size);
43 }
44 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)45 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
46 SHIM_ALWAYS_EXPORT void* operator new[](size_t size,
47                                         const std::nothrow_t&) noexcept {
48   return ShimCppNewNoThrow(size);
49 }
50 
51 extern "C" {
52 
53 namespace {
54 
55 int win_new_mode = 0;
56 
57 }  // namespace
58 
59 // This function behaves similarly to MSVC's _set_new_mode.
60 // If flag is 0 (default), calls to malloc will behave normally.
61 // If flag is 1, calls to malloc will behave like calls to new,
62 // and the std_new_handler will be invoked on failure.
63 // Returns the previous mode.
64 //
65 // Replaces _set_new_mode in ucrt\heap\new_mode.cpp
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)66 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM) int _set_new_mode(int flag) {
67   // The MS CRT calls this function early on in startup, so this serves as a low
68   // overhead proof that the allocator shim is in place for this process.
69   allocator_shim::g_is_win_shim_layer_initialized = true;
70   int old_mode = win_new_mode;
71   win_new_mode = flag;
72 
73   allocator_shim::SetCallNewHandlerOnMallocFailure(win_new_mode != 0);
74 
75   return old_mode;
76 }
77 
78 // Replaces _query_new_mode in ucrt\heap\new_mode.cpp
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)79 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM) int _query_new_mode() {
80   return win_new_mode;
81 }
82 
83 // These symbols override the CRT's implementation of the same functions.
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)84 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
85 __declspec(restrict) void* malloc(size_t size) {
86   return ShimMalloc(size, nullptr);
87 }
88 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)89 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM) void free(void* ptr) {
90   ShimFree(ptr, nullptr);
91 }
92 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)93 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
94 __declspec(restrict) void* realloc(void* ptr, size_t size) {
95   return ShimRealloc(ptr, size, nullptr);
96 }
97 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)98 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
99 __declspec(restrict) void* calloc(size_t n, size_t size) {
100   return ShimCalloc(n, size, nullptr);
101 }
102 
103 // _msize() is the Windows equivalent of malloc_size().
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)104 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM) size_t _msize(void* memblock) {
105   return ShimGetSizeEstimate(memblock, nullptr);
106 }
107 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)108 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
109 __declspec(restrict) void* _aligned_malloc(size_t size, size_t alignment) {
110   return ShimAlignedMalloc(size, alignment, nullptr);
111 }
112 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)113 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
114 __declspec(restrict) void* _aligned_realloc(void* address,
115                                             size_t size,
116                                             size_t alignment) {
117   return ShimAlignedRealloc(address, size, alignment, nullptr);
118 }
119 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)120 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM) void _aligned_free(void* address) {
121   ShimAlignedFree(address, nullptr);
122 }
123 
124 // _recalloc_base is called by CRT internally.
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)125 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
126 __declspec(restrict) void* _recalloc_base(void* block,
127                                           size_t count,
128                                           size_t size) {
129   const size_t old_block_size = (block != nullptr) ? _msize(block) : 0;
130   const size_t new_block_size =
131       allocator_shim::internal::CheckedMultiply(count, size);
132   void* const new_block = realloc(block, new_block_size);
133 
134   if (new_block != nullptr && old_block_size < new_block_size) {
135     memset(static_cast<char*>(new_block) + old_block_size, 0,
136            new_block_size - old_block_size);
137   }
138 
139   return new_block;
140 }
141 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)142 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
143 __declspec(restrict) void* _malloc_base(size_t size) {
144   return malloc(size);
145 }
146 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)147 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
148 __declspec(restrict) void* _calloc_base(size_t n, size_t size) {
149   return calloc(n, size);
150 }
151 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)152 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM) void _free_base(void* block) {
153   free(block);
154 }
155 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)156 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
157 __declspec(restrict) void* _recalloc(void* block, size_t count, size_t size) {
158   return _recalloc_base(block, count, size);
159 }
160 
161 // The following uncommon _aligned_* routines are not used in Chromium and have
162 // been shimmed to immediately crash to ensure that implementations are added if
163 // uses are introduced.
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)164 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
165 __declspec(restrict) void* _aligned_recalloc(void* address,
166                                              size_t num,
167                                              size_t size,
168                                              size_t alignment) {
169   PA_CHECK(false) << "This routine has not been implemented";
170   __builtin_unreachable();
171 }
172 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)173 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
174 size_t _aligned_msize(void* address, size_t alignment, size_t offset) {
175   PA_CHECK(false) << "This routine has not been implemented";
176   __builtin_unreachable();
177 }
178 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)179 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
180 __declspec(restrict) void* _aligned_offset_malloc(size_t size,
181                                                   size_t alignment,
182                                                   size_t offset) {
183   PA_CHECK(false) << "This routine has not been implemented";
184   __builtin_unreachable();
185 }
186 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)187 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
188 __declspec(restrict) void* _aligned_offset_realloc(void* address,
189                                                    size_t size,
190                                                    size_t alignment,
191                                                    size_t offset) {
192   PA_CHECK(false) << "This routine has not been implemented";
193   __builtin_unreachable();
194 }
195 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)196 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
197 __declspec(restrict) void* _aligned_offset_recalloc(void* address,
198                                                     size_t num,
199                                                     size_t size,
200                                                     size_t alignment,
201                                                     size_t offset) {
202   PA_CHECK(false) << "This routine has not been implemented";
203   __builtin_unreachable();
204 }
205 
206 #if defined(COMPONENT_BUILD)
207 // Overrides CRT functions which internally call malloc() and expect callers
208 // will free().
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)209 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
210 char* _strdup(const char* strSource) {
211   char* dest = static_cast<char*>(malloc(strlen(strSource) + 1));
212   strcpy(dest, strSource);
213   return dest;
214 }
215 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)216 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
217 errno_t _dupenv_s(char** buffer,
218                   size_t* number_of_elements,
219                   const char* varname) {
220   if (buffer == nullptr || varname == nullptr) {
221     return EINVAL;
222   }
223   size_t size = 0;
224   errno_t err = getenv_s(&size, nullptr, 0, varname);
225   if (err != 0) {
226     *buffer = nullptr;
227     if (number_of_elements) {
228       *number_of_elements = 0;
229     }
230     return err;
231   }
232   if (number_of_elements) {
233     *number_of_elements = size;
234   }
235   *buffer = static_cast<char*>(malloc(size));
236   return getenv_s(&size, *buffer, size, varname);
237 }
238 
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)239 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
240 errno_t _wdupenv_s(wchar_t** buffer,
241                    size_t* number_of_elements,
242                    const wchar_t* varname) {
243   if (buffer == nullptr || varname == nullptr) {
244     return EINVAL;
245   }
246   size_t size = 0;
247   errno_t err = _wgetenv_s(&size, nullptr, 0, varname);
248   if (err != 0) {
249     *buffer = nullptr;
250     if (number_of_elements) {
251       *number_of_elements = 0;
252     }
253     return err;
254   }
255   if (number_of_elements) {
256     *number_of_elements = size;
257   }
258   *buffer = static_cast<wchar_t*>(malloc(sizeof(wchar_t) * size));
259   return _wgetenv_s(&size, *buffer, size, varname);
260 }
261 #endif
262 
263 }  // extern "C"
264 #endif  // BUILDFLAG(USE_ALLOCATOR_SHIM)
265 
266 #endif  // PARTITION_ALLOC_SHIM_ALLOCATOR_SHIM_OVERRIDE_UCRT_SYMBOLS_WIN_H_
267