[KMTESTS:CC] Add more tests for CcPinMappedData()
[reactos.git] / modules / rostests / kmtests / ntos_cc / CcPinMappedData_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 CcPinMappedData 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 typedef struct _TEST_CONTEXT
24 {
25 PVOID Bcb;
26 PULONG Buffer;
27 ULONG Length;
28 } TEST_CONTEXT, *PTEST_CONTEXT;
29
30 static ULONG TestTestId = -1;
31 static PFILE_OBJECT TestFileObject;
32 static PDEVICE_OBJECT TestDeviceObject;
33 static KMT_IRP_HANDLER TestIrpHandler;
34 static KMT_MESSAGE_HANDLER TestMessageHandler;
35
36 NTSTATUS
37 TestEntry(
38 _In_ PDRIVER_OBJECT DriverObject,
39 _In_ PCUNICODE_STRING RegistryPath,
40 _Out_ PCWSTR *DeviceName,
41 _Inout_ INT *Flags)
42 {
43 NTSTATUS Status = STATUS_SUCCESS;
44
45 PAGED_CODE();
46
47 UNREFERENCED_PARAMETER(RegistryPath);
48
49 *DeviceName = L"CcPinMappedData";
50 *Flags = TESTENTRY_NO_EXCLUSIVE_DEVICE |
51 TESTENTRY_BUFFERED_IO_DEVICE |
52 TESTENTRY_NO_READONLY_DEVICE;
53
54 KmtRegisterIrpHandler(IRP_MJ_READ, NULL, TestIrpHandler);
55 KmtRegisterMessageHandler(0, NULL, TestMessageHandler);
56
57
58 return Status;
59 }
60
61 VOID
62 TestUnload(
63 _In_ PDRIVER_OBJECT DriverObject)
64 {
65 PAGED_CODE();
66 }
67
68 BOOLEAN
69 NTAPI
70 AcquireForLazyWrite(
71 _In_ PVOID Context,
72 _In_ BOOLEAN Wait)
73 {
74 return TRUE;
75 }
76
77 VOID
78 NTAPI
79 ReleaseFromLazyWrite(
80 _In_ PVOID Context)
81 {
82 return;
83 }
84
85 BOOLEAN
86 NTAPI
87 AcquireForReadAhead(
88 _In_ PVOID Context,
89 _In_ BOOLEAN Wait)
90 {
91 return TRUE;
92 }
93
94 VOID
95 NTAPI
96 ReleaseFromReadAhead(
97 _In_ PVOID Context)
98 {
99 return;
100 }
101
102 static CACHE_MANAGER_CALLBACKS Callbacks = {
103 AcquireForLazyWrite,
104 ReleaseFromLazyWrite,
105 AcquireForReadAhead,
106 ReleaseFromReadAhead,
107 };
108
109 static CC_FILE_SIZES FileSizes = {
110 RTL_CONSTANT_LARGE_INTEGER((LONGLONG)0x4000), // .AllocationSize
111 RTL_CONSTANT_LARGE_INTEGER((LONGLONG)0x4000), // .FileSize
112 RTL_CONSTANT_LARGE_INTEGER((LONGLONG)0x4000) // .ValidDataLength
113 };
114
115 static
116 PVOID
117 MapAndLockUserBuffer(
118 _In_ _Out_ PIRP Irp,
119 _In_ ULONG BufferLength)
120 {
121 PMDL Mdl;
122
123 if (Irp->MdlAddress == NULL)
124 {
125 Mdl = IoAllocateMdl(Irp->UserBuffer, BufferLength, FALSE, FALSE, Irp);
126 if (Mdl == NULL)
127 {
128 return NULL;
129 }
130
131 _SEH2_TRY
132 {
133 MmProbeAndLockPages(Mdl, Irp->RequestorMode, IoWriteAccess);
134 }
135 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
136 {
137 IoFreeMdl(Mdl);
138 Irp->MdlAddress = NULL;
139 _SEH2_YIELD(return NULL);
140 }
141 _SEH2_END;
142 }
143
144 return MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);
145 }
146
147 static
148 VOID
149 NTAPI
150 PinInAnotherThreadExclusive(IN PVOID Context)
151 {
152 BOOLEAN Ret;
153 PULONG Buffer;
154 PVOID Bcb, PinBcb;
155 LARGE_INTEGER Offset;
156 PTEST_CONTEXT TestContext;
157
158 ok(TestFileObject != NULL, "Called in invalid context!\n");
159 ok_eq_ulong(TestTestId, 2);
160
161 TestContext = Context;
162 ok(TestContext != NULL, "Called in invalid context!\n");
163 ok(TestContext->Bcb != NULL, "Called in invalid context!\n");
164 ok(TestContext->Buffer != NULL, "Called in invalid context!\n");
165 ok(TestContext->Length != 0, "Called in invalid context!\n");
166
167 Ret = FALSE;
168 Offset.QuadPart = 0;
169
170 KmtStartSeh();
171 Ret = CcMapData(TestFileObject, &Offset, TestContext->Length, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
172 KmtEndSeh(STATUS_SUCCESS);
173
174 if (!skip(Ret == TRUE, "CcMapData failed\n"))
175 {
176 ok(Bcb != TestContext->Bcb, "Returned same BCB!\n");
177 ok_eq_pointer(Buffer, TestContext->Buffer);
178
179 Ret = FALSE;
180 PinBcb = Bcb;
181
182 KmtStartSeh();
183 Ret = CcPinMappedData(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_EXCLUSIVE, &PinBcb);
184 KmtEndSeh(STATUS_SUCCESS);
185
186 if (!skip(Ret == FALSE, "CcPinMappedData succeed\n"))
187 {
188 ok_eq_pointer(PinBcb, Bcb);
189
190 Ret = FALSE;
191 PinBcb = Bcb;
192
193 KmtStartSeh();
194 Ret = CcPinMappedData(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, 0, &PinBcb);
195 KmtEndSeh(STATUS_SUCCESS);
196
197 if (!skip(Ret == FALSE, "CcPinMappedData succeed\n"))
198 {
199 ok_eq_pointer(PinBcb, Bcb);
200 }
201 else
202 {
203 Bcb = PinBcb;
204 }
205 }
206 else
207 {
208 Bcb = PinBcb;
209 }
210
211 CcUnpinData(Bcb);
212 }
213 }
214
215 static
216 VOID
217 PerformTest(
218 ULONG TestId,
219 PDEVICE_OBJECT DeviceObject)
220 {
221 PVOID Bcb, PinBcb, NewBcb;
222 BOOLEAN Ret;
223 PULONG Buffer;
224 PTEST_FCB Fcb;
225 LARGE_INTEGER Offset;
226
227 ok_eq_pointer(TestFileObject, NULL);
228 ok_eq_pointer(TestDeviceObject, NULL);
229 ok_eq_ulong(TestTestId, -1);
230
231 TestDeviceObject = DeviceObject;
232 TestTestId = TestId;
233 TestFileObject = IoCreateStreamFileObject(NULL, DeviceObject);
234 if (!skip(TestFileObject != NULL, "Failed to allocate FO\n"))
235 {
236 Fcb = ExAllocatePool(NonPagedPool, sizeof(TEST_FCB));
237 if (!skip(Fcb != NULL, "ExAllocatePool failed\n"))
238 {
239 RtlZeroMemory(Fcb, sizeof(TEST_FCB));
240 ExInitializeFastMutex(&Fcb->HeaderMutex);
241 FsRtlSetupAdvancedHeader(&Fcb->Header, &Fcb->HeaderMutex);
242
243 TestFileObject->FsContext = Fcb;
244 TestFileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
245
246 KmtStartSeh();
247 CcInitializeCacheMap(TestFileObject, &FileSizes, TRUE, &Callbacks, NULL);
248 KmtEndSeh(STATUS_SUCCESS);
249
250 if (!skip(CcIsFileCached(TestFileObject) == TRUE, "CcInitializeCacheMap failed\n"))
251 {
252 if (TestId == 0)
253 {
254 Ret = FALSE;
255 Offset.QuadPart = 0;
256 KmtStartSeh();
257 Ret = CcMapData(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
258 KmtEndSeh(STATUS_SUCCESS);
259
260 if (!skip(Ret == TRUE, "CcMapData failed\n"))
261 {
262 Ret = FALSE;
263 PinBcb = Bcb;
264 ok_eq_ulong(Buffer[0x3000 / sizeof(ULONG)], 0xDEADBABE);
265
266 KmtStartSeh();
267 Ret = CcPinMappedData(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT, &PinBcb);
268 KmtEndSeh(STATUS_SUCCESS);
269
270 if (!skip(Ret == TRUE, "CcPinMappedData failed\n"))
271 {
272 ok(Bcb != PinBcb, "Returned same BCB!\n");
273 ok(*(PUSHORT)PinBcb == 0x2FD, "Not a BCB: %x\n", *(PUSHORT)PinBcb);
274
275 /* Previous BCB isn't valid anymore! */
276 Bcb = PinBcb;
277 }
278
279 CcUnpinData(Bcb);
280 }
281 }
282 else if (TestId == 1)
283 {
284 Ret = FALSE;
285 Offset.QuadPart = 0;
286 KmtStartSeh();
287 Ret = CcPinRead(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT, &PinBcb, (PVOID *)&Buffer);
288 KmtEndSeh(STATUS_SUCCESS);
289
290 if (!skip(Ret == TRUE, "CcPinRead failed\n"))
291 {
292 ok(*(PUSHORT)PinBcb == 0x2FD, "Not a BCB: %x\n", *(PUSHORT)PinBcb);
293 ok_eq_ulong(Buffer[0x3000 / sizeof(ULONG)], 0xDEADBABE);
294
295 Ret = FALSE;
296 KmtStartSeh();
297 Ret = CcMapData(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
298 KmtEndSeh(STATUS_SUCCESS);
299
300 if (!skip(Ret == TRUE, "CcMapData failed\n"))
301 {
302 ok(Bcb != PinBcb, "Returned same BCB!\n");
303
304 Ret = FALSE;
305 NewBcb = Bcb;
306 KmtStartSeh();
307 Ret = CcPinMappedData(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT, &NewBcb);
308 KmtEndSeh(STATUS_SUCCESS);
309
310 if (!skip(Ret == TRUE, "CcPinMappedData failed\n"))
311 {
312 ok(Bcb != NewBcb, "Returned same BCB!\n");
313 ok_eq_pointer(NewBcb, PinBcb);
314 ok(*(PUSHORT)NewBcb == 0x2FD, "Not a BCB: %x\n", *(PUSHORT)NewBcb);
315
316 /* Previous BCB isn't valid anymore! */
317 Bcb = NewBcb;
318 }
319
320 CcUnpinData(Bcb);
321 }
322
323 CcUnpinData(PinBcb);
324 }
325 }
326 else if (TestId == 2)
327 {
328 PTEST_CONTEXT TestContext;
329
330 TestContext = ExAllocatePool(NonPagedPool, sizeof(TEST_CONTEXT));
331 if (!skip(TestContext != NULL, "ExAllocatePool failed\n"))
332 {
333 Ret = FALSE;
334 Offset.QuadPart = 0;
335 KmtStartSeh();
336 Ret = CcPinRead(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT | PIN_EXCLUSIVE, &TestContext->Bcb, (PVOID *)&TestContext->Buffer);
337 KmtEndSeh(STATUS_SUCCESS);
338
339 if (!skip(Ret == TRUE, "CcPinRead failed\n"))
340 {
341 PKTHREAD ThreadHandle;
342
343 ok(*(PUSHORT)TestContext->Bcb == 0x2FD, "Not a BCB: %x\n", *(PUSHORT)TestContext->Bcb);
344 ok_eq_ulong(TestContext->Buffer[0x3000 / sizeof(ULONG)], 0xDEADBABE);
345
346 TestContext->Length = FileSizes.FileSize.QuadPart - Offset.QuadPart;
347 ThreadHandle = KmtStartThread(PinInAnotherThreadExclusive, TestContext);
348 KmtFinishThread(ThreadHandle, NULL);
349
350 CcUnpinData(TestContext->Bcb);
351 }
352
353 ExFreePool(TestContext);
354 }
355 }
356 else if (TestId == 3)
357 {
358 Ret = FALSE;
359 Offset.QuadPart = 0;
360 KmtStartSeh();
361 Ret = CcMapData(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
362 KmtEndSeh(STATUS_SUCCESS);
363
364 if (!skip(Ret == TRUE, "CcMapData failed\n"))
365 {
366 Ret = FALSE;
367 PinBcb = Bcb;
368 ok_eq_ulong(Buffer[0x3000 / sizeof(ULONG)], 0xDEADBABE);
369
370 KmtStartSeh();
371 Ret = CcPinMappedData(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_IF_BCB, &PinBcb);
372 KmtEndSeh(STATUS_SUCCESS);
373
374 if (!skip(Ret == FALSE, "CcPinMappedData succeed\n"))
375 {
376 ok_eq_pointer(Bcb, PinBcb);
377 }
378 else
379 {
380 Bcb = PinBcb;
381 }
382
383 CcUnpinData(Bcb);
384 }
385 }
386 }
387 }
388 }
389 }
390
391
392 static
393 VOID
394 CleanupTest(
395 ULONG TestId,
396 PDEVICE_OBJECT DeviceObject)
397 {
398 LARGE_INTEGER Zero = RTL_CONSTANT_LARGE_INTEGER(0LL);
399 CACHE_UNINITIALIZE_EVENT CacheUninitEvent;
400
401 ok_eq_pointer(TestDeviceObject, DeviceObject);
402 ok_eq_ulong(TestTestId, TestId);
403
404 if (!skip(TestFileObject != NULL, "No test FO\n"))
405 {
406 if (CcIsFileCached(TestFileObject))
407 {
408 KeInitializeEvent(&CacheUninitEvent.Event, NotificationEvent, FALSE);
409 CcUninitializeCacheMap(TestFileObject, &Zero, &CacheUninitEvent);
410 KeWaitForSingleObject(&CacheUninitEvent.Event, Executive, KernelMode, FALSE, NULL);
411 }
412
413 if (TestFileObject->FsContext != NULL)
414 {
415 ExFreePool(TestFileObject->FsContext);
416 TestFileObject->FsContext = NULL;
417 TestFileObject->SectionObjectPointer = NULL;
418 }
419
420 ObDereferenceObject(TestFileObject);
421 }
422
423 TestFileObject = NULL;
424 TestDeviceObject = NULL;
425 TestTestId = -1;
426 }
427
428
429 static
430 NTSTATUS
431 TestMessageHandler(
432 _In_ PDEVICE_OBJECT DeviceObject,
433 _In_ ULONG ControlCode,
434 _In_opt_ PVOID Buffer,
435 _In_ SIZE_T InLength,
436 _Inout_ PSIZE_T OutLength)
437 {
438 NTSTATUS Status = STATUS_SUCCESS;
439
440 FsRtlEnterFileSystem();
441
442 switch (ControlCode)
443 {
444 case IOCTL_START_TEST:
445 ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
446 PerformTest(*(PULONG)Buffer, DeviceObject);
447 break;
448
449 case IOCTL_FINISH_TEST:
450 ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
451 CleanupTest(*(PULONG)Buffer, DeviceObject);
452 break;
453
454 default:
455 Status = STATUS_NOT_IMPLEMENTED;
456 break;
457 }
458
459 FsRtlExitFileSystem();
460
461 return Status;
462 }
463
464 static
465 NTSTATUS
466 TestIrpHandler(
467 _In_ PDEVICE_OBJECT DeviceObject,
468 _In_ PIRP Irp,
469 _In_ PIO_STACK_LOCATION IoStack)
470 {
471 NTSTATUS Status;
472
473 PAGED_CODE();
474
475 DPRINT("IRP %x/%x\n", IoStack->MajorFunction, IoStack->MinorFunction);
476 ASSERT(IoStack->MajorFunction == IRP_MJ_READ);
477
478 FsRtlEnterFileSystem();
479
480 Status = STATUS_NOT_SUPPORTED;
481 Irp->IoStatus.Information = 0;
482
483 if (IoStack->MajorFunction == IRP_MJ_READ)
484 {
485 PMDL Mdl;
486 ULONG Length;
487 PVOID Buffer;
488 LARGE_INTEGER Offset;
489
490 Offset = IoStack->Parameters.Read.ByteOffset;
491 Length = IoStack->Parameters.Read.Length;
492
493 ok_eq_pointer(DeviceObject, TestDeviceObject);
494 ok_eq_pointer(IoStack->FileObject, TestFileObject);
495
496 ok(FlagOn(Irp->Flags, IRP_NOCACHE), "Not coming from Cc\n");
497
498 ok_irql(APC_LEVEL);
499 ok((Offset.QuadPart % PAGE_SIZE == 0 || Offset.QuadPart == 0), "Offset is not aligned: %I64i\n", Offset.QuadPart);
500 ok(Length % PAGE_SIZE == 0, "Length is not aligned: %I64i\n", Length);
501
502 ok(Irp->AssociatedIrp.SystemBuffer == NULL, "A SystemBuffer was allocated!\n");
503 Buffer = MapAndLockUserBuffer(Irp, Length);
504 ok(Buffer != NULL, "Null pointer!\n");
505 RtlFillMemory(Buffer, Length, 0xBA);
506
507 Status = STATUS_SUCCESS;
508 if (Offset.QuadPart <= 0x3000 && Offset.QuadPart + Length > 0x3000)
509 {
510 *(PULONG)((ULONG_PTR)Buffer + (ULONG_PTR)(0x3000 - Offset.QuadPart)) = 0xDEADBABE;
511 }
512
513 Mdl = Irp->MdlAddress;
514 ok(Mdl != NULL, "Null pointer for MDL!\n");
515 ok((Mdl->MdlFlags & MDL_PAGES_LOCKED) != 0, "MDL not locked\n");
516 ok((Mdl->MdlFlags & MDL_SOURCE_IS_NONPAGED_POOL) == 0, "MDL from non paged\n");
517 ok((Mdl->MdlFlags & MDL_IO_PAGE_READ) != 0, "Non paging IO\n");
518 ok((Irp->Flags & IRP_PAGING_IO) != 0, "Non paging IO\n");
519
520 Irp->IoStatus.Information = Length;
521 }
522
523 if (Status == STATUS_PENDING)
524 {
525 IoMarkIrpPending(Irp);
526 IoCompleteRequest(Irp, IO_NO_INCREMENT);
527 Status = STATUS_PENDING;
528 }
529 else
530 {
531 Irp->IoStatus.Status = Status;
532 IoCompleteRequest(Irp, IO_NO_INCREMENT);
533 }
534
535 FsRtlExitFileSystem();
536
537 return Status;
538 }