ReactOS  0.4.15-dev-3187-ge372f2b
usrheap.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct _WIN32HEAP WIN32HEAP
 
typedef struct _WIN32HEAP * PWIN32HEAP
 

Functions

PWIN32HEAP UserCreateHeap (OUT PVOID *SectionObject, IN OUT PVOID *SystemBase, IN SIZE_T HeapSize)
 
NTSTATUS UnmapGlobalUserHeap (IN PEPROCESS Process)
 
NTSTATUS MapGlobalUserHeap (IN PEPROCESS Process, OUT PVOID *KernelMapping, OUT PVOID *UserMapping)
 
static __inline PVOID UserHeapAlloc (SIZE_T Bytes)
 
static __inline BOOL UserHeapFree (PVOID lpMem)
 
static __inline PVOID UserHeapReAlloc (PVOID lpMem, SIZE_T Bytes)
 
static __inline PVOID UserHeapAddressToUser (PVOID lpMem)
 

Variables

HANDLE GlobalUserHeap
 
PVOID GlobalUserHeapSection
 

Typedef Documentation

◆ PWIN32HEAP

typedef struct _WIN32HEAP * PWIN32HEAP

Definition at line 3 of file usrheap.h.

◆ WIN32HEAP

typedef struct _WIN32HEAP WIN32HEAP

Definition at line 3 of file usrheap.h.

Function Documentation

◆ MapGlobalUserHeap()

NTSTATUS MapGlobalUserHeap ( IN PEPROCESS  Process,
OUT PVOID KernelMapping,
OUT PVOID UserMapping 
)

Definition at line 266 of file usrheap.c.

269 {
271  PPROCESSINFO W32Process;
272  PW32HEAP_USER_MAPPING HeapMapping;
273  PVOID UserBase = NULL;
274 
275  SIZE_T ViewSize = 0;
277 
278  TRACE_CH(UserProcess, "MapGlobalUserHeap called for process 0x%p\n", Process);
279 
280  W32Process = PsGetProcessWin32Process(Process);
281  if (W32Process == NULL)
282  {
283  ERR_CH(UserProcess, "MapGlobalUserHeap - We don't have a Win32 process!\n");
284  ASSERT(FALSE);
285  }
286 
287  TRACE_CH(UserProcess, "MapGlobalUserHeap - We got a Win32 process, find for existing global user heap mapping...\n");
288 
289  /* The first mapping entry must be the global user heap */
290  HeapMapping = &W32Process->HeapMappings;
291 
292  /* Find out if another thread already mapped the global user heap */
293  if (HeapMapping->KernelMapping == (PVOID)GlobalUserHeap)
294  {
295  HeapMapping->Count++;
296 
297  TRACE_CH(UserProcess, "MapGlobalUserHeap - A mapping was found, return it.\n");
298 
299  *KernelMapping = HeapMapping->KernelMapping;
300  *UserMapping = HeapMapping->UserMapping;
301 
302  return STATUS_SUCCESS;
303  }
304 
305  TRACE_CH(UserProcess, "MapGlobalUserHeap - No mapping was found, let's map...\n");
306 
307  /* We're the first, map the global heap into the process */
308  Offset.QuadPart = 0;
310  Process,
311  &UserBase,
312  0,
313  0,
314  &Offset,
315  &ViewSize,
316  ViewUnmap,
318  PAGE_EXECUTE_READ); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
319  if (!NT_SUCCESS(Status))
320  {
321  ERR_CH(UserProcess, "MapGlobalUserHeap - Failed to map the global heap! 0x%x\n", Status);
322  return Status;
323  }
324 
325  TRACE_CH(UserProcess, "MapGlobalUserHeap -- Mapped kernel global heap 0x%p to user space at 0x%p\n",
326  GlobalUserHeap, UserBase);
327 
328  /* Add the mapping */
329  HeapMapping->Next = NULL;
330  HeapMapping->KernelMapping = (PVOID)GlobalUserHeap;
331  HeapMapping->UserMapping = UserBase;
332  HeapMapping->Limit = ViewSize;
333  HeapMapping->Count = 1;
334 
335  *KernelMapping = HeapMapping->KernelMapping;
336  *UserMapping = HeapMapping->UserMapping;
337 
338  return STATUS_SUCCESS;
339 }
PVOID NTAPI PsGetProcessWin32Process(PEPROCESS Process)
Definition: process.c:1193
LONG NTSTATUS
Definition: precomp.h:26
static GENERIC_MAPPING UserMapping
Definition: samrpc.c:48
#define FALSE
Definition: types.h:117
NTSTATUS NTAPI MmMapViewOfSection(IN PVOID SectionObject, IN PEPROCESS Process, IN OUT PVOID *BaseAddress, IN ULONG_PTR ZeroBits, IN SIZE_T CommitSize, IN OUT PLARGE_INTEGER SectionOffset OPTIONAL, IN OUT PSIZE_T ViewSize, IN SECTION_INHERIT InheritDisposition, IN ULONG AllocationType, IN ULONG Protect)
Definition: section.c:3906
W32HEAP_USER_MAPPING HeapMappings
Definition: win32.h:288
void * PVOID
Definition: retypes.h:9
Status
Definition: gdiplustypes.h:24
ULONG_PTR Limit
Definition: win32.h:204
#define ASSERT(a)
Definition: mode.c:44
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
PVOID GlobalUserHeapSection
Definition: usrheap.c:26
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define TRACE_CH(ch, fmt,...)
Definition: debug.h:108
HANDLE GlobalUserHeap
Definition: usrheap.c:25
#define NULL
Definition: types.h:112
#define ERR_CH(ch, fmt,...)
Definition: debug.h:105
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:404
#define PAGE_EXECUTE_READ
Definition: nt_native.h:1307
#define STATUS_SUCCESS
Definition: shellext.h:65
#define SEC_NO_CHANGE
Definition: mmtypes.h:94
struct _W32HEAP_USER_MAPPING * Next
Definition: win32.h:201

