1d2e9e9377c6e4ff680c78070e5459f03219c019
[reactos.git] / rostests / kmtests / ntos_ex / ExPools.c
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 static VOID PoolsTest(VOID)
16 {
17 PVOID Ptr;
18 ULONG AllocSize, i, AllocNumber;
19 PVOID *Allocs;
20
21 // Stress-test nonpaged pool
22 for (i=1; i<10000; i++)
23 {
24 // make up some increasing, a bit irregular size
25 AllocSize = i*10;
26
27 if (i % 10)
28 AllocSize++;
29
30 if (i % 25)
31 AllocSize += 13;
32
33 // start with non-paged pool
34 Ptr = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
35
36 // it may fail due to no-memory condition
37 if (!Ptr) break;
38
39 // try to fully fill it
40 RtlFillMemory(Ptr, AllocSize, 0xAB);
41
42 // free it
43 ExFreePoolWithTag(Ptr, TAG_POOLTEST);
44 }
45
46 // now paged one
47 for (i=1; i<10000; i++)
48 {
49 // make up some increasing, a bit irregular size
50 AllocSize = i*50;
51
52 if (i % 10)
53 AllocSize++;
54
55 if (i % 25)
56 AllocSize += 13;
57
58 // start with non-paged pool
59 Ptr = ExAllocatePoolWithTag(PagedPool, AllocSize, TAG_POOLTEST);
60
61 // it may fail due to no-memory condition
62 if (!Ptr) break;
63
64 // try to fully fill it
65 RtlFillMemory(Ptr, AllocSize, 0xAB);
66
67 // free it
68 ExFreePoolWithTag(Ptr, TAG_POOLTEST);
69 }
70
71 // test super-big allocations
72 /*AllocSize = 2UL * 1024 * 1024 * 1024;
73 Ptr = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
74 ok(Ptr == NULL, "Allocating 2Gb of nonpaged pool should fail\n");
75
76 Ptr = ExAllocatePoolWithTag(PagedPool, AllocSize, TAG_POOLTEST);
77 ok(Ptr == NULL, "Allocating 2Gb of paged pool should fail\n");*/
78
79 // now test allocating lots of small/medium blocks
80 AllocNumber = 100000;
81 Allocs = ExAllocatePoolWithTag(PagedPool, sizeof(Allocs) * AllocNumber, TAG_POOLTEST);
82
83 // alloc blocks
84 for (i=0; i<AllocNumber; i++)
85 {
86 AllocSize = 42;
87 Allocs[i] = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
88 }
89
90 // now free them
91 for (i=0; i<AllocNumber; i++)
92 {
93 ExFreePoolWithTag(Allocs[i], TAG_POOLTEST);
94 }
95
96
97 ExFreePoolWithTag(Allocs, TAG_POOLTEST);
98 }
99
100 static VOID PoolsCorruption(VOID)
101 {
102 PULONG Ptr;
103 ULONG AllocSize;
104
105 // start with non-paged pool
106 AllocSize = 4096 + 0x10;
107 Ptr = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
108
109 // touch all bytes, it shouldn't cause an exception
110 RtlZeroMemory(Ptr, AllocSize);
111
112 /* TODO: These fail because accessing invalid memory doesn't necessarily
113 cause an access violation */
114 #ifdef THIS_DOESNT_WORK
115 // test buffer overrun, right after our allocation ends
116 _SEH2_TRY
117 {
118 TestPtr = (PULONG)((PUCHAR)Ptr + AllocSize);
119 //Ptr[4] = 0xd33dbeef;
120 *TestPtr = 0xd33dbeef;
121 }
122 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
123 {
124 /* Get the status */
125 Status = _SEH2_GetExceptionCode();
126 } _SEH2_END;
127
128 ok(Status == STATUS_ACCESS_VIOLATION, "Exception should occur, but got Status 0x%08lX\n", Status);
129
130 // test overrun in a distant byte range, but within 4096KB
131 _SEH2_TRY
132 {
133 Ptr[2020] = 0xdeadb33f;
134 }
135 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
136 {
137 /* Get the status */
138 Status = _SEH2_GetExceptionCode();
139 } _SEH2_END;
140
141 ok(Status == STATUS_ACCESS_VIOLATION, "Exception should occur, but got Status 0x%08lX\n", Status);
142 #endif
143
144 // free the pool
145 ExFreePoolWithTag(Ptr, TAG_POOLTEST);
146 }
147
148 START_TEST(ExPools)
149 {
150 PoolsTest();
151 PoolsCorruption();
152 }