ReactOS  0.4.13-dev-73-gcfe54aa
RtlpEnsureBufferSize.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: GPLv2+ - See COPYING in the top level directory
4  * PURPOSE: Test for RtlpEnsureBufferSize
5  * PROGRAMMER: Mark Jansen
6  */
7 
8 #include "precomp.h"
9 
10 #include <tlhelp32.h>
11 
12 
14 
15 
17 {
18  /* Use when this is implemented */
19 #if 0
21  BOOL ret = FALSE;
22  if (!HeapLock(hHeap))
23  {
24  skip("Unable to lock heap\n");
25  return FALSE;
26  }
27 
28  Entry.lpData = NULL;
29  while (!ret && HeapWalk(hHeap, &Entry))
30  {
31  if ((Entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) &&
32  (Entry.lpData == ptr))
33  {
34  ret = TRUE;
35  }
36  }
37 
38  HeapUnlock(hHeap);
39  return ret;
40 #else
41  HEAPENTRY32 he;
42  BOOL ret = FALSE;
44 
45  if (hHeapSnap == INVALID_HANDLE_VALUE)
46  return FALSE;
47 
48  he.dwSize = sizeof(he);
49 
50  if (Heap32First(&he, GetCurrentProcessId(), (DWORD_PTR)hHeap))
51  {
52  do {
53  if ((DWORD_PTR)ptr >= he.dwAddress && (DWORD_PTR)ptr <= (he.dwAddress + he.dwBlockSize))
54  ret = TRUE;
55  } while (!ret && Heap32Next(&he));
56  }
57 
58  CloseHandle(hHeapSnap);
59 
60  return ret;
61 #endif
62 }
63 
64 
66 {
67  RTL_BUFFER Buffer = { 0 };
68  ULONG Flag;
69  UCHAR StaticBuf[4];
70  PVOID tmp;
71  BOOL SkipHeapCheck;
72 
73  HMODULE mod = GetModuleHandleW(L"ntdll.dll");
74  pRtlpEnsureBufferSize = (void *)GetProcAddress(mod, "RtlpEnsureBufferSize");
75 
76  if (!pRtlpEnsureBufferSize)
77  {
78  skip("No RtlpEnsureBufferSize\n");
79  return;
80  }
81 
82  memset(StaticBuf, 0xba, sizeof(StaticBuf));
83  RtlInitBuffer(&Buffer, StaticBuf, sizeof(StaticBuf));
84 
85  /* NULL buffer yields a failure */
86  ok_ntstatus(pRtlpEnsureBufferSize(0, NULL, 0), STATUS_INVALID_PARAMETER);
87 
88  /* All flags other than '1' yield a failure */
89  for (Flag = 2; Flag; Flag <<= 1)
90  {
91  ok_ntstatus(pRtlpEnsureBufferSize(Flag, &Buffer, 0), STATUS_INVALID_PARAMETER);
92  ok_ptr(Buffer.Buffer, Buffer.StaticBuffer);
93  ok_int(Buffer.Size, Buffer.StaticSize);
94  ok_ptr(Buffer.StaticBuffer, StaticBuf);
95  ok_int(Buffer.StaticSize, sizeof(StaticBuf));
96  RtlFreeBuffer(&Buffer);
97  }
98 
99  ok_ntstatus(pRtlpEnsureBufferSize(0, &Buffer, 0), STATUS_SUCCESS);
100  ok_ptr(Buffer.Buffer, Buffer.StaticBuffer);
101  ok_int(Buffer.Size, Buffer.StaticSize);
102  ok_ptr(Buffer.StaticBuffer, StaticBuf);
103  ok_int(Buffer.StaticSize, sizeof(StaticBuf));
104  RtlFreeBuffer(&Buffer);
105 
106  ok_ntstatus(pRtlpEnsureBufferSize(0, &Buffer, 1), STATUS_SUCCESS);
107  ok_ptr(Buffer.Buffer, Buffer.StaticBuffer);
108  ok_int(Buffer.Size, Buffer.StaticSize);
109  ok_ptr(Buffer.StaticBuffer, StaticBuf);
110  ok_int(Buffer.StaticSize, sizeof(StaticBuf));
111  RtlFreeBuffer(&Buffer);
112 
113  ok_ntstatus(pRtlpEnsureBufferSize(0, &Buffer, 2), STATUS_SUCCESS);
114  ok_ptr(Buffer.Buffer, Buffer.StaticBuffer);
115  ok_int(Buffer.Size, Buffer.StaticSize);
116  ok_ptr(Buffer.StaticBuffer, StaticBuf);
117  ok_int(Buffer.StaticSize, sizeof(StaticBuf));
118  RtlFreeBuffer(&Buffer);
119 
120  ok_ntstatus(pRtlpEnsureBufferSize(0, &Buffer, 3), STATUS_SUCCESS);
121  ok_ptr(Buffer.Buffer, Buffer.StaticBuffer);
122  ok_int(Buffer.Size, Buffer.StaticSize);
123  ok_ptr(Buffer.StaticBuffer, StaticBuf);
124  ok_int(Buffer.StaticSize, sizeof(StaticBuf));
125  RtlFreeBuffer(&Buffer);
126 
127  ok_ntstatus(pRtlpEnsureBufferSize(0, &Buffer, 4), STATUS_SUCCESS);
128  ok_ptr(Buffer.Buffer, Buffer.StaticBuffer);
129  ok_int(Buffer.Size, Buffer.StaticSize);
130  ok_ptr(Buffer.StaticBuffer, StaticBuf);
131  ok_int(Buffer.StaticSize, sizeof(StaticBuf));
132  RtlFreeBuffer(&Buffer);
133 
134  /* Check that IsBlockFromHeap works! */
135  tmp = RtlAllocateHeap(RtlGetProcessHeap(), 0, 5);
136  SkipHeapCheck = (IsBlockFromHeap(RtlGetProcessHeap(), StaticBuf) != FALSE) ||
137  (IsBlockFromHeap(RtlGetProcessHeap(), tmp) != TRUE);
138  RtlFreeHeap(RtlGetProcessHeap(), 0, tmp);
139 
140  if (SkipHeapCheck)
141  skip("Unable to verify the heap used\n");
142 
143  /* Allocated is exactly what is asked for, not rounded to nearest whatever */
144  ok_ntstatus(pRtlpEnsureBufferSize(0, &Buffer, 5), STATUS_SUCCESS);
145  if (!SkipHeapCheck)
146  ok_int(IsBlockFromHeap(RtlGetProcessHeap(), Buffer.Buffer), TRUE);
147  ok(!memcmp(Buffer.Buffer, StaticBuf, sizeof(StaticBuf)), "Expected First 4 bytes to be the same!\n");
148  ok_int(Buffer.Size, 5);
149  ok_ptr(Buffer.StaticBuffer, StaticBuf);
150  ok_int(Buffer.StaticSize, sizeof(StaticBuf));
151  RtlFreeBuffer(&Buffer);
152 
153  ok_ntstatus(pRtlpEnsureBufferSize(RTL_SKIP_BUFFER_COPY, &Buffer, 5), STATUS_SUCCESS);
154  if (!SkipHeapCheck)
155  ok_int(IsBlockFromHeap(RtlGetProcessHeap(), Buffer.Buffer), TRUE);
156  ok(memcmp(Buffer.Buffer, StaticBuf, sizeof(StaticBuf)), "Expected First 4 bytes to be different!\n");
157  ok_int(Buffer.Size, 5);
158  ok_ptr(Buffer.StaticBuffer, StaticBuf);
159  ok_int(Buffer.StaticSize, sizeof(StaticBuf));
160  RtlFreeBuffer(&Buffer);
161 
162  /* Size is not limited to UNICODE_STRING sizes */
164  if (!SkipHeapCheck)
165  ok_int(IsBlockFromHeap(RtlGetProcessHeap(), Buffer.Buffer), TRUE);
166  ok(memcmp(Buffer.Buffer, StaticBuf, sizeof(StaticBuf)), "Expected First 4 bytes to be different!\n");
168  ok_ptr(Buffer.StaticBuffer, StaticBuf);
169  ok_int(Buffer.StaticSize, sizeof(StaticBuf));
170  RtlFreeBuffer(&Buffer);
171 }
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
#define DWORD_PTR
Definition: treelist.c:76
struct _Entry Entry
Definition: kefuncs.h:640
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
START_TEST(RtlpEnsureBufferSize)
_Inout_ PRTL_BUFFER Buffer
#define ok_int(x, y)
Definition: sprintf.c:21
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:603
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
#define UNICODE_STRING_MAX_BYTES
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
NTSYSAPI NTSTATUS NTAPI RtlpEnsureBufferSize(_In_ ULONG Flags, _Inout_ PRTL_BUFFER Buffer, _In_ SIZE_T RequiredSize)
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
unsigned int BOOL
Definition: ntddk_ex.h:94
static PVOID ptr
Definition: dispmode.c:27
#define ok(value,...)
smooth NULL
Definition: ftsmooth.c:416
Definition: bufpool.h:45
NTSTATUS(NTAPI *pRtlpEnsureBufferSize)(_In_ ULONG Flags
#define ok_ntstatus(status, expected)
Definition: test.h:800
_Inout_ PRTL_BUFFER _In_ SIZE_T RequiredSize
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:585
#define PROCESS_HEAP_ENTRY_BUSY
Definition: winbase.h:333
BOOL WINAPI HeapWalk(HANDLE hHeap, LPPROCESS_HEAP_ENTRY lpEntry)
Definition: heapmem.c:291
Definition: xml2sdb.h:79
DWORD dwBlockSize
Definition: tlhelp32.h:41
#define _Inout_
Definition: no_sal2.h:244
#define ok_ptr(expression, result)
Definition: test.h:773
BOOL WINAPI HeapLock(HANDLE hHeap)
Definition: heapmem.c:123
unsigned char UCHAR
Definition: xmlstorage.h:181
int ret
static BOOL IsBlockFromHeap(HANDLE hHeap, PVOID ptr)
static const WCHAR L[]
Definition: oid.c:1250
Definition: winbase.h:1221
uint32_t DWORD_PTR
Definition: typedefs.h:63
DWORD dwSize
Definition: tlhelp32.h:38
#define _In_
Definition: no_sal2.h:204
ULONG_PTR SIZE_T
Definition: typedefs.h:78
BOOL WINAPI HeapUnlock(HANDLE hHeap)
Definition: heapmem.c:134
#define RTL_SKIP_BUFFER_COPY
Definition: rtlfuncs.h:2339
#define TH32CS_SNAPHEAPLIST
Definition: tlhelp32.h:25
BOOL WINAPI Heap32Next(LPHEAPENTRY32 lphe)
Definition: toolhelp.c:578
HANDLE WINAPI CreateToolhelp32Snapshot(DWORD dwFlags, DWORD th32ProcessID)
Definition: toolhelp.c:1255
DWORD dwAddress
Definition: tlhelp32.h:40
#define skip(...)
unsigned int ULONG
Definition: retypes.h:1
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:845
#define GetProcAddress(x, y)
Definition: compat.h:410
return STATUS_SUCCESS
Definition: btrfs.c:2725
#define memset(x, y, z)
Definition: compat.h:39
base of all file and directory entries
Definition: entries.h:82
DWORD WINAPI GetCurrentProcessId(VOID)
Definition: proc.c:1188
BOOL WINAPI Heap32First(LPHEAPENTRY32 lphe, DWORD th32ProcessID, DWORD th32HeapID)
Definition: toolhelp.c:489
static int mod
Definition: i386-dis.c:1273