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