ReactOS  0.4.15-dev-1384-g878186b
RtlReAllocateHeap.c File Reference
#include "precomp.h"
Include dependency graph for RtlReAllocateHeap.c:

Go to the source code of this file.

Functions

static BOOLEAN CheckBuffer (PVOID Buffer, SIZE_T Size, UCHAR Value)
 
static BOOLEAN ReAllocBuffer (PUCHAR *Buffer, SIZE_T Size, SIZE_T *OldSizePtr, PCSTR Action)
 
 START_TEST (RtlReAllocateHeap)
 

Function Documentation

◆ CheckBuffer()

static BOOLEAN CheckBuffer ( PVOID  Buffer,
SIZE_T  Size,
UCHAR  Value 
)
static

Definition at line 12 of file RtlReAllocateHeap.c.

16 {
17  PUCHAR Array = Buffer;
18  SIZE_T i;
19 
20  for (i = 0; i < Size; i++)
21  if (Array[i] != Value)
22  {
23  trace("Expected %x, found %x at offset %lu\n", Value, Array[i], (ULONG)i);
24  return FALSE;
25  }
26  return TRUE;
27 }
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
#define TRUE
Definition: types.h:120
unsigned char * PUCHAR
Definition: retypes.h:3
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
#define FALSE
Definition: types.h:117
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:406
#define trace
Definition: atltest.h:70
ULONG_PTR SIZE_T
Definition: typedefs.h:80
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
unsigned int ULONG
Definition: retypes.h:1

Referenced by ReAllocBuffer(), and START_TEST().

◆ ReAllocBuffer()

static BOOLEAN ReAllocBuffer ( PUCHAR Buffer,
SIZE_T  Size,
SIZE_T OldSizePtr,
PCSTR  Action 
)
static

Definition at line 31 of file RtlReAllocateHeap.c.

36 {
37  PUCHAR NewBuffer;
38  SIZE_T OldSize = *OldSizePtr;
39 
40  RtlFillMemory(*Buffer, OldSize, 0x7a);
41  NewBuffer = RtlReAllocateHeap(RtlGetProcessHeap(),
43  *Buffer,
44  Size);
45  if (!NewBuffer)
46  {
47  skip("RtlReAllocateHeap failed for size %lu (%s)\n", Size, Action);
48  return FALSE;
49  }
50  *Buffer = NewBuffer;
51  ok_hex(RtlSizeHeap(RtlGetProcessHeap(), 0, NewBuffer), Size);
52  if (OldSize < Size)
53  {
54  ok(CheckBuffer(NewBuffer, OldSize, 0x7a), "CheckBuffer failed at size 0x%lx -> 0x%lx\n", OldSize, Size);
55  ok(CheckBuffer(NewBuffer + OldSize, Size - OldSize, 0), "HEAP_ZERO_MEMORY not respected for 0x%lx -> 0x%lx\n", OldSize, Size);
56  }
57  else
58  {
59  ok(CheckBuffer(NewBuffer, Size, 0x7a), "CheckBuffer failed at size 0x%lx -> 0x%lx\n", OldSize, Size);
60  }
61  *OldSizePtr = Size;
62  return TRUE;
63 }
#define TRUE
Definition: types.h:120
unsigned char * PUCHAR
Definition: retypes.h:3
static BOOLEAN CheckBuffer(PVOID Buffer, SIZE_T Size, UCHAR Value)
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
#define ok_hex(expression, result)
Definition: atltest.h:94
#define FALSE
Definition: types.h:117
Definition: bufpool.h:45
#define RtlFillMemory(Dest, Length, Fill)
Definition: winternl.h:593
NTSYSAPI SIZE_T NTAPI RtlSizeHeap(_In_ PVOID HeapHandle, _In_ ULONG Flags, _In_ PVOID MemoryPointer)
ULONG_PTR SIZE_T
Definition: typedefs.h:80
_In_ WDFIOTARGET _In_ _Strict_type_match_ WDF_IO_TARGET_SENT_IO_ACTION Action
Definition: wdfiotarget.h:506
#define ok(value,...)
Definition: atltest.h:57
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define skip(...)
Definition: atltest.h:64
NTSYSAPI PVOID WINAPI RtlReAllocateHeap(HANDLE, ULONG, PVOID, SIZE_T)
Definition: heap.c:2622

Referenced by START_TEST().

◆ START_TEST()

START_TEST ( RtlReAllocateHeap  )

Definition at line 65 of file RtlReAllocateHeap.c.

