memory_mapped_file.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2013 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. #include "base/files/memory_mapped_file.h"
  5. #include <utility>
  6. #include "base/files/file_path.h"
  7. #include "base/logging.h"
  8. #include "base/notreached.h"
  9. #include "base/numerics/safe_math.h"
  10. #include "base/system/sys_info.h"
  11. #include "build/build_config.h"
  12. namespace base {
  13. const MemoryMappedFile::Region MemoryMappedFile::Region::kWholeFile = {0, 0};
  14. bool MemoryMappedFile::Region::operator==(
  15. const MemoryMappedFile::Region& other) const {
  16. return other.offset == offset && other.size == size;
  17. }
  18. bool MemoryMappedFile::Region::operator!=(
  19. const MemoryMappedFile::Region& other) const {
  20. return other.offset != offset || other.size != size;
  21. }
  22. MemoryMappedFile::~MemoryMappedFile() {
  23. CloseHandles();
  24. }
  25. #if !BUILDFLAG(IS_NACL)
  26. bool MemoryMappedFile::Initialize(const FilePath& file_name, Access access) {
  27. if (IsValid())
  28. return false;
  29. uint32_t flags = 0;
  30. switch (access) {
  31. case READ_ONLY:
  32. flags = File::FLAG_OPEN | File::FLAG_READ;
  33. break;
  34. case READ_WRITE:
  35. flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
  36. break;
  37. case READ_WRITE_EXTEND:
  38. // Can't open with "extend" because no maximum size is known.
  39. NOTREACHED();
  40. break;
  41. #if BUILDFLAG(IS_WIN)
  42. case READ_CODE_IMAGE:
  43. flags |= File::FLAG_OPEN | File::FLAG_READ |
  44. File::FLAG_WIN_EXCLUSIVE_WRITE | File::FLAG_WIN_EXECUTE;
  45. break;
  46. #endif
  47. }
  48. file_.Initialize(file_name, flags);
  49. if (!file_.IsValid()) {
  50. DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe();
  51. return false;
  52. }
  53. if (!MapFileRegionToMemory(Region::kWholeFile, access)) {
  54. CloseHandles();
  55. return false;
  56. }
  57. return true;
  58. }
  59. bool MemoryMappedFile::Initialize(File file, Access access) {
  60. DCHECK_NE(READ_WRITE_EXTEND, access);
  61. return Initialize(std::move(file), Region::kWholeFile, access);
  62. }
  63. bool MemoryMappedFile::Initialize(File file,
  64. const Region& region,
  65. Access access) {
  66. switch (access) {
  67. case READ_WRITE_EXTEND:
  68. DCHECK(Region::kWholeFile != region);
  69. {
  70. CheckedNumeric<int64_t> region_end(region.offset);
  71. region_end += region.size;
  72. if (!region_end.IsValid()) {
  73. DLOG(ERROR) << "Region bounds exceed maximum for base::File.";
  74. return false;
  75. }
  76. }
  77. [[fallthrough]];
  78. case READ_ONLY:
  79. case READ_WRITE:
  80. // Ensure that the region values are valid.
  81. if (region.offset < 0) {
  82. DLOG(ERROR) << "Region bounds are not valid.";
  83. return false;
  84. }
  85. break;
  86. #if BUILDFLAG(IS_WIN)
  87. case READ_CODE_IMAGE:
  88. // Can't open with "READ_CODE_IMAGE", not supported outside Windows
  89. // or with a |region|.
  90. NOTREACHED();
  91. break;
  92. #endif
  93. }
  94. if (IsValid())
  95. return false;
  96. if (region != Region::kWholeFile)
  97. DCHECK_GE(region.offset, 0);
  98. file_ = std::move(file);
  99. if (!MapFileRegionToMemory(region, access)) {
  100. CloseHandles();
  101. return false;
  102. }
  103. return true;
  104. }
  105. bool MemoryMappedFile::IsValid() const {
  106. return data_ != nullptr;
  107. }
  108. // static
  109. void MemoryMappedFile::CalculateVMAlignedBoundaries(int64_t start,
  110. size_t size,
  111. int64_t* aligned_start,
  112. size_t* aligned_size,
  113. int32_t* offset) {
  114. // Sadly, on Windows, the mmap alignment is not just equal to the page size.
  115. uint64_t mask = SysInfo::VMAllocationGranularity() - 1;
  116. CHECK(IsValueInRangeForNumericType<int32_t>(mask));
  117. *offset = static_cast<int32_t>(static_cast<uint64_t>(start) & mask);
  118. *aligned_start = static_cast<int64_t>(static_cast<uint64_t>(start) & ~mask);
  119. // The DCHECK above means bit 31 is not set in `mask`, which in turn means
  120. // *offset is positive. Therefore casting it to a size_t is safe.
  121. *aligned_size =
  122. (size + static_cast<size_t>(*offset) + static_cast<size_t>(mask)) & ~mask;
  123. }
  124. #endif // !BUILDFLAG(IS_NACL)
  125. } // namespace base