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