ReactOS  0.4.13-dev-101-g0ca4b50
mem.c File Reference
#include <win32k.h>
#include <debug.h>
Include dependency graph for mem.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

_Must_inspect_result_ _When_ (fl &FL_ZERO_MEMORY, _Ret_opt_bytecount_(cjMemSize)) _When_(!(fl &FL_ZERO_MEMORY)
 
_Must_inspect_result_ _Ret_opt_bytecap_ (cjMemSize)
 
VOID APIENTRY EngFreeMem (PVOID pvBaseAddress)
 
_Must_inspect_result_ _Ret_opt_bytecount_ (cjMemSize)
 
VOID APIENTRY EngFreeUserMem (PVOID pvBaseAddress)
 
PVOID APIENTRY HackSecureVirtualMemory (IN PVOID Address, IN SIZE_T Size, IN ULONG ProbeMode, OUT PVOID *SafeAddress)
 
VOID APIENTRY HackUnsecureVirtualMemory (IN PVOID SecureHandle)
 
HANDLE APIENTRY EngSecureMem (PVOID Address, ULONG Length)
 
HANDLE APIENTRY EngSecureMemForRead (PVOID Address, ULONG Length)
 
VOID APIENTRY EngUnsecureMem (HANDLE Mem)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 11 of file mem.c.

Function Documentation

◆ _Ret_opt_bytecap_()

_Must_inspect_result_ _Ret_opt_bytecap_ ( cjMemSize  )

Definition at line 19 of file mem.c.

28 {
29  PVOID pvBaseAddress;
30 
31  pvBaseAddress = ExAllocatePoolWithTag((fl & FL_NONPAGED_MEMORY) ?
33  cjMemSize,
34  ulTag);
35 
36  if (pvBaseAddress == NULL)
37  return NULL;
38 
39  if (fl & FL_ZERO_MEMORY)
40  RtlZeroMemory(pvBaseAddress, cjMemSize);
41 
42  return pvBaseAddress;
43 }
VOID NTAPI RtlZeroMemory(PVOID Destination, SIZE_T Length)
Definition: mem.c:155
_Must_inspect_result_ _In_ ULONG cjMemSize
Definition: winddi.h:1381
_In_ FLONG fl
Definition: winddi.h:1279
#define FL_NONPAGED_MEMORY
Definition: winddi.h:1359
smooth NULL
Definition: ftsmooth.c:416
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define FL_ZERO_MEMORY
Definition: polytest.cpp:58
_In_ ULONG _In_ ULONG ulTag
Definition: winddi.h:3941

◆ _Ret_opt_bytecount_()

_Must_inspect_result_ _Ret_opt_bytecount_ ( cjMemSize  )

Definition at line 64 of file mem.c.

72 {
73  PVOID pvBaseAddress = NULL;
75 
76  Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
77  &pvBaseAddress,
78  0,
79  &cjMemSize,
82 
83  if (!NT_SUCCESS(Status))
84  {
85  return NULL;
86  }
87 
88  /* TODO: Add allocation info to AVL tree (stored inside W32PROCESS structure) */
89  //hSecure = EngSecureMem(pvBaseAddress, cj);
90 
91  return pvBaseAddress;
92 }
LONG NTSTATUS
Definition: precomp.h:26
#define MEM_COMMIT
Definition: nt_native.h:1313
_Must_inspect_result_ _In_ ULONG cjMemSize
Definition: winddi.h:1381
#define MEM_RESERVE
Definition: nt_native.h:1314
smooth NULL
Definition: ftsmooth.c:416
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
Status
Definition: gdiplustypes.h:24
#define PAGE_READWRITE
Definition: nt_native.h:1304

◆ _When_()

_Must_inspect_result_ _When_ ( fl FL_ZERO_MEMORY,
_Ret_opt_bytecount_(cjMemSize  
) &

◆ EngFreeMem()

VOID APIENTRY EngFreeMem ( PVOID  pvBaseAddress)

Definition at line 50 of file mem.c.

51 {
52  /* Windows allows to pass NULL */
53  if (pvBaseAddress)
54  {
55  /* Use 0 as tag, which equals a call to ExFreePool */
56  ExFreePoolWithTag(pvBaseAddress, 0);
57  }
58 }
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099

◆ EngFreeUserMem()

VOID APIENTRY EngFreeUserMem ( PVOID  pvBaseAddress)

Definition at line 99 of file mem.c.

100 {
101  SIZE_T cjSize = 0;
102 
103  ZwFreeVirtualMemory(NtCurrentProcess(),
104  &pvBaseAddress,
105  &cjSize,
106  MEM_RELEASE);
107 
108  /* TODO: Remove allocation info from AVL tree */
109 }
#define NtCurrentProcess()
Definition: nt_native.h:1657
ULONG_PTR SIZE_T
Definition: typedefs.h:78
#define MEM_RELEASE
Definition: nt_native.h:1316
_In_ ULONG cjSize
Definition: winddi.h:3634

◆ EngSecureMem()

HANDLE APIENTRY EngSecureMem ( PVOID  Address,
ULONG  Length 
)

Definition at line 177 of file mem.c.

178 {
179  {// HACK!!!
180  _SEH2_TRY
181  {
183  }
185  {
186  _SEH2_YIELD(return NULL);
187  }
188  _SEH2_END;
189  return (HANDLE)-1;
190  }
192 }
VOID NTAPI ProbeForWrite(IN PVOID Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:143
_SEH2_TRY
Definition: create.c:4250
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
PVOID NTAPI MmSecureVirtualMemory(IN PVOID Address, IN SIZE_T Length, IN ULONG Mode)
Definition: virtual.c:2669
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
_SEH2_END
Definition: create.c:4424
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define PAGE_READWRITE
Definition: nt_native.h:1304

◆ EngSecureMemForRead()

HANDLE APIENTRY EngSecureMemForRead ( PVOID  Address,
ULONG  Length 
)

Definition at line 196 of file mem.c.

197 {
198  {// HACK!!!
199  ULONG cPages;
200  volatile BYTE *pjProbe;
201 
202  _SEH2_TRY
203  {
207  while(cPages--)
208  {
209  /* Do a read probe */
210  (void)*pjProbe;
211  pjProbe += PAGE_SIZE;
212  }
213  }
215  {
216  _SEH2_YIELD(return NULL);
217  }
218  _SEH2_END;
219  return (HANDLE)-1;
220  }
222 }
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
_SEH2_TRY
Definition: create.c:4250
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
#define ADDRESS_AND_SIZE_TO_SPAN_PAGES(_Va, _Size)
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
PVOID NTAPI MmSecureVirtualMemory(IN PVOID Address, IN SIZE_T Length, IN ULONG Mode)
Definition: virtual.c:2669
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
VOID NTAPI ProbeForRead(IN CONST VOID *Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:102
unsigned char BYTE
Definition: mem.h:68
#define PAGE_SIZE
Definition: env_spec_w32.h:49
_SEH2_END
Definition: create.c:4424
#define PAGE_READONLY
Definition: compat.h:127
unsigned int ULONG
Definition: retypes.h:1
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define ALIGN_DOWN_POINTER_BY(ptr, align)
Definition: umtypes.h:82

◆ EngUnsecureMem()

VOID APIENTRY EngUnsecureMem ( HANDLE  Mem)

Definition at line 228 of file mem.c.

229 {
230  if (Mem == (HANDLE)-1) return; // HACK!!!
232 }
VOID NTAPI MmUnsecureVirtualMemory(IN PVOID SecureMem)
Definition: virtual.c:2682

◆ HackSecureVirtualMemory()

PVOID APIENTRY HackSecureVirtualMemory ( IN PVOID  Address,
IN SIZE_T  Size,
IN ULONG  ProbeMode,
OUT PVOID SafeAddress 
)

Definition at line 113 of file mem.c.

118 {
120  PMDL pmdl;
122 
125  else return NULL;
126 
128  if (pmdl == NULL)
129  {
130  return NULL;
131  }
132 
133  _SEH2_TRY
134  {
136  }
138  {
140  }
141  _SEH2_END
142 
143  if (!NT_SUCCESS(Status))
144  {
145  IoFreeMdl(pmdl);
146  return NULL;
147  }
148 
149  *SafeAddress = MmGetSystemAddressForMdlSafe(pmdl, NormalPagePriority);
150 
151  if(!*SafeAddress)
152  {
153  MmUnlockPages(pmdl);
154  IoFreeMdl(pmdl);
155  return NULL;
156  }
157 
158  return pmdl;
159 }
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
_Must_inspect_result_ _In_ _In_ ULONG ProbeMode
Definition: mmfuncs.h:562
VOID NTAPI MmUnlockPages(IN PMDL Mdl)
Definition: mdlsup.c:1439
#define MmGetSystemAddressForMdlSafe(_Mdl, _Priority)
_SEH2_TRY
Definition: create.c:4250
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
VOID NTAPI IoFreeMdl(PMDL Mdl)
Definition: iomdl.c:146
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
Status
Definition: gdiplustypes.h:24
PMDL NTAPI IoAllocateMdl(IN PVOID VirtualAddress, IN ULONG Length, IN BOOLEAN SecondaryBuffer, IN BOOLEAN ChargeQuota, IN PIRP Irp)
Definition: iomdl.c:22
_SEH2_END
Definition: create.c:4424
VOID NTAPI MmProbeAndLockPages(IN PMDL Mdl, IN KPROCESSOR_MODE AccessMode, IN LOCK_OPERATION Operation)
Definition: mdlsup.c:935
enum _LOCK_OPERATION LOCK_OPERATION
#define PAGE_READONLY
Definition: compat.h:127
unsigned int ULONG
Definition: retypes.h:1
_In_ FLT_SET_CONTEXT_OPERATION Operation
Definition: fltkernel.h:1468
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
return STATUS_SUCCESS
Definition: btrfs.c:2725
#define PAGE_READWRITE
Definition: nt_native.h:1304

◆ HackUnsecureVirtualMemory()

VOID APIENTRY HackUnsecureVirtualMemory ( IN PVOID  SecureHandle)

Definition at line 163 of file mem.c.

165 {
166  PMDL pmdl = (PMDL)SecureHandle;
167 
168  MmUnlockPages(pmdl);
169  IoFreeMdl(pmdl);
170 }
VOID NTAPI MmUnlockPages(IN PMDL Mdl)
Definition: mdlsup.c:1439
PVOID PMDL
Definition: usb.h:39
VOID NTAPI IoFreeMdl(PMDL Mdl)
Definition: iomdl.c:146