ReactOS  0.4.14-dev-583-g2a1ba2c
RtlpEnsureBufferSize.c File Reference
#include "precomp.h"
#include <tlhelp32.h>
Include dependency graph for RtlpEnsureBufferSize.c:

Go to the source code of this file.

Functions

 NTSTATUS (NTAPI *pRtlpEnsureBufferSize)(_In_ ULONG Flags
 
static BOOL IsBlockFromHeap (HANDLE hHeap, PVOID ptr)
 
 START_TEST (RtlpEnsureBufferSize)
 

Variables

_Inout_ PRTL_BUFFER Buffer
 
_Inout_ PRTL_BUFFER _In_ SIZE_T RequiredSize
 

Function Documentation

◆ IsBlockFromHeap()

static BOOL IsBlockFromHeap ( HANDLE  hHeap,
PVOID  ptr 
)
static

Definition at line 16 of file RtlpEnsureBufferSize.c.

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 }
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:406
#define DWORD_PTR
Definition: treelist.c:76
struct _Entry Entry
Definition: kefuncs.h:640
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
unsigned int BOOL
Definition: ntddk_ex.h:94
static PVOID ptr
Definition: dispmode.c:27
smooth NULL
Definition: ftsmooth.c:416
#define PROCESS_HEAP_ENTRY_BUSY
Definition: winbase.h:333
BOOL WINAPI HeapWalk(HANDLE hHeap, LPPROCESS_HEAP_ENTRY lpEntry)
Definition: heapmem.c:291
DWORD dwBlockSize
Definition: tlhelp32.h:41
BOOL WINAPI HeapLock(HANDLE hHeap)
Definition: heapmem.c:123
int ret
Definition: winbase.h:1228
uint32_t DWORD_PTR
Definition: typedefs.h:63
DWORD dwSize
Definition: tlhelp32.h:38
BOOL WINAPI HeapUnlock(HANDLE hHeap)
Definition: heapmem.c:134
#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(...)
Definition: atltest.h:64
base of all file and directory entries
Definition: entries.h:82
DWORD WINAPI GetCurrentProcessId(VOID)
Definition: proc.c:1158
BOOL WINAPI Heap32First(LPHEAPENTRY32 lphe, DWORD th32ProcessID, DWORD th32HeapID)
Definition: toolhelp.c:489

Referenced by START_TEST().

◆ NTSTATUS()

NTSTATUS ( NTAPI pRtlpEnsureBufferSize)

◆ START_TEST()

START_TEST ( RtlpEnsureBufferSize  )

Definition at line 65 of file RtlpEnsureBufferSize.c.

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
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
#define ok_ptr(expression, result)
Definition: atltest.h:108
#define UNICODE_STRING_MAX_BYTES
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
#define ok_int(expression, result)
Definition: atltest.h:134
Definition: bufpool.h:45
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
Definition: xml2sdb.h:79
unsigned char UCHAR
Definition: xmlstorage.h:181
static BOOL IsBlockFromHeap(HANDLE hHeap, PVOID ptr)
static const WCHAR L[]
Definition: oid.c:1250
#define ok(value,...)
Definition: atltest.h:57
#define RTL_SKIP_BUFFER_COPY
Definition: rtlfuncs.h:2355
#define skip(...)
Definition: atltest.h:64
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
unsigned int ULONG
Definition: retypes.h:1
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:847
#define GetProcAddress(x, y)
Definition: compat.h:418
return STATUS_SUCCESS
Definition: btrfs.c:2938
#define memset(x, y, z)
Definition: compat.h:39
static int mod
Definition: i386-dis.c:1273

Variable Documentation

◆ Buffer

Definition at line 13 of file RtlpEnsureBufferSize.c.

◆ RequiredSize

Definition at line 13 of file RtlpEnsureBufferSize.c.

Referenced by AcpiNsHandleToPathname(), AddProvider(), AuthzInitializeResourceManager(), BiEnumerateSubElements(), BiEnumerateSubObjectElements(), BlFileReadAtOffsetEx(), BlFileReadEx(), DECLARE_INTERFACE_(), EnumDeviceClasses(), EnumNamesW(), FileInformationCheck(), GetDeletedFileDetailsA(), GetDeletedFileDetailsW(), GetHardwareAndCompatibleIDsLists(), GetStringField(), GetVersionInformationFromInfFile(), InfGetBinaryField(), InfGetMultiSzField(), InfGetStringField(), InfHostGetBinaryField(), InfHostGetMultiSzField(), InfHostGetStringField(), InfpGetBinaryField(), InfpGetMultiSzField(), InfpGetStringField(), InitializeFmIfsOnce(), IopEnumerateDetectedDevices(), IopGetDeviceRelations(), IsAcpiComputer(), LdrpInitializeDotLocalSupport(), PARSER_GetInfClassW(), PcMemCheckUsableMemorySize(), RecycleBin5File_RecycleBinFile_GetFileName(), RtlAddAttributeActionToRXact(), RtlComputePrivatizedDllName_U(), RtlpEnsureBufferSize(), SETUP_CreateClassKey(), SetupCopyOEMInfA(), SetupCopyOEMInfW(), SetupDiBuildClassInfoList(), SetupDiBuildClassInfoListExA(), SetupDiBuildClassInfoListExW(), SetupDiBuildDriverInfoList(), SetupDiClassGuidsFromNameA(), SetupDiClassGuidsFromNameExA(), SetupDiClassGuidsFromNameExW(), SetupDiClassGuidsFromNameW(), SetupDiClassNameFromGuidA(), SetupDiClassNameFromGuidExA(), SetupDiClassNameFromGuidExW(), SetupDiClassNameFromGuidW(), SetupDiGetActualSectionToInstallA(), SetupDiGetActualSectionToInstallExA(), SetupDiGetActualSectionToInstallExW(), SetupDiGetActualSectionToInstallW(), SetupDiGetClassDescriptionA(), SetupDiGetClassDescriptionExA(), SetupDiGetClassDescriptionExW(), SetupDiGetClassDescriptionW(), SetupDiGetClassDevPropertySheetsA(), SetupDiGetClassDevPropertySheetsW(), SetupDiGetClassImageListExW(), SetupDiGetClassInstallParamsA(), SetupDiGetClassInstallParamsW(), SetupDiGetDeviceInstanceIdA(), SetupDiGetDeviceInstanceIdW(), SetupDiGetDeviceInterfaceDetailA(), SetupDiGetDeviceInterfaceDetailW(), SetupDiGetDeviceRegistryPropertyA(), SetupDiGetDeviceRegistryPropertyW(), SetupDiGetDriverInfoDetailA(), SetupDiGetDriverInfoDetailW(), SetupDiGetINFClassA(), SetupDiGetINFClassW(), SetupDiInstallDevice(), SetupGetBinaryField(), SetupGetInfFileListA(), SetupGetInfFileListW(), SetupGetInfInformationA(), SetupGetInfInformationW(), SetupGetMultiSzFieldW(), SetupGetStringFieldW(), CClassNode::SetupNode(), SetupQueryInfFileInformationA(), and SetupQueryInfFileInformationW().