1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* This source code was derived from Chromium code, and as such is also subject
* to the [Chromium license](ipc/chromium/src/LICENSE). */
#include "SharedMemoryPlatform.h"
#include <windows.h>
#include "nsDebug.h"
#ifdef MOZ_MEMORY
# include "mozmemory_stall.h"
#endif
namespace {
// NtQuerySection is an internal (but believed to be stable) API and the
// structures it uses are defined in nt_internals.h.
// So we have to define them ourselves.
typedef enum _SECTION_INFORMATION_CLASS {
SectionBasicInformation,
} SECTION_INFORMATION_CLASS;
typedef struct _SECTION_BASIC_INFORMATION {
PVOID BaseAddress;
ULONG Attributes;
LARGE_INTEGER Size;
} SECTION_BASIC_INFORMATION, *PSECTION_BASIC_INFORMATION;
typedef ULONG(__stdcall* NtQuerySectionType)(
HANDLE SectionHandle, SECTION_INFORMATION_CLASS SectionInformationClass,
PVOID SectionInformation, ULONG SectionInformationLength,
PULONG ResultLength);
// Checks if the section object is safe to map. At the moment this just means
// it's not an image section.
bool IsSectionSafeToMap(HANDLE aHandle) {
static NtQuerySectionType nt_query_section_func =
reinterpret_cast<NtQuerySectionType>(
::GetProcAddress(::GetModuleHandle(L"ntdll.dll"), "NtQuerySection"));
MOZ_DIAGNOSTIC_ASSERT(nt_query_section_func,
"NtQuerySection function not found in ntdll.dll");
// The handle must have SECTION_QUERY access for this to succeed.
SECTION_BASIC_INFORMATION basic_information = {};
ULONG status = nt_query_section_func(aHandle, SectionBasicInformation,
&basic_information,
sizeof(basic_information), nullptr);
if (status) {
return false;
}
return (basic_information.Attributes & SEC_IMAGE) != SEC_IMAGE;
}
// Wrapper around CreateFileMappingW for pagefile-backed regions. When out of
// memory, may attempt to stall and retry rather than returning immediately, in
// hopes that the page file is about to be expanded by Windows. (bug 1822383,
// bug 1716727)
//
// This method is largely a copy of the MozVirtualAlloc method from
// mozjemalloc.cpp, which implements this strategy for VirtualAlloc calls,
// except re-purposed to handle CreateFileMapping.
HANDLE MozCreateFileMappingW(LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
DWORD flProtect, DWORD dwMaximumSizeHigh,
DWORD dwMaximumSizeLow, LPCWSTR lpName) {
#ifdef MOZ_MEMORY
constexpr auto IsOOMError = [] {
return ::GetLastError() == ERROR_COMMITMENT_LIMIT;
};
{
HANDLE handle = ::CreateFileMappingW(
INVALID_HANDLE_VALUE, lpFileMappingAttributes, flProtect,
dwMaximumSizeHigh, dwMaximumSizeLow, lpName);
if (MOZ_LIKELY(handle)) {
MOZ_DIAGNOSTIC_ASSERT(handle != INVALID_HANDLE_VALUE,
"::CreateFileMapping should return NULL, not "
"INVALID_HANDLE_VALUE, on failure");
return handle;
}
// We can't do anything for errors other than OOM.
if (!IsOOMError()) {
return nullptr;
}
}
// Retry as many times as desired (possibly zero).
const mozilla::StallSpecs stallSpecs = mozilla::GetAllocatorStallSpecs();
const auto ret =
stallSpecs.StallAndRetry(&::Sleep, [&]() -> std::optional<HANDLE> {
HANDLE handle = ::CreateFileMappingW(
INVALID_HANDLE_VALUE, lpFileMappingAttributes, flProtect,
dwMaximumSizeHigh, dwMaximumSizeLow, lpName);
if (handle) {
MOZ_DIAGNOSTIC_ASSERT(handle != INVALID_HANDLE_VALUE,
"::CreateFileMapping should return NULL, not "
"INVALID_HANDLE_VALUE, on failure");
return handle;
}
// Failure for some reason other than OOM.
if (!IsOOMError()) {
return nullptr;
}
return std::nullopt;
});
return ret.value_or(nullptr);
#else
return ::CreateFileMappingW(INVALID_HANDLE_VALUE, lpFileMappingAttributes,
flProtect, dwMaximumSizeHigh, dwMaximumSizeLow,
lpName);
#endif
}
} // namespace
namespace mozilla::ipc::shared_memory {
static Maybe<PlatformHandle> CreateImpl(size_t aSize, bool aFreezable) {
// If the shared memory object has no DACL, any process can
// duplicate its handles with any access rights; e.g., re-add write
// access to a read-only handle. To prevent that, we give it an
// empty DACL, so that no process can do that.
SECURITY_ATTRIBUTES sa, *psa = nullptr;
SECURITY_DESCRIPTOR sd;
ACL dacl;
if (aFreezable) {
psa = &sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = &sd;
sa.bInheritHandle = FALSE;
if (NS_WARN_IF(!::InitializeAcl(&dacl, sizeof(dacl), ACL_REVISION)) ||
NS_WARN_IF(!::InitializeSecurityDescriptor(
&sd, SECURITY_DESCRIPTOR_REVISION)) ||
NS_WARN_IF(!::SetSecurityDescriptorDacl(&sd, TRUE, &dacl, FALSE))) {
return Nothing();
}
}
auto handle = MozCreateFileMappingW(psa, PAGE_READWRITE, 0,
static_cast<DWORD>(aSize), nullptr);
if (!handle) {
return Nothing();
} else {
return Some(handle);
}
}
bool Platform::Create(MutableHandle& aHandle, size_t aSize) {
if (auto ph = CreateImpl(aSize, false)) {
aHandle.mHandle = std::move(*ph);
aHandle.SetSize(aSize);
return true;
}
return false;
}
bool Platform::CreateFreezable(FreezableHandle& aHandle, size_t aSize) {
if (auto ph = CreateImpl(aSize, true)) {
aHandle.mHandle = std::move(*ph);
aHandle.SetSize(aSize);
return true;
}
return false;
}
PlatformHandle Platform::CloneHandle(const PlatformHandle& aHandle) {
HANDLE h = INVALID_HANDLE_VALUE;
if (::DuplicateHandle(::GetCurrentProcess(), aHandle.get(),
::GetCurrentProcess(), &h, 0, false,
DUPLICATE_SAME_ACCESS)) {
return PlatformHandle(h);
}
NS_WARNING("DuplicateHandle Failed!");
return nullptr;
}
bool Platform::Freeze(FreezableHandle& aHandle) {
HANDLE ro_handle;
if (!::DuplicateHandle(::GetCurrentProcess(), aHandle.mHandle.get(),
::GetCurrentProcess(), &ro_handle,
GENERIC_READ | FILE_MAP_READ, false, 0)) {
return false;
}
aHandle.mHandle.reset(ro_handle);
return true;
}
Maybe<void*> Platform::Map(const HandleBase& aHandle, uint64_t aOffset,
size_t aSize, void* aFixedAddress, bool aReadOnly) {
DWORD fileOffsetHigh = (aOffset >> 32) & 0xffffffff;
DWORD fileOffsetLow = aOffset & 0xffffffff;
void* mem = ::MapViewOfFileEx(
aHandle.mHandle.get(),
aReadOnly ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE,
fileOffsetHigh, fileOffsetLow, aSize, aFixedAddress);
if (mem) {
MOZ_ASSERT(!aFixedAddress || mem == aFixedAddress,
"MapViewOfFileEx returned an expected address");
return Some(mem);
}
return Nothing();
}
void Platform::Unmap(void* aMemory, size_t aSize) {
::UnmapViewOfFile(aMemory);
}
bool Platform::Protect(char* aAddr, size_t aSize, Access aAccess) {
DWORD flags;
if ((aAccess & AccessReadWrite) == AccessReadWrite)
flags = PAGE_READWRITE;
else if (aAccess & AccessRead)
flags = PAGE_READONLY;
else
flags = PAGE_NOACCESS;
DWORD oldflags;
return ::VirtualProtect(aAddr, aSize, flags, &oldflags);
}
void* Platform::FindFreeAddressSpace(size_t aSize) {
void* memory = ::VirtualAlloc(NULL, aSize, MEM_RESERVE, PAGE_NOACCESS);
if (memory) {
::VirtualFree(memory, 0, MEM_RELEASE);
}
return memory;
}
size_t Platform::PageSize() {
SYSTEM_INFO si;
::GetSystemInfo(&si);
return si.dwPageSize;
}
size_t Platform::AllocationGranularity() {
SYSTEM_INFO si;
::GetSystemInfo(&si);
return si.dwAllocationGranularity;
}
bool Platform::IsSafeToMap(const PlatformHandle& aHandle) {
return IsSectionSafeToMap(aHandle.get());
}
} // namespace mozilla::ipc::shared_memory
|