Chromium Code Reviews| 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 <errno.h> |
| 8 #include <mach/mach_vm.h> | 8 #include <mach/mach_vm.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <sys/mman.h> | 10 #include <sys/mman.h> |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 // ownership of the underlying memory object. | 41 // ownership of the underlying memory object. |
| 42 // |handle| is the handle to copy. | 42 // |handle| is the handle to copy. |
| 43 // If |handle| is already mapped, |mapped_addr| is its mapped location. | 43 // If |handle| is already mapped, |mapped_addr| is its mapped location. |
| 44 // Otherwise, |mapped_addr| should be |nullptr|. | 44 // Otherwise, |mapped_addr| should be |nullptr|. |
| 45 bool MakeMachSharedMemoryHandleReadOnly(SharedMemoryHandle* new_handle, | 45 bool MakeMachSharedMemoryHandleReadOnly(SharedMemoryHandle* new_handle, |
| 46 SharedMemoryHandle handle, | 46 SharedMemoryHandle handle, |
| 47 void* mapped_addr) { | 47 void* mapped_addr) { |
| 48 if (!handle.IsValid()) | 48 if (!handle.IsValid()) |
| 49 return false; | 49 return false; |
| 50 | 50 |
| 51 size_t size; | 51 size_t size = handle.GetSize(); |
| 52 CHECK(handle.GetSize(&size)); | |
| 53 | 52 |
| 54 // Map if necessary. | 53 // Map if necessary. |
| 55 void* temp_addr = mapped_addr; | 54 void* temp_addr = mapped_addr; |
| 56 base::mac::ScopedMachVM scoper; | 55 base::mac::ScopedMachVM scoper; |
| 57 if (!temp_addr) { | 56 if (!temp_addr) { |
| 58 // Intentionally lower current prot and max prot to |VM_PROT_READ|. | 57 // Intentionally lower current prot and max prot to |VM_PROT_READ|. |
| 59 kern_return_t kr = mach_vm_map( | 58 kern_return_t kr = mach_vm_map( |
| 60 mach_task_self(), reinterpret_cast<mach_vm_address_t*>(&temp_addr), | 59 mach_task_self(), reinterpret_cast<mach_vm_address_t*>(&temp_addr), |
| 61 size, 0, VM_FLAGS_ANYWHERE, handle.GetMemoryObject(), 0, FALSE, | 60 size, 0, VM_FLAGS_ANYWHERE, handle.GetMemoryObject(), 0, FALSE, |
| 62 VM_PROT_READ, VM_PROT_READ, VM_INHERIT_NONE); | 61 VM_PROT_READ, VM_PROT_READ, VM_INHERIT_NONE); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 // static | 116 // static |
| 118 int SharedMemory::GetFdFromSharedMemoryHandle( | 117 int SharedMemory::GetFdFromSharedMemoryHandle( |
| 119 const SharedMemoryHandle& handle) { | 118 const SharedMemoryHandle& handle) { |
| 120 return handle.file_descriptor_.fd; | 119 return handle.file_descriptor_.fd; |
| 121 } | 120 } |
| 122 | 121 |
| 123 bool SharedMemory::CreateAndMapAnonymous(size_t size) { | 122 bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
| 124 return CreateAnonymous(size) && Map(size); | 123 return CreateAnonymous(size) && Map(size); |
| 125 } | 124 } |
| 126 | 125 |
| 127 // static | |
| 128 bool SharedMemory::GetSizeFromSharedMemoryHandle( | |
| 129 const SharedMemoryHandle& handle, | |
| 130 size_t* size) { | |
| 131 return handle.GetSize(size); | |
| 132 } | |
| 133 | |
| 134 // Chromium mostly only uses the unique/private shmem as specified by | 126 // Chromium mostly only uses the unique/private shmem as specified by |
| 135 // "name == L"". The exception is in the StatsTable. | 127 // "name == L"". The exception is in the StatsTable. |
| 136 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 128 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
| 137 DCHECK(!shm_.IsValid()); | 129 DCHECK(!shm_.IsValid()); |
| 138 if (options.size == 0) { | 130 if (options.size == 0) { |
| 139 last_error_ = SharedMemoryError::BAD_PARAMS; | 131 last_error_ = SharedMemoryError::BAD_PARAMS; |
| 140 return false; | 132 return false; |
| 141 } | 133 } |
| 142 | 134 |
| 143 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) { | 135 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 last_error_ = SharedMemoryError::TRUNCATE_FAILED; | 170 last_error_ = SharedMemoryError::TRUNCATE_FAILED; |
| 179 return false; | 171 return false; |
| 180 } | 172 } |
| 181 } | 173 } |
| 182 requested_size_ = options.size; | 174 requested_size_ = options.size; |
| 183 | 175 |
| 184 int mapped_file = -1; | 176 int mapped_file = -1; |
| 185 int readonly_mapped_file = -1; | 177 int readonly_mapped_file = -1; |
| 186 result = PrepareMapFile(std::move(fp), std::move(readonly_fd), &mapped_file, | 178 result = PrepareMapFile(std::move(fp), std::move(readonly_fd), &mapped_file, |
| 187 &readonly_mapped_file, &last_error_); | 179 &readonly_mapped_file, &last_error_); |
| 188 shm_ = SharedMemoryHandle(FileDescriptor(mapped_file, false), | 180 shm_ = SharedMemoryHandle(FileDescriptor(mapped_file, false), options.size, |
| 189 UnguessableToken::Create()); | 181 UnguessableToken::Create()); |
| 190 readonly_shm_ = SharedMemoryHandle( | 182 readonly_shm_ = |
| 191 FileDescriptor(readonly_mapped_file, false), shm_.GetGUID()); | 183 SharedMemoryHandle(FileDescriptor(readonly_mapped_file, false), |
| 184 options.size, shm_.GetGUID()); | |
| 192 return result; | 185 return result; |
| 193 } | 186 } |
| 194 | 187 |
| 195 bool SharedMemory::MapAt(off_t offset, size_t bytes) { | 188 bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
| 196 if (!shm_.IsValid()) { | 189 if (!shm_.IsValid()) { |
| 197 last_error_ = SharedMemoryError::BAD_PARAMS; | 190 last_error_ = SharedMemoryError::BAD_PARAMS; |
| 198 return false; | 191 return false; |
| 199 } | 192 } |
| 200 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) { | 193 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) { |
| 201 last_error_ = SharedMemoryError::BAD_PARAMS; | 194 last_error_ = SharedMemoryError::BAD_PARAMS; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 234 mapped_size_); | 227 mapped_size_); |
| 235 break; | 228 break; |
| 236 } | 229 } |
| 237 | 230 |
| 238 memory_ = NULL; | 231 memory_ = NULL; |
| 239 mapped_size_ = 0; | 232 mapped_size_ = 0; |
| 240 return true; | 233 return true; |
| 241 } | 234 } |
| 242 | 235 |
| 243 SharedMemoryHandle SharedMemory::handle() const { | 236 SharedMemoryHandle SharedMemory::handle() const { |
| 244 switch (shm_.type_) { | 237 return shm_; |
| 245 case SharedMemoryHandle::POSIX: | |
| 246 return SharedMemoryHandle(FileDescriptor(shm_.file_descriptor_.fd, false), | |
| 247 shm_.GetGUID()); | |
|
Nico
2017/05/15 22:13:36
Why is this changing?
erikchen
2017/05/15 22:57:18
The old code weas basically the same, expect it ch
| |
| 248 case SharedMemoryHandle::MACH: | |
| 249 return shm_; | |
| 250 } | |
| 251 } | 238 } |
| 252 | 239 |
| 253 SharedMemoryHandle SharedMemory::TakeHandle() { | 240 SharedMemoryHandle SharedMemory::TakeHandle() { |
| 254 SharedMemoryHandle dup = DuplicateHandle(handle()); | 241 SharedMemoryHandle dup = DuplicateHandle(handle()); |
| 255 Close(); | 242 Close(); |
| 256 return dup; | 243 return dup; |
| 257 } | 244 } |
| 258 | 245 |
| 259 void SharedMemory::Close() { | 246 void SharedMemory::Close() { |
| 260 shm_.Close(); | 247 shm_.Close(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 277 | 264 |
| 278 DCHECK(shm_.IsValid()); | 265 DCHECK(shm_.IsValid()); |
| 279 base::SharedMemoryHandle new_handle; | 266 base::SharedMemoryHandle new_handle; |
| 280 bool success = MakeMachSharedMemoryHandleReadOnly(&new_handle, shm_, memory_); | 267 bool success = MakeMachSharedMemoryHandleReadOnly(&new_handle, shm_, memory_); |
| 281 if (success) | 268 if (success) |
| 282 new_handle.SetOwnershipPassesToIPC(true); | 269 new_handle.SetOwnershipPassesToIPC(true); |
| 283 return new_handle; | 270 return new_handle; |
| 284 } | 271 } |
| 285 | 272 |
| 286 } // namespace base | 273 } // namespace base |
| OLD | NEW |