8fd6f6bd898c249b0b61b7776fa89c9a0cc0b261
[reactos.git] / modules / rostests / kmtests / ntos_cc / CcMapData_drv.c
1 /*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test driver for CcMapData function
5 * PROGRAMMER: Pierre Schweitzer <pierre@reactos.org>
6 */
7
8 #include <kmt_test.h>
9
10 #define NDEBUG
11 #include <debug.h>
12
13 #define IOCTL_START_TEST 1
14 #define IOCTL_FINISH_TEST 2
15
16 typedef struct _TEST_FCB
17 {
18 FSRTL_ADVANCED_FCB_HEADER Header;
19 SECTION_OBJECT_POINTERS SectionObjectPointers;
20 FAST_MUTEX HeaderMutex;
21 } TEST_FCB, *PTEST_FCB;
22
23 static ULONG TestTestId = -1;
24 static PFILE_OBJECT TestFileObject;
25 static PDEVICE_OBJECT TestDeviceObject;
26 static KMT_IRP_HANDLER TestIrpHandler;
27 static KMT_MESSAGE_HANDLER TestMessageHandler;
28
29 NTSTATUS
30 TestEntry(
31 _In_ PDRIVER_OBJECT DriverObject,
32 _In_ PCUNICODE_STRING RegistryPath,
33 _Out_ PCWSTR *DeviceName,
34 _Inout_ INT *Flags)
35 {
36 NTSTATUS Status = STATUS_SUCCESS;
37
38 PAGED_CODE();
39
40 UNREFERENCED_PARAMETER(RegistryPath);
41
42 *DeviceName = L"CcMapData";
43 *Flags = TESTENTRY_NO_EXCLUSIVE_DEVICE |
44 TESTENTRY_BUFFERED_IO_DEVICE |
45 TESTENTRY_NO_READONLY_DEVICE;
46
47 KmtRegisterIrpHandler(IRP_MJ_READ, NULL, TestIrpHandler);
48 KmtRegisterMessageHandler(0, NULL, TestMessageHandler);
49
50
51 return Status;
52 }
53
54 VOID
55 TestUnload(
56 _In_ PDRIVER_OBJECT DriverObject)
57 {
58 PAGED_CODE();
59 }
60
61 BOOLEAN
62 NTAPI
63 AcquireForLazyWrite(
64 _In_ PVOID Context,
65 _In_ BOOLEAN Wait)
66 {
67 return TRUE;
68 }
69
70 VOID
71 NTAPI
72 ReleaseFromLazyWrite(
73 _In_ PVOID Context)
74 {
75 return;
76 }
77
78 BOOLEAN
79 NTAPI
80 AcquireForReadAhead(
81 _In_ PVOID Context,
82 _In_ BOOLEAN Wait)
83 {
84 return TRUE;
85 }
86
87 VOID
88 NTAPI
89 ReleaseFromReadAhead(
90 _In_ PVOID Context)
91 {
92 return;
93 }
94
95 static CACHE_MANAGER_CALLBACKS Callbacks = {
96 AcquireForLazyWrite,
97 ReleaseFromLazyWrite,
98 AcquireForReadAhead,
99 ReleaseFromReadAhead,
100 };
101
102 static CC_FILE_SIZES FileSizes = {
103 RTL_CONSTANT_LARGE_INTEGER((LONGLONG)0x4000), // .AllocationSize
104 RTL_CONSTANT_LARGE_INTEGER((LONGLONG)0x4000), // .FileSize
105 RTL_CONSTANT_LARGE_INTEGER((LONGLONG)0x4000) // .ValidDataLength
106 };
107
108 static
109 PVOID
110 MapAndLockUserBuffer(
111 _In_ _Out_ PIRP Irp,
112 _In_ ULONG BufferLength)
113 {
114 PMDL Mdl;
115
116 if (Irp->MdlAddress == NULL)
117 {
118 Mdl = IoAllocateMdl(Irp->UserBuffer, BufferLength, FALSE, FALSE, Irp);
119 if (Mdl == NULL)
120 {
121 return NULL;
122 }
123
124 _SEH2_TRY
125 {
126 MmProbeAndLockPages(Mdl, Irp->RequestorMode, IoWriteAccess);
127 }
128 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
129 {
130 IoFreeMdl(Mdl);
131 Irp->MdlAddress = NULL;
132 _SEH2_YIELD(return NULL);
133 }
134 _SEH2_END;
135 }
136
137 return MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);
138 }
139
140 static
141 VOID
142 PerformTest(
143 ULONG TestId,
144 PDEVICE_OBJECT DeviceObject)
145 {
146 PVOID Bcb;
147 BOOLEAN Ret;
148 PULONG Buffer;
149 PTEST_FCB Fcb;
150 LARGE_INTEGER Offset;
151
152 ok_eq_pointer(TestFileObject, NULL);
153 ok_eq_pointer(TestDeviceObject, NULL);
154 ok_eq_ulong(TestTestId, -1);
155
156 TestDeviceObject = DeviceObject;
157 TestTestId = TestId;
158 TestFileObject = IoCreateStreamFileObject(NULL, DeviceObject);
159 if (!skip(TestFileObject != NULL, "Failed to allocate FO\n"))
160 {
161 Fcb = ExAllocatePool(NonPagedPool, sizeof(TEST_FCB));
162 if (!skip(Fcb != NULL, "ExAllocatePool failed\n"))
163 {
164 RtlZeroMemory(Fcb, sizeof(TEST_FCB));
165 ExInitializeFastMutex(&Fcb->HeaderMutex);
166 FsRtlSetupAdvancedHeader(&Fcb->Header, &Fcb->HeaderMutex);
167
168 TestFileObject->FsContext = Fcb;
169 TestFileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
170
171 KmtStartSeh();
172 CcInitializeCacheMap(TestFileObject, &FileSizes, FALSE, &Callbacks, NULL);
173 KmtEndSeh(STATUS_SUCCESS);
174
175 if (!skip(CcIsFileCached(TestFileObject) == TRUE, "CcInitializeCacheMap failed\n"))
176 {
177 Ret = FALSE;
178 Offset.QuadPart = TestId * 0x1000;
179 KmtStartSeh();
180 Ret = CcMapData(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
181 KmtEndSeh(STATUS_SUCCESS);
182
183 if (!skip(Ret == TRUE, "CcMapData failed\n"))
184 {
185 ok_eq_ulong(Buffer[(0x3000 - TestId * 0x1000) / sizeof(ULONG)], 0xDEADBABE);
186
187 CcUnpinData(Bcb);
188 }
189 }
190 }
191 }
192 }
193
194
195 static
196 VOID
197 CleanupTest(
198 ULONG TestId,
199 PDEVICE_OBJECT DeviceObject)
200 {
201 CACHE_UNINITIALIZE_EVENT CacheUninitEvent;
202
203 ok_eq_pointer(TestDeviceObject, DeviceObject);
204 ok_eq_ulong(TestTestId, TestId);
205
206 if (!skip(TestFileObject != NULL, "No test FO\n"))
207 {
208 if (CcIsFileCached(TestFileObject))
209 {
210 KeInitializeEvent(&CacheUninitEvent.Event, NotificationEvent, FALSE);
211 CcUninitializeCacheMap(TestFileObject, NULL, &CacheUninitEvent);
212 KeWaitForSingleObject(&CacheUninitEvent.Event, Executive, KernelMode, FALSE, NULL);
213 }
214
215 if (TestFileObject->FsContext != NULL)
216 {
217 ExFreePool(TestFileObject->FsContext);
218 TestFileObject->FsContext = NULL;
219 TestFileObject->SectionObjectPointer = NULL;
220 }
221
222 ObDereferenceObject(TestFileObject);
223 }
224
225 TestFileObject = NULL;
226 TestDeviceObject = NULL;
227 TestTestId = -1;
228 }
229
230
231 static
232 NTSTATUS
233 TestMessageHandler(
234 _In_ PDEVICE_OBJECT DeviceObject,
235 _In_ ULONG ControlCode,
236 _In_opt_ PVOID Buffer,
237 _In_ SIZE_T InLength,
238 _Inout_ PSIZE_T OutLength)
239 {
240 NTSTATUS Status = STATUS_SUCCESS;
241
242 switch (ControlCode)
243 {
244 case IOCTL_START_TEST:
245 ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
246 PerformTest(*(PULONG)Buffer, DeviceObject);
247 break;
248
249 case IOCTL_FINISH_TEST:
250 ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
251 CleanupTest(*(PULONG)Buffer, DeviceObject);
252 break;
253
254 default:
255 Status = STATUS_NOT_IMPLEMENTED;
256 break;
257 }
258
259 return Status;
260 }
261
262 static
263 NTSTATUS
264 TestIrpHandler(
265 _In_ PDEVICE_OBJECT DeviceObject,
266 _In_ PIRP Irp,
267 _In_ PIO_STACK_LOCATION IoStack)
268 {
269 NTSTATUS Status;
270
271 PAGED_CODE();
272
273 DPRINT("IRP %x/%x\n", IoStack->MajorFunction, IoStack->MinorFunction);
274 ASSERT(IoStack->MajorFunction == IRP_MJ_READ);
275
276 Status = STATUS_NOT_SUPPORTED;
277 Irp->IoStatus.Information = 0;
278
279 if (IoStack->MajorFunction == IRP_MJ_READ)
280 {
281 PMDL Mdl;
282 ULONG Length;
283 PVOID Buffer;
284 LARGE_INTEGER Offset;
285
286 Offset = IoStack->Parameters.Read.ByteOffset;
287 Length = IoStack->Parameters.Read.Length;
288
289 ok_eq_pointer(DeviceObject, TestDeviceObject);
290 ok_eq_pointer(IoStack->FileObject, TestFileObject);
291
292 ok(FlagOn(Irp->Flags, IRP_NOCACHE), "Not coming from Cc\n");
293
294 ok_irql(APC_LEVEL);
295 ok((Offset.QuadPart % PAGE_SIZE == 0 || Offset.QuadPart == 0), "Offset is not aligned: %I64i\n", Offset.QuadPart);
296 ok(Length % PAGE_SIZE == 0, "Length is not aligned: %I64i\n", Length);
297
298 ok(Irp->AssociatedIrp.SystemBuffer == NULL, "A SystemBuffer was allocated!\n");
299 Buffer = MapAndLockUserBuffer(Irp, Length);
300 ok(Buffer != NULL, "Null pointer!\n");
301 RtlFillMemory(Buffer, Length, 0xBA);
302
303 Status = STATUS_SUCCESS;
304 if (Offset.QuadPart <= 0x3000 && Offset.QuadPart + Length > 0x3000)
305 {
306 *(PULONG)((ULONG_PTR)Buffer + (ULONG_PTR)(0x3000 - Offset.QuadPart)) = 0xDEADBABE;
307 }
308
309 Mdl = Irp->MdlAddress;
310 ok(Mdl != NULL, "Null pointer for MDL!\n");
311 ok((Mdl->MdlFlags & MDL_PAGES_LOCKED) != 0, "MDL not locked\n");
312 ok((Mdl->MdlFlags & MDL_SOURCE_IS_NONPAGED_POOL) == 0, "MDL from non paged\n");
313 ok((Mdl->MdlFlags & MDL_IO_PAGE_READ) != 0, "Non paging IO\n");
314 ok((Irp->Flags & IRP_PAGING_IO) != 0, "Non paging IO\n");
315
316 Irp->IoStatus.Information = Length;
317 }
318
319 if (Status == STATUS_PENDING)
320 {
321 IoMarkIrpPending(Irp);
322 IoCompleteRequest(Irp, IO_NO_INCREMENT);
323 Status = STATUS_PENDING;
324 }
325 else
326 {
327 Irp->IoStatus.Status = Status;
328 IoCompleteRequest(Irp, IO_NO_INCREMENT);
329 }
330
331 return Status;
332 }