ReactOS  0.4.14-dev-115-g4576127
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 262 of file usrheap.c.

265 {
267  PPROCESSINFO W32Process;
268  PW32HEAP_USER_MAPPING HeapMapping;
269  PVOID UserBase = NULL;
270 
271  SIZE_T ViewSize = 0;
273 
274  TRACE_CH(UserProcess, "MapGlobalUserHeap called for process 0x%p\n", Process);
275 
276  W32Process = PsGetProcessWin32Process(Process);
277  if (W32Process == NULL)
278  {
279  ERR_CH(UserProcess, "MapGlobalUserHeap - We don't have a Win32 process!\n");
280  ASSERT(FALSE);
281  }
282 
283  TRACE_CH(UserProcess, "MapGlobalUserHeap - We got a Win32 process, find for existing global user heap mapping...\n");
284 
285  /* The first mapping entry must be the global user heap */
286  HeapMapping = &W32Process->HeapMappings;
287 
288  /* Find out if another thread already mapped the global user heap */
289  if (HeapMapping->KernelMapping == (PVOID)GlobalUserHeap)
290  {
291  HeapMapping->Count++;
292 
293  TRACE_CH(UserProcess, "MapGlobalUserHeap - A mapping was found, return it.\n");
294 
295  *KernelMapping = HeapMapping->KernelMapping;
296  *UserMapping = HeapMapping->UserMapping;
297 
298  return STATUS_SUCCESS;
299  }
300 
301  TRACE_CH(UserProcess, "MapGlobalUserHeap - No mapping was found, let's map...\n");
302 
303  /* We're the first, map the global heap into the process */
304  Offset.QuadPart = 0;
306  Process,
307  &UserBase,
308  0,
309  0,
310  &Offset,
311  &ViewSize,
312  ViewUnmap,
314  PAGE_EXECUTE_READ); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
315  if (!NT_SUCCESS(Status))
316  {
317  ERR_CH(UserProcess, "MapGlobalUserHeap - Failed to map the global heap! 0x%x\n", Status);
318  return Status;
319  }
320 
321  TRACE_CH(UserProcess, "MapGlobalUserHeap -- Mapped kernel global heap 0x%p to user space at 0x%p\n",
322  GlobalUserHeap, UserBase);
323 
324  /* Add the mapping */
325  HeapMapping->Next = NULL;
326  HeapMapping->KernelMapping = (PVOID)GlobalUserHeap;
327  HeapMapping->UserMapping = UserBase;
328  HeapMapping->Limit = ViewSize;
329  HeapMapping->Count = 1;
330 
331  *KernelMapping = HeapMapping->KernelMapping;
332  *UserMapping = HeapMapping->UserMapping;
333 
334  return STATUS_SUCCESS;
335 }
PVOID NTAPI PsGetProcessWin32Process(PEPROCESS Process)
Definition: process.c:1193
LONG NTSTATUS
Definition: precomp.h:26
static GENERIC_MAPPING UserMapping
Definition: samrpc.c:48
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
smooth NULL
Definition: ftsmooth.c:416
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:4502
W32HEAP_USER_MAPPING HeapMappings
Definition: win32.h:281
void * PVOID
Definition: retypes.h:9
ULONG_PTR Limit
Definition: win32.h:197
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
PVOID GlobalUserHeapSection
Definition: usrheap.c:26
Status
Definition: gdiplustypes.h:24
ULONG_PTR SIZE_T
Definition: typedefs.h:78
#define TRACE_CH(ch, fmt,...)
Definition: debug.h:107
HANDLE GlobalUserHeap
Definition: usrheap.c:25
#define ERR_CH(ch, fmt,...)
Definition: debug.h:104
_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
return STATUS_SUCCESS
Definition: btrfs.c:2966
#define SEC_NO_CHANGE
Definition: mmtypes.h:94
struct _W32HEAP_USER_MAPPING * Next
Definition: win32.h:194

Referenced by InitProcessCallback().

◆ UnmapGlobalUserHeap()

NTSTATUS UnmapGlobalUserHeap ( IN PEPROCESS  Process)

Definition at line 232 of file usrheap.c.

233 {
235  PPROCESSINFO W32Process;
236  PW32HEAP_USER_MAPPING HeapMapping;
237 
238  TRACE_CH(UserProcess, "IntUnmapDesktopView called for process 0x%p\n", Process);
239 
240  W32Process = PsGetProcessWin32Process(Process);
241  if (W32Process == NULL)
242  {
243  ERR_CH(UserProcess, "UnmapGlobalUserHeap - We don't have a Win32 process!\n");
244  ASSERT(FALSE);
245  }
246 
247  /* The first mapping entry must be the global user heap */
248  HeapMapping = &W32Process->HeapMappings;
249  ASSERT(HeapMapping->KernelMapping == (PVOID)GlobalUserHeap);
250 
251  /* Unmap if we're the last thread using the global user heap */
252  if (--HeapMapping->Count == 0)
253  {
254  TRACE_CH(UserProcess, "UnmapGlobalUserHeap - Unmapping\n");
256  }
257 
258  return Status;
259 }
PVOID NTAPI PsGetProcessWin32Process(PEPROCESS Process)
Definition: process.c:1193
LONG NTSTATUS
Definition: precomp.h:26
smooth NULL
Definition: ftsmooth.c:416
W32HEAP_USER_MAPPING HeapMappings
Definition: win32.h:281
NTSTATUS NTAPI MmUnmapViewOfSection(IN PEPROCESS Process, IN PVOID BaseAddress)
Definition: section.c:3053
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
Status
Definition: gdiplustypes.h:24
#define TRACE_CH(ch, fmt,...)
Definition: debug.h:107
HANDLE GlobalUserHeap
Definition: usrheap.c:25
#define ERR_CH(ch, fmt,...)
Definition: debug.h:104
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
return STATUS_SUCCESS
Definition: btrfs.c:2966

