xref: /aosp_15_r20/external/cronet/base/files/memory_mapped_file.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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       break;
49 #if BUILDFLAG(IS_WIN)
50     case READ_CODE_IMAGE:
51       flags |= File::FLAG_OPEN | File::FLAG_READ |
52                File::FLAG_WIN_EXCLUSIVE_WRITE | File::FLAG_WIN_EXECUTE;
53       break;
54 #endif
55   }
56   file_.Initialize(file_name, flags);
57 
58   if (!file_.IsValid()) {
59     DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe();
60     return false;
61   }
62 
63   if (!MapFileRegionToMemory(Region::kWholeFile, access)) {
64     CloseHandles();
65     return false;
66   }
67 
68   return true;
69 }
70 
Initialize(File file,Access access)71 bool MemoryMappedFile::Initialize(File file, Access access) {
72   DCHECK_NE(READ_WRITE_EXTEND, access);
73   return Initialize(std::move(file), Region::kWholeFile, access);
74 }
75 
Initialize(File file,const Region & region,Access access)76 bool MemoryMappedFile::Initialize(File file,
77                                   const Region& region,
78                                   Access access) {
79   switch (access) {
80     case READ_WRITE_EXTEND:
81       DCHECK(Region::kWholeFile != region);
82       {
83         CheckedNumeric<int64_t> region_end(region.offset);
84         region_end += region.size;
85         if (!region_end.IsValid()) {
86           DLOG(ERROR) << "Region bounds exceed maximum for base::File.";
87           return false;
88         }
89       }
90       [[fallthrough]];
91     case READ_ONLY:
92     case READ_WRITE:
93     case READ_WRITE_COPY:
94       // Ensure that the region values are valid.
95       if (region.offset < 0) {
96         DLOG(ERROR) << "Region bounds are not valid.";
97         return false;
98       }
99       break;
100 #if BUILDFLAG(IS_WIN)
101     case READ_CODE_IMAGE:
102       // Can't open with "READ_CODE_IMAGE", not supported outside Windows
103       // or with a |region|.
104       NOTREACHED();
105       break;
106 #endif
107   }
108 
109   if (IsValid())
110     return false;
111 
112   if (region != Region::kWholeFile)
113     DCHECK_GE(region.offset, 0);
114 
115   file_ = std::move(file);
116 
117   if (!MapFileRegionToMemory(region, access)) {
118     CloseHandles();
119     return false;
120   }
121 
122   return true;
123 }
124 
IsValid() const125 bool MemoryMappedFile::IsValid() const {
126   return !bytes_.empty();
127 }
128 
129 // static
CalculateVMAlignedBoundaries(int64_t start,size_t size,int64_t * aligned_start,size_t * aligned_size,int32_t * offset)130 void MemoryMappedFile::CalculateVMAlignedBoundaries(int64_t start,
131                                                     size_t size,
132                                                     int64_t* aligned_start,
133                                                     size_t* aligned_size,
134                                                     int32_t* offset) {
135   // Sadly, on Windows, the mmap alignment is not just equal to the page size.
136   uint64_t mask = SysInfo::VMAllocationGranularity() - 1;
137   CHECK(IsValueInRangeForNumericType<int32_t>(mask));
138   *offset = static_cast<int32_t>(static_cast<uint64_t>(start) & mask);
139   *aligned_start = static_cast<int64_t>(static_cast<uint64_t>(start) & ~mask);
140   // The CHECK above means bit 31 is not set in `mask`, which in turn means
141   // *offset is positive.  Therefore casting it to a size_t is safe.
142   *aligned_size =
143       (size + static_cast<size_t>(*offset) + static_cast<size_t>(mask)) & ~mask;
144 }
145 #endif  // !BUILDFLAG(IS_NACL)
146 
147 }  // namespace base
148