Referenced by InitProcessCallback().

◆ UnmapGlobalUserHeap()

NTSTATUS UnmapGlobalUserHeap ( IN PEPROCESS  Process)

Definition at line 236 of file usrheap.c.

237 {
239  PPROCESSINFO W32Process;
240  PW32HEAP_USER_MAPPING HeapMapping;
241 
242  TRACE_CH(UserProcess, "IntUnmapDesktopView called for process 0x%p\n", Process);
243 
244  W32Process = PsGetProcessWin32Process(Process);
245  if (W32Process == NULL)
246  {
247  ERR_CH(UserProcess, "UnmapGlobalUserHeap - We don't have a Win32 process!\n");
248  ASSERT(FALSE);
249  }
250 
251  /* The first mapping entry must be the global user heap */
252  HeapMapping = &W32Process->HeapMappings;
253  ASSERT(HeapMapping->KernelMapping == (PVOID)GlobalUserHeap);
254 
255  /* Unmap if we're the last thread using the global user heap */
256  if (--HeapMapping->Count == 0)
257  {
258  TRACE_CH(UserProcess, "UnmapGlobalUserHeap - Unmapping\n");
260  }
261 
262  return Status;
263 }
PVOID NTAPI PsGetProcessWin32Process(PEPROCESS Process)
Definition: process.c:1193
LONG NTSTATUS
Definition: precomp.h:26
#define FALSE
Definition: types.h:117
W32HEAP_USER_MAPPING HeapMappings
Definition: win32.h:288
Status
Definition: gdiplustypes.h:24
NTSTATUS NTAPI MmUnmapViewOfSection(IN PEPROCESS Process, IN PVOID BaseAddress)
Definition: section.c:3109
#define ASSERT(a)
Definition: mode.c:44
#define TRACE_CH(ch, fmt,...)
Definition: debug.h:108
HANDLE GlobalUserHeap
Definition: usrheap.c:25
#define NULL
Definition: types.h:112
#define ERR_CH(ch, fmt,...)
Definition: debug.h:105
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
#define STATUS_SUCCESS
Definition: shellext.h:65