◆ UserCreateHeap()

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

Definition at line 177 of file usrheap.c.

180 {
181  LARGE_INTEGER SizeHeap;
182  PWIN32HEAP pHeap = NULL;
184 
185  SizeHeap.QuadPart = HeapSize;
186 
187  /* Create the section and map it into session space */
190  NULL,
191  &SizeHeap,
192  PAGE_EXECUTE_READWRITE, /* Would prefer PAGE_READWRITE, but thanks to RTL heaps... */
193  SEC_RESERVE | 1,
194  NULL,
195  NULL);
196 
197  if (!NT_SUCCESS(Status))
198  {
200  return FALSE;
201  }
202 
204  SystemBase,
205  &HeapSize);
206  if (!NT_SUCCESS(Status))
207  {
209  *SectionObject = NULL;
210 
212  return FALSE;
213  }
214 
215  /* Create the heap */
217  SystemBase,
218  HeapSize);
219 
220  if (pHeap == NULL)
221  {
223  *SectionObject = NULL;
224 
226  }
227 
228  return pHeap;
229 }
_Must_inspect_result_ _Outptr_ PVOID * SectionObject
Definition: fsrtlfuncs.h:860
struct _WIN32HEAP * PWIN32HEAP
Definition: usrheap.h:3
LONG NTSTATUS
Definition: precomp.h:26
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
SIZE_T WINAPI HeapSize(HANDLE, DWORD, LPCVOID)
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
#define SEC_RESERVE
Definition: nt_native.h:1323
smooth NULL
Definition: ftsmooth.c:416
#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:4975
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
VOID FASTCALL SetLastNtError(NTSTATUS Status)
Definition: error.c:36
Status
Definition: gdiplustypes.h:24
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:2993
LONGLONG QuadPart
Definition: typedefs.h:112

Referenced by DriverEntry(), and UserInitializeDesktop().

◆ UserHeapAddressToUser()

static __inline PVOID UserHeapAddressToUser ( PVOID  lpMem)
static

Definition at line 92 of file usrheap.h.

93 {
95 
96  /* The first mapping entry is the global user heap mapping */
97  return (PVOID)(((ULONG_PTR)lpMem - (ULONG_PTR)GlobalUserHeap) +
98  (ULONG_PTR)W32Process->HeapMappings.UserMapping);
99 }
HANDLE GlobalUserHeap
Definition: usrheap.c:25
uint32_t ULONG_PTR
Definition: typedefs.h:63
W32HEAP_USER_MAPPING HeapMappings
Definition: win32.h:281
PVOID NTAPI PsGetCurrentProcessWin32Process(VOID)
Definition: process.c:1183
#define ULONG_PTR
Definition: config.h:101

Referenced by co_UserCreateWindowEx(), and UserGetClassInfo().

◆ UserHeapAlloc()

static __inline PVOID UserHeapAlloc ( SIZE_T  Bytes)
static

Definition at line 34 of file usrheap.h.

35 {
38  Bytes);
39 }
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

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

◆ UserHeapFree()

static __inline BOOL UserHeapFree ( PVOID  lpMem)
static

Definition at line 42 of file usrheap.h.

43 {
46  lpMem);
47 }
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

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

◆ UserHeapReAlloc()

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

Definition at line 50 of file usrheap.h.

52 {
53 #if 0
54  /* NOTE: ntoskrnl doesn't export RtlReAllocateHeap... */
57  lpMem,
58  Bytes);
59 #else
60  SIZE_T PrevSize;
61  PVOID pNew;
62 
63  PrevSize = RtlSizeHeap(GlobalUserHeap,
65  lpMem);
66 
67  if (PrevSize == Bytes)
68  return lpMem;
69 
72  Bytes);
73  if (pNew != NULL)
74  {
75  if (PrevSize < Bytes)
76  Bytes = PrevSize;
77 
78  RtlCopyMemory(pNew,
79  lpMem,
80  Bytes);
81 
84  lpMem);
85  }
86 
87  return pNew;
88 #endif
89 }
HANDLE GlobalUserHeap
Definition: usrheap.c:25
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#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
NTSYSAPI PVOID WINAPI RtlReAllocateHeap(HANDLE, ULONG, PVOID, SIZE_T)
Definition: heap.c:2561
smooth NULL
Definition: ftsmooth.c:416
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
NTSYSAPI SIZE_T NTAPI RtlSizeHeap(_In_ PVOID HeapHandle, _In_ ULONG Flags, _In_ PVOID MemoryPointer)
ULONG_PTR SIZE_T
Definition: typedefs.h:78

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().