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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 bool SharedMemory::GetSizeFromSharedMemoryHandle( | 137 bool SharedMemory::GetSizeFromSharedMemoryHandle( |
138 const SharedMemoryHandle& handle, | 138 const SharedMemoryHandle& handle, |
139 size_t* size) { | 139 size_t* size) { |
140 return handle.GetSize(size); | 140 return handle.GetSize(size); |
141 } | 141 } |
142 | 142 |
143 // Chromium mostly only uses the unique/private shmem as specified by | 143 // Chromium mostly only uses the unique/private shmem as specified by |
144 // "name == L"". The exception is in the StatsTable. | 144 // "name == L"". The exception is in the StatsTable. |
145 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 145 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
146 DCHECK(!shm_.IsValid()); | 146 DCHECK(!shm_.IsValid()); |
147 if (options.size == 0) return false; | 147 if (options.size == 0) { |
| 148 last_error_ = SharedMemoryError::BAD_PARAMS; |
| 149 return false; |
| 150 } |
148 | 151 |
149 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) | 152 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) { |
| 153 last_error_ = SharedMemoryError::BAD_PARAMS; |
150 return false; | 154 return false; |
| 155 } |
151 | 156 |
152 if (options.type == SharedMemoryHandle::MACH) { | 157 if (options.type == SharedMemoryHandle::MACH) { |
153 shm_ = SharedMemoryHandle(options.size, UnguessableToken::Create()); | 158 shm_ = SharedMemoryHandle(options.size, UnguessableToken::Create()); |
154 requested_size_ = options.size; | 159 requested_size_ = options.size; |
155 return shm_.IsValid(); | 160 return shm_.IsValid(); |
156 } | 161 } |
157 | 162 |
158 // This function theoretically can block on the disk. Both profiling of real | 163 // This function theoretically can block on the disk. Both profiling of real |
159 // users and local instrumentation shows that this is a real problem. | 164 // users and local instrumentation shows that this is a real problem. |
160 // https://code.google.com/p/chromium/issues/detail?id=466437 | 165 // https://code.google.com/p/chromium/issues/detail?id=466437 |
161 base::ThreadRestrictions::ScopedAllowIO allow_io; | 166 base::ThreadRestrictions::ScopedAllowIO allow_io; |
162 | 167 |
163 ScopedFILE fp; | 168 ScopedFILE fp; |
164 ScopedFD readonly_fd; | 169 ScopedFD readonly_fd; |
165 | 170 |
166 FilePath path; | 171 FilePath path; |
167 bool result = CreateAnonymousSharedMemory(options, &fp, &readonly_fd, &path); | 172 bool result = CreateAnonymousSharedMemory(options, &fp, &readonly_fd, &path, |
| 173 &last_error_); |
168 if (!result) | 174 if (!result) |
169 return false; | 175 return false; |
170 | 176 DCHECK(fp); // Should be guaranteed by CreateAnonymousSharedMemory(). |
171 if (!fp) { | |
172 PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; | |
173 return false; | |
174 } | |
175 | 177 |
176 // Get current size. | 178 // Get current size. |
177 struct stat stat; | 179 struct stat stat; |
178 if (fstat(fileno(fp.get()), &stat) != 0) | 180 if (fstat(fileno(fp.get()), &stat) != 0) { |
| 181 last_error_ = SharedMemoryError::STAT_FAILED; |
179 return false; | 182 return false; |
| 183 } |
180 const size_t current_size = stat.st_size; | 184 const size_t current_size = stat.st_size; |
181 if (current_size != options.size) { | 185 if (current_size != options.size) { |
182 if (HANDLE_EINTR(ftruncate(fileno(fp.get()), options.size)) != 0) | 186 if (HANDLE_EINTR(ftruncate(fileno(fp.get()), options.size)) != 0) { |
| 187 last_error_ = SharedMemoryError::TRUNCATE_FAILED; |
183 return false; | 188 return false; |
| 189 } |
184 } | 190 } |
185 requested_size_ = options.size; | 191 requested_size_ = options.size; |
186 | 192 |
187 int mapped_file = -1; | 193 int mapped_file = -1; |
188 int readonly_mapped_file = -1; | 194 int readonly_mapped_file = -1; |
189 result = PrepareMapFile(std::move(fp), std::move(readonly_fd), &mapped_file, | 195 result = PrepareMapFile(std::move(fp), std::move(readonly_fd), &mapped_file, |
190 &readonly_mapped_file); | 196 &readonly_mapped_file, &last_error_); |
191 shm_ = SharedMemoryHandle(FileDescriptor(mapped_file, false), | 197 shm_ = SharedMemoryHandle(FileDescriptor(mapped_file, false), |
192 UnguessableToken::Create()); | 198 UnguessableToken::Create()); |
193 readonly_shm_ = SharedMemoryHandle( | 199 readonly_shm_ = SharedMemoryHandle( |
194 FileDescriptor(readonly_mapped_file, false), shm_.GetGUID()); | 200 FileDescriptor(readonly_mapped_file, false), shm_.GetGUID()); |
195 return result; | 201 return result; |
196 } | 202 } |
197 | 203 |
198 bool SharedMemory::MapAt(off_t offset, size_t bytes) { | 204 bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
199 if (!shm_.IsValid()) | 205 if (!shm_.IsValid()) { |
| 206 last_error_ = SharedMemoryError::BAD_PARAMS; |
200 return false; | 207 return false; |
201 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) | 208 } |
| 209 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) { |
| 210 last_error_ = SharedMemoryError::BAD_PARAMS; |
202 return false; | 211 return false; |
203 if (memory_) | 212 } |
| 213 if (memory_) { |
| 214 last_error_ = SharedMemoryError::BAD_PARAMS; |
204 return false; | 215 return false; |
| 216 } |
205 | 217 |
206 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_); | 218 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_); |
207 if (success) { | 219 if (success) { |
208 mapped_size_ = bytes; | 220 mapped_size_ = bytes; |
209 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 221 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
210 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 222 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
211 mapped_memory_mechanism_ = shm_.type_; | 223 mapped_memory_mechanism_ = shm_.type_; |
212 } else { | 224 } else { |
| 225 last_error_ = SharedMemoryError::MMAP_FAILED; |
213 memory_ = NULL; | 226 memory_ = NULL; |
214 } | 227 } |
215 | 228 |
216 return success; | 229 return success; |
217 } | 230 } |
218 | 231 |
219 bool SharedMemory::Unmap() { | 232 bool SharedMemory::Unmap() { |
220 if (memory_ == NULL) | 233 if (memory_ == NULL) |
221 return false; | 234 return false; |
222 | 235 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 | 286 |
274 DCHECK(shm_.IsValid()); | 287 DCHECK(shm_.IsValid()); |
275 base::SharedMemoryHandle new_handle; | 288 base::SharedMemoryHandle new_handle; |
276 bool success = MakeMachSharedMemoryHandleReadOnly(&new_handle, shm_, memory_); | 289 bool success = MakeMachSharedMemoryHandleReadOnly(&new_handle, shm_, memory_); |
277 if (success) | 290 if (success) |
278 new_handle.SetOwnershipPassesToIPC(true); | 291 new_handle.SetOwnershipPassesToIPC(true); |
279 return new_handle; | 292 return new_handle; |
280 } | 293 } |
281 | 294 |
282 } // namespace base | 295 } // namespace base |
OLD | NEW |