OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/metrics/persistent_memory_allocator.h" | 5 #include "base/metrics/persistent_memory_allocator.h" |
6 | 6 |
7 #include <assert.h> | 7 #include <assert.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 | 9 |
10 #include "base/files/memory_mapped_file.h" | 10 #include "base/files/memory_mapped_file.h" |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 return std::min(shared_meta()->freeptr.load(), mem_size_); | 267 return std::min(shared_meta()->freeptr.load(), mem_size_); |
268 } | 268 } |
269 | 269 |
270 size_t PersistentMemoryAllocator::GetAllocSize(Reference ref) const { | 270 size_t PersistentMemoryAllocator::GetAllocSize(Reference ref) const { |
271 const volatile BlockHeader* const block = GetBlock(ref, 0, 0, false, false); | 271 const volatile BlockHeader* const block = GetBlock(ref, 0, 0, false, false); |
272 if (!block) | 272 if (!block) |
273 return 0; | 273 return 0; |
274 uint32_t size = block->size; | 274 uint32_t size = block->size; |
275 // Header was verified by GetBlock() but a malicious actor could change | 275 // Header was verified by GetBlock() but a malicious actor could change |
276 // the value between there and here. Check it again. | 276 // the value between there and here. Check it again. |
277 if (size <= sizeof(BlockHeader) || ref + size >= mem_size_) { | 277 if (size <= sizeof(BlockHeader) || ref + size > mem_size_) { |
278 SetCorrupt(); | 278 SetCorrupt(); |
279 return 0; | 279 return 0; |
280 } | 280 } |
281 return size - sizeof(BlockHeader); | 281 return size - sizeof(BlockHeader); |
282 } | 282 } |
283 | 283 |
284 uint32_t PersistentMemoryAllocator::GetType(Reference ref) const { | 284 uint32_t PersistentMemoryAllocator::GetType(Reference ref) const { |
285 const volatile BlockHeader* const block = GetBlock(ref, 0, 0, false, false); | 285 const volatile BlockHeader* const block = GetBlock(ref, 0, 0, false, false); |
286 if (!block) | 286 if (!block) |
287 return 0; | 287 return 0; |
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
661 FilePersistentMemoryAllocator::~FilePersistentMemoryAllocator() { | 661 FilePersistentMemoryAllocator::~FilePersistentMemoryAllocator() { |
662 } | 662 } |
663 | 663 |
664 // static | 664 // static |
665 bool FilePersistentMemoryAllocator::IsFileAcceptable( | 665 bool FilePersistentMemoryAllocator::IsFileAcceptable( |
666 const MemoryMappedFile& file) { | 666 const MemoryMappedFile& file) { |
667 return IsMemoryAcceptable(file.data(), file.length(), 0, true); | 667 return IsMemoryAcceptable(file.data(), file.length(), 0, true); |
668 } | 668 } |
669 | 669 |
670 } // namespace base | 670 } // namespace base |
OLD | NEW |