◆ UserCreateHeap()

PWIN32HEAP UserCreateHeap ( OUT PVOID SectionObject,
IN OUT PVOID SystemBase,
IN SIZE_T  HeapSize 
)

Definition at line 181 of file usrheap.c.

184 {
185  LARGE_INTEGER SizeHeap;
186  PWIN32HEAP pHeap = NULL;
188 
189  SizeHeap.QuadPart = HeapSize;
190 
191  /* Create the section and map it into session space */
194  NULL,
195  &SizeHeap,
196  PAGE_EXECUTE_READWRITE, /* Would prefer PAGE_READWRITE, but thanks to RTL heaps... */
197  SEC_RESERVE | 1,
198  NULL,
199  NULL);
200 
201  if (!NT_SUCCESS(Status))
202  {
204  return FALSE;
205  }
206 
208  SystemBase,
209  &HeapSize);
210  if (!NT_SUCCESS(Status))
211  {
213  *SectionObject = NULL;
214 
216  return FALSE;
217  }
218 
219  /* Create the heap */
221  SystemBase,
222  HeapSize);
223 
224  if (pHeap == NULL)
225  {
227  *SectionObject = NULL;
228 
230  }
231 
232  return pHeap;
233 }
_Must_inspect_result_ _Outptr_ PVOID * SectionObject
Definition: fsrtlfuncs.h:860
struct _WIN32HEAP * PWIN32HEAP
Definition: usrheap.h:3
LONG NTSTATUS
Definition: precomp.h:26
SIZE_T WINAPI HeapSize(HANDLE, DWORD, LPCVOID)
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
#define FALSE
Definition: types.h:117
#define SEC_RESERVE
Definition: nt_native.h:1323
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSTATUS NTAPI MmCreateSection(OUT PVOID *Section, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN PLARGE_INTEGER MaximumSize, IN ULONG SectionPageProtection, IN ULONG AllocationAttributes, IN HANDLE FileHandle OPTIONAL, IN PFILE_OBJECT FileObject OPTIONAL)
Definition: section.c:4509
#define ObDereferenceObject
Definition: obfuncs.h:203
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
VOID FASTCALL SetLastNtError(NTSTATUS Status)
Definition: error.c:36
#define NULL
Definition: types.h:112
static PWIN32HEAP IntUserHeapCreate(IN PVOID SectionObject, IN PVOID *SystemMappedBase, IN ULONG HeapSize)
Definition: usrheap.c:119
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
NTSTATUS NTAPI MmMapViewInSessionSpace(IN PVOID Section, OUT PVOID *MappedBase, IN OUT PSIZE_T ViewSize)
Definition: section.c:3046
LONGLONG QuadPart
Definition: typedefs.h:114

Referenced by DriverEntry(), and UserInitializeDesktop().

◆ UserHeapAddressToUser()

static __inline PVOID UserHeapAddressToUser ( PVOID  lpMem)
static

Definition at line 99 of file usrheap.h.

100 {
102 
103  /* The first mapping entry is the global user heap mapping */
104  return (PVOID)(((ULONG_PTR)lpMem - (ULONG_PTR)GlobalUserHeap) +
105  (ULONG_PTR)W32Process->HeapMappings.UserMapping);
106 }
HANDLE GlobalUserHeap
Definition: usrheap.c:25
uint32_t ULONG_PTR
Definition: typedefs.h:65
W32HEAP_USER_MAPPING HeapMappings
Definition: win32.h:288
PVOID NTAPI PsGetCurrentProcessWin32Process(VOID)
Definition: process.c:1183
#define ULONG_PTR
Definition: config.h:101

Referenced by IntMsgCreateStructW(), and UserGetClassInfo().

