• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_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 
12 #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_ALLOCATOR_SHIM_OVERRIDE_UCRT_SYMBOLS_WIN_H_
13 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_ALLOCATOR_SHIM_OVERRIDE_UCRT_SYMBOLS_WIN_H_
14 
15 #include <malloc.h>
16 
17 #include <windows.h>
18 
19 #include "base/allocator/partition_allocator/shim/allocator_shim_internals.h"
20 
21 // Even though most C++ allocation operators can be left alone since the
22 // interception works at a lower level, these ones should be
23 // overridden. Otherwise they redirect to malloc(), which is configured to crash
24 // with an OOM in failure cases, such as allocation requests that are too large.
new(size_t size,const std::nothrow_t &)25 SHIM_ALWAYS_EXPORT void* operator new(size_t size,
26                                       const std::nothrow_t&) noexcept {
27   return ShimCppNewNoThrow(size);
28 }
29 
30 SHIM_ALWAYS_EXPORT void* operator new[](size_t size,
31                                         const std::nothrow_t&) noexcept {
32   return ShimCppNewNoThrow(size);
33 }
34 
35 extern "C" {
36 
37 void* (*malloc_unchecked)(size_t) = &allocator_shim::UncheckedAlloc;
38 
39 namespace {
40 
41 int win_new_mode = 0;
42 
43 }  // namespace
44 
45 // This function behaves similarly to MSVC's _set_new_mode.
46 // If flag is 0 (default), calls to malloc will behave normally.
47 // If flag is 1, calls to malloc will behave like calls to new,
48 // and the std_new_handler will be invoked on failure.
49 // Returns the previous mode.
50 //
51 // Replaces _set_new_mode in ucrt\heap\new_mode.cpp
_set_new_mode(int flag)52 int _set_new_mode(int flag) {
53   // The MS CRT calls this function early on in startup, so this serves as a low
54   // overhead proof that the allocator shim is in place for this process.
55   allocator_shim::g_is_win_shim_layer_initialized = true;
56   int old_mode = win_new_mode;
57   win_new_mode = flag;
58 
59   allocator_shim::SetCallNewHandlerOnMallocFailure(win_new_mode != 0);
60 
61   return old_mode;
62 }
63 
64 // Replaces _query_new_mode in ucrt\heap\new_mode.cpp
_query_new_mode()65 int _query_new_mode() {
66   return win_new_mode;
67 }
68 
69 // These symbols override the CRT's implementation of the same functions.
malloc(size_t size)70 __declspec(restrict) void* malloc(size_t size) {
71   return ShimMalloc(size, nullptr);
72 }
73 
free(void * ptr)74 void free(void* ptr) {
75   ShimFree(ptr, nullptr);
76 }
77 
realloc(void * ptr,size_t size)78 __declspec(restrict) void* realloc(void* ptr, size_t size) {
79   return ShimRealloc(ptr, size, nullptr);
80 }
81 
calloc(size_t n,size_t size)82 __declspec(restrict) void* calloc(size_t n, size_t size) {
83   return ShimCalloc(n, size, nullptr);
84 }
85 
86 // _msize() is the Windows equivalent of malloc_size().
_msize(void * memblock)87 size_t _msize(void* memblock) {
88   return ShimGetSizeEstimate(memblock, nullptr);
89 }
90 
_aligned_malloc(size_t size,size_t alignment)91 __declspec(restrict) void* _aligned_malloc(size_t size, size_t alignment) {
92   return ShimAlignedMalloc(size, alignment, nullptr);
93 }
94 
_aligned_realloc(void * address,size_t size,size_t alignment)95 __declspec(restrict) void* _aligned_realloc(void* address,
96                                             size_t size,
97                                             size_t alignment) {
98   return ShimAlignedRealloc(address, size, alignment, nullptr);
99 }
100 
_aligned_free(void * address)101 void _aligned_free(void* address) {
102   ShimAlignedFree(address, nullptr);
103 }
104 
105 // _recalloc_base is called by CRT internally.
_recalloc_base(void * block,size_t count,size_t size)106 __declspec(restrict) void* _recalloc_base(void* block,
107                                           size_t count,
108                                           size_t size) {
109   const size_t old_block_size = (block != nullptr) ? _msize(block) : 0;
110   base::CheckedNumeric<size_t> new_block_size_checked = count;
111   new_block_size_checked *= size;
112   const size_t new_block_size = new_block_size_checked.ValueOrDie();
113 
114   void* const new_block = realloc(block, new_block_size);
115 
116   if (new_block != nullptr && old_block_size < new_block_size) {
117     memset(static_cast<char*>(new_block) + old_block_size, 0,
118            new_block_size - old_block_size);
119   }
120 
121   return new_block;
122 }
123 
_malloc_base(size_t size)124 __declspec(restrict) void* _malloc_base(size_t size) {
125   return malloc(size);
126 }
127 
_calloc_base(size_t n,size_t size)128 __declspec(restrict) void* _calloc_base(size_t n, size_t size) {
129   return calloc(n, size);
130 }
131 
_free_base(void * block)132 void _free_base(void* block) {
133   free(block);
134 }
135 
_recalloc(void * block,size_t count,size_t size)136 __declspec(restrict) void* _recalloc(void* block, size_t count, size_t size) {
137   return _recalloc_base(block, count, size);
138 }
139 
140 // The following uncommon _aligned_* routines are not used in Chromium and have
141 // been shimmed to immediately crash to ensure that implementations are added if
142 // uses are introduced.
_aligned_recalloc(void * address,size_t num,size_t size,size_t alignment)143 __declspec(restrict) void* _aligned_recalloc(void* address,
144                                              size_t num,
145                                              size_t size,
146                                              size_t alignment) {
147   CHECK(false) << "This routine has not been implemented";
148   __builtin_unreachable();
149 }
150 
_aligned_msize(void * address,size_t alignment,size_t offset)151 size_t _aligned_msize(void* address, size_t alignment, size_t offset) {
152   CHECK(false) << "This routine has not been implemented";
153   __builtin_unreachable();
154 }
155 
_aligned_offset_malloc(size_t size,size_t alignment,size_t offset)156 __declspec(restrict) void* _aligned_offset_malloc(size_t size,
157                                                   size_t alignment,
158                                                   size_t offset) {
159   CHECK(false) << "This routine has not been implemented";
160   __builtin_unreachable();
161 }
162 
_aligned_offset_realloc(void * address,size_t size,size_t alignment,size_t offset)163 __declspec(restrict) void* _aligned_offset_realloc(void* address,
164                                                    size_t size,
165                                                    size_t alignment,
166                                                    size_t offset) {
167   CHECK(false) << "This routine has not been implemented";
168   __builtin_unreachable();
169 }
170 
_aligned_offset_recalloc(void * address,size_t num,size_t size,size_t alignment,size_t offset)171 __declspec(restrict) void* _aligned_offset_recalloc(void* address,
172                                                     size_t num,
173                                                     size_t size,
174                                                     size_t alignment,
175                                                     size_t offset) {
176   CHECK(false) << "This routine has not been implemented";
177   __builtin_unreachable();
178 }
179 
180 }  // extern "C"
181 
182 #endif  // BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_ALLOCATOR_SHIM_OVERRIDE_UCRT_SYMBOLS_WIN_H_
183