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