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
~MemoryMappedFile()20 MemoryMappedFile::~MemoryMappedFile() {
21 CloseHandles();
22 }
23
24 #if !BUILDFLAG(IS_NACL)
Initialize(const FilePath & file_name,Access access)25 bool MemoryMappedFile::Initialize(const FilePath& file_name, Access access) {
26 if (IsValid())
27 return false;
28
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_COPY:
35 flags = File::FLAG_OPEN | File::FLAG_READ;
36 #if BUILDFLAG(IS_FUCHSIA)
37 // Fuchsia's mmap() implementation does not allow us to create a
38 // copy-on-write mapping of a file opened as read-only.
39 flags |= File::FLAG_WRITE;
40 #endif
41 break;
42 case READ_WRITE:
43 flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
44 break;
45 case READ_WRITE_EXTEND:
46 // Can't open with "extend" because no maximum size is known.
47 NOTREACHED();
48 #if BUILDFLAG(IS_WIN)
49 case READ_CODE_IMAGE:
50 flags |= File::FLAG_OPEN | File::FLAG_READ |
51 File::FLAG_WIN_EXCLUSIVE_WRITE | File::FLAG_WIN_EXECUTE;
52 break;
53 #endif
54 }
55 file_.Initialize(file_name, flags);
56
57 if (!file_.IsValid()) {
58 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe();
59 return false;
60 }
61
62 if (!MapFileRegionToMemory(Region::kWholeFile, access)) {
63 CloseHandles();
64 return false;
65 }
66
67 return true;
68 }
69
Initialize(File file,Access access)70 bool MemoryMappedFile::Initialize(File file, Access access) {
71 DCHECK_NE(READ_WRITE_EXTEND, access);
72 return Initialize(std::move(file), Region::kWholeFile, access);
73 }
74
Initialize(File file,const Region & region,Access access)75 bool MemoryMappedFile::Initialize(File file,
76 const Region& region,
77 Access access) {
78 switch (access) {
79 case READ_WRITE_EXTEND:
80 DCHECK(Region::kWholeFile != region);
81 {
82 CheckedNumeric<int64_t> region_end(region.offset);
83 region_end += region.size;
84 if (!region_end.IsValid()) {
85 DLOG(ERROR) << "Region bounds exceed maximum for base::File.";
86 return false;
87 }
88 }
89 [[fallthrough]];
90 case READ_ONLY:
91 case READ_WRITE:
92 case READ_WRITE_COPY:
93 // Ensure that the region values are valid.
94 if (region.offset < 0) {
95 DLOG(ERROR) << "Region bounds are not valid.";
96 return false;
97 }
98 break;
99 #if BUILDFLAG(IS_WIN)
100 case READ_CODE_IMAGE:
101 DCHECK(Region::kWholeFile == region);
102 break;
103 #endif
104 }
105
106 if (IsValid())
107 return false;
108
109 if (region != Region::kWholeFile)
110 DCHECK_GE(region.offset, 0);
111
112 file_ = std::move(file);
113
114 if (!MapFileRegionToMemory(region, access)) {
115 CloseHandles();
116 return false;
117 }
118
119 return true;
120 }
121
IsValid() const122 bool MemoryMappedFile::IsValid() const {
123 return !bytes_.empty();
124 }
125
126 // static
CalculateVMAlignedBoundaries(int64_t start,size_t size,int64_t * aligned_start,size_t * aligned_size,int32_t * offset)127 void MemoryMappedFile::CalculateVMAlignedBoundaries(int64_t start,
128 size_t size,
129 int64_t* aligned_start,
130 size_t* aligned_size,
131 int32_t* offset) {
132 // Sadly, on Windows, the mmap alignment is not just equal to the page size.
133 uint64_t mask = SysInfo::VMAllocationGranularity() - 1;
134 CHECK(IsValueInRangeForNumericType<int32_t>(mask));
135 *offset = static_cast<int32_t>(static_cast<uint64_t>(start) & mask);
136 *aligned_start = static_cast<int64_t>(static_cast<uint64_t>(start) & ~mask);
137 // The CHECK above means bit 31 is not set in `mask`, which in turn means
138 // *offset is positive. Therefore casting it to a size_t is safe.
139 *aligned_size =
140 (size + static_cast<size_t>(*offset) + static_cast<size_t>(mask)) & ~mask;
141 }
142 #endif // !BUILDFLAG(IS_NACL)
143
144 } // namespace base
145