[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 | |
| 5 | #include "base/shared_memory.h" |
| 6 | |
| 7 | #include "base/logging.h" |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 8 | #include "base/utf_string_conversions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 10 | namespace base { |
| 11 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | SharedMemory::SharedMemory() |
| 13 | : mapped_file_(NULL), |
| 14 | memory_(NULL), |
| 15 | read_only_(false), |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 16 | created_size_(0), |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 17 | lock_(NULL) { |
| 18 | } |
| 19 | |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 20 | SharedMemory::SharedMemory(const std::wstring& name) |
| 21 | : mapped_file_(NULL), |
| 22 | memory_(NULL), |
| 23 | read_only_(false), |
| 24 | created_size_(0), |
| 25 | lock_(NULL), |
| 26 | name_(name) { |
| 27 | } |
| 28 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 29 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
| 30 | : mapped_file_(handle), |
| 31 | memory_(NULL), |
| 32 | read_only_(read_only), |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 33 | created_size_(0), |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 34 | lock_(NULL) { |
| 35 | } |
| 36 | |
| 37 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
| 38 | ProcessHandle process) |
| 39 | : mapped_file_(NULL), |
| 40 | memory_(NULL), |
| 41 | read_only_(read_only), |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 42 | created_size_(0), |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 43 | lock_(NULL) { |
| 44 | ::DuplicateHandle(process, handle, |
| 45 | GetCurrentProcess(), &mapped_file_, |
| 46 | STANDARD_RIGHTS_REQUIRED | |
| 47 | (read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS), |
| 48 | FALSE, 0); |
| 49 | } |
| 50 | |
| 51 | SharedMemory::~SharedMemory() { |
| 52 | Close(); |
| 53 | if (lock_ != NULL) |
| 54 | CloseHandle(lock_); |
| 55 | } |
| 56 | |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 57 | // static |
| 58 | bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
| 59 | return handle != NULL; |
| 60 | } |
| 61 | |
[email protected] | 76aac1e | 2009-03-16 16:45:36 | [diff] [blame] | 62 | // static |
| 63 | SharedMemoryHandle SharedMemory::NULLHandle() { |
| 64 | return NULL; |
| 65 | } |
| 66 | |
[email protected] | b0af04c | 2009-05-18 17:46:31 | [diff] [blame] | 67 | // static |
| 68 | void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
| 69 | DCHECK(handle != NULL); |
| 70 | ::CloseHandle(handle); |
| 71 | } |
| 72 | |
[email protected] | 374f1a8 | 2013-01-10 02:16:24 | [diff] [blame^] | 73 | bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 74 | return CreateAnonymous(size) && Map(size); |
| 75 | } |
| 76 | |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 77 | bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
| 78 | DCHECK(!options.executable); |
[email protected] | 5d2b449 | 2011-03-01 02:48:05 | [diff] [blame] | 79 | DCHECK(!mapped_file_); |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 80 | if (options.size == 0) |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 81 | return false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 82 | |
[email protected] | 374f1a8 | 2013-01-10 02:16:24 | [diff] [blame^] | 83 | if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) |
| 84 | return false; |
| 85 | |
[email protected] | 9768435 | 2010-03-25 15:40:24 | [diff] [blame] | 86 | // NaCl's memory allocator requires 0mod64K alignment and size for |
| 87 | // shared memory objects. To allow passing shared memory to NaCl, |
| 88 | // therefore we round the size actually created to the nearest 64K unit. |
| 89 | // To avoid client impact, we continue to retain the size as the |
| 90 | // actual requested size. |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 91 | uint32 rounded_size = (options.size + 0xffff) & ~0xffff; |
[email protected] | 935cb0de | 2012-12-06 00:49:08 | [diff] [blame] | 92 | if (rounded_size < options.size) |
| 93 | return false; |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 94 | name_ = ASCIIToWide(options.name == NULL ? "" : *options.name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 95 | mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 96 | PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size), |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 97 | name_.empty() ? NULL : name_.c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 98 | if (!mapped_file_) |
| 99 | return false; |
| 100 | |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 101 | created_size_ = options.size; |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 102 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 103 | // Check if the shared memory pre-exists. |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 104 | if (GetLastError() == ERROR_ALREADY_EXISTS) { |
| 105 | // If the file already existed, set created_size_ to 0 to show that |
| 106 | // we don't know the size. |
| 107 | created_size_ = 0; |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 108 | if (!options.open_existing) { |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 109 | Close(); |
| 110 | return false; |
| 111 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 112 | } |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 113 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 117 | bool SharedMemory::Delete(const std::string& name) { |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 118 | // intentionally empty -- there is nothing for us to do on Windows. |
| 119 | return true; |
| 120 | } |
| 121 | |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 122 | bool SharedMemory::Open(const std::string& name, bool read_only) { |
[email protected] | 5d2b449 | 2011-03-01 02:48:05 | [diff] [blame] | 123 | DCHECK(!mapped_file_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 124 | |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 125 | name_ = ASCIIToWide(name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 126 | read_only_ = read_only; |
| 127 | mapped_file_ = OpenFileMapping( |
| 128 | read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, false, |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 129 | name_.empty() ? NULL : name_.c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 130 | if (mapped_file_ != NULL) { |
| 131 | // Note: size_ is not set in this case. |
| 132 | return true; |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
[email protected] | 374f1a8 | 2013-01-10 02:16:24 | [diff] [blame^] | 137 | bool SharedMemory::Map(size_t bytes) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 138 | if (mapped_file_ == NULL) |
| 139 | return false; |
| 140 | |
[email protected] | 374f1a8 | 2013-01-10 02:16:24 | [diff] [blame^] | 141 | if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
| 142 | return false; |
| 143 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 144 | memory_ = MapViewOfFile(mapped_file_, |
| 145 | read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, 0, 0, bytes); |
| 146 | if (memory_ != NULL) { |
[email protected] | 404a058 | 2012-08-18 02:17:26 | [diff] [blame] | 147 | DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
| 148 | (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | bool SharedMemory::Unmap() { |
| 155 | if (memory_ == NULL) |
| 156 | return false; |
| 157 | |
| 158 | UnmapViewOfFile(memory_); |
| 159 | memory_ = NULL; |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
| 164 | SharedMemoryHandle *new_handle, |
| 165 | bool close_self) { |
| 166 | *new_handle = 0; |
| 167 | DWORD access = STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ; |
| 168 | DWORD options = 0; |
| 169 | HANDLE mapped_file = mapped_file_; |
| 170 | HANDLE result; |
| 171 | if (!read_only_) |
| 172 | access |= FILE_MAP_WRITE; |
| 173 | if (close_self) { |
| 174 | // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file. |
| 175 | options = DUPLICATE_CLOSE_SOURCE; |
| 176 | mapped_file_ = NULL; |
| 177 | Unmap(); |
| 178 | } |
| 179 | |
| 180 | if (process == GetCurrentProcess() && close_self) { |
| 181 | *new_handle = mapped_file; |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | if (!DuplicateHandle(GetCurrentProcess(), mapped_file, process, |
| 186 | &result, access, FALSE, options)) |
| 187 | return false; |
| 188 | *new_handle = result; |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void SharedMemory::Close() { |
| 194 | if (memory_ != NULL) { |
| 195 | UnmapViewOfFile(memory_); |
| 196 | memory_ = NULL; |
| 197 | } |
| 198 | |
| 199 | if (mapped_file_ != NULL) { |
| 200 | CloseHandle(mapped_file_); |
| 201 | mapped_file_ = NULL; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void SharedMemory::Lock() { |
[email protected] | 4215516 | 2010-11-16 14:25:03 | [diff] [blame] | 206 | Lock(INFINITE, NULL); |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | 4215516 | 2010-11-16 14:25:03 | [diff] [blame] | 209 | bool SharedMemory::Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 210 | if (lock_ == NULL) { |
| 211 | std::wstring name = name_; |
| 212 | name.append(L"lock"); |
[email protected] | 4215516 | 2010-11-16 14:25:03 | [diff] [blame] | 213 | lock_ = CreateMutex(sec_attr, FALSE, name.c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 214 | if (lock_ == NULL) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 215 | DPLOG(ERROR) << "Could not create mutex."; |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 216 | return false; // there is nothing good we can do here. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 217 | } |
| 218 | } |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 219 | DWORD result = WaitForSingleObject(lock_, timeout_ms); |
| 220 | |
| 221 | // Return false for WAIT_ABANDONED, WAIT_TIMEOUT or WAIT_FAILED. |
| 222 | return (result == WAIT_OBJECT_0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void SharedMemory::Unlock() { |
| 226 | DCHECK(lock_ != NULL); |
| 227 | ReleaseMutex(lock_); |
| 228 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 229 | |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 230 | SharedMemoryHandle SharedMemory::handle() const { |
| 231 | return mapped_file_; |
| 232 | } |
| 233 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 234 | } // namespace base |