OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/memory/shared_memory.h" | 5 #include "base/memory/shared_memory.h" |
6 | 6 |
7 #include <errno.h> | |
7 #include <mach/mach_vm.h> | 8 #include <mach/mach_vm.h> |
9 #include <stddef.h> | |
10 #include <sys/mman.h> | |
11 #include <sys/stat.h> | |
12 #include <unistd.h> | |
8 | 13 |
9 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
10 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
11 #include "base/logging.h" | 16 #include "base/logging.h" |
12 #include "base/mac/foundation_util.h" | |
13 #include "base/mac/mac_util.h" | 17 #include "base/mac/mac_util.h" |
14 #include "base/mac/scoped_mach_vm.h" | 18 #include "base/mac/scoped_mach_vm.h" |
19 #include "base/memory/shared_memory_helper.h" | |
15 #include "base/metrics/field_trial.h" | 20 #include "base/metrics/field_trial.h" |
16 #include "base/metrics/histogram_macros.h" | 21 #include "base/metrics/histogram_macros.h" |
22 #include "base/posix/eintr_wrapper.h" | |
23 #include "base/posix/safe_strerror.h" | |
17 #include "base/process/process_metrics.h" | 24 #include "base/process/process_metrics.h" |
18 #include "base/profiler/scoped_tracker.h" | |
19 #include "base/scoped_generic.h" | 25 #include "base/scoped_generic.h" |
20 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
27 #include "base/threading/thread_restrictions.h" | |
21 #include "build/build_config.h" | 28 #include "build/build_config.h" |
22 | 29 |
30 #if defined(OS_MACOSX) | |
31 #include "base/mac/foundation_util.h" | |
32 #endif // OS_MACOSX | |
33 | |
23 namespace base { | 34 namespace base { |
24 | 35 |
25 namespace { | 36 namespace { |
26 | 37 |
27 // Returns whether the operation succeeded. | 38 // Returns whether the operation succeeded. |
28 // |new_handle| is an output variable, populated on success. The caller takes | 39 // |new_handle| is an output variable, populated on success. The caller takes |
29 // ownership of the underlying memory object. | 40 // ownership of the underlying memory object. |
30 // |handle| is the handle to copy. | 41 // |handle| is the handle to copy. |
31 // If |handle| is already mapped, |mapped_addr| is its mapped location. | 42 // If |handle| is already mapped, |mapped_addr| is its mapped location. |
32 // Otherwise, |mapped_addr| should be |nullptr|. | 43 // Otherwise, |mapped_addr| should be |nullptr|. |
(...skipping 30 matching lines...) Expand all Loading... | |
63 if (kr != KERN_SUCCESS) | 74 if (kr != KERN_SUCCESS) |
64 return false; | 75 return false; |
65 | 76 |
66 *new_handle = SharedMemoryHandle(named_right, size, base::GetCurrentProcId()); | 77 *new_handle = SharedMemoryHandle(named_right, size, base::GetCurrentProcId()); |
67 return true; | 78 return true; |
68 } | 79 } |
69 | 80 |
70 } // namespace | 81 } // namespace |
71 | 82 |
72 SharedMemory::SharedMemory() | 83 SharedMemory::SharedMemory() |
73 : mapped_size_(0), memory_(NULL), read_only_(false), requested_size_(0) {} | 84 : mapped_memory_mechanism_(SharedMemoryHandle::MACH), |
85 readonly_mapped_file_(-1), | |
86 mapped_size_(0), | |
87 memory_(NULL), | |
88 read_only_(false), | |
89 requested_size_(0) {} | |
74 | 90 |
75 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) | 91 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) |
76 : shm_(handle), | 92 : shm_(handle), |
93 mapped_memory_mechanism_(SharedMemoryHandle::POSIX), | |
94 readonly_mapped_file_(-1), | |
77 mapped_size_(0), | 95 mapped_size_(0), |
78 memory_(NULL), | 96 memory_(NULL), |
79 read_only_(read_only), | 97 read_only_(read_only), |
80 requested_size_(0) {} | 98 requested_size_(0) {} |
81 | 99 |
82 SharedMemory::~SharedMemory() { | 100 SharedMemory::~SharedMemory() { |
83 Unmap(); | 101 Unmap(); |
84 Close(); | 102 Close(); |
85 } | 103 } |
86 | 104 |
87 // static | 105 // static |
88 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { | 106 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
89 return handle.IsValid(); | 107 return handle.IsValid(); |
90 } | 108 } |
91 | 109 |
92 // static | 110 // static |
93 SharedMemoryHandle SharedMemory::NULLHandle() { | 111 SharedMemoryHandle SharedMemory::NULLHandle() { |
94 return SharedMemoryHandle(); | 112 return SharedMemoryHandle(); |
95 } | 113 } |
96 | 114 |
97 // static | 115 // static |
98 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { | 116 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
99 handle.Close(); | 117 handle.Close(); |
100 } | 118 } |
101 | 119 |
102 // static | 120 // static |
103 size_t SharedMemory::GetHandleLimit() { | 121 size_t SharedMemory::GetHandleLimit() { |
104 // This should be effectively unlimited on OS X. | 122 return GetMaxFds(); |
105 return 10000; | |
106 } | 123 } |
107 | 124 |
108 // static | 125 // static |
109 SharedMemoryHandle SharedMemory::DuplicateHandle( | 126 SharedMemoryHandle SharedMemory::DuplicateHandle( |
110 const SharedMemoryHandle& handle) { | 127 const SharedMemoryHandle& handle) { |
111 return handle.Duplicate(); | 128 return handle.Duplicate(); |
112 } | 129 } |
113 | 130 |
131 // static | |
132 int SharedMemory::GetFdFromSharedMemoryHandle( | |
133 const SharedMemoryHandle& handle) { | |
134 return handle.file_descriptor_.fd; | |
135 } | |
136 | |
114 bool SharedMemory::CreateAndMapAnonymous(size_t size) { | 137 bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
115 return CreateAnonymous(size) && Map(size); | 138 return CreateAnonymous(size) && Map(size); |
116 } | 139 } |
117 | 140 |
118 // static | 141 // static |
119 bool SharedMemory::GetSizeFromSharedMemoryHandle( | 142 bool SharedMemory::GetSizeFromSharedMemoryHandle( |
120 const SharedMemoryHandle& handle, | 143 const SharedMemoryHandle& handle, |
121 size_t* size) { | 144 size_t* size) { |
122 return handle.GetSize(size); | 145 return handle.GetSize(size); |
123 } | 146 } |
124 | 147 |
125 // Chromium mostly only uses the unique/private shmem as specified by | 148 // Chromium mostly only uses the unique/private shmem as specified by |
126 // "name == L"". The exception is in the StatsTable. | 149 // "name == L"". The exception is in the StatsTable. |
127 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 150 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
128 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 | |
129 // is fixed. | |
130 tracked_objects::ScopedTracker tracking_profile1( | |
131 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
132 "466437 SharedMemory::Create::Start")); | |
133 DCHECK(!shm_.IsValid()); | 151 DCHECK(!shm_.IsValid()); |
134 if (options.size == 0) return false; | 152 if (options.size == 0) return false; |
135 | 153 |
136 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) | 154 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) |
137 return false; | 155 return false; |
138 | 156 |
139 shm_ = SharedMemoryHandle(options.size); | 157 if (options.type == SharedMemoryHandle::MACH) { |
158 shm_ = SharedMemoryHandle(options.size); | |
159 requested_size_ = options.size; | |
160 return shm_.IsValid(); | |
161 } | |
162 | |
163 // This function theoretically can block on the disk. Both profiling of real | |
164 // users and local instrumentation shows that this is a real problem. | |
165 // https://code.google.com/p/chromium/issues/detail?id=466437 | |
166 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
167 | |
168 ScopedFILE fp; | |
169 ScopedFD readonly_fd; | |
170 | |
171 FilePath path; | |
172 bool result = CreateAnonymousSharedMemory(options, &fp, &readonly_fd, &path); | |
173 if (!result) | |
174 return false; | |
175 | |
176 if (!fp) { | |
177 PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; | |
178 return false; | |
179 } | |
180 | |
181 // Get current size. | |
182 struct stat stat; | |
183 if (fstat(fileno(fp.get()), &stat) != 0) | |
184 return false; | |
185 const size_t current_size = stat.st_size; | |
186 if (current_size != options.size) { | |
187 if (HANDLE_EINTR(ftruncate(fileno(fp.get()), options.size)) != 0) | |
188 return false; | |
189 } | |
140 requested_size_ = options.size; | 190 requested_size_ = options.size; |
141 return shm_.IsValid(); | 191 |
192 int mapped_file = -1; | |
193 result = PrepareMapFile(std::move(fp), std::move(readonly_fd), &mapped_file, | |
194 &readonly_mapped_file_); | |
erikchen
2016/12/08 21:33:34
Do we still need readonly_mapped_file_?
lawrencewu
2016/12/08 23:06:30
I think so -- we're sharing the fd as readonly:
ht
| |
195 | |
196 shm_ = SharedMemoryHandle(FileDescriptor(mapped_file, false)); | |
197 return result; | |
142 } | 198 } |
143 | 199 |
144 bool SharedMemory::MapAt(off_t offset, size_t bytes) { | 200 bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
145 if (!shm_.IsValid()) | 201 if (!shm_.IsValid()) |
146 return false; | 202 return false; |
147 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) | 203 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
148 return false; | 204 return false; |
149 if (memory_) | 205 if (memory_) |
150 return false; | 206 return false; |
151 | 207 |
152 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_); | 208 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_); |
153 if (success) { | 209 if (success) { |
154 mapped_size_ = bytes; | 210 mapped_size_ = bytes; |
155 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 211 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
156 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 212 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
213 mapped_memory_mechanism_ = shm_.type_; | |
157 } else { | 214 } else { |
158 memory_ = NULL; | 215 memory_ = NULL; |
159 } | 216 } |
160 | 217 |
161 return success; | 218 return success; |
162 } | 219 } |
163 | 220 |
164 bool SharedMemory::Unmap() { | 221 bool SharedMemory::Unmap() { |
165 if (memory_ == NULL) | 222 if (memory_ == NULL) |
166 return false; | 223 return false; |
167 | 224 |
168 mach_vm_deallocate(mach_task_self(), | 225 switch (mapped_memory_mechanism_) { |
169 reinterpret_cast<mach_vm_address_t>(memory_), | 226 case SharedMemoryHandle::POSIX: |
170 mapped_size_); | 227 munmap(memory_, mapped_size_); |
228 break; | |
229 case SharedMemoryHandle::MACH: | |
230 mach_vm_deallocate(mach_task_self(), | |
231 reinterpret_cast<mach_vm_address_t>(memory_), | |
232 mapped_size_); | |
233 break; | |
234 } | |
235 | |
171 memory_ = NULL; | 236 memory_ = NULL; |
172 mapped_size_ = 0; | 237 mapped_size_ = 0; |
173 return true; | 238 return true; |
174 } | 239 } |
175 | 240 |
176 SharedMemoryHandle SharedMemory::handle() const { | 241 SharedMemoryHandle SharedMemory::handle() const { |
177 return shm_; | 242 switch (shm_.type_) { |
243 case SharedMemoryHandle::POSIX: | |
244 return SharedMemoryHandle( | |
245 FileDescriptor(shm_.file_descriptor_.fd, false)); | |
246 case SharedMemoryHandle::MACH: | |
247 return shm_; | |
248 } | |
178 } | 249 } |
179 | 250 |
180 SharedMemoryHandle SharedMemory::TakeHandle() { | 251 SharedMemoryHandle SharedMemory::TakeHandle() { |
181 SharedMemoryHandle dup = DuplicateHandle(handle()); | 252 SharedMemoryHandle dup = DuplicateHandle(handle()); |
182 Close(); | 253 Close(); |
183 return dup; | 254 return dup; |
184 } | 255 } |
185 | 256 |
186 void SharedMemory::Close() { | 257 void SharedMemory::Close() { |
187 shm_.Close(); | 258 shm_.Close(); |
188 shm_ = SharedMemoryHandle(); | 259 shm_ = SharedMemoryHandle(); |
260 if (shm_.type_ == SharedMemoryHandle::POSIX) { | |
261 if (readonly_mapped_file_ > 0) { | |
262 if (IGNORE_EINTR(close(readonly_mapped_file_)) < 0) | |
263 PLOG(ERROR) << "close"; | |
264 readonly_mapped_file_ = -1; | |
265 } | |
266 } | |
189 } | 267 } |
190 | 268 |
191 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, | 269 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
192 SharedMemoryHandle* new_handle, | 270 SharedMemoryHandle* new_handle, |
193 bool close_self, | 271 bool close_self, |
194 ShareMode share_mode) { | 272 ShareMode share_mode) { |
195 DCHECK(shm_.IsValid()); | 273 if (shm_.type_ == SharedMemoryHandle::MACH) { |
274 DCHECK(shm_.IsValid()); | |
196 | 275 |
197 bool success = false; | 276 bool success = false; |
277 switch (share_mode) { | |
278 case SHARE_CURRENT_MODE: | |
279 *new_handle = shm_.Duplicate(); | |
280 success = true; | |
281 break; | |
282 case SHARE_READONLY: | |
283 success = MakeMachSharedMemoryHandleReadOnly(new_handle, shm_, memory_); | |
284 break; | |
285 } | |
286 | |
287 if (success) | |
288 new_handle->SetOwnershipPassesToIPC(true); | |
289 | |
290 if (close_self) { | |
291 Unmap(); | |
292 Close(); | |
293 } | |
294 | |
295 return success; | |
296 } | |
297 | |
298 int handle_to_dup = -1; | |
198 switch (share_mode) { | 299 switch (share_mode) { |
199 case SHARE_CURRENT_MODE: | 300 case SHARE_CURRENT_MODE: |
200 *new_handle = shm_.Duplicate(); | 301 handle_to_dup = shm_.file_descriptor_.fd; |
201 success = true; | |
202 break; | 302 break; |
203 case SHARE_READONLY: | 303 case SHARE_READONLY: |
204 success = MakeMachSharedMemoryHandleReadOnly(new_handle, shm_, memory_); | 304 // We could imagine re-opening the file from /dev/fd, but that can't make |
305 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10 | |
306 CHECK_GE(readonly_mapped_file_, 0); | |
307 handle_to_dup = readonly_mapped_file_; | |
erikchen
2016/12/08 21:33:34
Do we still need both "readonly_mapped_file_" and
lawrencewu
2016/12/08 23:06:30
I think so -- the browser process uses readonly_ma
| |
205 break; | 308 break; |
206 } | 309 } |
207 | 310 |
208 if (success) | 311 const int new_fd = HANDLE_EINTR(dup(handle_to_dup)); |
209 new_handle->SetOwnershipPassesToIPC(true); | 312 if (new_fd < 0) { |
313 DPLOG(ERROR) << "dup() failed."; | |
314 if (close_self) { | |
erikchen
2016/12/08 21:33:34
This close_self logic is duplicated in three place
lawrencewu
2016/12/08 23:06:30
Done.
erikchen
2016/12/09 00:30:38
Perhaps you could pull all the logic for handle du
lawrencewu
2016/12/09 15:21:39
Yeah, that is cleaner. Done.
| |
315 Unmap(); | |
316 Close(); | |
317 } | |
318 return false; | |
319 } | |
320 | |
321 new_handle->file_descriptor_.fd = new_fd; | |
322 new_handle->type_ = SharedMemoryHandle::POSIX; | |
210 | 323 |
211 if (close_self) { | 324 if (close_self) { |
212 Unmap(); | 325 Unmap(); |
213 Close(); | 326 Close(); |
214 } | 327 } |
215 | 328 |
216 return success; | 329 return true; |
217 } | 330 } |
218 | 331 |
219 } // namespace base | 332 } // namespace base |
OLD | NEW |