OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <aclapi.h> | 7 #include <aclapi.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 // So, we generate a random name when we need to enforce read-only. | 220 // So, we generate a random name when we need to enforce read-only. |
221 uint64_t rand_values[4]; | 221 uint64_t rand_values[4]; |
222 RandBytes(&rand_values, sizeof(rand_values)); | 222 RandBytes(&rand_values, sizeof(rand_values)); |
223 name_ = StringPrintf(L"CrSharedMem_%016llx%016llx%016llx%016llx", | 223 name_ = StringPrintf(L"CrSharedMem_%016llx%016llx%016llx%016llx", |
224 rand_values[0], rand_values[1], | 224 rand_values[0], rand_values[1], |
225 rand_values[2], rand_values[3]); | 225 rand_values[2], rand_values[3]); |
226 } | 226 } |
227 DCHECK(!name_.empty()); | 227 DCHECK(!name_.empty()); |
228 shm_ = SharedMemoryHandle( | 228 shm_ = SharedMemoryHandle( |
229 CreateFileMappingWithReducedPermissions(&sa, rounded_size, name_.c_str()), | 229 CreateFileMappingWithReducedPermissions(&sa, rounded_size, name_.c_str()), |
230 UnguessableToken::Create()); | 230 rounded_size, UnguessableToken::Create()); |
231 if (!shm_.IsValid()) { | 231 if (!shm_.IsValid()) { |
232 // The error is logged within CreateFileMappingWithReducedPermissions(). | 232 // The error is logged within CreateFileMappingWithReducedPermissions(). |
233 return false; | 233 return false; |
234 } | 234 } |
235 | 235 |
236 requested_size_ = options.size; | 236 requested_size_ = options.size; |
237 | 237 |
238 // Check if the shared memory pre-exists. | 238 // Check if the shared memory pre-exists. |
239 if (GetLastError() == ERROR_ALREADY_EXISTS) { | 239 if (GetLastError() == ERROR_ALREADY_EXISTS) { |
240 // If the file already existed, set requested_size_ to 0 to show that | 240 // If the file already existed, set requested_size_ to 0 to show that |
(...skipping 26 matching lines...) Expand all Loading... |
267 read_only_ = read_only; | 267 read_only_ = read_only; |
268 | 268 |
269 // This form of sharing shared memory is deprecated. https://crbug.com/345734. | 269 // This form of sharing shared memory is deprecated. https://crbug.com/345734. |
270 // However, we can't get rid of it without a significant refactor because its | 270 // However, we can't get rid of it without a significant refactor because its |
271 // used to communicate between two versions of the same service process, very | 271 // used to communicate between two versions of the same service process, very |
272 // early in the life cycle. | 272 // early in the life cycle. |
273 // Technically, we should also pass the GUID from the original shared memory | 273 // Technically, we should also pass the GUID from the original shared memory |
274 // region. We don't do that - this means that we will overcount this memory, | 274 // region. We don't do that - this means that we will overcount this memory, |
275 // which thankfully isn't relevant since Chrome only communicates with a | 275 // which thankfully isn't relevant since Chrome only communicates with a |
276 // single version of the service process. | 276 // single version of the service process. |
| 277 // We pass the size |0|, which is a dummy size and wrong, but otherwise |
| 278 // harmless. |
277 shm_ = SharedMemoryHandle( | 279 shm_ = SharedMemoryHandle( |
278 OpenFileMapping(access, false, name_.empty() ? nullptr : name_.c_str()), | 280 OpenFileMapping(access, false, name_.empty() ? nullptr : name_.c_str()), |
279 UnguessableToken::Create()); | 281 0u, UnguessableToken::Create()); |
280 if (!shm_.IsValid()) | 282 if (!shm_.IsValid()) |
281 return false; | 283 return false; |
282 // If a name specified assume it's an external section. | 284 // If a name specified assume it's an external section. |
283 if (!name_.empty()) | 285 if (!name_.empty()) |
284 external_section_ = true; | 286 external_section_ = true; |
285 // Note: size_ is not set in this case. | 287 // Note: size_ is not set in this case. |
286 return true; | 288 return true; |
287 } | 289 } |
288 | 290 |
289 bool SharedMemory::MapAt(off_t offset, size_t bytes) { | 291 bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 return true; | 323 return true; |
322 } | 324 } |
323 | 325 |
324 SharedMemoryHandle SharedMemory::GetReadOnlyHandle() { | 326 SharedMemoryHandle SharedMemory::GetReadOnlyHandle() { |
325 HANDLE result; | 327 HANDLE result; |
326 ProcessHandle process = GetCurrentProcess(); | 328 ProcessHandle process = GetCurrentProcess(); |
327 if (!::DuplicateHandle(process, shm_.GetHandle(), process, &result, | 329 if (!::DuplicateHandle(process, shm_.GetHandle(), process, &result, |
328 FILE_MAP_READ | SECTION_QUERY, FALSE, 0)) { | 330 FILE_MAP_READ | SECTION_QUERY, FALSE, 0)) { |
329 return SharedMemoryHandle(); | 331 return SharedMemoryHandle(); |
330 } | 332 } |
331 SharedMemoryHandle handle = SharedMemoryHandle(result, shm_.GetGUID()); | 333 SharedMemoryHandle handle = |
| 334 SharedMemoryHandle(result, shm_.GetSize(), shm_.GetGUID()); |
332 handle.SetOwnershipPassesToIPC(true); | 335 handle.SetOwnershipPassesToIPC(true); |
333 return handle; | 336 return handle; |
334 } | 337 } |
335 | 338 |
336 void SharedMemory::Close() { | 339 void SharedMemory::Close() { |
337 if (shm_.IsValid()) { | 340 if (shm_.IsValid()) { |
338 shm_.Close(); | 341 shm_.Close(); |
339 shm_ = SharedMemoryHandle(); | 342 shm_ = SharedMemoryHandle(); |
340 } | 343 } |
341 } | 344 } |
342 | 345 |
343 SharedMemoryHandle SharedMemory::handle() const { | 346 SharedMemoryHandle SharedMemory::handle() const { |
344 return shm_; | 347 return shm_; |
345 } | 348 } |
346 | 349 |
347 SharedMemoryHandle SharedMemory::TakeHandle() { | 350 SharedMemoryHandle SharedMemory::TakeHandle() { |
348 SharedMemoryHandle handle(shm_); | 351 SharedMemoryHandle handle(shm_); |
349 handle.SetOwnershipPassesToIPC(true); | 352 handle.SetOwnershipPassesToIPC(true); |
350 shm_ = SharedMemoryHandle(); | 353 shm_ = SharedMemoryHandle(); |
351 memory_ = nullptr; | 354 memory_ = nullptr; |
352 mapped_size_ = 0; | 355 mapped_size_ = 0; |
353 return handle; | 356 return handle; |
354 } | 357 } |
355 | 358 |
356 } // namespace base | 359 } // namespace base |
OLD | NEW |