| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Runtime.InteropServices; |
| | | 5 | | |
| | | 6 | | namespace System.Buffers |
| | | 7 | | { |
| | | 8 | | public static unsafe partial class BoundedMemory |
| | | 9 | | { |
| | | 10 | | private static WindowsImplementation<T> AllocateWithoutDataPopulationWindows<T>(int elementCount, PoisonPagePlac |
| | 0 | 11 | | { |
| | | 12 | | long cb, totalBytesToAllocate; |
| | | 13 | | checked |
| | 0 | 14 | | { |
| | 0 | 15 | | cb = elementCount * sizeof(T); |
| | 0 | 16 | | totalBytesToAllocate = cb; |
| | | 17 | | |
| | | 18 | | // We only need to round the count up if it's not an exact multiple |
| | | 19 | | // of the system page size. |
| | | 20 | | |
| | 0 | 21 | | long leftoverBytes = totalBytesToAllocate % SystemPageSize; |
| | 0 | 22 | | if (leftoverBytes != 0) |
| | 0 | 23 | | { |
| | 0 | 24 | | totalBytesToAllocate += SystemPageSize - leftoverBytes; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | // Finally, account for the poison pages at the front and back. |
| | | 28 | | |
| | 0 | 29 | | totalBytesToAllocate += 2 * SystemPageSize; |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | // Reserve and commit the entire range as NOACCESS. |
| | | 33 | | VirtualAllocHandle handle; |
| | | 34 | | checked |
| | 0 | 35 | | { |
| | 0 | 36 | | handle = VirtualAllocHandle.Allocate( |
| | 0 | 37 | | lpAddress: IntPtr.Zero, |
| | 0 | 38 | | dwSize: (IntPtr)totalBytesToAllocate /* cast throws OverflowException if out of range */, |
| | 0 | 39 | | flAllocationType: VirtualAllocAllocationType.MEM_RESERVE | VirtualAllocAllocationType.MEM_COMMIT, |
| | 0 | 40 | | flProtect: VirtualAllocProtection.PAGE_NOACCESS); |
| | 0 | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | if (handle == null || handle.IsInvalid) |
| | 0 | 44 | | { |
| | 0 | 45 | | int lastError = Marshal.GetHRForLastWin32Error(); |
| | 0 | 46 | | handle?.Dispose(); |
| | 0 | 47 | | Marshal.ThrowExceptionForHR(lastError); |
| | 0 | 48 | | throw new InvalidOperationException("VirtualAlloc failed unexpectedly."); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | // Done allocating! Now carve out a READWRITE section bookended by the NOACCESS |
| | | 52 | | // pages and return that carved-out section to the caller. Since memory protection |
| | | 53 | | // flags only apply at page-level granularity, we need to "left-align" or "right- |
| | | 54 | | // align" the section we carve out so that it's guaranteed adjacent to one of |
| | | 55 | | // the NOACCESS bookend pages. |
| | | 56 | | |
| | 0 | 57 | | return new WindowsImplementation<T>( |
| | 0 | 58 | | handle: handle, |
| | 0 | 59 | | byteOffsetIntoHandle: (placement == PoisonPagePlacement.Before) |
| | 0 | 60 | | ? SystemPageSize /* just after leading poison page */ |
| | 0 | 61 | | : checked((int)(totalBytesToAllocate - SystemPageSize - cb)) /* just before trailing poison page */, |
| | 0 | 62 | | elementCount: elementCount) |
| | 0 | 63 | | { |
| | 0 | 64 | | Protection = VirtualAllocProtection.PAGE_READWRITE |
| | 0 | 65 | | }; |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | private sealed class WindowsImplementation<T> : BoundedMemory<T> where T : unmanaged |
| | | 69 | | { |
| | | 70 | | private readonly VirtualAllocHandle _handle; |
| | | 71 | | private readonly int _byteOffsetIntoHandle; |
| | | 72 | | private readonly int _elementCount; |
| | | 73 | | private readonly BoundedMemoryManager _memoryManager; |
| | | 74 | | |
| | 0 | 75 | | internal WindowsImplementation(VirtualAllocHandle handle, int byteOffsetIntoHandle, int elementCount) |
| | 0 | 76 | | { |
| | 0 | 77 | | _handle = handle; |
| | 0 | 78 | | _byteOffsetIntoHandle = byteOffsetIntoHandle; |
| | 0 | 79 | | _elementCount = elementCount; |
| | 0 | 80 | | _memoryManager = new BoundedMemoryManager(this); |
| | 0 | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | public override bool IsReadonly => (Protection != VirtualAllocProtection.PAGE_READWRITE); |
| | | 84 | | |
| | 0 | 85 | | public override int Length => _elementCount; |
| | | 86 | | |
| | | 87 | | internal VirtualAllocProtection Protection |
| | | 88 | | { |
| | | 89 | | get |
| | 0 | 90 | | { |
| | 0 | 91 | | bool refAdded = false; |
| | | 92 | | try |
| | 0 | 93 | | { |
| | 0 | 94 | | _handle.DangerousAddRef(ref refAdded); |
| | | 95 | | MEMORY_BASIC_INFORMATION memoryInfo; |
| | 0 | 96 | | if (UnsafeNativeMethods.VirtualQuery( |
| | 0 | 97 | | lpAddress: _handle.DangerousGetHandle() + _byteOffsetIntoHandle, |
| | 0 | 98 | | lpBuffer: &memoryInfo, |
| | 0 | 99 | | dwLength: (IntPtr)sizeof(MEMORY_BASIC_INFORMATION)) == IntPtr.Zero) |
| | 0 | 100 | | { |
| | 0 | 101 | | Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); |
| | 0 | 102 | | throw new InvalidOperationException("VirtualQuery failed unexpectedly."); |
| | | 103 | | } |
| | 0 | 104 | | return memoryInfo.Protect; |
| | | 105 | | } |
| | | 106 | | finally |
| | 0 | 107 | | { |
| | 0 | 108 | | if (refAdded) |
| | 0 | 109 | | { |
| | 0 | 110 | | _handle.DangerousRelease(); |
| | 0 | 111 | | } |
| | 0 | 112 | | } |
| | 0 | 113 | | } |
| | | 114 | | set |
| | 0 | 115 | | { |
| | 0 | 116 | | if (_elementCount > 0) |
| | 0 | 117 | | { |
| | 0 | 118 | | bool refAdded = false; |
| | | 119 | | try |
| | 0 | 120 | | { |
| | 0 | 121 | | _handle.DangerousAddRef(ref refAdded); |
| | | 122 | | VirtualAllocProtection flOldProtect; |
| | 0 | 123 | | if (UnsafeNativeMethods.VirtualProtect( |
| | 0 | 124 | | lpAddress: _handle.DangerousGetHandle() + _byteOffsetIntoHandle, |
| | 0 | 125 | | dwSize: (IntPtr)(&((T*)null)[_elementCount]), |
| | 0 | 126 | | flNewProtect: value, |
| | 0 | 127 | | lpflOldProtect: &flOldProtect) == 0) |
| | 0 | 128 | | { |
| | 0 | 129 | | Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); |
| | 0 | 130 | | throw new InvalidOperationException("VirtualProtect failed unexpectedly."); |
| | | 131 | | } |
| | 0 | 132 | | } |
| | | 133 | | finally |
| | 0 | 134 | | { |
| | 0 | 135 | | if (refAdded) |
| | 0 | 136 | | { |
| | 0 | 137 | | _handle.DangerousRelease(); |
| | 0 | 138 | | } |
| | 0 | 139 | | } |
| | 0 | 140 | | } |
| | 0 | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | 0 | 144 | | public override Memory<T> Memory => _memoryManager.Memory; |
| | | 145 | | |
| | | 146 | | public override Span<T> Span |
| | | 147 | | { |
| | | 148 | | get |
| | 0 | 149 | | { |
| | 0 | 150 | | bool refAdded = false; |
| | | 151 | | try |
| | 0 | 152 | | { |
| | 0 | 153 | | _handle.DangerousAddRef(ref refAdded); |
| | 0 | 154 | | return new Span<T>((void*)(_handle.DangerousGetHandle() + _byteOffsetIntoHandle), _elementCount) |
| | | 155 | | } |
| | | 156 | | finally |
| | 0 | 157 | | { |
| | 0 | 158 | | if (refAdded) |
| | 0 | 159 | | { |
| | 0 | 160 | | _handle.DangerousRelease(); |
| | 0 | 161 | | } |
| | 0 | 162 | | } |
| | 0 | 163 | | } |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | public override void Dispose() |
| | 0 | 167 | | { |
| | 0 | 168 | | _handle.Dispose(); |
| | 0 | 169 | | } |
| | | 170 | | |
| | | 171 | | public override void MakeReadonly() |
| | 0 | 172 | | { |
| | 0 | 173 | | Protection = VirtualAllocProtection.PAGE_READONLY; |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | public override void MakeWriteable() |
| | 0 | 177 | | { |
| | 0 | 178 | | Protection = VirtualAllocProtection.PAGE_READWRITE; |
| | 0 | 179 | | } |
| | | 180 | | |
| | | 181 | | private sealed class BoundedMemoryManager : MemoryManager<T> |
| | | 182 | | { |
| | | 183 | | private readonly WindowsImplementation<T> _impl; |
| | | 184 | | |
| | 0 | 185 | | public BoundedMemoryManager(WindowsImplementation<T> impl) |
| | 0 | 186 | | { |
| | 0 | 187 | | _impl = impl; |
| | 0 | 188 | | } |
| | | 189 | | |
| | 0 | 190 | | public override Memory<T> Memory => CreateMemory(_impl._elementCount); |
| | | 191 | | |
| | | 192 | | protected override void Dispose(bool disposing) |
| | 0 | 193 | | { |
| | | 194 | | // no-op; the handle will be disposed separately |
| | 0 | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | public override Span<T> GetSpan() => _impl.Span; |
| | | 198 | | |
| | | 199 | | public override MemoryHandle Pin(int elementIndex) |
| | 0 | 200 | | { |
| | 0 | 201 | | if ((uint)elementIndex > (uint)_impl._elementCount) |
| | 0 | 202 | | { |
| | 0 | 203 | | throw new ArgumentOutOfRangeException(paramName: nameof(elementIndex)); |
| | | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | bool refAdded = false; |
| | | 207 | | try |
| | 0 | 208 | | { |
| | 0 | 209 | | _impl._handle.DangerousAddRef(ref refAdded); |
| | 0 | 210 | | return new MemoryHandle((T*)(_impl._handle.DangerousGetHandle() + _impl._byteOffsetIntoHandle) + |
| | | 211 | | } |
| | | 212 | | finally |
| | 0 | 213 | | { |
| | 0 | 214 | | if (refAdded) |
| | 0 | 215 | | { |
| | 0 | 216 | | _impl._handle.DangerousRelease(); |
| | 0 | 217 | | } |
| | 0 | 218 | | } |
| | 0 | 219 | | } |
| | | 220 | | |
| | | 221 | | public override void Unpin() |
| | 0 | 222 | | { |
| | | 223 | | // no-op - we don't unpin native memory |
| | 0 | 224 | | } |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | // from winnt.h |
| | | 229 | | [Flags] |
| | | 230 | | private enum VirtualAllocAllocationType : uint |
| | | 231 | | { |
| | | 232 | | MEM_COMMIT = 0x1000, |
| | | 233 | | MEM_RESERVE = 0x2000, |
| | | 234 | | MEM_DECOMMIT = 0x4000, |
| | | 235 | | MEM_RELEASE = 0x8000, |
| | | 236 | | MEM_FREE = 0x10000, |
| | | 237 | | MEM_PRIVATE = 0x20000, |
| | | 238 | | MEM_MAPPED = 0x40000, |
| | | 239 | | MEM_RESET = 0x80000, |
| | | 240 | | MEM_TOP_DOWN = 0x100000, |
| | | 241 | | MEM_WRITE_WATCH = 0x200000, |
| | | 242 | | MEM_PHYSICAL = 0x400000, |
| | | 243 | | MEM_ROTATE = 0x800000, |
| | | 244 | | MEM_LARGE_PAGES = 0x20000000, |
| | | 245 | | MEM_4MB_PAGES = 0x80000000, |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | // from winnt.h |
| | | 249 | | [Flags] |
| | | 250 | | private enum VirtualAllocProtection : uint |
| | | 251 | | { |
| | | 252 | | PAGE_NOACCESS = 0x01, |
| | | 253 | | PAGE_READONLY = 0x02, |
| | | 254 | | PAGE_READWRITE = 0x04, |
| | | 255 | | PAGE_WRITECOPY = 0x08, |
| | | 256 | | PAGE_EXECUTE = 0x10, |
| | | 257 | | PAGE_EXECUTE_READ = 0x20, |
| | | 258 | | PAGE_EXECUTE_READWRITE = 0x40, |
| | | 259 | | PAGE_EXECUTE_WRITECOPY = 0x80, |
| | | 260 | | PAGE_GUARD = 0x100, |
| | | 261 | | PAGE_NOCACHE = 0x200, |
| | | 262 | | PAGE_WRITECOMBINE = 0x400, |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | [StructLayout(LayoutKind.Sequential)] |
| | | 266 | | private struct MEMORY_BASIC_INFORMATION |
| | | 267 | | { |
| | | 268 | | public IntPtr BaseAddress; |
| | | 269 | | public IntPtr AllocationBase; |
| | | 270 | | public VirtualAllocProtection AllocationProtect; |
| | | 271 | | public IntPtr RegionSize; |
| | | 272 | | public VirtualAllocAllocationType State; |
| | | 273 | | public VirtualAllocProtection Protect; |
| | | 274 | | public VirtualAllocAllocationType Type; |
| | | 275 | | }; |
| | | 276 | | |
| | | 277 | | private sealed class VirtualAllocHandle : SafeHandle |
| | | 278 | | { |
| | | 279 | | // Called by P/Invoke when returning SafeHandles |
| | | 280 | | public VirtualAllocHandle() |
| | 0 | 281 | | : base(IntPtr.Zero, ownsHandle: true) |
| | 0 | 282 | | { |
| | 0 | 283 | | } |
| | | 284 | | |
| | | 285 | | internal static VirtualAllocHandle Allocate(IntPtr lpAddress, IntPtr dwSize, VirtualAllocAllocationType flAl |
| | 0 | 286 | | { |
| | 0 | 287 | | VirtualAllocHandle retVal = new VirtualAllocHandle(); |
| | 0 | 288 | | retVal.SetHandle(UnsafeNativeMethods.VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect)); |
| | 0 | 289 | | return retVal; |
| | 0 | 290 | | } |
| | | 291 | | |
| | | 292 | | // Do not provide a finalizer - SafeHandle's critical finalizer will |
| | | 293 | | // call ReleaseHandle for you. |
| | | 294 | | |
| | 0 | 295 | | public override bool IsInvalid => (handle == IntPtr.Zero); |
| | | 296 | | |
| | | 297 | | protected override bool ReleaseHandle() => |
| | 0 | 298 | | UnsafeNativeMethods.VirtualFree(handle, IntPtr.Zero, VirtualAllocAllocationType.MEM_RELEASE) != 0; |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | private static partial class UnsafeNativeMethods |
| | | 302 | | { |
| | | 303 | | private const string KERNEL32_LIB = "kernel32.dll"; |
| | | 304 | | |
| | | 305 | | // https://msdn.microsoft.com/en-us/library/windows/desktop/aa366887(v=vs.85).aspx |
| | | 306 | | [DllImport(KERNEL32_LIB, SetLastError = true)] |
| | | 307 | | public static extern IntPtr VirtualAlloc(IntPtr lpAddress, IntPtr dwSize, VirtualAllocAllocationType flAlloc |
| | | 308 | | |
| | | 309 | | // https://msdn.microsoft.com/en-us/library/windows/desktop/aa366892(v=vs.85).aspx |
| | | 310 | | [DllImport(KERNEL32_LIB, SetLastError = true)] |
| | | 311 | | public static extern int VirtualFree(IntPtr lpAddress, IntPtr dwSize, VirtualAllocAllocationType dwFreeType) |
| | | 312 | | |
| | | 313 | | // https://msdn.microsoft.com/en-us/library/windows/desktop/aa366898(v=vs.85).aspx |
| | | 314 | | [DllImport(KERNEL32_LIB, SetLastError = true)] |
| | | 315 | | public static extern int VirtualProtect(IntPtr lpAddress, IntPtr dwSize, VirtualAllocProtection flNewProtect |
| | | 316 | | |
| | | 317 | | // https://msdn.microsoft.com/en-us/library/windows/desktop/aa366902(v=vs.85).aspx |
| | | 318 | | [DllImport(KERNEL32_LIB, SetLastError = true)] |
| | | 319 | | public static extern IntPtr VirtualQuery(IntPtr lpAddress, MEMORY_BASIC_INFORMATION* lpBuffer, IntPtr dwLeng |
| | | 320 | | } |
| | | 321 | | } |
| | | 322 | | } |