OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015 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/memory/shared_memory_allocator.h" | |
6 | |
7 #include <assert.h> | |
8 #include <algorithm> | |
9 | |
10 #include "base/logging.h" | |
11 | |
12 // All integer constants in this file are signed because Atomic32 is signed | |
13 // and keeping all others consistent with this avoids a lot of unnecessary | |
14 // casting to avoid signed/unsigned operations just to avoid compiler errors. | |
15 // This means an occasonal cast of a constant from sizeof() to "int" but | |
16 // is far simpler than the alternative. | |
17 | |
18 namespace { | |
19 | |
20 // Required range of memory segment sizes. It has to fit in a signed 32-bit | |
21 // number and should be a power of 2 in order to accomodate almost any page | |
22 // size. | |
23 const int32_t kSegmentMinSize = 1 << 10; // 1 KiB | |
24 const int32_t kSegmentMaxSize = 1 << 30; // 1 GiB | |
25 | |
26 // All allocations and data-structures must be aligned to this byte boundary. | |
27 // Alignment as large as the physical bus between CPU and RAM is _required_ | |
28 // for some architectures, is simply more efficient on other CPUs, and | |
29 // generally a Good Idea(tm) for all platforms as it reduces/eliminates the | |
30 // chance that a type will span cache lines. Alignment mustn't be less | |
31 // than 8 to ensure proper alignment for all types. The rest is a balance | |
32 // between reducing spans across multiple cache lines and wasted space spent | |
33 // padding out allocations. An alignment of 16 would ensure that the block | |
34 // header structure always sits in a single cache line. An average of about | |
35 // 1/2 this value will be wasted with every allocation. | |
36 const int32_t kAllocAlignment = 8; | |
37 | |
38 // A constant (random) value placed in the shared metadata to identify | |
39 // an already initialized memory segment. | |
40 const int32_t kGlobalCookie = 0x408305DC; | |
41 | |
42 // The current version of the metadata. If updates are made that change | |
43 // the metadata, the version number can be queried to operate in a backward- | |
44 // compatible manner until the memory segment is completely re-initalized. | |
45 const int32_t kGlobalVersion = 1; | |
46 | |
47 // Constant values placed in the block headers to indicate its state. | |
48 const int32_t kBlockCookieFree = 0; | |
49 const int32_t kBlockCookieQueue = 1; | |
50 const int32_t kBlockCookieWasted = -1; | |
51 const int32_t kBlockCookieAllocated = 0xC8799269; | |
52 | |
53 // TODO(bcwhite): When acceptable, consider moving flags to std::atomic<char> | |
54 // types rather than combined bitfield. | |
55 | |
56 enum { | |
chrisha
2015/11/09 16:53:51
You can type this to be the same as the 'flags_' f
bcwhite
2015/11/09 18:02:05
Done.
| |
57 kFlagCorrupt, | |
chrisha
2015/11/09 16:53:51
It's more common in Chrome to define the flags in
bcwhite
2015/11/09 18:02:05
Done.
| |
58 kFlagFull | |
59 }; | |
60 | |
61 bool CheckFlag(base::subtle::Atomic32* flags, int flag) { | |
62 base::subtle::Atomic32 loaded_flags = base::subtle::Acquire_Load(flags); | |
63 return (loaded_flags & 1 << flag) != 0; | |
64 } | |
65 | |
66 void SetFlag(base::subtle::Atomic32* flags, int flag) { | |
67 for (;;) { | |
68 base::subtle::Atomic32 loaded_flags = base::subtle::Acquire_Load(flags); | |
69 base::subtle::Atomic32 new_flags = | |
70 (loaded_flags & ~(1 << flag)) | (1 << flag); | |
71 if (base::subtle::Release_CompareAndSwap( | |
72 flags, loaded_flags, new_flags) == loaded_flags) { | |
chrisha
2015/11/09 16:53:51
All of this is more readable if you hide the bitsh
bcwhite
2015/11/09 18:02:05
Done.
| |
73 break; | |
74 } | |
75 } | |
76 } | |
77 | |
78 } // namespace | |
79 | |
80 namespace base { | |
81 | |
82 // The block-header is placed at the top of every allocation within the | |
83 // segment to describe the data that follows it. | |
84 struct SharedMemoryAllocator::BlockHeader { | |
85 int32_t size; // Number of bytes in this block, including header. | |
86 int32_t cookie; // Constant value indicating completed allocation. | |
87 uint32_t type_id; // A number provided by caller indicating data type. | |
88 subtle::Atomic32 next; // Pointer to the next block when iterating. | |
89 }; | |
90 | |
91 // The shared metadata exists once at the top of the memory segment to | |
92 // describe the state of the allocator to all processes. | |
93 struct SharedMemoryAllocator::SharedMetadata { | |
94 int32_t cookie; // Some value that indicates complete initialization. | |
95 int32_t size; // Total size of memory segment. | |
96 int32_t page_size; // Paging size within memory segment. | |
97 int32_t version; // Version code so upgrades don't break. | |
98 subtle::Atomic32 freeptr; // Offset/ref to first free space in the segment. | |
99 subtle::Atomic32 flags; // Bitfield of information flags. | |
100 int32_t reserved; // Padding to ensure size is multiple of alignment. | |
101 | |
102 // The "iterable" queue is an M&S Queue as described here, append-only: | |
103 // https://www.research.ibm.com/people/m/michael/podc-1996.pdf | |
104 subtle::Atomic32 tailptr; // Last block available for iteration. | |
105 BlockHeader queue; // Empty block for linked-list head/tail. (must be last) | |
106 }; | |
107 | |
108 // The "queue" block header is used to detect "last node" so that zero/null | |
109 // can be used to indicate that it hasn't been added at all. It is part of | |
110 // the SharedMetadata structure which itself is always located at offset zero. | |
111 // This can't be a constant because SharedMetadata is a private definition. | |
112 #define REF_QUEUE offsetof(SharedMetadata, queue) | |
113 #define REF_NULL 0 // the equivalest NULL value for a reference | |
114 | |
115 SharedMemoryAllocator::SharedMemoryAllocator(void* base, | |
116 size_t size, | |
117 size_t page_size) | |
118 : shared_meta_(static_cast<SharedMetadata*>(base)), | |
119 mem_base_(static_cast<char*>(base)), | |
120 mem_size_((int32_t)size), | |
121 mem_page_((int32_t)(page_size ? page_size : size)), | |
122 corrupted_(0) { | |
123 static_assert(sizeof(BlockHeader) % kAllocAlignment == 0, | |
124 "BlockHeader is not a multiple of kAllocAlignment"); | |
125 static_assert(sizeof(SharedMetadata) % kAllocAlignment == 0, | |
126 "SharedMetadata is not a multiple of kAllocAlignment"); | |
127 | |
128 CHECK(base && reinterpret_cast<uintptr_t>(base) % kAllocAlignment == 0); | |
129 CHECK(size >= kSegmentMinSize && size <= kSegmentMaxSize && | |
130 size % kAllocAlignment == 0); | |
131 CHECK(page_size == 0 || size % page_size == 0); | |
132 | |
133 if (shared_meta_->cookie != kGlobalCookie) { | |
134 // This block is only executed when a completely new memory segment is | |
135 // being initialized. It's unshared and single-threaded... | |
136 const BlockHeader* first_block = reinterpret_cast<BlockHeader*>( | |
137 mem_base_ + sizeof(SharedMetadata)); | |
138 if (shared_meta_->cookie != 0 || | |
139 shared_meta_->size != 0 || | |
140 shared_meta_->version != 0 || | |
141 subtle::NoBarrier_Load(&shared_meta_->freeptr) != 0 || | |
142 subtle::NoBarrier_Load(&shared_meta_->flags) != 0 || | |
143 shared_meta_->tailptr != 0 || | |
144 shared_meta_->queue.cookie != 0 || | |
145 subtle::NoBarrier_Load(&shared_meta_->queue.next) != 0 || | |
146 first_block->size != 0 || | |
147 first_block->cookie != 0 || | |
148 first_block->type_id != 0 || | |
149 first_block->next != 0) { | |
150 // ...or something malicious has been playing with the metadata. | |
151 NOTREACHED(); | |
152 SetCorrupt(); | |
153 } | |
154 | |
155 // This is still safe to do even if corruption has been detected. | |
156 shared_meta_->cookie = kGlobalCookie; | |
157 shared_meta_->size = mem_size_; | |
158 shared_meta_->page_size = mem_page_; | |
159 shared_meta_->version = kGlobalVersion; | |
160 subtle::NoBarrier_Store(&shared_meta_->freeptr, sizeof(SharedMetadata)); | |
161 | |
162 // Set up the queue of iterable allocations. | |
163 shared_meta_->queue.size = sizeof(BlockHeader); | |
164 shared_meta_->queue.cookie = kBlockCookieQueue; | |
165 subtle::NoBarrier_Store(&shared_meta_->queue.next, REF_QUEUE); | |
166 subtle::NoBarrier_Store(&shared_meta_->tailptr, REF_QUEUE); | |
167 } else { | |
168 // The allocator is attaching to a previously initialized segment of | |
169 // memory. Make sure the embedded data matches what has been passed. | |
170 if (shared_meta_->size != mem_size_ || | |
171 shared_meta_->page_size != mem_page_) { | |
172 NOTREACHED(); | |
173 SetCorrupt(); | |
174 } | |
175 } | |
176 } | |
177 | |
178 SharedMemoryAllocator::~SharedMemoryAllocator() {} | |
179 | |
180 size_t SharedMemoryAllocator::GetAllocSize(Reference ref) { | |
181 BlockHeader* block = GetBlock(ref, 0, 0, false, false); | |
182 if (!block) | |
183 return 0; | |
184 int32_t size = block->size; | |
185 // Header was verified by GetBlock() but a malicious actor could change | |
186 // the value between there and here. Check it again. | |
187 if (size <= (int)sizeof(BlockHeader) || ref + size >= mem_size_) | |
188 return 0; | |
189 return (size_t)size - sizeof(BlockHeader); | |
190 } | |
191 | |
192 int32_t SharedMemoryAllocator::Allocate(size_t usize, uint32_t type_id) { | |
193 // Round up the requested size, plus header, to the next allocation alignment. | |
194 int32_t size = (int)usize + sizeof(BlockHeader); | |
195 size = (size + (kAllocAlignment - 1)) & ~(kAllocAlignment - 1); | |
196 if (usize > (size_t)std::numeric_limits<int32_t>::max() || | |
197 size <= (int)sizeof(BlockHeader) || size > mem_page_) { | |
198 NOTREACHED(); | |
199 return REF_NULL; | |
200 } | |
201 | |
202 // Allocation is lockless so we do all our caculation and then, if saving | |
203 // indicates a change has occurred since we started, scrap everything and | |
204 // start over. | |
205 for (;;) { | |
206 if (IsCorrupt()) | |
207 return REF_NULL; | |
208 | |
209 // Get the current start of unallocated memory. Other threads may | |
210 // update this at any time and cause us to retry these operations. | |
211 int32_t freeptr = subtle::NoBarrier_Load(&shared_meta_->freeptr); | |
212 if (freeptr + size > mem_size_) { | |
213 SetFlag(&shared_meta_->flags, kFlagFull); | |
214 return REF_NULL; | |
215 } | |
216 | |
217 // Get pointer to the "free" block. It doesn't even have a header; pass | |
218 // -sizeof(header) so accouting for that will yield an expected size of | |
219 // zero which is what will be stored at that location. If something | |
220 // has been allocated since the load of freeptr above, it is still safe | |
221 // as nothing will be written to that location until after the CAS below. | |
222 BlockHeader* block = GetBlock(freeptr, 0, 0, false, true); | |
223 if (!block) { | |
224 SetCorrupt(); | |
225 return REF_NULL; | |
226 } | |
227 | |
228 // An allocation cannot cross page boundaries. If it would, create a | |
229 // "wasted" block and begin again at the top of the next page. This | |
230 // area could just be left empty but we fill in the block header just | |
231 // for completeness sake. | |
chrisha
2015/11/09 16:53:51
What will your hardening code make of a partial bl
bcwhite
2015/11/09 18:02:05
There is code below to round-up an allocation size
| |
232 int32_t page_free = mem_page_ - freeptr % mem_page_; | |
233 if (size > page_free) { | |
234 int32_t new_freeptr = freeptr + page_free; | |
235 if (subtle::NoBarrier_CompareAndSwap( | |
236 &shared_meta_->freeptr, freeptr, new_freeptr) == freeptr) { | |
237 block->size = page_free; | |
238 block->cookie = kBlockCookieWasted; | |
239 } | |
240 continue; | |
241 } | |
242 | |
243 // Don't leave a slice at the end of a page too small for anything. This | |
244 // can result in an allocation up to two alignment-sizes greater than the | |
245 // minimum required by requested-size + header + alignment. | |
246 if (page_free - size < (int)(sizeof(BlockHeader) + kAllocAlignment)) | |
247 size = page_free; | |
248 | |
249 int32_t new_freeptr = freeptr + size; | |
250 if (new_freeptr > mem_size_) { | |
251 SetCorrupt(); | |
252 return REF_NULL; | |
253 } | |
254 | |
255 if (subtle::NoBarrier_CompareAndSwap( | |
256 &shared_meta_->freeptr, freeptr, new_freeptr) != freeptr) { | |
257 // Another thread must have completed an allocation while we were working. | |
258 // Try again. | |
259 continue; | |
260 } | |
261 | |
262 // Given that all memory was zeroed before ever being given to an instance | |
263 // of this class and given that we only allocate in a monotomic fashion | |
264 // going forward, it must be that the newly allocated block is completely | |
265 // full of zeros. If we find anything in the block header that is NOT a | |
266 // zero then something must have previously run amuck through memory, | |
267 // writing beyond the allocated space and into unallocated space. | |
268 if (block->size != 0 || | |
269 block->cookie != kBlockCookieFree || | |
270 block->type_id != 0 || | |
271 subtle::NoBarrier_Load(&block->next) != 0) { | |
272 SetCorrupt(); | |
273 return REF_NULL; | |
274 } | |
275 | |
276 block->size = size; | |
277 block->cookie = kBlockCookieAllocated; | |
278 block->type_id = type_id; | |
279 return freeptr; | |
280 } | |
281 } | |
282 | |
283 void SharedMemoryAllocator::GetMemoryInfo(MemoryInfo* meminfo) { | |
284 int32_t remaining = | |
285 mem_size_ - subtle::NoBarrier_Load(&shared_meta_->freeptr); | |
286 meminfo->total = mem_size_; | |
287 meminfo->free = IsCorrupt() ? 0 : remaining - sizeof(BlockHeader); | |
288 } | |
289 | |
290 void SharedMemoryAllocator::MakeIterable(Reference ref) { | |
291 if (IsCorrupt()) | |
292 return; | |
293 BlockHeader* block = GetBlock(ref, 0, 0, false, false); | |
294 if (!block) // invalid reference | |
295 return; | |
296 if (subtle::Acquire_Load(&block->next) != 0) // previously set iterable | |
297 return; | |
298 subtle::Release_Store(&block->next, REF_QUEUE); // will be tail block | |
299 | |
300 // Try to add this block to the tail of the queue. May take multiple tries. | |
301 int32_t tail; | |
302 for (;;) { | |
303 // Acquire the current tail-pointer released by previous call to this | |
304 // method and validate it. | |
305 tail = subtle::Acquire_Load(&shared_meta_->tailptr); | |
306 block = GetBlock(tail, 0, 0, true, false); | |
307 if (!block) { | |
308 SetCorrupt(); | |
309 return; | |
310 } | |
311 | |
312 // Try to insert the block at the tail of the queue. The tail node always | |
313 // has an existing value of REF_QUEUE; if that is not the value returned, | |
314 // another thread has acted in the meantime. | |
315 int32_t next = subtle::Release_CompareAndSwap(&block->next, REF_QUEUE, ref); | |
316 if (next == REF_QUEUE) { | |
317 // Update the tail pointer to the new offset. If the "else" clause did | |
318 // not exist, then this could be a simple Release_Store to set the new | |
319 // value but because it does, it's possible that other threads could add | |
320 // one or more nodes at the tail before reaching this point. We don't | |
321 // have to check the return value because it either operates correctly | |
322 // or the exact same operation has already been done (by the "else" | |
323 // clause). | |
324 subtle::Release_CompareAndSwap(&shared_meta_->tailptr, tail, ref); | |
325 return; | |
326 } else { | |
327 // In the unlikely case that a thread crashed or was killed between the | |
328 // update of "next" and the update of "tailptr", it is necessary to | |
329 // perform the operation that would have been done. There's no explicit | |
330 // check for crash/kill which means that this operation may also happen | |
331 // even when the other thread is in perfect working order which is what | |
332 // necessitates the CompareAndSwap above. | |
333 subtle::Release_CompareAndSwap(&shared_meta_->tailptr, tail, next); | |
334 } | |
335 } | |
336 } | |
337 | |
338 void SharedMemoryAllocator::CreateIterator(Iterator* state) { | |
339 state->last = REF_QUEUE; | |
340 state->niter = 0; | |
341 } | |
342 | |
343 int32_t SharedMemoryAllocator::GetNextIterable(Iterator* state, | |
344 uint32_t* type_id) { | |
345 const BlockHeader* block = GetBlock(state->last, 0, 0, true, false); | |
346 if (!block) // invalid iterator state | |
347 return REF_NULL; | |
348 | |
349 // The compiler and CPU can freely reorder all memory accesses on which | |
350 // there are no dependencies. It could, for example, move the load of | |
351 // "freeptr" above this point because there are no explicit dependencies | |
352 // between it and "next". If it did, however, then another block could | |
353 // be queued after that but before the following load meaning there is | |
354 // one more queued block than the future "detect loop by having more | |
355 // blocks that could fit before freeptr" will allow. | |
356 // | |
357 // By "acquiring" the "next" value here, it's synchronized to the enqueue | |
358 // of the node which in turn is synchronized to the allocation (which sets | |
359 // freeptr). Thus, the scenario above cannot happen. | |
360 int32_t next = subtle::Acquire_Load(&block->next); | |
361 block = GetBlock(next, 0, 0, false, false); | |
362 if (!block) // no next allocation in queue | |
363 return REF_NULL; | |
364 | |
365 // Memory corruption could cause a loop in the list. We need to detect | |
366 // that so as to not cause an infinite loop in the caller. We do this | |
367 // simply by making sure we don't iterate more than the absolute maximum | |
368 // number of allocations that could have been made. Callers are likely | |
369 // to loop multiple times before it is detected but at least it stops. | |
370 int32_t freeptr = std::min(subtle::Acquire_Load(&shared_meta_->freeptr), | |
371 mem_size_); | |
372 if (state->niter > freeptr / (sizeof(BlockHeader) + kAllocAlignment)) { | |
373 SetCorrupt(); | |
374 return REF_NULL; | |
375 } | |
376 | |
377 state->last = next; | |
378 state->niter++; | |
379 *type_id = block->type_id; | |
380 | |
381 return next; | |
382 } | |
383 | |
384 // The "corrupted" state is held both locally and globally (shared). The | |
385 // shared flag can't be trusted since a malicious actor could overwrite it. | |
386 // The local version is immune to foreign actors. Thus, if seen shared, | |
387 // copy it locally and, once known, always restore it globally. | |
388 void SharedMemoryAllocator::SetCorrupt() { | |
389 LOG(ERROR) << "Corruption detected in shared-memory segment."; | |
390 subtle::NoBarrier_Store(&corrupted_, 1); | |
391 SetFlag(&shared_meta_->flags, kFlagCorrupt); | |
392 } | |
393 | |
394 bool SharedMemoryAllocator::IsCorrupt() { | |
395 if (subtle::NoBarrier_Load(&corrupted_) || | |
396 CheckFlag(&shared_meta_->flags, kFlagCorrupt)) { | |
397 SetCorrupt(); // Make sure all indicators are set. | |
398 return true; | |
399 } | |
400 return false; | |
401 } | |
402 | |
403 bool SharedMemoryAllocator::IsFull() { | |
404 return CheckFlag(&shared_meta_->flags, kFlagFull); | |
405 } | |
406 | |
407 // Dereference a block |ref| and ensure that it's valid for the desired | |
408 // |type_id| and |size|. |special| indicates that we may try to access block | |
409 // headers not available to callers but still accessed by this module. By | |
410 // having internal dereferences go through this same function, the allocator | |
411 // is hardened against corruption. | |
412 SharedMemoryAllocator::BlockHeader* SharedMemoryAllocator::GetBlock( | |
413 Reference ref, | |
414 uint32_t type_id, | |
415 int32_t size, | |
416 bool queue_ok, | |
417 bool free_ok) { | |
418 // Validation of parameters. | |
419 if (ref % kAllocAlignment != 0) | |
420 return nullptr; | |
421 if (ref < (int)(queue_ok ? REF_QUEUE : sizeof(SharedMetadata))) | |
422 return nullptr; | |
423 size += sizeof(BlockHeader); | |
424 if (ref + size > mem_size_) | |
425 return nullptr; | |
426 | |
427 // Validation of referenced block-header. | |
428 if (!free_ok) { | |
429 int32_t freeptr = subtle::NoBarrier_Load(&shared_meta_->freeptr); | |
430 if (ref + size > freeptr) | |
431 return nullptr; | |
432 const BlockHeader* block = | |
433 reinterpret_cast<BlockHeader*>(mem_base_ + ref); | |
434 if (block->size < size) | |
435 return nullptr; | |
436 if (ref != REF_QUEUE && block->cookie != kBlockCookieAllocated) | |
437 return nullptr; | |
438 if (type_id != 0 && block->type_id != type_id) | |
439 return nullptr; | |
440 } | |
441 | |
442 // Return pointer to block data. | |
443 return reinterpret_cast<BlockHeader*>(mem_base_ + ref); | |
444 } | |
445 | |
446 void* SharedMemoryAllocator::GetBlockData(Reference ref, | |
447 uint32_t type_id, | |
448 int32_t size) { | |
449 DCHECK(size > 0); | |
450 BlockHeader* block = GetBlock(ref, type_id, size, false, false); | |
451 if (!block) | |
452 return nullptr; | |
453 return reinterpret_cast<char*>(block) + sizeof(BlockHeader); | |
454 } | |
455 | |
456 } // namespace base | |
OLD | NEW |