◆ UserHeapAlloc()

static __inline PVOID UserHeapAlloc ( SIZE_T  Bytes)
static

Definition at line 34 of file usrheap.h.

35 {
36  /* User heap has no lock, using global user lock instead. */
40  Bytes);
41 }
HANDLE GlobalUserHeap
Definition: usrheap.c:25
#define HEAP_NO_SERIALIZE
Definition: nt_native.h:1692
_In_ UINT Bytes
Definition: mmcopy.h:9
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
#define ASSERT(a)
Definition: mode.c:44
BOOL FASTCALL UserIsEnteredExclusive(VOID)
Definition: ntuser.c:229

Referenced by _Success_(), DriverEntry(), IntCreateClass(), IntMapDesktopView(), IntMoveClassToSharedHeap(), IntMsgCreateStructW(), IntSetClassMenuName(), and UserCreateHandleTable().

◆ UserHeapFree()

static __inline BOOL UserHeapFree ( PVOID  lpMem)
static

Definition at line 44 of file usrheap.h.

45 {
46  /* User heap has no lock, using global user lock instead. */
50  lpMem);
51 }
HANDLE GlobalUserHeap
Definition: usrheap.c:25
#define HEAP_NO_SERIALIZE
Definition: nt_native.h:1692
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
#define ASSERT(a)
Definition: mode.c:44
BOOL FASTCALL UserIsEnteredExclusive(VOID)
Definition: ntuser.c:229

Referenced by _Success_(), co_UserCreateWindowEx(), FreeProcMarkObject(), FreeSysObject(), FreeThreadObject(), IntCallWndProc(), IntCallWndProcRet(), IntCreateClass(), IntDestroyClass(), IntFreeClassMenuName(), IntSetClassMenuName(), IntUnmapDesktopView(), UserCreateHandleTable(), and UserRemoveWindowProps().

◆ UserHeapReAlloc()

static __inline PVOID UserHeapReAlloc ( PVOID  lpMem,
SIZE_T  Bytes 
)
static

Definition at line 54 of file usrheap.h.

56 {
57 #if 0
58  /* NOTE: ntoskrnl doesn't export RtlReAllocateHeap... */
61  lpMem,
62  Bytes);
63 #else
64  SIZE_T PrevSize;
65  PVOID pNew;
66 
67  /* User heap has no lock, using global user lock instead. */
69 
70  PrevSize = RtlSizeHeap(GlobalUserHeap,
72  lpMem);
73 
74  if (PrevSize == Bytes)
75  return lpMem;
76 
79  Bytes);
80  if (pNew != NULL)
81  {
82  if (PrevSize < Bytes)
83  Bytes = PrevSize;
84 
85  RtlCopyMemory(pNew,
86  lpMem,
87  Bytes);
88 
91  lpMem);
92  }
93 
94  return pNew;
95 #endif
96 }
HANDLE GlobalUserHeap
Definition: usrheap.c:25
#define HEAP_NO_SERIALIZE
Definition: nt_native.h:1692
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
_In_ UINT Bytes
Definition: mmcopy.h:9
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
#define ASSERT(a)
Definition: mode.c:44
NTSYSAPI SIZE_T NTAPI RtlSizeHeap(_In_ PVOID HeapHandle, _In_ ULONG Flags, _In_ PVOID MemoryPointer)
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define NULL
Definition: types.h:112
BOOL FASTCALL UserIsEnteredExclusive(VOID)
Definition: ntuser.c:229
NTSYSAPI PVOID WINAPI RtlReAllocateHeap(HANDLE, ULONG, PVOID, SIZE_T)
Definition: heap.c:2667
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263

Referenced by alloc_user_entry().

Variable Documentation

◆ GlobalUserHeap

◆ GlobalUserHeapSection

PVOID GlobalUserHeapSection

Definition at line 26 of file usrheap.c.

Referenced by _Function_class_(), DriverEntry(), and MapGlobalUserHeap().