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 "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/rand_util.h" |
| 9 #include "base/strings/stringprintf.h" |
8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
9 | 11 |
10 namespace { | 12 namespace { |
11 | 13 |
12 // Returns the length of the memory section starting at the supplied address. | 14 // Returns the length of the memory section starting at the supplied address. |
13 size_t GetMemorySectionSize(void* address) { | 15 size_t GetMemorySectionSize(void* address) { |
14 MEMORY_BASIC_INFORMATION memory_info; | 16 MEMORY_BASIC_INFORMATION memory_info; |
15 if (!::VirtualQuery(address, &memory_info, sizeof(memory_info))) | 17 if (!::VirtualQuery(address, &memory_info, sizeof(memory_info))) |
16 return 0; | 18 return 0; |
17 return memory_info.RegionSize - (static_cast<char*>(address) - | 19 return memory_info.RegionSize - (static_cast<char*>(address) - |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 if (options.size == 0) | 109 if (options.size == 0) |
108 return false; | 110 return false; |
109 | 111 |
110 // Check maximum accounting for overflow. | 112 // Check maximum accounting for overflow. |
111 if (options.size > | 113 if (options.size > |
112 static_cast<size_t>(std::numeric_limits<int>::max()) - kSectionMask) | 114 static_cast<size_t>(std::numeric_limits<int>::max()) - kSectionMask) |
113 return false; | 115 return false; |
114 | 116 |
115 size_t rounded_size = (options.size + kSectionMask) & ~kSectionMask; | 117 size_t rounded_size = (options.size + kSectionMask) & ~kSectionMask; |
116 name_ = ASCIIToWide(options.name_deprecated == NULL ? "" : | 118 name_ = ASCIIToWide(options.name_deprecated == NULL ? "" : |
117 *options.name_deprecated); | 119 *options.name_deprecated); |
| 120 if (options.share_read_only && name_.empty()) { |
| 121 // Windows ignores DACLs on certain unnamed objects (like shared sections). |
| 122 // So, we generate a random name when we need to enforce read-only. |
| 123 uint64_t rand_values[4]; |
| 124 base::RandBytes(&rand_values, sizeof(rand_values)); |
| 125 name_ = base::StringPrintf(L"CrSharedMem_%016x%016x%016x%016x", |
| 126 rand_values[0], rand_values[1], |
| 127 rand_values[2], rand_values[3]); |
| 128 } |
118 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, | 129 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, |
119 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size), | 130 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size), name_.c_str()); |
120 name_.empty() ? NULL : name_.c_str()); | |
121 if (!mapped_file_) | 131 if (!mapped_file_) |
122 return false; | 132 return false; |
123 | 133 |
124 requested_size_ = options.size; | 134 requested_size_ = options.size; |
125 | 135 |
126 // Check if the shared memory pre-exists. | 136 // Check if the shared memory pre-exists. |
127 if (GetLastError() == ERROR_ALREADY_EXISTS) { | 137 if (GetLastError() == ERROR_ALREADY_EXISTS) { |
128 // If the file already existed, set requested_size_ to 0 to show that | 138 // If the file already existed, set requested_size_ to 0 to show that |
129 // we don't know the size. | 139 // we don't know the size. |
130 requested_size_ = 0; | 140 requested_size_ = 0; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 void SharedMemory::UnlockDeprecated() { | 262 void SharedMemory::UnlockDeprecated() { |
253 DCHECK(lock_ != NULL); | 263 DCHECK(lock_ != NULL); |
254 ReleaseMutex(lock_); | 264 ReleaseMutex(lock_); |
255 } | 265 } |
256 | 266 |
257 SharedMemoryHandle SharedMemory::handle() const { | 267 SharedMemoryHandle SharedMemory::handle() const { |
258 return mapped_file_; | 268 return mapped_file_; |
259 } | 269 } |
260 | 270 |
261 } // namespace base | 271 } // namespace base |
OLD | NEW |