|
| 1 | +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 6 | + |
| 7 | +#include "mozilla/WindowsMapRemoteView.h" |
| 8 | + |
| 9 | +#include "mozilla/Assertions.h" |
| 10 | +#include "mozilla/DynamicallyLinkedFunctionPtr.h" |
| 11 | + |
| 12 | +#include <winternl.h> |
| 13 | + |
| 14 | +#if (NTDDI_VERSION < NTDDI_WIN10_RS2) |
| 15 | + |
| 16 | +// MapViewOfFile2 is just an inline function that calls MapViewOfFileNuma2 with |
| 17 | +// its preferred node set to NUMA_NO_PREFERRED_NODE |
| 18 | +PVOID WINAPI |
| 19 | +MapViewOfFileNuma2(HANDLE aFileMapping, HANDLE aProcess, ULONG64 aOffset, |
| 20 | + PVOID aBaseAddress, SIZE_T aViewSize, ULONG aAllocationType, |
| 21 | + ULONG aPageProtection, ULONG aPreferredNode); |
| 22 | + |
| 23 | +BOOL WINAPI |
| 24 | +UnmapViewOfFile2(HANDLE aProcess, PVOID aBaseAddress, ULONG aUnmapFlags); |
| 25 | + |
| 26 | +#endif // (NTDDI_VERSION < NTDDI_WIN10_RS2) |
| 27 | + |
| 28 | +enum SECTION_INHERIT |
| 29 | +{ |
| 30 | + ViewShare = 1, |
| 31 | + ViewUnmap = 2 |
| 32 | +}; |
| 33 | + |
| 34 | +NTSTATUS NTAPI |
| 35 | +NtMapViewOfSection(HANDLE aSection, HANDLE aProcess, PVOID* aBaseAddress, |
| 36 | + ULONG_PTR aZeroBits, SIZE_T aCommitSize, |
| 37 | + PLARGE_INTEGER aSectionOffset, PSIZE_T aViewSize, |
| 38 | + SECTION_INHERIT aInheritDisposition, ULONG aAllocationType, |
| 39 | + ULONG aProtectionFlags); |
| 40 | + |
| 41 | +NTSTATUS NTAPI |
| 42 | +NtUnmapViewOfSection(HANDLE aProcess, PVOID aBaseAddress); |
| 43 | + |
| 44 | +static DWORD |
| 45 | +GetWin32ErrorCode(NTSTATUS aNtStatus) |
| 46 | +{ |
| 47 | + static const mozilla::DynamicallyLinkedFunctionPtr<decltype(&RtlNtStatusToDosError)> |
| 48 | + pRtlNtStatusToDosError(L"ntdll.dll", "RtlNtStatusToDosError"); |
| 49 | + |
| 50 | + MOZ_ASSERT(!!pRtlNtStatusToDosError); |
| 51 | + if (!pRtlNtStatusToDosError) { |
| 52 | + return ERROR_GEN_FAILURE; |
| 53 | + } |
| 54 | + |
| 55 | + return pRtlNtStatusToDosError(aNtStatus); |
| 56 | +} |
| 57 | + |
| 58 | +namespace mozilla { |
| 59 | + |
| 60 | +MFBT_API void* |
| 61 | +MapRemoteViewOfFile(HANDLE aFileMapping, HANDLE aProcess, ULONG64 aOffset, |
| 62 | + PVOID aBaseAddress, SIZE_T aViewSize, ULONG aAllocationType, |
| 63 | + ULONG aProtectionFlags) |
| 64 | +{ |
| 65 | + static const DynamicallyLinkedFunctionPtr<decltype(&MapViewOfFileNuma2)> |
| 66 | + pMapViewOfFileNuma2(L"Api-ms-win-core-memory-l1-1-5.dll", "MapViewOfFileNuma2"); |
| 67 | + |
| 68 | + if (!!pMapViewOfFileNuma2) { |
| 69 | + return pMapViewOfFileNuma2(aFileMapping, aProcess, aOffset, aBaseAddress, |
| 70 | + aViewSize, aAllocationType, aProtectionFlags, |
| 71 | + NUMA_NO_PREFERRED_NODE); |
| 72 | + } |
| 73 | + |
| 74 | + static const DynamicallyLinkedFunctionPtr<decltype(&NtMapViewOfSection)> |
| 75 | + pNtMapViewOfSection(L"ntdll.dll", "NtMapViewOfSection"); |
| 76 | + |
| 77 | + MOZ_ASSERT(!!pNtMapViewOfSection); |
| 78 | + if (!pNtMapViewOfSection) { |
| 79 | + return nullptr; |
| 80 | + } |
| 81 | + |
| 82 | + // For the sake of consistency, we only permit the same flags that |
| 83 | + // MapViewOfFileNuma2 allows |
| 84 | + if (aAllocationType != 0 && aAllocationType != MEM_RESERVE && |
| 85 | + aAllocationType != MEM_LARGE_PAGES) { |
| 86 | + ::SetLastError(ERROR_INVALID_PARAMETER); |
| 87 | + return nullptr; |
| 88 | + } |
| 89 | + |
| 90 | + NTSTATUS ntStatus; |
| 91 | + |
| 92 | + LARGE_INTEGER offset; |
| 93 | + offset.QuadPart = aOffset; |
| 94 | + |
| 95 | + ntStatus = pNtMapViewOfSection(aFileMapping, aProcess, &aBaseAddress, 0, 0, |
| 96 | + &offset, &aViewSize, ViewUnmap, |
| 97 | + aAllocationType, aProtectionFlags); |
| 98 | + if (NT_SUCCESS(ntStatus)) { |
| 99 | + ::SetLastError(ERROR_SUCCESS); |
| 100 | + return aBaseAddress; |
| 101 | + } |
| 102 | + |
| 103 | + ::SetLastError(GetWin32ErrorCode(ntStatus)); |
| 104 | + return nullptr; |
| 105 | +} |
| 106 | + |
| 107 | +MFBT_API bool |
| 108 | +UnmapRemoteViewOfFile(HANDLE aProcess, PVOID aBaseAddress) |
| 109 | +{ |
| 110 | + static const DynamicallyLinkedFunctionPtr<decltype(&UnmapViewOfFile2)> |
| 111 | + pUnmapViewOfFile2(L"kernel32.dll", "UnmapViewOfFile2"); |
| 112 | + |
| 113 | + if (!!pUnmapViewOfFile2) { |
| 114 | + return !!pUnmapViewOfFile2(aProcess, aBaseAddress, 0); |
| 115 | + } |
| 116 | + |
| 117 | + static const DynamicallyLinkedFunctionPtr<decltype(&NtUnmapViewOfSection)> |
| 118 | + pNtUnmapViewOfSection(L"ntdll.dll", "NtUnmapViewOfSection"); |
| 119 | + |
| 120 | + MOZ_ASSERT(!!pNtUnmapViewOfSection); |
| 121 | + if (!pNtUnmapViewOfSection) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + NTSTATUS ntStatus = pNtUnmapViewOfSection(aProcess, aBaseAddress); |
| 126 | + ::SetLastError(GetWin32ErrorCode(ntStatus)); |
| 127 | + return NT_SUCCESS(ntStatus); |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace mozilla |
| 131 | + |
0 commit comments