68e666c1b3cd0bae5c30752bc2926aca3b23c952
[reactos.git] / modules / rostests / kmtests / ntos_cc / CcPinRead_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 CcPinRead 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 PVOID 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"CcPinRead";
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 PinInAnotherThread(IN PVOID Context)
151 {
152 BOOLEAN Ret;
153 PULONG Buffer;
154 PVOID Bcb;
155 LARGE_INTEGER Offset;
156 PTEST_CONTEXT TestContext;
157
158 ok(TestFileObject != NULL, "Called in invalid context!\n");
159 ok_eq_ulong(TestTestId, 3);
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 = 0x1000;
169 KmtStartSeh();
170 Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_WAIT, &Bcb, (PVOID *)&Buffer);
171 KmtEndSeh(STATUS_SUCCESS);
172
173 if (!skip(Ret == TRUE, "CcPinRead failed\n"))
174 {
175 ok_eq_pointer(Bcb, TestContext->Bcb);
176 ok_eq_pointer(Buffer, TestContext->Buffer);
177
178 CcUnpinData(Bcb);
179 }
180
181 KmtStartSeh();
182 Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_WAIT | PIN_IF_BCB, &Bcb, (PVOID *)&Buffer);
183 KmtEndSeh(STATUS_SUCCESS);
184
185 if (!skip(Ret == TRUE, "CcPinRead failed\n"))
186 {
187 ok_eq_pointer(Bcb, TestContext->Bcb);
188 ok_eq_pointer(Buffer, TestContext->Buffer);
189
190 CcUnpinData(Bcb);
191 }
192
193 KmtStartSeh();
194 Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_EXCLUSIVE, &Bcb, (PVOID *)&Buffer);
195 KmtEndSeh(STATUS_SUCCESS);
196
197 if (!skip(Ret == TRUE, "CcPinRead failed\n"))
198 {
199 ok_eq_pointer(Bcb, TestContext->Bcb);
200 ok_eq_pointer(Buffer, TestContext->Buffer);
201
202 CcUnpinData(Bcb);
203 }
204
205 KmtStartSeh();
206 Ret = CcMapData(TestFileObject, &Offset, TestContext->Length, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
207 KmtEndSeh(STATUS_SUCCESS);
208
209 if (!skip(Ret == TRUE, "CcMapData failed\n"))
210 {
211 ok(Bcb != TestContext->Bcb, "Returned same BCB!\n");
212 ok_eq_pointer(Buffer, TestContext->Buffer);
213
214 CcUnpinData(Bcb);
215 }
216
217 return;
218 }
219
220 static
221 VOID
222 NTAPI
223 PinInAnotherThreadExclusive(IN PVOID Context)
224 {
225 BOOLEAN Ret;
226 PULONG Buffer;
227 PVOID Bcb;
228 LARGE_INTEGER Offset;
229 PTEST_CONTEXT TestContext;
230
231 ok(TestFileObject != NULL, "Called in invalid context!\n");
232 ok_eq_ulong(TestTestId, 3);
233
234 TestContext = Context;
235 ok(TestContext != NULL, "Called in invalid context!\n");
236 ok(TestContext->Bcb != NULL, "Called in invalid context!\n");
237 ok(TestContext->Buffer != NULL, "Called in invalid context!\n");
238 ok(TestContext->Length != 0, "Called in invalid context!\n");
239
240 Ret = FALSE;
241 Offset.QuadPart = 0x1000;
242 KmtStartSeh();
243 Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_EXCLUSIVE, &Bcb, (PVOID *)&Buffer);
244 KmtEndSeh(STATUS_SUCCESS);
245 ok(Ret == FALSE, "CcPinRead succeed\n");
246
247 if (Ret)
248 {
249 CcUnpinData(Bcb);
250 }
251
252 KmtStartSeh();
253 Ret = CcMapData(TestFileObject, &Offset, TestContext->Length, 0, &Bcb, (PVOID *)&Buffer);
254 KmtEndSeh(STATUS_SUCCESS);
255
256 if (!skip(Ret == TRUE, "CcMapData failed\n"))
257 {
258 ok(Bcb != TestContext->Bcb, "Returned same BCB!\n");
259 ok_eq_pointer(Buffer, TestContext->Buffer);
260
261 CcUnpinData(Bcb);
262 }
263
264 return;
265 }
266
267 static
268 VOID
269 PerformTest(
270 ULONG TestId,
271 PDEVICE_OBJECT DeviceObject)
272 {
273 PVOID Bcb;
274 BOOLEAN Ret;
275 PULONG Buffer;
276 PTEST_FCB Fcb;
277 LARGE_INTEGER Offset;
278
279 ok_eq_pointer(TestFileObject, NULL);
280 ok_eq_pointer(TestDeviceObject, NULL);
281 ok_eq_ulong(TestTestId, -1);
282
283 TestDeviceObject = DeviceObject;
284 TestTestId = TestId;
285 TestFileObject = IoCreateStreamFileObject(NULL, DeviceObject);
286 if (!skip(TestFileObject != NULL, "Failed to allocate FO\n"))
287 {
288 Fcb = ExAllocatePool(NonPagedPool, sizeof(TEST_FCB));
289 if (!skip(Fcb != NULL, "ExAllocatePool failed\n"))
290 {
291 BOOLEAN PinAccess = (TestId != 4);
292
293 RtlZeroMemory(Fcb, sizeof(TEST_FCB));
294 ExInitializeFastMutex(&Fcb->HeaderMutex);
295 FsRtlSetupAdvancedHeader(&Fcb->Header, &Fcb->HeaderMutex);
296
297 TestFileObject->FsContext = Fcb;
298 TestFileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
299
300 KmtStartSeh();
301 CcInitializeCacheMap(TestFileObject, &FileSizes, PinAccess, &Callbacks, NULL);
302 KmtEndSeh(STATUS_SUCCESS);
303
304 if (!skip(CcIsFileCached(TestFileObject) == TRUE, "CcInitializeCacheMap failed\n"))
305 {
306 if (TestId < 3)
307 {
308 Ret = FALSE;
309 Offset.QuadPart = TestId * 0x1000;
310 KmtStartSeh();
311 Ret = CcPinRead(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT, &Bcb, (PVOID *)&Buffer);
312 KmtEndSeh(STATUS_SUCCESS);
313
314 if (!skip(Ret == TRUE, "CcPinRead failed\n"))
315 {
316 ok_eq_ulong(Buffer[(0x3000 - TestId * 0x1000) / sizeof(ULONG)], 0xDEADBABE);
317
318 CcUnpinData(Bcb);
319 }
320 }
321 else if (TestId == 3)
322 {
323 PTEST_CONTEXT TestContext;
324
325 TestContext = ExAllocatePool(NonPagedPool, sizeof(TEST_CONTEXT));
326 if (!skip(Fcb != NULL, "ExAllocatePool failed\n"))
327 {
328 Ret = FALSE;
329 Offset.QuadPart = 0x1000;
330
331 /* Try enforce BCB first */
332 KmtStartSeh();
333 Ret = CcPinRead(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT | PIN_IF_BCB, &Bcb, (PVOID *)&Buffer);
334 KmtEndSeh(STATUS_SUCCESS);
335 ok(Ret == FALSE, "CcPinRead succeed\n");
336 if (Ret)
337 {
338 CcUnpinData(Bcb);
339 }
340
341 KmtStartSeh();
342 Ret = CcPinRead(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT, &TestContext->Bcb, &TestContext->Buffer);
343 KmtEndSeh(STATUS_SUCCESS);
344
345 if (!skip(Ret == TRUE, "CcPinRead failed\n"))
346 {
347 PKTHREAD ThreadHandle;
348
349 #ifdef _X86_
350 /* FIXME: Should be fixed, will fail under certains conditions */
351 ok(TestContext->Buffer > (PVOID)0xC1000000 && TestContext->Buffer < (PVOID)0xDCFFFFFF,
352 "Buffer %p not mapped in system space\n", TestContext->Buffer);
353 #else
354 #ifdef _M_AMD64
355 ok(TestContext->Buffer > (PVOID)0xFFFFF98000000000 && TestContext->Buffer < (PVOID)0xFFFFFA8000000000,
356 "Buffer %p not mapped in system space\n", TestContext->Buffer);
357 #else
358 skip(FALSE, "System space mapping not defined\n");
359 #endif
360 #endif
361
362 TestContext->Length = FileSizes.FileSize.QuadPart - Offset.QuadPart;
363 ThreadHandle = KmtStartThread(PinInAnotherThread, TestContext);
364 KmtFinishThread(ThreadHandle, NULL);
365
366 TestContext->Length = FileSizes.FileSize.QuadPart - 2 * Offset.QuadPart;
367 ThreadHandle = KmtStartThread(PinInAnotherThread, TestContext);
368 KmtFinishThread(ThreadHandle, NULL);
369
370 CcUnpinData(TestContext->Bcb);
371 }
372
373 KmtStartSeh();
374 Ret = CcPinRead(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT | PIN_EXCLUSIVE, &TestContext->Bcb, &TestContext->Buffer);
375 KmtEndSeh(STATUS_SUCCESS);
376
377 if (!skip(Ret == TRUE, "CcPinRead failed\n"))
378 {
379 PKTHREAD ThreadHandle;
380
381 TestContext->Length = FileSizes.FileSize.QuadPart - Offset.QuadPart;
382 ThreadHandle = KmtStartThread(PinInAnotherThreadExclusive, TestContext);
383 KmtFinishThread(ThreadHandle, NULL);
384
385 CcUnpinData(TestContext->Bcb);
386 }
387
388 ExFreePool(TestContext);
389 }
390 }
391 else if (TestId == 4)
392 {
393 Ret = FALSE;
394 Offset.QuadPart = 0x1000;
395 KmtStartSeh();
396 Ret = CcPinRead(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT, &Bcb, (PVOID *)&Buffer);
397 KmtEndSeh(STATUS_SUCCESS);
398
399 if (!skip(Ret == TRUE, "CcPinRead failed\n"))
400 {
401 ok_eq_ulong(Buffer[0x2000 / sizeof(ULONG)], 0);
402
403 CcUnpinData(Bcb);
404 }
405 }
406 }
407 }
408 }
409 }
410
411
412 static
413 VOID
414 CleanupTest(
415 ULONG TestId,
416 PDEVICE_OBJECT DeviceObject)
417 {
418 LARGE_INTEGER Zero = RTL_CONSTANT_LARGE_INTEGER(0LL);
419 CACHE_UNINITIALIZE_EVENT CacheUninitEvent;
420
421 ok_eq_pointer(TestDeviceObject, DeviceObject);
422 ok_eq_ulong(TestTestId, TestId);
423
424 if (!skip(TestFileObject != NULL, "No test FO\n"))
425 {
426 if (CcIsFileCached(TestFileObject))
427 {
428 KeInitializeEvent(&CacheUninitEvent.Event, NotificationEvent, FALSE);
429 CcUninitializeCacheMap(TestFileObject, &Zero, &CacheUninitEvent);
430 KeWaitForSingleObject(&CacheUninitEvent.Event, Executive, KernelMode, FALSE, NULL);
431 }
432
433 if (TestFileObject->FsContext != NULL)
434 {
435 ExFreePool(TestFileObject->FsContext);
436 TestFileObject->FsContext = NULL;
437 TestFileObject->SectionObjectPointer = NULL;
438 }
439
440 ObDereferenceObject(TestFileObject);
441 }
442
443 TestFileObject = NULL;
444 TestDeviceObject = NULL;
445 TestTestId = -1;
446 }
447
448
449 static
450 NTSTATUS
451 TestMessageHandler(
452 _In_ PDEVICE_OBJECT DeviceObject,
453 _In_ ULONG ControlCode,
454 _In_opt_ PVOID Buffer,
455 _In_ SIZE_T InLength,
456 _Inout_ PSIZE_T OutLength)
457 {
458 NTSTATUS Status = STATUS_SUCCESS;
459
460 FsRtlEnterFileSystem();
461
462 switch (ControlCode)
463 {
464 case IOCTL_START_TEST:
465 ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
466 PerformTest(*(PULONG)Buffer, DeviceObject);
467 break;
468
469 case IOCTL_FINISH_TEST:
470 ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
471 CleanupTest(*(PULONG)Buffer, DeviceObject);
472 break;
473
474 default:
475 Status = STATUS_NOT_IMPLEMENTED;
476 break;
477 }
478
479 FsRtlExitFileSystem();
480
481 return Status;
482 }
483
484 static
485 NTSTATUS
486 TestIrpHandler(
487 _In_ PDEVICE_OBJECT DeviceObject,
488 _In_ PIRP Irp,
489 _In_ PIO_STACK_LOCATION IoStack)
490 {
491 NTSTATUS Status;
492
493 PAGED_CODE();
494
495 DPRINT("IRP %x/%x\n", IoStack->MajorFunction, IoStack->MinorFunction);
496 ASSERT(IoStack->MajorFunction == IRP_MJ_READ);
497
498 FsRtlEnterFileSystem();
499
500 Status = STATUS_NOT_SUPPORTED;
501 Irp->IoStatus.Information = 0;
502
503 if (IoStack->MajorFunction == IRP_MJ_READ)
504 {
505 PMDL Mdl;
506 ULONG Length;
507 PVOID Buffer;
508 LARGE_INTEGER Offset;
509
510 Offset = IoStack->Parameters.Read.ByteOffset;
511 Length = IoStack->Parameters.Read.Length;
512
513 ok_eq_pointer(DeviceObject, TestDeviceObject);
514 ok_eq_pointer(IoStack->FileObject, TestFileObject);
515
516 ok(FlagOn(Irp->Flags, IRP_NOCACHE), "Not coming from Cc\n");
517
518 ok_irql(APC_LEVEL);
519 ok((Offset.QuadPart % PAGE_SIZE == 0 || Offset.QuadPart == 0), "Offset is not aligned: %I64i\n", Offset.QuadPart);
520 ok(Length % PAGE_SIZE == 0, "Length is not aligned: %I64i\n", Length);
521
522 ok(Irp->AssociatedIrp.SystemBuffer == NULL, "A SystemBuffer was allocated!\n");
523 Buffer = MapAndLockUserBuffer(Irp, Length);
524 ok(Buffer != NULL, "Null pointer!\n");
525 RtlFillMemory(Buffer, Length, 0xBA);
526
527 Status = STATUS_SUCCESS;
528 if (Offset.QuadPart <= 0x3000 && Offset.QuadPart + Length > 0x3000)
529 {
530 *(PULONG)((ULONG_PTR)Buffer + (ULONG_PTR)(0x3000 - Offset.QuadPart)) = 0xDEADBABE;
531 }
532
533 Mdl = Irp->MdlAddress;
534 ok(Mdl != NULL, "Null pointer for MDL!\n");
535 ok((Mdl->MdlFlags & MDL_PAGES_LOCKED) != 0, "MDL not locked\n");
536 ok((Mdl->MdlFlags & MDL_SOURCE_IS_NONPAGED_POOL) == 0, "MDL from non paged\n");
537 ok((Mdl->MdlFlags & MDL_IO_PAGE_READ) != 0, "Non paging IO\n");
538 ok((Irp->Flags & IRP_PAGING_IO) != 0, "Non paging IO\n");
539
540 Irp->IoStatus.Information = Length;
541 }
542
543 if (Status == STATUS_PENDING)
544 {
545 IoMarkIrpPending(Irp);
546 IoCompleteRequest(Irp, IO_NO_INCREMENT);
547 Status = STATUS_PENDING;
548 }
549 else
550 {
551 Irp->IoStatus.Status = Status;
552 IoCompleteRequest(Irp, IO_NO_INCREMENT);
553 }
554
555 FsRtlExitFileSystem();
556
557 return Status;
558 }