public static void VirtualMemorySetProtection(void* pointer, IntPtr size, MemoryProtection protection) { int old; switch (protection) { case MemoryProtection.Execute: Kernel32.VirtualProtect(pointer, size, Kernel32.VirtualMemProtectionFlag.PageExecute, out old); break; case MemoryProtection.ReadWrite: Kernel32.VirtualProtect(pointer, size, Kernel32.VirtualMemProtectionFlag.PageReadWrite, out old); break; } }
public MemoryMappedFileStream(string name, uint maxLength, MemoryProtection protection) { if(string.IsNullOrEmpty(name)) { throw(new ArgumentException("Argument_Name")); } if(maxLength < 0) { throw(new ArgumentOutOfRangeException("ArgumentOutOfRange_MaxLength")); } objectName = name; if (protection < MemoryProtection.PageNoAccess || protection > MemoryProtection.SecReserve) { throw new ArgumentOutOfRangeException("ArgumentOutOfRange_Enum"); } if( maxLength == 0 ) { mapLength = 5120; } else { mapLength = maxLength; } memProtection = protection; isReadable = true; isWritable = true; isSeekable = true; // Initialize mapViewPointer mapViewPointer = new IntPtr(-1); SECURITY_ATTRIBUTES sa = SECURITY_ATTRIBUTES.GetNullDacl(); try { mapHandle = MemoryMappedFileHelper.CreateFileMapping( IntPtr.Zero, sa, memProtection, 0, mapLength, objectName); } finally { Marshal.FreeHGlobal(sa.lpSecurityDescriptor); sa.lpSecurityDescriptor = IntPtr.Zero; } if(mapHandle == IntPtr.Zero ) { uint lastError = MemoryMappedFileHelper.GetLastError(); throw new IOException( MemoryMappedFileHelper.GetWin32ErrorMessage( lastError ) ); } }
public PhysicalPagesMapping Map(IntPtr address, MemoryProtection protection) { IntPtr allocAddress = ProcessHandle.Current.AllocateMemory( address, _count * Windows.PageSize, MemoryFlags.Reserve | MemoryFlags.Physical, protection ); if (!Win32.MapUserPhysicalPages( allocAddress, new IntPtr(_count), _pfnArray )) Win32.ThrowLastError(); return new PhysicalPagesMapping(this, allocAddress); }
static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, IntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
internal static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, IntPtr dwSize, MemoryProtection flNewProtect, ref MemoryProtection lpflOldProtect);
public static extern bool VirtualProtectEx( IntPtr hProcess, IntPtr lpAddress, uint dwSize, MemoryProtection flNewProtect, [Out] MemoryProtection lpflOldProtect);
public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, MemoryProtection prot, out MemoryProtection oldProt);
public static extern IntPtr VirtualAlloc(IntPtr blockAddress, UIntPtr sizeInBytes, MemoryAllocationType allocationType, MemoryProtection protection);
public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
private static extern UIntPtr VirtualAlloc( UIntPtr lpAddress, UIntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
internal MemoryProtection ProtectVirtualMemory(IntPtr baseAddress, int protectionSize, MemoryProtection protectionType) { if (!VirtualProtectEx(_processHandle, baseAddress, protectionSize, protectionType, out var oldProtectionType)) { ExceptionHandler.ThrowWin32Exception("Failed to protect a region of virtual memory in the remote process"); } return(oldProtectionType); }
private static extern IntPtr VirtualAlloc(UInt32 lpStartAddr, UInt32 size, UInt32 flAllocationType, MemoryProtection flProtect);
public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, int nSize, MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
/// <summary> /// Reserves, commits, or changes the state of a region of pages in the virtual address space of the calling process. /// Memory allocated by this function is automatically initialized to zero. /// </summary> /// <param name="sizeInBytes">The size of the region, in bytes</param> /// <param name="allocationType">The type of memory allocation.</param> /// <param name="protection">The memory protection for the region of pages to be allocated.</param> /// <returns>The base address of the allocated region of pages.</returns> public static void* VirtualAlloc(ulong sizeInBytes, MemoryAllocationType allocationType, MemoryProtection protection) { var pointer = VirtualAlloc(IntPtr.Zero, new UIntPtr(sizeInBytes), allocationType, protection).ToPointer(); if (pointer == null) throw new InvalidOperationException(Marshal.GetLastWin32Error().ToString()); return pointer; }
public static int mprotect(IntPtr addr, UIntPtr len, MemoryProtection prot) => mprotect(addr, len, (int)prot);
private static extern bool __VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
public static void VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect) { if (!__VirtualProtectEx(hProcess, lpAddress, dwSize, flNewProtect, out lpflOldProtect)) { throw new Win32Exception(); } }
public static void VirtualMemorySetProtection(void* pointer, IntPtr size, MemoryProtection protection) { NativeUtilsImplementation.VirtualMemorySetProtection(pointer, size, protection); }
public static extern int VirtualAllocEx(IntPtr hProcess, Int32 lpAddress, Int32 dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
public void Protect(SafeProcessHandle handle, uint offset, uint size, MemoryProtection newProtect) { if (Address == IntPtr.Zero) throw new ObjectDisposedException("RemoteMemoryRegion"); if ((offset + size) > (Size)) throw new ArgumentException("Size too large for region"); MemoryProtection oldProtect; int result = Win32.VirtualProtectEx( handle.DangerousGetHandle(), (uint)(Address.ToInt64() + offset), size, newProtect, out oldProtect ); if (result == 0) { var error = Win32.GetLastError(); throw new Exception(String.Format("Protect failed: Error {0:x8}", error)); } }
public static IntPtr Alloc(Int32 size, MemoryProtection protection, AllocationType allocationType) { return(Memory.Alloc(IntPtr.Zero, size, protection, allocationType)); }
public static extern DWORD_PTR VirtualAllocEx( IntPtr hProcess, DWORD_PTR lpAddress, uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect );
public static IntPtr Alloc(IntPtr address, Int32 size, MemoryProtection protection, AllocationType allocationType) { return(Kernel32.VirtualAlloc(address, size, allocationType, protection)); }
public PhysicalPagesMapping Map(MemoryProtection protection) { return this.Map(IntPtr.Zero, protection); }
public static IntPtr Alloc(Int32 size, MemoryProtection protection) { return(Memory.Alloc(size, protection, AllocationType.Commit)); }
public static Win32FileMapAccess GetWin32FileMapAccess( MemoryProtection protection ) { switch ( protection ) { case MemoryProtection.PageReadOnly: return Win32FileMapAccess.FILE_MAP_READ; case MemoryProtection.PageWriteCopy: return Win32FileMapAccess.FILE_MAP_WRITE; default: return Win32FileMapAccess.FILE_MAP_ALL_ACCESS; } }
public static extern ulong VirtualAllocEx(IntPtr hProcess, ulong lpAddress, uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
internal static extern IntPtr VirtualAllocEx(SafeProcessHandle processHandle, IntPtr address, SizeT size, AllocationType flAllocationType, MemoryProtection flProtect);
public SafeVirtualMemoryHandle(IntPtr processHandle, Int64 address, Int64 size, AllocationType allocation, MemoryProtection protection) : base(true) { _processHandle = processHandle; _size = size; handle = Kernel32.VirtualAllocEx(processHandle, (IntPtr)address, (IntPtr)_size, allocation, protection); if (IsInvalid) { throw new Win32Exception(); } }
public static IntPtr mmap(IntPtr addr, UIntPtr length, MemoryProtection prot, int flags, int fd, IntPtr offset) => mmap(addr, length, (int)prot, flags, fd, offset);
private static extern IntPtr VirtualAllocEx(SafeMemoryHandle hProcess, IntPtr lpAddress, UIntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
public static extern void *VirtualAlloc(void *lpAddress, SizeT dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
public SafeVirtualMemoryHandle Allocate(Int64 size, AllocationType allocation, MemoryProtection protection) { return(new SafeVirtualMemoryHandle(handle, 0, size, allocation, protection)); }
static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, AllocationType lAllocationType, MemoryProtection flProtect);
public static bool Reprotect(IntPtr address, IntPtr size, Memory.MemoryProtection protection) { MemoryProtection prot = GetProtection(protection); return(VirtualProtect(address, size, prot, out _)); }
public static extern bool VirtualProtect(IntPtr address, uint size, MemoryProtection newProtect, out MemoryProtection oldProtect);
public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, IntPtr dwSize, MemoryProtection flNewProtect, ref MemoryProtection lpflOldProtect);
internal static extern IntPtr VirtualAllocEx(SafeProcessHandle hProcess, uint dwAddress, int nSize, AllocationType dwAllocationType, MemoryProtection dwProtect);
public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
public static IntPtr VirtualAlloc(UIntPtr sizeInBytes, MemoryAllocationType allocationType, MemoryProtection protection) { return VirtualAlloc(IntPtr.Zero, sizeInBytes, allocationType, protection); }
public static extern bool VirtualProtectEx([In] Handle hProcess, [In] IntPtr lpAddress, [In] int dwSize, [In] MemoryProtection flNewProtect, [Out] out MemoryProtection lpflOldProtect);
public static extern int VirtualProtectEx( IntPtr hProcess, UInt32 lpAddress, uint dwSize, MemoryProtection flNewProtect, out MemoryProtection flOldProtect );
public static extern bool VirtualProtect(byte *lpAddress, UIntPtr dwSize, MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
public static extern IntPtr VirtualAlloc(IntPtr address, Int32 size, AllocationType allocationType, MemoryProtection protection);
public static extern byte *VirtualAlloc(byte *lpAddress, UIntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
public MemoryFileSystem(string fileName, MfsOpenMode mode, bool readOnly, MfsParameters createParams) { FileCreationDispositionWin32 cdWin32; if (readOnly && mode != MfsOpenMode.Open) throw new ArgumentException("Invalid mode for read only access."); switch (mode) { case MfsOpenMode.Open: cdWin32 = FileCreationDispositionWin32.OpenExisting; break; default: case MfsOpenMode.OpenIf: cdWin32 = FileCreationDispositionWin32.OpenAlways; break; case MfsOpenMode.OverwriteIf: cdWin32 = FileCreationDispositionWin32.CreateAlways; break; } using (FileHandle fhandle = FileHandle.CreateWin32( fileName, FileAccess.GenericRead | (!readOnly ? FileAccess.GenericWrite : 0), FileShareMode.Read, cdWin32 )) { bool justCreated = false; _readOnly = readOnly; _protection = !readOnly ? MemoryProtection.ReadWrite : MemoryProtection.ReadOnly; if (fhandle.FileSize == 0) { if (readOnly) { throw new MfsInvalidFileSystemException(); } // File is too small. Make it 1 byte large and we'll deal with it soon. fhandle.SetEnd(1); } _section = new Section(fhandle, _protection); _blockSize = MfsBlockSizeBase; // fake block size to begin with; we'll fix it up later. if (fhandle.FileSize < _blockSize) { if (readOnly) { throw new MfsInvalidFileSystemException(); } // We're creating a new file system. We need the correct block size now. if (createParams != null) this._blockSize = createParams.BlockSize; else this._blockSize = MfsDefaultBlockSize; this._section.Extend(this._blockSize); using (SectionView view = this._section.MapView(0, this._blockSize, this._protection)) { this.InitializeFs((MfsFsHeader*)view.Memory, createParams); } justCreated = true; } _header = (MfsFsHeader*)this.ReferenceBlock(0); // Check the magic. if (_header->Magic != MfsMagic) throw new MfsInvalidFileSystemException(); // Set up the local constants. _blockSize = _header->BlockSize; _cellSize = _header->CellSize; // Backwards compatibility. if (_blockSize == 0) _blockSize = MfsDefaultBlockSize; if (_cellSize == 0) _cellSize = MfsDefaultCellSize; // Validate the parameters. this.ValidateFsParameters(_blockSize, _cellSize); _blockMask = _blockSize - 1; _cellCount = _blockSize / _cellSize; _dataCellDataMaxLength = _cellSize - MfsDataCell.DataOffset; // Remap block 0 with the correct block size. this.DereferenceBlock(0); // If we just created a new file system, fix the section size. if (justCreated) _section.Extend(_blockSize); _header = (MfsFsHeader*)this.ReferenceBlock(0); // Set up the root object. _rootObjectMo = new MemoryObject(this, _rootObjectCellId, true); if (_header->NextFreeBlock != 1 && !readOnly) { ushort lastBlockId = (ushort)(_header->NextFreeBlock - 1); this.ReferenceBlock(lastBlockId); _cachedLastBlockView = _views[lastBlockId]; } } }
internal static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr address, int size, AllocationType allocationType, MemoryProtection protect);
public PhysicalPagesMapping Map(IntPtr address, MemoryProtection protection) { // Reserve an address range. IntPtr allocAddress = ProcessHandle.Current.AllocateMemory( address, _count * Windows.PageSize, MemoryFlags.Reserve | MemoryFlags.Physical, protection ); // Map the physical memory into the address range. if (!Win32.MapUserPhysicalPages( allocAddress, new IntPtr(_count), _pfnArray )) Win32.Throw(); return new PhysicalPagesMapping(this, allocAddress); }
private static extern int VirtualProtect(IntPtr lpAddress, IntPtr dwSize, MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
public static extern IntPtr CreateFileMapping( IntPtr hFile, // Handle to file SECURITY_ATTRIBUTES lpAttributes, // Security MemoryProtection flProtect, // protection uint dwMaximumSizeHigh, // High-order DWORD of size uint dwMaximumSizeLow, // Low-order DWORD of size string lpName // Object name );
internal static extern bool VirtualProtect(IntPtr baseAddress, int protectionSize, MemoryProtection protectionType, out MemoryProtection oldProtectionType);
private string GetProtectStr(MemoryProtection protect) { string protectStr; if (protect == MemoryProtection.AccessDenied) protectStr = ""; else if ((protect & MemoryProtection.Execute) != 0) protectStr = "X"; else if ((protect & MemoryProtection.ExecuteRead) != 0) protectStr = "RX"; else if ((protect & MemoryProtection.ExecuteReadWrite) != 0) protectStr = "RWX"; else if ((protect & MemoryProtection.ExecuteWriteCopy) != 0) protectStr = "WCX"; else if ((protect & MemoryProtection.NoAccess) != 0) protectStr = "NA"; else if ((protect & MemoryProtection.ReadOnly) != 0) protectStr = "R"; else if ((protect & MemoryProtection.ReadWrite) != 0) protectStr = "RW"; else if ((protect & MemoryProtection.WriteCopy) != 0) protectStr = "WC"; else protectStr = "?"; if ((protect & MemoryProtection.Guard) != 0) protectStr += "+G"; if ((protect & MemoryProtection.NoCache) != 0) protectStr += "+NC"; if ((protect & MemoryProtection.WriteCombine) != 0) protectStr = "+WCM"; return protectStr; }
public static extern IntPtr VirtualAllocEx(SafeProcessHandle processHandle, IntPtr address, UIntPtr size, AllocationType allocationType, MemoryProtection memoryProtection);
public static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
internal static extern IntPtr VirtualAlloc(IntPtr startAddress, UIntPtr size, AllocationType allocationType, MemoryProtection protect);
public MemoryMappedFileStream(string name, MemoryProtection protection) : this(name,0,protection) { }