[SMSS]
[reactos.git] / rostests / drivers / kmtest / ntos_pools.c
1 /*
2 * NTOSKRNL Pools test routines KM-Test
3 * ReactOS Kernel Mode Regression Testing framework
4 *
5 * Copyright 2008 Aleksey Bragin <aleksey@reactos.org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; see the file COPYING.LIB.
19 * If not, write to the Free Software Foundation,
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23 /* INCLUDES *******************************************************************/
24
25 #include <ddk/ntddk.h>
26 #include <ntifs.h>
27 #include <ndk/ntndk.h>
28 /* SEH support with PSEH */
29 #include <pseh/pseh2.h>
30 #include "kmtest.h"
31
32 #define NDEBUG
33 #include "debug.h"
34
35 #define TAG_POOLTEST 'tstP'
36
37 /* PUBLIC FUNCTIONS ***********************************************************/
38
39 VOID
40 PoolsTest(HANDLE KeyHandle)
41 {
42 PVOID Ptr;
43 ULONG AllocSize, i, AllocNumber;
44 PVOID *Allocs;
45
46 StartTest();
47
48 // Stress-test nonpaged pool
49 for (i=1; i<10000; i++)
50 {
51 // make up some increasing, a bit irregular size
52 AllocSize = i*10;
53
54 if (i % 10)
55 AllocSize++;
56
57 if (i % 25)
58 AllocSize += 13;
59
60 // start with non-paged pool
61 Ptr = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
62
63 // it may fail due to no-memory condition
64 if (!Ptr) break;
65
66 // try to fully fill it
67 RtlFillMemory(Ptr, AllocSize, 0xAB);
68
69 // free it
70 ExFreePoolWithTag(Ptr, TAG_POOLTEST);
71 }
72
73 // now paged one
74 for (i=1; i<10000; i++)
75 {
76 // make up some increasing, a bit irregular size
77 AllocSize = i*50;
78
79 if (i % 10)
80 AllocSize++;
81
82 if (i % 25)
83 AllocSize += 13;
84
85 // start with non-paged pool
86 Ptr = ExAllocatePoolWithTag(PagedPool, AllocSize, TAG_POOLTEST);
87
88 // it may fail due to no-memory condition
89 if (!Ptr) break;
90
91 // try to fully fill it
92 RtlFillMemory(Ptr, AllocSize, 0xAB);
93
94 // free it
95 ExFreePoolWithTag(Ptr, TAG_POOLTEST);
96 }
97
98 // test super-big allocations
99 /*AllocSize = 2UL * 1024 * 1024 * 1024;
100 Ptr = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
101 ok(Ptr == NULL, "Allocating 2Gb of nonpaged pool should fail\n");
102
103 Ptr = ExAllocatePoolWithTag(PagedPool, AllocSize, TAG_POOLTEST);
104 ok(Ptr == NULL, "Allocating 2Gb of paged pool should fail\n");*/
105
106 // now test allocating lots of small/medium blocks
107 AllocNumber = 100000;
108 Allocs = ExAllocatePoolWithTag(PagedPool, sizeof(Allocs) * AllocNumber, TAG_POOLTEST);
109
110 // alloc blocks
111 for (i=0; i<AllocNumber; i++)
112 {
113 AllocSize = 42;
114 Allocs[i] = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
115 }
116
117 // now free them
118 for (i=0; i<AllocNumber; i++)
119 {
120 ExFreePoolWithTag(Allocs[i], TAG_POOLTEST);
121 }
122
123
124 ExFreePoolWithTag(Allocs, TAG_POOLTEST);
125
126
127 FinishTest(KeyHandle, L"MmPoolAllocTest");
128 }
129
130 VOID
131 PoolsCorruption(HANDLE KeyHandle)
132 {
133 PULONG Ptr, TestPtr;
134 ULONG AllocSize;
135 NTSTATUS Status = STATUS_SUCCESS;
136
137 StartTest();
138
139 // start with non-paged pool
140 AllocSize = 4096 + 0x10;
141 Ptr = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
142
143 // touch all bytes, it shouldn't cause an exception
144 RtlZeroMemory(Ptr, AllocSize);
145
146 // test buffer overrun, right after our allocation ends
147 _SEH2_TRY
148 {
149 TestPtr = (PULONG)((PUCHAR)Ptr + AllocSize);
150 //Ptr[4] = 0xd33dbeef;
151 *TestPtr = 0xd33dbeef;
152 }
153 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
154 {
155 /* Get the status */
156 Status = _SEH2_GetExceptionCode();
157 } _SEH2_END;
158
159 ok(Status == STATUS_ACCESS_VIOLATION, "Exception should occur, but got Status 0x%08lX\n", Status);
160
161 // test overrun in a distant byte range, but within 4096KB
162 _SEH2_TRY
163 {
164 Ptr[2020] = 0xdeadb33f;
165 }
166 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
167 {
168 /* Get the status */
169 Status = _SEH2_GetExceptionCode();
170 } _SEH2_END;
171
172 ok(Status == STATUS_ACCESS_VIOLATION, "Exception should occur, but got Status 0x%08lX\n", Status);
173
174 // free the pool
175 ExFreePoolWithTag(Ptr, TAG_POOLTEST);
176
177 FinishTest(KeyHandle, L"MmPoolCorruptionTest");
178 }