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
3 // found in the LICENSE file.
5 #include "base/memory/shared_memory.h"
7 #include "base/logging.h"
8 #include "base/rand_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
14 // Returns the length of the memory section starting at the supplied address.
15 size_t GetMemorySectionSize(void* address
) {
16 MEMORY_BASIC_INFORMATION memory_info
;
17 if (!::VirtualQuery(address
, &memory_info
, sizeof(memory_info
)))
19 return memory_info
.RegionSize
- (static_cast<char*>(address
) -
20 static_cast<char*>(memory_info
.AllocationBase
));
27 SharedMemory::SharedMemory()
36 SharedMemory::SharedMemory(const std::wstring
& name
)
46 SharedMemory::SharedMemory(SharedMemoryHandle handle
, bool read_only
)
47 : mapped_file_(handle
),
49 read_only_(read_only
),
55 SharedMemory::SharedMemory(SharedMemoryHandle handle
, bool read_only
,
56 ProcessHandle process
)
59 read_only_(read_only
),
63 ::DuplicateHandle(process
, handle
,
64 GetCurrentProcess(), &mapped_file_
,
65 read_only_
? FILE_MAP_READ
: FILE_MAP_READ
|
70 SharedMemory::~SharedMemory() {
77 bool SharedMemory::IsHandleValid(const SharedMemoryHandle
& handle
) {
78 return handle
!= NULL
;
82 SharedMemoryHandle
SharedMemory::NULLHandle() {
87 void SharedMemory::CloseHandle(const SharedMemoryHandle
& handle
) {
88 DCHECK(handle
!= NULL
);
89 ::CloseHandle(handle
);
93 size_t SharedMemory::GetHandleLimit() {
94 // Rounded down from value reported here:
95 // http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx
96 return static_cast<size_t>(1 << 23);
99 bool SharedMemory::CreateAndMapAnonymous(size_t size
) {
100 return CreateAnonymous(size
) && Map(size
);
103 bool SharedMemory::Create(const SharedMemoryCreateOptions
& options
) {
104 // TODO(bsy,sehr): crbug.com/210609 NaCl forces us to round up 64k here,
105 // wasting 32k per mapping on average.
106 static const size_t kSectionMask
= 65536 - 1;
107 DCHECK(!options
.executable
);
108 DCHECK(!mapped_file_
);
109 if (options
.size
== 0)
112 // Check maximum accounting for overflow.
114 static_cast<size_t>(std::numeric_limits
<int>::max()) - kSectionMask
)
117 size_t rounded_size
= (options
.size
+ kSectionMask
) & ~kSectionMask
;
118 name_
= ASCIIToWide(options
.name_deprecated
== NULL
? "" :
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]);
129 mapped_file_
= CreateFileMapping(INVALID_HANDLE_VALUE
, NULL
,
130 PAGE_READWRITE
, 0, static_cast<DWORD
>(rounded_size
), name_
.c_str());
134 requested_size_
= options
.size
;
136 // Check if the shared memory pre-exists.
137 if (GetLastError() == ERROR_ALREADY_EXISTS
) {
138 // If the file already existed, set requested_size_ to 0 to show that
139 // we don't know the size.
141 if (!options
.open_existing_deprecated
) {
150 bool SharedMemory::Delete(const std::string
& name
) {
151 // intentionally empty -- there is nothing for us to do on Windows.
155 bool SharedMemory::Open(const std::string
& name
, bool read_only
) {
156 DCHECK(!mapped_file_
);
158 name_
= ASCIIToWide(name
);
159 read_only_
= read_only
;
160 mapped_file_
= OpenFileMapping(
161 read_only_
? FILE_MAP_READ
: FILE_MAP_READ
| FILE_MAP_WRITE
,
162 false, name_
.empty() ? NULL
: name_
.c_str());
163 if (mapped_file_
!= NULL
) {
164 // Note: size_ is not set in this case.
170 bool SharedMemory::MapAt(off_t offset
, size_t bytes
) {
171 if (mapped_file_
== NULL
)
174 if (bytes
> static_cast<size_t>(std::numeric_limits
<int>::max()))
180 memory_
= MapViewOfFile(mapped_file_
,
181 read_only_
? FILE_MAP_READ
: FILE_MAP_READ
|
183 static_cast<uint64
>(offset
) >> 32,
184 static_cast<DWORD
>(offset
),
186 if (memory_
!= NULL
) {
187 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_
) &
188 (SharedMemory::MAP_MINIMUM_ALIGNMENT
- 1));
189 mapped_size_
= GetMemorySectionSize(memory_
);
195 bool SharedMemory::Unmap() {
199 UnmapViewOfFile(memory_
);
204 bool SharedMemory::ShareToProcessCommon(ProcessHandle process
,
205 SharedMemoryHandle
*new_handle
,
207 ShareMode share_mode
) {
209 DWORD access
= FILE_MAP_READ
;
211 HANDLE mapped_file
= mapped_file_
;
213 if (share_mode
== SHARE_CURRENT_MODE
&& !read_only_
)
214 access
|= FILE_MAP_WRITE
;
216 // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file.
217 options
= DUPLICATE_CLOSE_SOURCE
;
222 if (process
== GetCurrentProcess() && close_self
) {
223 *new_handle
= mapped_file
;
227 if (!DuplicateHandle(GetCurrentProcess(), mapped_file
, process
,
228 &result
, access
, FALSE
, options
))
230 *new_handle
= result
;
235 void SharedMemory::Close() {
236 if (memory_
!= NULL
) {
237 UnmapViewOfFile(memory_
);
241 if (mapped_file_
!= NULL
) {
242 CloseHandle(mapped_file_
);
247 void SharedMemory::LockDeprecated() {
249 std::wstring name
= name_
;
250 name
.append(L
"lock");
251 lock_
= CreateMutex(NULL
, FALSE
, name
.c_str());
253 DPLOG(ERROR
) << "Could not create mutex.";
255 return; // There is nothing good we can do here.
258 DWORD result
= WaitForSingleObject(lock_
, INFINITE
);
259 DCHECK_EQ(result
, WAIT_OBJECT_0
);
262 void SharedMemory::UnlockDeprecated() {
263 DCHECK(lock_
!= NULL
);
267 SharedMemoryHandle
SharedMemory::handle() const {