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 27 matching lines...) Expand all Loading... |
60 mach_task_self(), reinterpret_cast<memory_object_size_t*>(&size), | 71 mach_task_self(), reinterpret_cast<memory_object_size_t*>(&size), |
61 reinterpret_cast<memory_object_offset_t>(temp_addr), VM_PROT_READ, | 72 reinterpret_cast<memory_object_offset_t>(temp_addr), VM_PROT_READ, |
62 &named_right, MACH_PORT_NULL); | 73 &named_right, MACH_PORT_NULL); |
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 |
| 81 |
70 } // namespace | 82 } // namespace |
71 | 83 |
72 SharedMemory::SharedMemory() | 84 SharedMemory::SharedMemory() |
73 : mapped_size_(0), memory_(NULL), read_only_(false), requested_size_(0) {} | 85 : mapped_memory_mechanism_(SharedMemoryHandle::MACH), |
| 86 readonly_mapped_file_(-1), |
| 87 mapped_size_(0), |
| 88 memory_(NULL), |
| 89 read_only_(false), |
| 90 requested_size_(0) {} |
74 | 91 |
75 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) | 92 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) |
76 : shm_(handle), | 93 : shm_(handle), |
| 94 mapped_memory_mechanism_(SharedMemoryHandle::POSIX), |
| 95 readonly_mapped_file_(-1), |
77 mapped_size_(0), | 96 mapped_size_(0), |
78 memory_(NULL), | 97 memory_(NULL), |
79 read_only_(read_only), | 98 read_only_(read_only), |
80 requested_size_(0) {} | 99 requested_size_(0) {} |
81 | 100 |
82 SharedMemory::~SharedMemory() { | 101 SharedMemory::~SharedMemory() { |
83 Unmap(); | 102 Unmap(); |
84 Close(); | 103 Close(); |
85 } | 104 } |
86 | 105 |
87 // static | 106 // static |
88 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { | 107 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
89 return handle.IsValid(); | 108 return handle.IsValid(); |
90 } | 109 } |
91 | 110 |
92 // static | 111 // static |
93 SharedMemoryHandle SharedMemory::NULLHandle() { | 112 SharedMemoryHandle SharedMemory::NULLHandle() { |
94 return SharedMemoryHandle(); | 113 return SharedMemoryHandle(); |
95 } | 114 } |
96 | 115 |
97 // static | 116 // static |
98 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { | 117 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
99 handle.Close(); | 118 handle.Close(); |
100 } | 119 } |
101 | 120 |
102 // static | 121 // static |
103 size_t SharedMemory::GetHandleLimit() { | 122 size_t SharedMemory::GetHandleLimit() { |
104 // This should be effectively unlimited on OS X. | 123 return GetMaxFds(); |
105 return 10000; | |
106 } | 124 } |
107 | 125 |
108 // static | 126 // static |
109 SharedMemoryHandle SharedMemory::DuplicateHandle( | 127 SharedMemoryHandle SharedMemory::DuplicateHandle( |
110 const SharedMemoryHandle& handle) { | 128 const SharedMemoryHandle& handle) { |
111 return handle.Duplicate(); | 129 return handle.Duplicate(); |
112 } | 130 } |
113 | 131 |
| 132 // static |
| 133 int SharedMemory::GetFdFromSharedMemoryHandle( |
| 134 const SharedMemoryHandle& handle) { |
| 135 return handle.file_descriptor_.fd; |
| 136 } |
| 137 |
114 bool SharedMemory::CreateAndMapAnonymous(size_t size) { | 138 bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
115 return CreateAnonymous(size) && Map(size); | 139 return CreateAnonymous(size) && Map(size); |
116 } | 140 } |
117 | 141 |
118 // static | 142 // static |
119 bool SharedMemory::GetSizeFromSharedMemoryHandle( | 143 bool SharedMemory::GetSizeFromSharedMemoryHandle( |
120 const SharedMemoryHandle& handle, | 144 const SharedMemoryHandle& handle, |
121 size_t* size) { | 145 size_t* size) { |
122 return handle.GetSize(size); | 146 return handle.GetSize(size); |
123 } | 147 } |
124 | 148 |
125 // Chromium mostly only uses the unique/private shmem as specified by | 149 // Chromium mostly only uses the unique/private shmem as specified by |
126 // "name == L"". The exception is in the StatsTable. | 150 // "name == L"". The exception is in the StatsTable. |
127 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 151 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()); | 152 DCHECK(!shm_.IsValid()); |
134 if (options.size == 0) return false; | 153 if (options.size == 0) return false; |
135 | 154 |
136 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) | 155 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) |
137 return false; | 156 return false; |
138 | 157 |
139 shm_ = SharedMemoryHandle(options.size); | 158 if (options.type == SharedMemoryHandle::MACH) { |
| 159 shm_ = SharedMemoryHandle(options.size); |
| 160 requested_size_ = options.size; |
| 161 return shm_.IsValid(); |
| 162 } |
| 163 |
| 164 // This function theoretically can block on the disk. Both profiling of real |
| 165 // users and local instrumentation shows that this is a real problem. |
| 166 // https://code.google.com/p/chromium/issues/detail?id=466437 |
| 167 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 168 |
| 169 ScopedFILE fp; |
| 170 ScopedFD readonly_fd; |
| 171 |
| 172 FilePath path; |
| 173 bool result = CreateAnonymousSharedMemory(options, &fp, &readonly_fd, &path); |
| 174 if (!result) |
| 175 return false; |
| 176 |
| 177 if (!fp) { |
| 178 PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; |
| 179 return false; |
| 180 } |
| 181 |
| 182 // Get current size. |
| 183 struct stat stat; |
| 184 if (fstat(fileno(fp.get()), &stat) != 0) |
| 185 return false; |
| 186 const size_t current_size = stat.st_size; |
| 187 if (current_size != options.size) { |
| 188 if (HANDLE_EINTR(ftruncate(fileno(fp.get()), options.size)) != 0) |
| 189 return false; |
| 190 } |
140 requested_size_ = options.size; | 191 requested_size_ = options.size; |
141 return shm_.IsValid(); | 192 |
| 193 int mapped_file = -1; |
| 194 result = PrepareMapFile(std::move(fp), std::move(readonly_fd), &mapped_file, |
| 195 &readonly_mapped_file_); |
| 196 |
| 197 shm_ = SharedMemoryHandle(FileDescriptor(mapped_file, false)); |
| 198 return result; |
142 } | 199 } |
143 | 200 |
144 bool SharedMemory::MapAt(off_t offset, size_t bytes) { | 201 bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
145 if (!shm_.IsValid()) | 202 if (!shm_.IsValid()) |
146 return false; | 203 return false; |
147 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) | 204 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
148 return false; | 205 return false; |
149 if (memory_) | 206 if (memory_) |
150 return false; | 207 return false; |
151 | 208 |
152 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_); | 209 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_); |
153 if (success) { | 210 if (success) { |
154 mapped_size_ = bytes; | 211 mapped_size_ = bytes; |
155 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 212 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
156 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 213 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
| 214 mapped_memory_mechanism_ = shm_.type_; |
157 } else { | 215 } else { |
158 memory_ = NULL; | 216 memory_ = NULL; |
159 } | 217 } |
160 | 218 |
161 return success; | 219 return success; |
162 } | 220 } |
163 | 221 |
164 bool SharedMemory::Unmap() { | 222 bool SharedMemory::Unmap() { |
165 if (memory_ == NULL) | 223 if (memory_ == NULL) |
166 return false; | 224 return false; |
167 | 225 |
168 mach_vm_deallocate(mach_task_self(), | 226 switch (mapped_memory_mechanism_) { |
169 reinterpret_cast<mach_vm_address_t>(memory_), | 227 case SharedMemoryHandle::POSIX: |
170 mapped_size_); | 228 munmap(memory_, mapped_size_); |
| 229 break; |
| 230 case SharedMemoryHandle::MACH: |
| 231 mach_vm_deallocate(mach_task_self(), |
| 232 reinterpret_cast<mach_vm_address_t>(memory_), |
| 233 mapped_size_); |
| 234 break; |
| 235 } |
| 236 |
171 memory_ = NULL; | 237 memory_ = NULL; |
172 mapped_size_ = 0; | 238 mapped_size_ = 0; |
173 return true; | 239 return true; |
174 } | 240 } |
175 | 241 |
176 SharedMemoryHandle SharedMemory::handle() const { | 242 SharedMemoryHandle SharedMemory::handle() const { |
177 return shm_; | 243 switch (shm_.type_) { |
| 244 case SharedMemoryHandle::POSIX: |
| 245 return SharedMemoryHandle( |
| 246 FileDescriptor(shm_.file_descriptor_.fd, false)); |
| 247 case SharedMemoryHandle::MACH: |
| 248 return shm_; |
| 249 } |
178 } | 250 } |
179 | 251 |
180 SharedMemoryHandle SharedMemory::TakeHandle() { | 252 SharedMemoryHandle SharedMemory::TakeHandle() { |
181 SharedMemoryHandle dup = DuplicateHandle(handle()); | 253 SharedMemoryHandle dup = DuplicateHandle(handle()); |
182 Close(); | 254 Close(); |
183 return dup; | 255 return dup; |
184 } | 256 } |
185 | 257 |
186 void SharedMemory::Close() { | 258 void SharedMemory::Close() { |
187 shm_.Close(); | 259 shm_.Close(); |
188 shm_ = SharedMemoryHandle(); | 260 shm_ = SharedMemoryHandle(); |
| 261 if (shm_.type_ == SharedMemoryHandle::POSIX) { |
| 262 if (readonly_mapped_file_ > 0) { |
| 263 if (IGNORE_EINTR(close(readonly_mapped_file_)) < 0) |
| 264 PLOG(ERROR) << "close"; |
| 265 readonly_mapped_file_ = -1; |
| 266 } |
| 267 } |
| 268 } |
| 269 |
| 270 bool SharedMemory::Share(SharedMemoryHandle* new_handle, ShareMode share_mode) { |
| 271 if (shm_.type_ == SharedMemoryHandle::MACH) { |
| 272 DCHECK(shm_.IsValid()); |
| 273 |
| 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 return success; |
| 289 } |
| 290 |
| 291 int handle_to_dup = -1; |
| 292 switch (share_mode) { |
| 293 case SHARE_CURRENT_MODE: |
| 294 handle_to_dup = shm_.file_descriptor_.fd; |
| 295 break; |
| 296 case SHARE_READONLY: |
| 297 // We could imagine re-opening the file from /dev/fd, but that can't make |
| 298 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10 |
| 299 CHECK_GE(readonly_mapped_file_, 0); |
| 300 handle_to_dup = readonly_mapped_file_; |
| 301 break; |
| 302 } |
| 303 |
| 304 const int new_fd = HANDLE_EINTR(dup(handle_to_dup)); |
| 305 if (new_fd < 0) { |
| 306 DPLOG(ERROR) << "dup() failed."; |
| 307 return false; |
| 308 } |
| 309 |
| 310 new_handle->file_descriptor_.fd = new_fd; |
| 311 new_handle->type_ = SharedMemoryHandle::POSIX; |
| 312 |
| 313 return true; |
189 } | 314 } |
190 | 315 |
191 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, | 316 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
192 SharedMemoryHandle* new_handle, | 317 SharedMemoryHandle* new_handle, |
193 bool close_self, | 318 bool close_self, |
194 ShareMode share_mode) { | 319 ShareMode share_mode) { |
195 DCHECK(shm_.IsValid()); | 320 bool success = Share(new_handle, share_mode); |
196 | |
197 bool success = false; | |
198 switch (share_mode) { | |
199 case SHARE_CURRENT_MODE: | |
200 *new_handle = shm_.Duplicate(); | |
201 success = true; | |
202 break; | |
203 case SHARE_READONLY: | |
204 success = MakeMachSharedMemoryHandleReadOnly(new_handle, shm_, memory_); | |
205 break; | |
206 } | |
207 | |
208 if (success) | |
209 new_handle->SetOwnershipPassesToIPC(true); | |
210 | |
211 if (close_self) { | 321 if (close_self) { |
212 Unmap(); | 322 Unmap(); |
213 Close(); | 323 Close(); |
214 } | 324 } |
215 | |
216 return success; | 325 return success; |
217 } | 326 } |
218 | 327 |
219 } // namespace base | 328 } // namespace base |
OLD | NEW |