66 {
67  PUCHAR Buffer = NULL;
68  SIZE_T OldSize = 0;
69  SIZE_T Size;
72  PVOID UserValue;
73  ULONG UserFlags;
74  PVOID Buffer2;
75 
76  OldSize = 0x100;
77  Buffer = RtlReAllocateHeap(RtlGetProcessHeap(),
79  NULL,
80  OldSize);
81  ok(Buffer == NULL, "RtlReAllocateHeap succeeded for NULL\n");
82  if (Buffer)
83  RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
84 
85  Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
87  OldSize);
88  if (!Buffer)
89  {
90  skip("RtlAllocateHeap failed for size %lu\n", OldSize);
91  return;
92  }
93  ok(CheckBuffer(Buffer, OldSize, 0), "HEAP_ZERO_MEMORY not respected for 0x%lx\n", OldSize);
94 
95  for (Size = 0x78000; Size < 0x90000 && Continue; Size += 0x100)
96  {
97  Continue = ReAllocBuffer(&Buffer, Size, &OldSize, "growing");
98  }
99 
100  /* and back again */
101  for (Size -= 0x100; Size >= 0x78000 && Continue; Size -= 0x100)
102  {
103  Continue = ReAllocBuffer(&Buffer, Size, &OldSize, "shrinking");
104  }
105  RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
106 
107  /* Now make sure user flags/values get preserved */
108  OldSize = 0x100;
109  Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
111  OldSize);
112  if (!Buffer)
113  {
114  skip("RtlAllocateHeap failed for size %lu\n", OldSize);
115  return;
116  }
117 
118  UserValue = InvalidPointer;
119  UserFlags = 0x55555555;
120  Success = RtlGetUserInfoHeap(RtlGetProcessHeap(),
121  0,
122  Buffer,
123  &UserValue,
124  &UserFlags);
125  ok(Success == TRUE, "RtlGetUserInfoHeap returned %u\n", Success);
126  ok(UserValue == NULL, "UserValue = %p\n", UserValue);
127  ok(UserFlags == HEAP_SETTABLE_USER_FLAG2, "UserFlags = %lx\n", UserFlags);
128 
129  Success = RtlSetUserFlagsHeap(RtlGetProcessHeap(),
130  0,
131  Buffer,
134  ok(Success == TRUE, "RtlSetUserFlagsHeap returned %u\n", Success);
135 
136  Success = RtlSetUserValueHeap(RtlGetProcessHeap(),
137  0,
138  Buffer,
139  &UserValue);
140  ok(Success == TRUE, "RtlSetUserValueHeap returned %u\n", Success);
141 
142  UserValue = InvalidPointer;
143  UserFlags = 0x55555555;
144  Success = RtlGetUserInfoHeap(RtlGetProcessHeap(),
145  0,
146  Buffer,
147  &UserValue,
148  &UserFlags);
149  ok(Success == TRUE, "RtlGetUserInfoHeap returned %u\n", Success);
150  ok(UserValue == &UserValue, "UserValue = %p, expected %p\n", UserValue, &UserValue);
151  ok(UserFlags == HEAP_SETTABLE_USER_FLAG3, "UserFlags = %lx\n", UserFlags);
152 
153  /* shrink (preserves flags) */
154  Buffer2 = RtlReAllocateHeap(RtlGetProcessHeap(),
156  Buffer,
157  OldSize / 2);
158  ok(Buffer2 == Buffer, "New Buffer is %p, expected %p\n", Buffer2, Buffer);
159  if (Buffer2) Buffer = Buffer2;
160  UserValue = InvalidPointer;
161  UserFlags = 0x55555555;
162  Success = RtlGetUserInfoHeap(RtlGetProcessHeap(),
163  0,
164  Buffer,
165  &UserValue,
166  &UserFlags);
167  ok(Success == TRUE, "RtlGetUserInfoHeap returned %u\n", Success);
168  ok(UserValue == &UserValue, "UserValue = %p, expected %p\n", UserValue, &UserValue);
169  ok(UserFlags == HEAP_SETTABLE_USER_FLAG3, "UserFlags = %lx\n", UserFlags);
170 
171  /* grow (overwrites flags) */
172  Buffer2 = RtlReAllocateHeap(RtlGetProcessHeap(),
174  Buffer,
175  OldSize / 4 * 3);
176  ok(Buffer2 == Buffer, "New Buffer is %p, expected %p\n", Buffer2, Buffer);
177  if (Buffer2) Buffer = Buffer2;
178  UserValue = InvalidPointer;
179  UserFlags = 0x55555555;
180  Success = RtlGetUserInfoHeap(RtlGetProcessHeap(),
181  0,
182  Buffer,
183  &UserValue,
184  &UserFlags);
185  ok(Success == TRUE, "RtlGetUserInfoHeap returned %u\n", Success);
186  ok(UserValue == &UserValue, "UserValue = %p, expected %p\n", UserValue, &UserValue);
187  ok(UserFlags == HEAP_SETTABLE_USER_FLAG1, "UserFlags = %lx\n", UserFlags);
188 
189  RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
190 }
#define TRUE
Definition: types.h:120
#define HEAP_SETTABLE_USER_FLAG2
Definition: nt_native.h:1706
unsigned char * PUCHAR
Definition: retypes.h:3
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
static BOOLEAN CheckBuffer(PVOID Buffer, SIZE_T Size, UCHAR Value)
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
#define HEAP_SETTABLE_USER_VALUE
Definition: nt_native.h:1704
#define HEAP_SETTABLE_USER_FLAG3
Definition: nt_native.h:1707
BOOLEAN NTAPI RtlSetUserValueHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID BaseAddress, IN PVOID UserValue)
Definition: heap.c:3736
static BOOLEAN ReAllocBuffer(PUCHAR *Buffer, SIZE_T Size, SIZE_T *OldSizePtr, PCSTR Action)
unsigned char BOOLEAN
Definition: bufpool.h:45
#define HEAP_SETTABLE_USER_FLAG1
Definition: nt_native.h:1705
BOOLEAN NTAPI RtlGetUserInfoHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID BaseAddress, OUT PVOID *UserValue, OUT PULONG UserFlags)
Definition: heap.c:3853
#define InvalidPointer
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
volatile int Continue
Definition: gdblib.c:102
BOOLEAN NTAPI RtlSetUserFlagsHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID BaseAddress, IN ULONG UserFlagsReset, IN ULONG UserFlagsSet)
Definition: heap.c:3798
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define ok(value,...)
Definition: atltest.h:57
#define NULL
Definition: types.h:112
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define skip(...)
Definition: atltest.h:64
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI PVOID WINAPI RtlReAllocateHeap(HANDLE, ULONG, PVOID, SIZE_T)
Definition: heap.c:2622
#define HEAP_REALLOC_IN_PLACE_ONLY
Definition: nt_native.h:1696