OLD | NEW |
---|---|
(Empty) | |
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 #include "base/allocator/allocator_shim.h" | |
6 | |
7 #include <errno.h> | |
8 #include <unistd.h> | |
9 | |
10 #include <new> | |
11 | |
12 #include "base/atomicops.h" | |
13 #include "base/logging.h" | |
14 #include "base/macros.h" | |
15 #include "base/threading/platform_thread.h" | |
16 #include "build/build_config.h" | |
17 | |
18 // No calls to malloc / new in this file. They would would cause re-entrancy of | |
19 // the shim, which is hard to deal with. Keep this code as simple as possible | |
20 // and don't use any external C++ object here, not even //base ones. Even if | |
21 // they are safe to use today, in future they might be refactored. | |
22 | |
23 namespace { | |
24 | |
25 using namespace base; | |
26 | |
27 const allocator::AllocatorDispatch* g_chain_head = | |
28 &allocator::AllocatorDispatch::default_dispatch; | |
29 | |
30 bool g_call_new_handler_on_malloc_failure = false; | |
31 subtle::Atomic32 g_new_handler_lock = 0; | |
32 | |
33 // In theory this should be just base::ThreadChecker. But we can't afford | |
34 // the luxury of a LazyInstance<ThreadChecker> here as it would cause a new(). | |
35 bool CalledOnValidThread() { | |
36 static PlatformThreadId thread_id = kInvalidThreadId; | |
37 if (thread_id == kInvalidThreadId) | |
38 thread_id = PlatformThread::CurrentId(); | |
39 return thread_id == PlatformThread::CurrentId(); | |
40 } | |
41 | |
42 inline size_t GetPageSize() { | |
43 static size_t pagesize = 0; | |
44 if (!pagesize) | |
45 pagesize = sysconf(_SC_PAGESIZE); | |
46 return pagesize; | |
47 } | |
48 | |
49 // Calls the std::new handler thread-safely. Returns true if a new_handler was | |
50 // set and called, false if no new_handler was set. | |
51 bool CallNewHandler() { | |
52 // TODO(primiano): C++11 has introduced ::get_new_handler() which is supposed | |
53 // to be thread safe and would avoid the spinlock boilerplate here. However | |
54 // it doesn't seem to be available yet in the Linux chroot headers yet. | |
55 std::new_handler nh; | |
56 { | |
57 while (subtle::Acquire_CompareAndSwap(&g_new_handler_lock, 0, 1)) | |
58 PlatformThread::YieldCurrentThread(); | |
59 nh = std::set_new_handler(0); | |
60 ignore_result(std::set_new_handler(nh)); | |
61 subtle::Release_Store(&g_new_handler_lock, 0); | |
62 } | |
63 if (!nh) | |
64 return false; | |
65 (*nh)(); | |
66 return true; // Assume the new_handler will abort if it fails. | |
67 } | |
68 | |
69 } // namespace | |
70 | |
71 namespace base { | |
72 namespace allocator { | |
73 | |
74 void SetCallNewHandlerOnMallocFailure(bool value) { | |
75 g_call_new_handler_on_malloc_failure = value; | |
76 } | |
77 | |
78 void* UncheckedAlloc(size_t size) { | |
79 return g_chain_head->alloc_function(size, g_chain_head); | |
80 } | |
81 | |
82 void InsertAllocatorDispatch(AllocatorDispatch* dispatch) { | |
83 // Ensure this (and Remove below) are always called on the same thread. | |
84 DCHECK(CalledOnValidThread()); | |
85 | |
86 dispatch->next = g_chain_head; | |
Primiano Tucci (use gerrit)
2016/02/16 22:58:06
Ok, this I think is going to be the most controver
| |
87 | |
88 // This function does not guarantee to be thread-safe w.r.t. concurrent | |
89 // insertion / removals, but still has to guarantee that all the threads | |
90 // always see a consistent chain, hence the MemoryBarrier() below. | |
91 // InsertAllocatorDispatch() is NOT a fastpath, as opposite to malloc(), so | |
92 // we don't really want this to be a release-store with a corresponding | |
93 // acquire-load during malloc(). | |
94 base::subtle::MemoryBarrier(); | |
95 | |
96 g_chain_head = dispatch; | |
97 } | |
98 | |
99 void RemoveAllocatorDispatch(AllocatorDispatch* dispatch) { | |
100 DCHECK(CalledOnValidThread()); | |
101 g_chain_head = dispatch->next; | |
102 } | |
103 | |
104 } // namespace allocator | |
105 } // namespace base | |
106 | |
107 // THe Shim* functions below are the entry-points into the shim-layer and | |
108 // are supposed to be invoked / aliased by the allocator_shim_override_* | |
109 // headers to route the malloc / new symbols through the shim layer. | |
110 extern "C" { | |
111 | |
112 // The general pattern for allocations is: | |
113 // - Try to allocate, if succeded return the pointer. | |
114 // - If the allocation failed: | |
115 // - Call the std::new_handler if it was a C++ allocation. | |
116 // - Call the std::new_handler if it was a malloc() (or calloc() or similar) | |
117 // AND SetCallNewHandlerOnMallocFailure(true). | |
118 // - If the std::new_handler is NOT set just return nullptr. | |
119 // - If the std::new_handler is set: | |
120 // - Assume it will abort() if it fails (very likely the new_handler will | |
121 // just suicide priting a message). | |
122 // - Assume it did succeed if it returns, in which case reattempt the alloc. | |
123 | |
124 void* ShimCppNew(size_t size) { | |
125 void* ptr; | |
126 do { | |
127 ptr = g_chain_head->alloc_function(size, g_chain_head); | |
128 } while (!ptr && CallNewHandler()); | |
129 return ptr; | |
130 } | |
131 | |
132 void ShimCppDelete(void* address) { | |
133 return g_chain_head->free_function(address, g_chain_head); | |
134 } | |
135 | |
136 void* ShimMalloc(size_t size) { | |
137 void* ptr; | |
138 do { | |
139 ptr = g_chain_head->alloc_function(size, g_chain_head); | |
140 } while (!ptr && g_call_new_handler_on_malloc_failure && CallNewHandler()); | |
141 return ptr; | |
142 } | |
143 | |
144 void* ShimCalloc(size_t n, size_t size) { | |
145 void* ptr; | |
146 do { | |
147 ptr = g_chain_head->alloc_zero_initialized_function(n, size, g_chain_head); | |
148 } while (!ptr && g_call_new_handler_on_malloc_failure && CallNewHandler()); | |
149 return ptr; | |
150 } | |
151 | |
152 void* ShimRealloc(void* address, size_t size) { | |
153 // realloc(size == 0) means free() and might return a nullptr. We should | |
154 // not call the std::new_handler in that case, though. | |
155 void* ptr; | |
156 do { | |
157 ptr = g_chain_head->realloc_function(address, size, g_chain_head); | |
158 } while (!ptr && size && g_call_new_handler_on_malloc_failure && | |
159 CallNewHandler()); | |
160 return ptr; | |
161 } | |
162 | |
163 void* ShimMemalign(size_t alignment, size_t size) { | |
164 void* ptr; | |
165 do { | |
166 ptr = g_chain_head->alloc_aligned_function(alignment, size, g_chain_head); | |
167 } while (!ptr && g_call_new_handler_on_malloc_failure && CallNewHandler()); | |
168 return ptr; | |
169 } | |
170 | |
171 int ShimPosixMemalign(void** res, size_t alignment, size_t size) { | |
172 // posix_memalign is supposed to check the arguments. See tc_posix_memalign() | |
173 // in tc_malloc.cc. | |
174 if (((alignment % sizeof(void*)) != 0) || | |
175 ((alignment & (alignment - 1)) != 0) || (alignment == 0)) { | |
176 return EINVAL; | |
177 } | |
178 void* ptr = ShimMemalign(alignment, size); | |
179 *res = ptr; | |
180 return ptr ? 0 : ENOMEM; | |
181 } | |
182 | |
183 void* ShimValloc(size_t size) { | |
184 return ShimMemalign(GetPageSize(), size); | |
185 } | |
186 | |
187 void* ShimPvalloc(size_t size) { | |
188 // pvalloc(0) should allocate one page, according to its man page. | |
189 if (size == 0) { | |
190 size = GetPageSize(); | |
191 } else { | |
192 size = (size + GetPageSize() - 1) & ~(GetPageSize() - 1); | |
193 } | |
194 return ShimMemalign(GetPageSize(), size); | |
195 } | |
196 | |
197 void ShimFree(void* address) { | |
198 return g_chain_head->free_function(address, g_chain_head); | |
199 } | |
200 | |
201 } // extern "C" | |
202 | |
203 // Cpp symbols (new / delete) should always be routed through the shim layer. | |
204 #include "base/allocator/allocator_shim_override_cpp_symbols.h" | |
205 | |
206 // Ditto for plain malloc() / calloc() / free() etc. symbols. | |
207 #include "base/allocator/allocator_shim_override_libc_symbols.h" | |
208 | |
209 // In the case of tcmalloc we also want to plumb into the glibc hooks | |
210 // to avoid that allocations made in glibc itself (e.g., strdup()) get | |
211 // accidentally performed on the glibc heap instead of the tcmalloc one. | |
212 #if defined(USE_TCMALLOC) | |
213 #include "base/allocator/allocator_shim_override_glibc_weak_symbols.h" | |
214 #endif | |
215 | |
216 // Cross-checks. | |
217 | |
218 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | |
219 #error The allocator shim should not be compiled when building for memory tools. | |
220 #endif | |
221 | |
222 #if (defined(__GNUC__) && defined(__EXCEPTIONS)) || \ | |
223 (defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS) | |
224 #error This code cannot be used when exceptions are turned on. | |
225 #endif | |
OLD | NEW |