ReactOS 0.4.16-dev-1489-g8fbbb41
ExPools.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: LGPLv2+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Kernel-Mode Test Suite Pools test routines KM-Test
5 * PROGRAMMER: Aleksey Bragin <aleksey@reactos.org>
6 */
7
8#include <kmt_test.h>
9
10#define NDEBUG
11#include <debug.h>
12
13#define TAG_POOLTEST 'tstP'
14
15#define BASE_POOL_TYPE_MASK 1
16#define QUOTA_POOL_MASK 8
17
18static
19LONG
22{
24 return Header->PointerCount;
25}
26
28{
29 PVOID Ptr;
30 ULONG AllocSize, i, AllocNumber;
31 PVOID *Allocs;
32
33 // Stress-test nonpaged pool
34 for (i=1; i<10000; i++)
35 {
36 // make up some increasing, a bit irregular size
37 AllocSize = i*10;
38
39 if (i % 10)
40 AllocSize++;
41
42 if (i % 25)
43 AllocSize += 13;
44
45 // start with non-paged pool
47
48 // it may fail due to no-memory condition
49 if (!Ptr) break;
50
51 // try to fully fill it
52 RtlFillMemory(Ptr, AllocSize, 0xAB);
53
54 // free it
56 }
57
58 // now paged one
59 for (i=1; i<10000; i++)
60 {
61 // make up some increasing, a bit irregular size
62 AllocSize = i*50;
63
64 if (i % 10)
65 AllocSize++;
66
67 if (i % 25)
68 AllocSize += 13;
69
70 // start with non-paged pool
72
73 // it may fail due to no-memory condition
74 if (!Ptr) break;
75
76 // try to fully fill it
77 RtlFillMemory(Ptr, AllocSize, 0xAB);
78
79 // free it
81 }
82
83 // test super-big allocations
84 /*AllocSize = 2UL * 1024 * 1024 * 1024;
85 Ptr = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
86 ok(Ptr == NULL, "Allocating 2Gb of nonpaged pool should fail\n");
87
88 Ptr = ExAllocatePoolWithTag(PagedPool, AllocSize, TAG_POOLTEST);
89 ok(Ptr == NULL, "Allocating 2Gb of paged pool should fail\n");*/
90
91 // now test allocating lots of small/medium blocks
92 AllocNumber = 100000;
93 Allocs = ExAllocatePoolWithTag(PagedPool, sizeof(*Allocs) * AllocNumber, TAG_POOLTEST);
94
95 // alloc blocks
96 for (i=0; i<AllocNumber; i++)
97 {
98 AllocSize = 42;
99 Allocs[i] = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
100 }
101
102 // now free them
103 for (i=0; i<AllocNumber; i++)
104 {
106 }
107
108
110}
111
112static
113VOID
115{
117
120 ExFreePoolWithTag(Memory, 'MyTa');
121
124 ExFreePoolWithTag(Memory, 'MyTa');
125
126 Memory = ExAllocatePoolWithTag(PagedPool, PAGE_SIZE - 3 * sizeof(PVOID), 'MyTa');
128 ExFreePoolWithTag(Memory, 'MyTa');
129
130 Memory = ExAllocatePoolWithTag(PagedPool, PAGE_SIZE - 4 * sizeof(PVOID) + 1, 'MyTa');
132 ExFreePoolWithTag(Memory, 'MyTa');
133
134 Memory = ExAllocatePoolWithTag(PagedPool, PAGE_SIZE - 4 * sizeof(PVOID), 'MyTa');
136 ExFreePoolWithTag(Memory, 'MyTa');
137}
138
139static
140VOID
142{
144 PEPROCESS StoredProcess;
146 LONG InitialRefCount;
147 LONG RefCount;
149
150 InitialRefCount = GetRefCount(Process);
151
152 /* We get some memory from this function, and it's properly aligned.
153 * Also, it takes a reference to the process, and releases it on free */
155 sizeof(LIST_ENTRY),
156 'tQmK');
157 ok(Memory != NULL, "ExAllocatePoolWithQuotaTag returned NULL\n");
158 if (!skip(Memory != NULL, "No memory\n"))
159 {
160 ok((ULONG_PTR)Memory % sizeof(LIST_ENTRY) == 0,
161 "Allocation %p is badly aligned\n",
162 Memory);
163 RefCount = GetRefCount(Process);
164 ok_eq_long(RefCount, InitialRefCount + 1);
165
166 /* A pointer to the process is found right before the next pool header */
167 StoredProcess = ((PVOID *)((ULONG_PTR)Memory + 2 * sizeof(LIST_ENTRY)))[-1];
168 ok_eq_pointer(StoredProcess, Process);
169
170 /* Pool type should have QUOTA_POOL_MASK set */
172 ok(PoolType != 0, "PoolType is 0\n");
173 PoolType--;
174 ok(PoolType & QUOTA_POOL_MASK, "PoolType = %x\n", PoolType);
175 ok((PoolType & BASE_POOL_TYPE_MASK) == PagedPool, "PoolType = %x\n", PoolType);
176
177 ExFreePoolWithTag(Memory, 'tQmK');
178 RefCount = GetRefCount(Process);
179 ok_eq_long(RefCount, InitialRefCount);
180 }
181
182 /* Large allocations are page-aligned, don't reference the process */
184 PAGE_SIZE,
185 'tQmK');
186 ok(Memory != NULL, "ExAllocatePoolWithQuotaTag returned NULL\n");
187 if (!skip(Memory != NULL, "No memory\n"))
188 {
190 "Allocation %p is badly aligned\n",
191 Memory);
192 RefCount = GetRefCount(Process);
193 ok_eq_long(RefCount, InitialRefCount);
194 ExFreePoolWithTag(Memory, 'tQmK');
195 RefCount = GetRefCount(Process);
196 ok_eq_long(RefCount, InitialRefCount);
197 }
198
199 /* Function raises by default */
202 0x7FFFFFFF,
203 'tQmK');
204 if (Memory)
205 ExFreePoolWithTag(Memory, 'tQmK');
207
208 /* Function returns NULL with POOL_QUOTA_FAIL_INSTEAD_OF_RAISE */
211 0x7FFFFFFF,
212 'tQmK');
213 ok(Memory == NULL, "Successfully got 2GB block: %p\n", Memory);
214 if (Memory)
215 ExFreePoolWithTag(Memory, 'tQmK');
217}
218
219static
220VOID
222{
224 PVOID *BigAllocations;
225 const ULONG MaxAllocations = 1024 * 128;
226 ULONG NumAllocations;
227
229 {
230 BigAllocations = ExAllocatePoolWithTag(PoolType,
231 MaxAllocations * sizeof(*BigAllocations),
232 'ABmK');
233
234 /* Allocate a lot of pages (== big pool allocations) */
235 for (NumAllocations = 0; NumAllocations < MaxAllocations; NumAllocations++)
236 {
237 BigAllocations[NumAllocations] = ExAllocatePoolWithTag(PoolType,
238 PAGE_SIZE,
239 'aPmK');
240 if (BigAllocations[NumAllocations] == NULL)
241 {
242 NumAllocations--;
243 break;
244 }
245 }
246
247 trace("Got %lu allocations for PoolType %d\n", NumAllocations, PoolType);
248
249 /* Free them */
250 for (; NumAllocations < MaxAllocations; NumAllocations--)
251 {
252 ASSERT(BigAllocations[NumAllocations] != NULL);
253 ExFreePoolWithTag(BigAllocations[NumAllocations],
254 'aPmK');
255 }
256 ExFreePoolWithTag(BigAllocations, 'ABmK');
257 }
258}
259
261{
262 PoolsTest();
263 TestPoolTags();
266}
#define BASE_POOL_TYPE_MASK
Definition: ExPools.c:15
static VOID PoolsTest(VOID)
Definition: ExPools.c:27
static VOID TestBigPoolExpansion(VOID)
Definition: ExPools.c:221
static VOID TestPoolTags(VOID)
Definition: ExPools.c:114
static VOID TestPoolQuota(VOID)
Definition: ExPools.c:141
#define TAG_POOLTEST
Definition: ExPools.c:13
static LONG GetRefCount(_In_ PVOID Object)
Definition: ExPools.c:20
#define QUOTA_POOL_MASK
Definition: ExPools.c:16
#define ok_eq_pointer(value, expected)
Definition: apitest.h:44
#define ok_eq_long(value, expected)
Definition: apitest.h:47
#define ok_eq_tag(value, expected)
Definition: apitest.h:70
#define trace
Definition: atltest.h:70
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
Definition: Header.h:9
#define NULL
Definition: types.h:112
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define NonPagedPool
Definition: env_spec_w32.h:307
#define PagedPool
Definition: env_spec_w32.h:308
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:223
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
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
#define RtlFillMemory(Dest, Length, Fill)
Definition: winternl.h:599
#define KmtStartSeh()
Definition: kmt_test.h:285
#define KmtEndSeh(ExpectedStatus)
Definition: kmt_test.h:291
USHORT KmtGetPoolType(PVOID Memory)
ULONG KmtGetPoolTag(PVOID Memory)
#define ASSERT(a)
Definition: mode.c:44
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
#define OBJECT_TO_OBJECT_HEADER(o)
Definition: obtypes.h:111
#define _In_
Definition: no_sal2.h:158
long LONG
Definition: pedump.c:60
unsigned short USHORT
Definition: pedump.c:61
#define STATUS_SUCCESS
Definition: shellext.h:65
Definition: typedefs.h:120
#define LIST_ENTRY(type)
Definition: queue.h:175
INT POOL_TYPE
Definition: typedefs.h:78
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_Must_inspect_result_ _In_ WDFCOLLECTION _In_ WDFOBJECT Object
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ _Strict_type_match_ POOL_TYPE PoolType
Definition: wdfdevice.h:3815
_Must_inspect_result_ _In_opt_ PWDF_OBJECT_ATTRIBUTES _In_ _Strict_type_match_ POOL_TYPE _In_opt_ ULONG _In_ _Out_ WDFMEMORY * Memory
Definition: wdfmemory.h:169
#define ExAllocatePoolWithQuotaTag(a, b, c)
Definition: exfuncs.h:530
#define POOL_QUOTA_FAIL_INSTEAD_OF_RAISE
#define PsGetCurrentProcess
Definition: psfuncs.h:17