[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 | |
| 7 | #include "base/logging.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 8 | #include "base/strings/utf_string_conversions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 10 | namespace { |
| 11 | |
| 12 | // Returns the length of the memory section starting at the supplied address. |
| 13 | size_t GetMemorySectionSize(void* address) { |
| 14 | MEMORY_BASIC_INFORMATION memory_info; |
| 15 | if (!::VirtualQuery(address, &memory_info, sizeof(memory_info))) |
| 16 | return 0; |
| 17 | return memory_info.RegionSize - (static_cast<char*>(address) - |
| 18 | static_cast<char*>(memory_info.AllocationBase)); |
| 19 | } |
| 20 | |
| 21 | } // namespace. |
| 22 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 23 | namespace base { |
| 24 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 25 | SharedMemory::SharedMemory() |
| 26 | : mapped_file_(NULL), |
| 27 | memory_(NULL), |
| 28 | read_only_(false), |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 29 | mapped_size_(0), |
| 30 | requested_size_(0), |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 31 | lock_(NULL) { |
| 32 | } |
| 33 | |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 34 | SharedMemory::SharedMemory(const std::wstring& name) |
| 35 | : mapped_file_(NULL), |
| 36 | memory_(NULL), |
| 37 | read_only_(false), |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 38 | requested_size_(0), |
| 39 | mapped_size_(0), |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 40 | lock_(NULL), |
| 41 | name_(name) { |
| 42 | } |
| 43 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 44 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
| 45 | : mapped_file_(handle), |
| 46 | memory_(NULL), |
| 47 | read_only_(read_only), |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 48 | requested_size_(0), |
| 49 | mapped_size_(0), |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 50 | lock_(NULL) { |
| 51 | } |
| 52 | |
| 53 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
| 54 | ProcessHandle process) |
| 55 | : mapped_file_(NULL), |
| 56 | memory_(NULL), |
| 57 | read_only_(read_only), |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 58 | requested_size_(0), |
| 59 | mapped_size_(0), |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 60 | lock_(NULL) { |
| 61 | ::DuplicateHandle(process, handle, |
| 62 | GetCurrentProcess(), &mapped_file_, |
[email protected] | 5c74461 | 2013-10-30 15:17:05 | [diff] [blame] | 63 | read_only_ ? FILE_MAP_READ : FILE_MAP_READ | |
| 64 | FILE_MAP_WRITE, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 65 | FALSE, 0); |
| 66 | } |
| 67 | |
| 68 | SharedMemory::~SharedMemory() { |
| 69 | Close(); |
| 70 | if (lock_ != NULL) |
| 71 | CloseHandle(lock_); |
| 72 | } |
| 73 | |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 74 | // static |
| 75 | bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
| 76 | return handle != NULL; |
| 77 | } |
| 78 | |
[email protected] | 76aac1e | 2009-03-16 16:45:36 | [diff] [blame] | 79 | // static |
| 80 | SharedMemoryHandle SharedMemory::NULLHandle() { |
| 81 | return NULL; |
| 82 | } |
| 83 | |
[email protected] | b0af04c | 2009-05-18 17:46:31 | [diff] [blame] | 84 | // static |
| 85 | void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
| 86 | DCHECK(handle != NULL); |
| 87 | ::CloseHandle(handle); |
| 88 | } |
| 89 | |
[email protected] | c14eda9 | 2013-05-09 23:15:40 | [diff] [blame] | 90 | // static |
| 91 | size_t SharedMemory::GetHandleLimit() { |
| 92 | // Rounded down from value reported here: |
| 93 | // http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx |
| 94 | return static_cast<size_t>(1 << 23); |
| 95 | } |
| 96 | |
[email protected] | 374f1a8 | 2013-01-10 02:16:24 | [diff] [blame] | 97 | bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 98 | return CreateAnonymous(size) && Map(size); |
| 99 | } |
| 100 | |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 101 | bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 102 | // TODO(bsy,sehr): crbug.com/210609 NaCl forces us to round up 64k here, |
| 103 | // wasting 32k per mapping on average. |
| 104 | static const size_t kSectionMask = 65536 - 1; |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 105 | DCHECK(!options.executable); |
[email protected] | 5d2b449 | 2011-03-01 02:48:05 | [diff] [blame] | 106 | DCHECK(!mapped_file_); |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 107 | if (options.size == 0) |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 108 | return false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 109 | |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 110 | // Check maximum accounting for overflow. |
| 111 | if (options.size > |
| 112 | static_cast<size_t>(std::numeric_limits<int>::max()) - kSectionMask) |
[email protected] | 374f1a8 | 2013-01-10 02:16:24 | [diff] [blame] | 113 | return false; |
| 114 | |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 115 | size_t rounded_size = (options.size + kSectionMask) & ~kSectionMask; |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 116 | name_ = ASCIIToWide(options.name == NULL ? "" : *options.name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 117 | mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 118 | PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size), |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 119 | name_.empty() ? NULL : name_.c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 120 | if (!mapped_file_) |
| 121 | return false; |
| 122 | |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 123 | requested_size_ = options.size; |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 124 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 125 | // Check if the shared memory pre-exists. |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 126 | if (GetLastError() == ERROR_ALREADY_EXISTS) { |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 127 | // If the file already existed, set requested_size_ to 0 to show that |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 128 | // we don't know the size. |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 129 | requested_size_ = 0; |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 130 | if (!options.open_existing) { |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 131 | Close(); |
| 132 | return false; |
| 133 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 134 | } |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 135 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 139 | bool SharedMemory::Delete(const std::string& name) { |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 140 | // intentionally empty -- there is nothing for us to do on Windows. |
| 141 | return true; |
| 142 | } |
| 143 | |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 144 | bool SharedMemory::Open(const std::string& name, bool read_only) { |
[email protected] | 5d2b449 | 2011-03-01 02:48:05 | [diff] [blame] | 145 | DCHECK(!mapped_file_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 146 | |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 147 | name_ = ASCIIToWide(name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 148 | read_only_ = read_only; |
| 149 | mapped_file_ = OpenFileMapping( |
[email protected] | 5c74461 | 2013-10-30 15:17:05 | [diff] [blame] | 150 | read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE, |
| 151 | false, name_.empty() ? NULL : name_.c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 152 | if (mapped_file_ != NULL) { |
| 153 | // Note: size_ is not set in this case. |
| 154 | return true; |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | |
[email protected] | e29e3f55 | 2013-01-16 09:02:34 | [diff] [blame] | 159 | bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 160 | if (mapped_file_ == NULL) |
| 161 | return false; |
| 162 | |
[email protected] | 374f1a8 | 2013-01-10 02:16:24 | [diff] [blame] | 163 | if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
| 164 | return false; |
| 165 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 166 | memory_ = MapViewOfFile(mapped_file_, |
[email protected] | 5c74461 | 2013-10-30 15:17:05 | [diff] [blame] | 167 | read_only_ ? FILE_MAP_READ : FILE_MAP_READ | |
| 168 | FILE_MAP_WRITE, |
[email protected] | e29e3f55 | 2013-01-16 09:02:34 | [diff] [blame] | 169 | static_cast<uint64>(offset) >> 32, |
| 170 | static_cast<DWORD>(offset), |
| 171 | bytes); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 172 | if (memory_ != NULL) { |
[email protected] | 404a058 | 2012-08-18 02:17:26 | [diff] [blame] | 173 | DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
| 174 | (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
[email protected] | 67ea507 | 2013-03-28 02:02:18 | [diff] [blame] | 175 | mapped_size_ = GetMemorySectionSize(memory_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 176 | return true; |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | bool SharedMemory::Unmap() { |
| 182 | if (memory_ == NULL) |
| 183 | return false; |
| 184 | |
| 185 | UnmapViewOfFile(memory_); |
| 186 | memory_ = NULL; |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
| 191 | SharedMemoryHandle *new_handle, |
[email protected] | 5f58adab | 2013-11-20 23:41:25 | [diff] [blame^] | 192 | bool close_self, |
| 193 | ShareMode share_mode) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 194 | *new_handle = 0; |
[email protected] | 5c74461 | 2013-10-30 15:17:05 | [diff] [blame] | 195 | DWORD access = FILE_MAP_READ; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 196 | DWORD options = 0; |
| 197 | HANDLE mapped_file = mapped_file_; |
| 198 | HANDLE result; |
[email protected] | 5f58adab | 2013-11-20 23:41:25 | [diff] [blame^] | 199 | if (share_mode == SHARE_CURRENT_MODE && !read_only_) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 200 | access |= FILE_MAP_WRITE; |
| 201 | if (close_self) { |
| 202 | // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file. |
| 203 | options = DUPLICATE_CLOSE_SOURCE; |
| 204 | mapped_file_ = NULL; |
| 205 | Unmap(); |
| 206 | } |
| 207 | |
| 208 | if (process == GetCurrentProcess() && close_self) { |
| 209 | *new_handle = mapped_file; |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | if (!DuplicateHandle(GetCurrentProcess(), mapped_file, process, |
| 214 | &result, access, FALSE, options)) |
| 215 | return false; |
| 216 | *new_handle = result; |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | void SharedMemory::Close() { |
| 222 | if (memory_ != NULL) { |
| 223 | UnmapViewOfFile(memory_); |
| 224 | memory_ = NULL; |
| 225 | } |
| 226 | |
| 227 | if (mapped_file_ != NULL) { |
| 228 | CloseHandle(mapped_file_); |
| 229 | mapped_file_ = NULL; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void SharedMemory::Lock() { |
[email protected] | 4215516 | 2010-11-16 14:25:03 | [diff] [blame] | 234 | Lock(INFINITE, NULL); |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 235 | } |
| 236 | |
[email protected] | 4215516 | 2010-11-16 14:25:03 | [diff] [blame] | 237 | bool SharedMemory::Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 238 | if (lock_ == NULL) { |
| 239 | std::wstring name = name_; |
| 240 | name.append(L"lock"); |
[email protected] | 4215516 | 2010-11-16 14:25:03 | [diff] [blame] | 241 | lock_ = CreateMutex(sec_attr, FALSE, name.c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 242 | if (lock_ == NULL) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 243 | DPLOG(ERROR) << "Could not create mutex."; |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 244 | return false; // there is nothing good we can do here. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 245 | } |
| 246 | } |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 247 | DWORD result = WaitForSingleObject(lock_, timeout_ms); |
| 248 | |
| 249 | // Return false for WAIT_ABANDONED, WAIT_TIMEOUT or WAIT_FAILED. |
| 250 | return (result == WAIT_OBJECT_0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void SharedMemory::Unlock() { |
| 254 | DCHECK(lock_ != NULL); |
| 255 | ReleaseMutex(lock_); |
| 256 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 257 | |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 258 | SharedMemoryHandle SharedMemory::handle() const { |
| 259 | return mapped_file_; |
| 260 | } |
| 261 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 262 | } // namespace base |