[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 99873aa | 2013-03-29 17:46:23 | [diff] [blame] | 5 | #include "base/memory/shared_memory.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | |
[email protected] | 6d6797eb | 2014-08-07 22:07:43 | [diff] [blame] | 7 | #include <aclapi.h> |
| 8 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 6d6797eb | 2014-08-07 22:07:43 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | dc84fcc | 2014-07-24 11:42:59 | [diff] [blame] | 11 | #include "base/rand_util.h" |
| 12 | #include "base/strings/stringprintf.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 15 | namespace { |
| 16 | |
| 17 | // Returns the length of the memory section starting at the supplied address. |
| 18 | size_t GetMemorySectionSize(void* address) { |
| 19 | MEMORY_BASIC_INFORMATION memory_info; |
| 20 | if (!::VirtualQuery(address, &memory_info, sizeof(memory_info))) |
| 21 | return 0; |
| 22 | return memory_info.RegionSize - (static_cast<char*>(address) - |
| 23 | static_cast<char*>(memory_info.AllocationBase)); |
| 24 | } |
| 25 | |
| 26 | } // namespace. |
| 27 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 28 | namespace base { |
| 29 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | SharedMemory::SharedMemory() |
| 31 | : mapped_file_(NULL), |
sammc | 5648c85 | 2015-07-02 01:25:00 | [diff] [blame^] | 32 | mapped_size_(0), |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 33 | memory_(NULL), |
| 34 | read_only_(false), |
erikchen | 893dadc6 | 2015-06-18 21:48:33 | [diff] [blame] | 35 | requested_size_(0) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 36 | } |
| 37 | |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 38 | SharedMemory::SharedMemory(const std::wstring& name) |
sammc | 5648c85 | 2015-07-02 01:25:00 | [diff] [blame^] | 39 | : name_(name), |
| 40 | mapped_file_(NULL), |
| 41 | mapped_size_(0), |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 42 | memory_(NULL), |
| 43 | read_only_(false), |
sammc | 5648c85 | 2015-07-02 01:25:00 | [diff] [blame^] | 44 | requested_size_(0) { |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 45 | } |
| 46 | |
scottmg | d19b4f7 | 2015-06-19 22:51:00 | [diff] [blame] | 47 | SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 48 | : mapped_file_(handle), |
sammc | 5648c85 | 2015-07-02 01:25:00 | [diff] [blame^] | 49 | mapped_size_(0), |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 50 | memory_(NULL), |
| 51 | read_only_(read_only), |
sammc | 5648c85 | 2015-07-02 01:25:00 | [diff] [blame^] | 52 | requested_size_(0) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 53 | } |
| 54 | |
scottmg | d19b4f7 | 2015-06-19 22:51:00 | [diff] [blame] | 55 | SharedMemory::SharedMemory(const SharedMemoryHandle& handle, |
erikchen | 893dadc6 | 2015-06-18 21:48:33 | [diff] [blame] | 56 | bool read_only, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 57 | ProcessHandle process) |
| 58 | : mapped_file_(NULL), |
sammc | 5648c85 | 2015-07-02 01:25:00 | [diff] [blame^] | 59 | mapped_size_(0), |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 60 | memory_(NULL), |
| 61 | read_only_(read_only), |
sammc | 5648c85 | 2015-07-02 01:25:00 | [diff] [blame^] | 62 | requested_size_(0) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 63 | ::DuplicateHandle(process, handle, |
| 64 | GetCurrentProcess(), &mapped_file_, |
[email protected] | 5c74461 | 2013-10-30 15:17:05 | [diff] [blame] | 65 | read_only_ ? FILE_MAP_READ : FILE_MAP_READ | |
| 66 | FILE_MAP_WRITE, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 67 | FALSE, 0); |
| 68 | } |
| 69 | |
| 70 | SharedMemory::~SharedMemory() { |
jbauman | 569918b | 2014-12-10 22:07:20 | [diff] [blame] | 71 | Unmap(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 72 | Close(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 75 | // static |
| 76 | bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
| 77 | return handle != NULL; |
| 78 | } |
| 79 | |
[email protected] | 76aac1e | 2009-03-16 16:45:36 | [diff] [blame] | 80 | // static |
| 81 | SharedMemoryHandle SharedMemory::NULLHandle() { |
| 82 | return NULL; |
| 83 | } |
| 84 | |
[email protected] | b0af04c | 2009-05-18 17:46:31 | [diff] [blame] | 85 | // static |
| 86 | void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
| 87 | DCHECK(handle != NULL); |
| 88 | ::CloseHandle(handle); |
| 89 | } |
| 90 | |
[email protected] | c14eda9 | 2013-05-09 23:15:40 | [diff] [blame] | 91 | // static |
| 92 | size_t SharedMemory::GetHandleLimit() { |
| 93 | // Rounded down from value reported here: |
| 94 | // http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx |
| 95 | return static_cast<size_t>(1 << 23); |
| 96 | } |
| 97 | |
erikchen | 2096f62 | 2015-06-03 00:26:59 | [diff] [blame] | 98 | // static |
| 99 | SharedMemoryHandle SharedMemory::DuplicateHandle( |
erikchen | 8539d85 | 2015-05-30 01:49:19 | [diff] [blame] | 100 | const SharedMemoryHandle& handle) { |
erikchen | 2096f62 | 2015-06-03 00:26:59 | [diff] [blame] | 101 | ProcessHandle process = GetCurrentProcess(); |
| 102 | SharedMemoryHandle duped_handle; |
| 103 | BOOL success = ::DuplicateHandle(process, handle, process, &duped_handle, 0, |
| 104 | FALSE, DUPLICATE_SAME_ACCESS); |
| 105 | if (success) |
| 106 | return duped_handle; |
| 107 | return NULLHandle(); |
erikchen | 8539d85 | 2015-05-30 01:49:19 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | 374f1a8 | 2013-01-10 02:16:24 | [diff] [blame] | 110 | bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 111 | return CreateAnonymous(size) && Map(size); |
| 112 | } |
| 113 | |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 114 | bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 115 | // TODO(bsy,sehr): crbug.com/210609 NaCl forces us to round up 64k here, |
| 116 | // wasting 32k per mapping on average. |
| 117 | static const size_t kSectionMask = 65536 - 1; |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 118 | DCHECK(!options.executable); |
[email protected] | 5d2b449 | 2011-03-01 02:48:05 | [diff] [blame] | 119 | DCHECK(!mapped_file_); |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 120 | if (options.size == 0) |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 121 | return false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 122 | |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 123 | // Check maximum accounting for overflow. |
| 124 | if (options.size > |
| 125 | static_cast<size_t>(std::numeric_limits<int>::max()) - kSectionMask) |
[email protected] | 374f1a8 | 2013-01-10 02:16:24 | [diff] [blame] | 126 | return false; |
| 127 | |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 128 | size_t rounded_size = (options.size + kSectionMask) & ~kSectionMask; |
thestig | 8badc79 | 2014-12-04 22:14:22 | [diff] [blame] | 129 | name_ = options.name_deprecated ? |
| 130 | ASCIIToUTF16(*options.name_deprecated) : L""; |
[email protected] | 6d6797eb | 2014-08-07 22:07:43 | [diff] [blame] | 131 | SECURITY_ATTRIBUTES sa = { sizeof(sa), NULL, FALSE }; |
| 132 | SECURITY_DESCRIPTOR sd; |
| 133 | ACL dacl; |
| 134 | |
[email protected] | dc84fcc | 2014-07-24 11:42:59 | [diff] [blame] | 135 | if (options.share_read_only && name_.empty()) { |
[email protected] | 6d6797eb | 2014-08-07 22:07:43 | [diff] [blame] | 136 | // Add an empty DACL to enforce anonymous read-only sections. |
| 137 | sa.lpSecurityDescriptor = &sd; |
| 138 | if (!InitializeAcl(&dacl, sizeof(dacl), ACL_REVISION)) |
| 139 | return false; |
| 140 | if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) |
| 141 | return false; |
| 142 | if (!SetSecurityDescriptorDacl(&sd, TRUE, &dacl, FALSE)) |
| 143 | return false; |
| 144 | |
[email protected] | dc84fcc | 2014-07-24 11:42:59 | [diff] [blame] | 145 | // Windows ignores DACLs on certain unnamed objects (like shared sections). |
| 146 | // So, we generate a random name when we need to enforce read-only. |
| 147 | uint64_t rand_values[4]; |
thestig | 8badc79 | 2014-12-04 22:14:22 | [diff] [blame] | 148 | RandBytes(&rand_values, sizeof(rand_values)); |
| 149 | name_ = StringPrintf(L"CrSharedMem_%016x%016x%016x%016x", |
| 150 | rand_values[0], rand_values[1], |
| 151 | rand_values[2], rand_values[3]); |
[email protected] | dc84fcc | 2014-07-24 11:42:59 | [diff] [blame] | 152 | } |
[email protected] | 6d6797eb | 2014-08-07 22:07:43 | [diff] [blame] | 153 | mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, &sa, |
shrikant | 9426fb8 | 2015-02-24 00:27:33 | [diff] [blame] | 154 | PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size), |
| 155 | name_.empty() ? nullptr : name_.c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 156 | if (!mapped_file_) |
| 157 | return false; |
| 158 | |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 159 | requested_size_ = options.size; |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 160 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 161 | // Check if the shared memory pre-exists. |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 162 | if (GetLastError() == ERROR_ALREADY_EXISTS) { |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 163 | // If the file already existed, set requested_size_ to 0 to show that |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 164 | // we don't know the size. |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 165 | requested_size_ = 0; |
[email protected] | ff672b7 | 2014-03-05 21:13:52 | [diff] [blame] | 166 | if (!options.open_existing_deprecated) { |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 167 | Close(); |
| 168 | return false; |
| 169 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | } |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 171 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 172 | return true; |
| 173 | } |
| 174 | |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 175 | bool SharedMemory::Delete(const std::string& name) { |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 176 | // intentionally empty -- there is nothing for us to do on Windows. |
| 177 | return true; |
| 178 | } |
| 179 | |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 180 | bool SharedMemory::Open(const std::string& name, bool read_only) { |
[email protected] | 5d2b449 | 2011-03-01 02:48:05 | [diff] [blame] | 181 | DCHECK(!mapped_file_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 182 | |
thestig | 8badc79 | 2014-12-04 22:14:22 | [diff] [blame] | 183 | name_ = ASCIIToUTF16(name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 184 | read_only_ = read_only; |
| 185 | mapped_file_ = OpenFileMapping( |
[email protected] | 5c74461 | 2013-10-30 15:17:05 | [diff] [blame] | 186 | read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE, |
| 187 | false, name_.empty() ? NULL : name_.c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 188 | if (mapped_file_ != NULL) { |
| 189 | // Note: size_ is not set in this case. |
| 190 | return true; |
| 191 | } |
| 192 | return false; |
| 193 | } |
| 194 | |
[email protected] | e29e3f55 | 2013-01-16 09:02:34 | [diff] [blame] | 195 | bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 196 | if (mapped_file_ == NULL) |
| 197 | return false; |
| 198 | |
[email protected] | 374f1a8 | 2013-01-10 02:16:24 | [diff] [blame] | 199 | if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
| 200 | return false; |
| 201 | |
[email protected] | 421c150 | 2014-03-18 22:33:28 | [diff] [blame] | 202 | if (memory_) |
| 203 | return false; |
| 204 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 205 | memory_ = MapViewOfFile(mapped_file_, |
[email protected] | 5c74461 | 2013-10-30 15:17:05 | [diff] [blame] | 206 | read_only_ ? FILE_MAP_READ : FILE_MAP_READ | |
| 207 | FILE_MAP_WRITE, |
[email protected] | e29e3f55 | 2013-01-16 09:02:34 | [diff] [blame] | 208 | static_cast<uint64>(offset) >> 32, |
| 209 | static_cast<DWORD>(offset), |
| 210 | bytes); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 211 | if (memory_ != NULL) { |
[email protected] | 404a058 | 2012-08-18 02:17:26 | [diff] [blame] | 212 | DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
| 213 | (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 214 | mapped_size_ = GetMemorySectionSize(memory_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 215 | return true; |
| 216 | } |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | bool SharedMemory::Unmap() { |
| 221 | if (memory_ == NULL) |
| 222 | return false; |
| 223 | |
| 224 | UnmapViewOfFile(memory_); |
| 225 | memory_ = NULL; |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
thestig | 8badc79 | 2014-12-04 22:14:22 | [diff] [blame] | 230 | SharedMemoryHandle* new_handle, |
[email protected] | 5f58adab | 2013-11-20 23:41:25 | [diff] [blame] | 231 | bool close_self, |
| 232 | ShareMode share_mode) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 233 | *new_handle = 0; |
[email protected] | 5c74461 | 2013-10-30 15:17:05 | [diff] [blame] | 234 | DWORD access = FILE_MAP_READ; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 235 | DWORD options = 0; |
| 236 | HANDLE mapped_file = mapped_file_; |
| 237 | HANDLE result; |
[email protected] | 5f58adab | 2013-11-20 23:41:25 | [diff] [blame] | 238 | if (share_mode == SHARE_CURRENT_MODE && !read_only_) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 239 | access |= FILE_MAP_WRITE; |
| 240 | if (close_self) { |
| 241 | // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file. |
| 242 | options = DUPLICATE_CLOSE_SOURCE; |
| 243 | mapped_file_ = NULL; |
| 244 | Unmap(); |
| 245 | } |
| 246 | |
| 247 | if (process == GetCurrentProcess() && close_self) { |
| 248 | *new_handle = mapped_file; |
| 249 | return true; |
| 250 | } |
| 251 | |
erikchen | 2096f62 | 2015-06-03 00:26:59 | [diff] [blame] | 252 | if (!::DuplicateHandle(GetCurrentProcess(), mapped_file, process, &result, |
| 253 | access, FALSE, options)) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 254 | return false; |
erikchen | 2096f62 | 2015-06-03 00:26:59 | [diff] [blame] | 255 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 256 | *new_handle = result; |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | |
| 261 | void SharedMemory::Close() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 262 | if (mapped_file_ != NULL) { |
| 263 | CloseHandle(mapped_file_); |
| 264 | mapped_file_ = NULL; |
| 265 | } |
| 266 | } |
| 267 | |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 268 | SharedMemoryHandle SharedMemory::handle() const { |
| 269 | return mapped_file_; |
| 270 | } |
| 271 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 272 | } // namespace base |