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

Side by Side Diff: base/memory/shared_memory_mac.cc

Issue 2555483002: Add POSIX shared memory support for Mac (Closed)
Patch Set: Readd Android ifdef. Created 4 years 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
OLDNEW
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
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_);
195 shm_ = SharedMemoryHandle(mapped_file);
196 return result;
142 } 197 }
143 198
144 bool SharedMemory::MapAt(off_t offset, size_t bytes) { 199 bool SharedMemory::MapAt(off_t offset, size_t bytes) {
145 if (!shm_.IsValid()) 200 if (!shm_.IsValid())
146 return false; 201 return false;
147 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) 202 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max()))
148 return false; 203 return false;
149 if (memory_) 204 if (memory_)
150 return false; 205 return false;
151 206
152 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_); 207 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_);
153 if (success) { 208 if (success) {
154 mapped_size_ = bytes; 209 mapped_size_ = bytes;
155 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & 210 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
156 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); 211 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
212 mapped_memory_mechanism_ = shm_.type_;
157 } else { 213 } else {
158 memory_ = NULL; 214 memory_ = NULL;
159 } 215 }
160 216
161 return success; 217 return success;
162 } 218 }
163 219
164 bool SharedMemory::Unmap() { 220 bool SharedMemory::Unmap() {
165 if (memory_ == NULL) 221 if (memory_ == NULL)
166 return false; 222 return false;
167 223
168 mach_vm_deallocate(mach_task_self(), 224 switch (mapped_memory_mechanism_) {
169 reinterpret_cast<mach_vm_address_t>(memory_), 225 case SharedMemoryHandle::POSIX:
170 mapped_size_); 226 munmap(memory_, mapped_size_);
227 break;
228 case SharedMemoryHandle::MACH:
229 mach_vm_deallocate(mach_task_self(),
230 reinterpret_cast<mach_vm_address_t>(memory_),
231 mapped_size_);
232 break;
233 }
234
171 memory_ = NULL; 235 memory_ = NULL;
172 mapped_size_ = 0; 236 mapped_size_ = 0;
173 return true; 237 return true;
174 } 238 }
175 239
176 SharedMemoryHandle SharedMemory::handle() const { 240 SharedMemoryHandle SharedMemory::handle() const {
177 return shm_; 241 switch (shm_.type_) {
242 case SharedMemoryHandle::POSIX:
243 return SharedMemoryHandle(shm_.file_descriptor_.fd);
244 case SharedMemoryHandle::MACH:
245 return shm_;
246 }
178 } 247 }
179 248
180 SharedMemoryHandle SharedMemory::TakeHandle() { 249 SharedMemoryHandle SharedMemory::TakeHandle() {
181 SharedMemoryHandle dup = DuplicateHandle(handle()); 250 SharedMemoryHandle dup = DuplicateHandle(handle());
182 Close(); 251 Close();
183 return dup; 252 return dup;
184 } 253 }
185 254
186 void SharedMemory::Close() { 255 void SharedMemory::Close() {
187 shm_.Close(); 256 shm_.Close();
188 shm_ = SharedMemoryHandle(); 257 shm_ = SharedMemoryHandle();
258 if (shm_.type_ == SharedMemoryHandle::POSIX) {
259 if (readonly_mapped_file_ > 0) {
260 if (IGNORE_EINTR(close(readonly_mapped_file_)) < 0)
261 PLOG(ERROR) << "close";
262 readonly_mapped_file_ = -1;
263 }
264 }
189 } 265 }
190 266
191 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, 267 bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
192 SharedMemoryHandle* new_handle, 268 SharedMemoryHandle* new_handle,
193 bool close_self, 269 bool close_self,
194 ShareMode share_mode) { 270 ShareMode share_mode) {
195 DCHECK(shm_.IsValid()); 271 if (shm_.type_ == SharedMemoryHandle::MACH) {
272 DCHECK(shm_.IsValid());
196 273
197 bool success = false; 274 bool success = false;
275 switch (share_mode) {
276 case SHARE_CURRENT_MODE:
277 *new_handle = shm_.Duplicate();
278 success = true;
279 break;
280 case SHARE_READONLY:
281 success = MakeMachSharedMemoryHandleReadOnly(new_handle, shm_, memory_);
282 break;
283 }
284
285 if (success)
286 new_handle->SetOwnershipPassesToIPC(true);
287
288 if (close_self) {
289 Unmap();
290 Close();
291 }
292
293 return success;
294 }
295
296 int handle_to_dup = -1;
198 switch (share_mode) { 297 switch (share_mode) {
199 case SHARE_CURRENT_MODE: 298 case SHARE_CURRENT_MODE:
200 *new_handle = shm_.Duplicate(); 299 handle_to_dup = shm_.file_descriptor_.fd;
201 success = true;
202 break; 300 break;
203 case SHARE_READONLY: 301 case SHARE_READONLY:
204 success = MakeMachSharedMemoryHandleReadOnly(new_handle, shm_, memory_); 302 // We could imagine re-opening the file from /dev/fd, but that can't make
303 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10
304 CHECK_GE(readonly_mapped_file_, 0);
305 handle_to_dup = readonly_mapped_file_;
205 break; 306 break;
206 } 307 }
207 308
208 if (success) 309 const int new_fd = HANDLE_EINTR(dup(handle_to_dup));
209 new_handle->SetOwnershipPassesToIPC(true); 310 if (new_fd < 0) {
311 if (close_self) {
312 Unmap();
313 Close();
314 }
315 DPLOG(ERROR) << "dup() failed.";
Robert Sesek 2016/12/07 16:39:42 The DPLOG should go ahead of the if because errno
lawrencewu 2016/12/07 19:44:26 Done.
316 return false;
317 }
318
319 new_handle->file_descriptor_.fd = new_fd;
320 new_handle->type_ = SharedMemoryHandle::POSIX;
210 321
211 if (close_self) { 322 if (close_self) {
212 Unmap(); 323 Unmap();
213 Close(); 324 Close();
214 } 325 }
215 326
216 return success; 327 return true;
217 } 328 }
218 329
219 } // namespace base 330 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698