Quelle SharedMemoryPlatform_windows.cpp
Sprache: C
/* -*- 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_utils.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. typedefenum _SECTION_INFORMATION_CLASS {
SectionBasicInformation,
} SECTION_INFORMATION_CLASS;
// 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"));
DCHECK(nt_query_section_func);
// 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) { returnfalse;
}
// 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();
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;
}
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;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.