Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: base/files/memory_mapped_file_posix.cc

Issue 2860943005: Ensure that memory-maped files are fully realized. (Closed)
Patch Set: fix other build problems Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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/files/memory_mapped_file.h" 5 #include "base/files/memory_mapped_file.h"
6 6
7 #include <fcntl.h>
7 #include <stddef.h> 8 #include <stddef.h>
8 #include <stdint.h> 9 #include <stdint.h>
9 #include <sys/mman.h> 10 #include <sys/mman.h>
10 #include <sys/stat.h> 11 #include <sys/stat.h>
11 #include <unistd.h> 12 #include <unistd.h>
12 13
13 #include "base/logging.h" 14 #include "base/logging.h"
14 #include "base/threading/thread_restrictions.h" 15 #include "base/threading/thread_restrictions.h"
15 #include "build/build_config.h" 16 #include "build/build_config.h"
16 17
18 #if defined(OS_ANDROID)
19 #include <android/api-level.h>
20 #endif
21
17 namespace base { 22 namespace base {
18 23
19 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0) { 24 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0) {
20 } 25 }
21 26
22 #if !defined(OS_NACL) 27 #if !defined(OS_NACL)
23 bool MemoryMappedFile::MapFileRegionToMemory( 28 bool MemoryMappedFile::MapFileRegionToMemory(
24 const MemoryMappedFile::Region& region, 29 const MemoryMappedFile::Region& region,
25 Access access) { 30 Access access) {
26 ThreadRestrictions::AssertIOAllowed(); 31 ThreadRestrictions::AssertIOAllowed();
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 map_start = static_cast<off_t>(aligned_start); 69 map_start = static_cast<off_t>(aligned_start);
65 map_size = static_cast<size_t>(aligned_size); 70 map_size = static_cast<size_t>(aligned_size);
66 length_ = static_cast<size_t>(region.size); 71 length_ = static_cast<size_t>(region.size);
67 } 72 }
68 73
69 int flags = 0; 74 int flags = 0;
70 switch (access) { 75 switch (access) {
71 case READ_ONLY: 76 case READ_ONLY:
72 flags |= PROT_READ; 77 flags |= PROT_READ;
73 break; 78 break;
79
74 case READ_WRITE: 80 case READ_WRITE:
75 flags |= PROT_READ | PROT_WRITE; 81 flags |= PROT_READ | PROT_WRITE;
76 break; 82 break;
83
77 case READ_WRITE_EXTEND: 84 case READ_WRITE_EXTEND:
85 flags |= PROT_READ | PROT_WRITE;
86
78 // POSIX won't auto-extend the file when it is written so it must first 87 // POSIX won't auto-extend the file when it is written so it must first
79 // be explicitly extended to the maximum size. Zeros will fill the new 88 // be explicitly extended to the maximum size. Zeros will fill the new
80 // space. 89 // space.
81 auto file_len = file_.GetLength(); 90 const int64_t original_file_len = file_.GetLength();
82 if (file_len < 0) { 91 if (original_file_len < 0) {
83 DPLOG(ERROR) << "fstat " << file_.GetPlatformFile(); 92 DPLOG(ERROR) << "fstat " << file_.GetPlatformFile();
84 return false; 93 return false;
85 } 94 }
86 file_.SetLength(std::max(file_len, region.offset + region.size)); 95
87 flags |= PROT_READ | PROT_WRITE; 96 // Increase the actual length of the file, if necessary. This can fail if
97 // the disk is full and the OS doesn't support sparse files.
98 if (!file_.SetLength(
99 std::max(original_file_len, region.offset + region.size))) {
100 DPLOG(ERROR) << "ftruncate " << file_.GetPlatformFile();
101 return false;
102 }
103
104 // Realize the extent of the file so that it can't fail (and crash) later
105 // when trying to write to a memory page that can't be created. This can
106 // fail if the disk is full and the file is sparse.
107 //
108 // Only Android API>=21 supports the fallocate call. Older versions need
109 // to manually extend the file by writing zeros at block intervals.
110 //
111 // Mac OSX doesn't support this call but the primary filesystem doesn't
112 // support sparse files so is unneeded.
113 bool do_manual_extension = false;
114
115 #if defined(OS_ANDROID) && __ANDROID_API__ < 21
116 do_manual_extension = true;
117 #elif !defined(OS_MACOSX)
118 if (posix_fallocate(file_.GetPlatformFile(), region.offset,
119 region.size) != 0) {
120 DPLOG(ERROR) << "posix_fallocate " << file_.GetPlatformFile();
121 // This can fail because the filesystem doesn't support it so don't
122 // give up just yet. Try the manual method below.
123 do_manual_extension = true;
124 }
125 #endif
126
127 // Manually realize the entire file by writing bytes to it at intervals.
128 if (do_manual_extension) {
129 int64_t block_size = 1024; // Start with something safe.
130 struct stat statbuf;
131 if (fstat(file_.GetPlatformFile(), &statbuf) == 0)
132 block_size = statbuf.st_blksize;
133 const off_t map_end = map_start + static_cast<off_t>(map_size);
134 for (off_t i = map_start; i < map_end; i += block_size) {
135 if (i > original_file_len) {
Mark Mentovai 2017/05/05 18:31:44 I don’t see anything here that requires file_ to h
bcwhite 2017/05/08 14:15:35 Correct. It's making the assumption that whatever
136 if (pwrite(file_.GetPlatformFile(), "", 1, i) != 1)
137 return false;
138 }
139 }
140 }
141
88 break; 142 break;
89 } 143 }
144
90 data_ = static_cast<uint8_t*>(mmap(NULL, map_size, flags, MAP_SHARED, 145 data_ = static_cast<uint8_t*>(mmap(NULL, map_size, flags, MAP_SHARED,
91 file_.GetPlatformFile(), map_start)); 146 file_.GetPlatformFile(), map_start));
92 if (data_ == MAP_FAILED) { 147 if (data_ == MAP_FAILED) {
93 DPLOG(ERROR) << "mmap " << file_.GetPlatformFile(); 148 DPLOG(ERROR) << "mmap " << file_.GetPlatformFile();
94 return false; 149 return false;
95 } 150 }
96 151
97 data_ += data_offset; 152 data_ += data_offset;
98 return true; 153 return true;
99 } 154 }
100 #endif 155 #endif
101 156
102 void MemoryMappedFile::CloseHandles() { 157 void MemoryMappedFile::CloseHandles() {
103 ThreadRestrictions::AssertIOAllowed(); 158 ThreadRestrictions::AssertIOAllowed();
104 159
105 if (data_ != NULL) 160 if (data_ != NULL)
106 munmap(data_, length_); 161 munmap(data_, length_);
107 file_.Close(); 162 file_.Close();
108 163
109 data_ = NULL; 164 data_ = NULL;
110 length_ = 0; 165 length_ = 0;
111 } 166 }
112 167
113 } // namespace base 168 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698