1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/*
 * PROJECT:         ReactOS kernel-mode tests
 * LICENSE:         LGPLv2.1+ - See COPYING.LIB in the top level directory
 * PURPOSE:         Test driver for CcPinRead function
 * PROGRAMMER:      Pierre Schweitzer <pierre@reactos.org>
 */

#include <kmt_test.h>

#define NDEBUG
#include <debug.h>

#define IOCTL_START_TEST  1
#define IOCTL_FINISH_TEST 2

typedef struct _TEST_FCB
{
    FSRTL_ADVANCED_FCB_HEADER Header;
    SECTION_OBJECT_POINTERS SectionObjectPointers;
    FAST_MUTEX HeaderMutex;
} TEST_FCB, *PTEST_FCB;

typedef struct _TEST_CONTEXT
{
    PVOID Bcb;
    PVOID Buffer;
    ULONG Length;
} TEST_CONTEXT, *PTEST_CONTEXT;

static ULONG TestTestId = -1;
static PFILE_OBJECT TestFileObject;
static PDEVICE_OBJECT TestDeviceObject;
static KMT_IRP_HANDLER TestIrpHandler;
static KMT_MESSAGE_HANDLER TestMessageHandler;
static BOOLEAN TestWriteCalled = FALSE;
static ULONGLONG Memory = 0;
static BOOLEAN TS = FALSE;

NTSTATUS
TestEntry(
    _In_ PDRIVER_OBJECT DriverObject,
    _In_ PCUNICODE_STRING RegistryPath,
    _Out_ PCWSTR *DeviceName,
    _Inout_ INT *Flags)
{
    ULONG Length;
    SYSTEM_BASIC_INFORMATION SBI;
    NTSTATUS Status = STATUS_SUCCESS;
    RTL_OSVERSIONINFOEXW VersionInfo;

    PAGED_CODE();

    UNREFERENCED_PARAMETER(RegistryPath);

    *DeviceName = L"CcPinRead";
    *Flags = TESTENTRY_NO_EXCLUSIVE_DEVICE |
             TESTENTRY_BUFFERED_IO_DEVICE |
             TESTENTRY_NO_READONLY_DEVICE;

    KmtRegisterIrpHandler(IRP_MJ_READ, NULL, TestIrpHandler);
    KmtRegisterIrpHandler(IRP_MJ_WRITE, NULL, TestIrpHandler);
    KmtRegisterMessageHandler(0, NULL, TestMessageHandler);

    Status = ZwQuerySystemInformation(SystemBasicInformation,
                                      &SBI,
                                      sizeof(SBI),
                                      &Length);
    if (NT_SUCCESS(Status))
    {
        Memory = (SBI.NumberOfPhysicalPages * SBI.PageSize) / 1024 / 1024;
    }
    else
    {
        Status = STATUS_SUCCESS;<--- Status is assigned
    }

    VersionInfo.dwOSVersionInfoSize = sizeof(VersionInfo);
    Status = RtlGetVersion((PRTL_OSVERSIONINFOW)&VersionInfo);<--- Status is overwritten
    if (NT_SUCCESS(Status))
    {
        TS = BooleanFlagOn(VersionInfo.wSuiteMask, VER_SUITE_TERMINAL) &&
             !BooleanFlagOn(VersionInfo.wSuiteMask, VER_SUITE_SINGLEUSERTS);
    }
    else
    {
        Status = STATUS_SUCCESS;
    }

    trace("System with %I64dMb RAM and terminal services %S\n",  Memory, (TS ? L"enabled" : L"disabled"));

    return Status;
}

VOID
TestUnload(
    _In_ PDRIVER_OBJECT DriverObject)
{
    PAGED_CODE();
}

BOOLEAN
NTAPI
AcquireForLazyWrite(
    _In_ PVOID Context,
    _In_ BOOLEAN Wait)
{
    return TRUE;
}

VOID
NTAPI
ReleaseFromLazyWrite(
    _In_ PVOID Context)
{
    return;
}

BOOLEAN
NTAPI
AcquireForReadAhead(
    _In_ PVOID Context,
    _In_ BOOLEAN Wait)
{
    return TRUE;
}

VOID
NTAPI
ReleaseFromReadAhead(
    _In_ PVOID Context)
{
    return;
}

static CACHE_MANAGER_CALLBACKS Callbacks = {
    AcquireForLazyWrite,
    ReleaseFromLazyWrite,
    AcquireForReadAhead,
    ReleaseFromReadAhead,
};

static CC_FILE_SIZES FileSizes = {
    RTL_CONSTANT_LARGE_INTEGER((LONGLONG)0x4000), // .AllocationSize
    RTL_CONSTANT_LARGE_INTEGER((LONGLONG)0x4000), // .FileSize
    RTL_CONSTANT_LARGE_INTEGER((LONGLONG)0x4000)  // .ValidDataLength
};

static CC_FILE_SIZES SmallFileSizes = {
    RTL_CONSTANT_LARGE_INTEGER((LONGLONG)512), // .AllocationSize
    RTL_CONSTANT_LARGE_INTEGER((LONGLONG)496), // .FileSize
    RTL_CONSTANT_LARGE_INTEGER((LONGLONG)496)  // .ValidDataLength
};

static
PVOID
MapAndLockUserBuffer(
    _In_ _Out_ PIRP Irp,
    _In_ ULONG BufferLength)
{
    PMDL Mdl;

    if (Irp->MdlAddress == NULL)
    {
        Mdl = IoAllocateMdl(Irp->UserBuffer, BufferLength, FALSE, FALSE, Irp);
        if (Mdl == NULL)
        {
            return NULL;
        }

        _SEH2_TRY
        {
            MmProbeAndLockPages(Mdl, Irp->RequestorMode, IoWriteAccess);
        }
        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
        {
            IoFreeMdl(Mdl);
            Irp->MdlAddress = NULL;
            _SEH2_YIELD(return NULL);
        }
        _SEH2_END;
    }

    return MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);
}

#define ok_bcb(B, L, O)                                                                 \
{                                                                                       \
    PPUBLIC_BCB public_bcb = (B);                                                       \
    ok(public_bcb->NodeTypeCode == 0x2FD, "Not a BCB: %x\n", public_bcb->NodeTypeCode); \
    ok(public_bcb->NodeByteSize == 0, "Invalid size: %d\n", public_bcb->NodeByteSize);  \
    ok_eq_ulong(public_bcb->MappedLength, (L));                                         \
    ok_eq_longlong(public_bcb->MappedFileOffset.QuadPart, (O));                         \
}

static
VOID
NTAPI
PinInAnotherThread(IN PVOID Context)
{
    BOOLEAN Ret;
    PULONG Buffer;
    PVOID Bcb;
    LARGE_INTEGER Offset;
    PTEST_CONTEXT TestContext;

    ok(TestFileObject != NULL, "Called in invalid context!\n");
    ok_eq_ulong(TestTestId, 3);

    TestContext = Context;<--- Assignment 'TestContext=Context', assigned value is 0<--- Assignment 'TestContext=Context', assigned value is 0<--- Assignment 'TestContext=Context', assigned value is 0<--- Assignment 'TestContext=Context', assigned value is 0
    ok(TestContext != NULL, "Called in invalid context!\n");<--- Assuming that condition 'TestContext!=NULL' is not redundant<--- Assuming that condition 'TestContext!=NULL' is not redundant<--- Assuming that condition 'TestContext!=NULL' is not redundant<--- Assuming that condition 'TestContext!=NULL' is not redundant
    ok(TestContext->Bcb != NULL, "Called in invalid context!\n");<--- Null pointer dereference
    ok(TestContext->Buffer != NULL, "Called in invalid context!\n");<--- Null pointer dereference
    ok(TestContext->Length != 0, "Called in invalid context!\n");<--- Null pointer dereference

    Ret = FALSE;<--- Ret is assigned
    Offset.QuadPart = 0x1000;
    KmtStartSeh();
    Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_WAIT, &Bcb, (PVOID *)&Buffer);<--- Null pointer dereference<--- Ret is overwritten
    KmtEndSeh(STATUS_SUCCESS);

    if (!skip(Ret == TRUE, "CcPinRead failed\n"))
    {
        ok_bcb(Bcb, 12288, Offset.QuadPart);
        ok_eq_pointer(Bcb, TestContext->Bcb);
        ok_eq_pointer(Buffer, TestContext->Buffer);

        CcUnpinData(Bcb);
    }

    KmtStartSeh();
    Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_WAIT | PIN_IF_BCB, &Bcb, (PVOID *)&Buffer);
    KmtEndSeh(STATUS_SUCCESS);

    if (!skip(Ret == TRUE, "CcPinRead failed\n"))
    {
        ok_bcb(Bcb, 12288, Offset.QuadPart);
        ok_eq_pointer(Bcb, TestContext->Bcb);
        ok_eq_pointer(Buffer, TestContext->Buffer);

        CcUnpinData(Bcb);
    }

    KmtStartSeh();
    Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_EXCLUSIVE, &Bcb, (PVOID *)&Buffer);
    KmtEndSeh(STATUS_SUCCESS);

    if (!skip(Ret == TRUE, "CcPinRead failed\n"))
    {
        ok_bcb(Bcb, 12288, Offset.QuadPart);
        ok_eq_pointer(Bcb, TestContext->Bcb);
        ok_eq_pointer(Buffer, TestContext->Buffer);

        CcUnpinData(Bcb);
    }

    KmtStartSeh();
    Ret = CcMapData(TestFileObject, &Offset, TestContext->Length, MAP_WAIT, &Bcb, (PVOID *)&Buffer);
    KmtEndSeh(STATUS_SUCCESS);

    if (!skip(Ret == TRUE, "CcMapData failed\n"))
    {
        ok(Bcb != TestContext->Bcb, "Returned same BCB!\n");
        ok_eq_pointer(Buffer, TestContext->Buffer);

        CcUnpinData(Bcb);
    }

    Offset.QuadPart = 0x1500;
    TestContext->Length -= 0x500;

    KmtStartSeh();
    Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_WAIT | PIN_IF_BCB, &Bcb, (PVOID *)&Buffer);
    KmtEndSeh(STATUS_SUCCESS);

    if (!skip(Ret == TRUE, "CcPinRead failed\n"))
    {
        ok_bcb(Bcb, 12288, 4096);
        ok_eq_pointer(Bcb, TestContext->Bcb);
        ok_eq_pointer(Buffer, (PVOID)((ULONG_PTR)TestContext->Buffer + 0x500));

        CcUnpinData(Bcb);
    }

    KmtStartSeh();
    Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_WAIT, &Bcb, (PVOID *)&Buffer);
    KmtEndSeh(STATUS_SUCCESS);

    if (!skip(Ret == TRUE, "CcPinRead failed\n"))
    {
        ok_bcb(Bcb, 12288, 4096);
        ok_eq_pointer(Bcb, TestContext->Bcb);
        ok_eq_pointer(Buffer, (PVOID)((ULONG_PTR)TestContext->Buffer + 0x500));

        CcUnpinData(Bcb);
    }

    KmtStartSeh();
    Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_EXCLUSIVE, &Bcb, (PVOID *)&Buffer);
    KmtEndSeh(STATUS_SUCCESS);

    if (!skip(Ret == TRUE, "CcPinRead failed\n"))
    {
        ok_bcb(Bcb, 12288, 4096);
        ok_eq_pointer(Bcb, TestContext->Bcb);
        ok_eq_pointer(Buffer, (PVOID)((ULONG_PTR)TestContext->Buffer + 0x500));

        CcUnpinData(Bcb);
    }

    return;
}

static
VOID
NTAPI
PinInAnotherThreadExclusive(IN PVOID Context)
{
    BOOLEAN Ret;
    PULONG Buffer;
    PVOID Bcb;
    LARGE_INTEGER Offset;
    PTEST_CONTEXT TestContext;

    ok(TestFileObject != NULL, "Called in invalid context!\n");
    ok_eq_ulong(TestTestId, 3);

    TestContext = Context;<--- Assignment 'TestContext=Context', assigned value is 0<--- Assignment 'TestContext=Context', assigned value is 0<--- Assignment 'TestContext=Context', assigned value is 0<--- Assignment 'TestContext=Context', assigned value is 0<--- Assignment 'TestContext=Context', assigned value is 0
    ok(TestContext != NULL, "Called in invalid context!\n");<--- Assuming that condition 'TestContext!=NULL' is not redundant<--- Assuming that condition 'TestContext!=NULL' is not redundant<--- Assuming that condition 'TestContext!=NULL' is not redundant<--- Assuming that condition 'TestContext!=NULL' is not redundant<--- Assuming that condition 'TestContext!=NULL' is not redundant
    ok(TestContext->Bcb != NULL, "Called in invalid context!\n");<--- Null pointer dereference
    ok(TestContext->Buffer != NULL, "Called in invalid context!\n");<--- Null pointer dereference
    ok(TestContext->Length != 0, "Called in invalid context!\n");<--- Null pointer dereference

    Ret = FALSE;<--- Ret is assigned
    Offset.QuadPart = 0x1000;
    KmtStartSeh();
    Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_EXCLUSIVE, &Bcb, (PVOID *)&Buffer);<--- Null pointer dereference<--- Ret is overwritten
    KmtEndSeh(STATUS_SUCCESS);
    ok(Ret == FALSE, "CcPinRead succeed\n");

    if (Ret)
    {
        CcUnpinData(Bcb);
    }

    KmtStartSeh();
    Ret = CcMapData(TestFileObject, &Offset, TestContext->Length, 0, &Bcb, (PVOID *)&Buffer);<--- Null pointer dereference
    KmtEndSeh(STATUS_SUCCESS);

    if (!skip(Ret == TRUE, "CcMapData failed\n"))
    {
        ok(Bcb != TestContext->Bcb, "Returned same BCB!\n");
        ok_eq_pointer(Buffer, TestContext->Buffer);

        CcUnpinData(Bcb);
    }

    Offset.QuadPart = 0x1500;
    TestContext->Length -= 0x500;

    KmtStartSeh();
    Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, PIN_IF_BCB, &Bcb, (PVOID *)&Buffer);
    KmtEndSeh(STATUS_SUCCESS);
    ok(Ret == FALSE, "CcPinRead succeed\n");

    if (Ret)
    {
        CcUnpinData(Bcb);
    }

    KmtStartSeh();
    Ret = CcPinRead(TestFileObject, &Offset, TestContext->Length, 0, &Bcb, (PVOID *)&Buffer);
    KmtEndSeh(STATUS_SUCCESS);
    ok(Ret == FALSE, "CcPinRead succeed\n");

    if (Ret)
    {
        CcUnpinData(Bcb);
    }

    KmtStartSeh();
    Ret = CcMapData(TestFileObject, &Offset, TestContext->Length, 0, &Bcb, (PVOID *)&Buffer);
    KmtEndSeh(STATUS_SUCCESS);

    if (!skip(Ret == TRUE, "CcMapData failed\n"))
    {
        ok(Bcb != TestContext->Bcb, "Returned same BCB!\n");
        ok_eq_pointer(Buffer, (PVOID)((ULONG_PTR)TestContext->Buffer + 0x500));

        CcUnpinData(Bcb);
    }

    return;
}

static
VOID
PerformTest(
    ULONG TestId,
    PDEVICE_OBJECT DeviceObject)
{
    PVOID Bcb;
    BOOLEAN Ret;
    PULONG Buffer;
    PTEST_FCB Fcb;
    LARGE_INTEGER Offset;

    ok_eq_pointer(TestFileObject, NULL);
    ok_eq_pointer(TestDeviceObject, NULL);
    ok_eq_ulong(TestTestId, -1);

    TestDeviceObject = DeviceObject;
    TestTestId = TestId;
    TestFileObject = IoCreateStreamFileObject(NULL, DeviceObject);
    if (!skip(TestFileObject != NULL, "Failed to allocate FO\n"))
    {
        Fcb = ExAllocatePool(NonPagedPool, sizeof(TEST_FCB));
        if (!skip(Fcb != NULL, "ExAllocatePool failed\n"))
        {
            BOOLEAN PinAccess = (TestId != 4);

            RtlZeroMemory(Fcb, sizeof(TEST_FCB));
            ExInitializeFastMutex(&Fcb->HeaderMutex);
            FsRtlSetupAdvancedHeader(&Fcb->Header, &Fcb->HeaderMutex);

            TestFileObject->FsContext = Fcb;
            TestFileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;

            KmtStartSeh();
            if (TestId < 6)
            {
                CcInitializeCacheMap(TestFileObject, &FileSizes, PinAccess, &Callbacks, NULL);
            }
            else
            {
                CcInitializeCacheMap(TestFileObject, &SmallFileSizes, PinAccess, &Callbacks, NULL);
            }
            KmtEndSeh(STATUS_SUCCESS);

            if (!skip(CcIsFileCached(TestFileObject) == TRUE, "CcInitializeCacheMap failed\n"))
            {
                if (TestId < 3)
                {
                    Ret = FALSE;<--- Ret is assigned
                    Offset.QuadPart = TestId * 0x1000;
                    KmtStartSeh();
                    Ret = CcPinRead(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT, &Bcb, (PVOID *)&Buffer);<--- Ret is overwritten
                    KmtEndSeh(STATUS_SUCCESS);

                    if (!skip(Ret == TRUE, "CcPinRead failed\n"))
                    {
                        ok_bcb(Bcb, ((4 - TestId) * 4096), Offset.QuadPart);
                        ok_eq_ulong(Buffer[(0x3000 - TestId * 0x1000) / sizeof(ULONG)], 0xDEADBABE);

                        CcUnpinData(Bcb);
                    }
                }
                else if (TestId == 3)
                {
                    PTEST_CONTEXT TestContext;

                    TestContext = ExAllocatePool(NonPagedPool, sizeof(TEST_CONTEXT));
                    if (!skip(TestContext != NULL, "ExAllocatePool failed\n"))
                    {
                        Ret = FALSE;<--- Ret is assigned
                        Offset.QuadPart = 0x1000;

                        /* Try enforce BCB first */
                        KmtStartSeh();
                        Ret = CcPinRead(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT | PIN_IF_BCB, &Bcb, (PVOID *)&Buffer);<--- Ret is overwritten
                        KmtEndSeh(STATUS_SUCCESS);
                        ok(Ret == FALSE, "CcPinRead succeed\n");
                        if (Ret)
                        {
                            CcUnpinData(Bcb);
                        }

                        KmtStartSeh();
                        Ret = CcPinRead(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT, &TestContext->Bcb, &TestContext->Buffer);
                        KmtEndSeh(STATUS_SUCCESS);

                        if (!skip(Ret == TRUE, "CcPinRead failed\n"))
                        {
                            PKTHREAD ThreadHandle;

                            ok_bcb(TestContext->Bcb, 12288, Offset.QuadPart);

                            /* That's a bit rough but should do the job */
#ifdef _X86_
                            if (Memory >= 2 * 1024)
                            {
                                ok((TestContext->Buffer >= (PVOID)0xC1000000 && TestContext->Buffer < (PVOID)0xE0FFFFFF) ||
                                   (TestContext->Buffer >= (PVOID)0xA4000000 && TestContext->Buffer < (PVOID)0xBFFFFFFF),
                                   "Buffer %p not mapped in system space\n", TestContext->Buffer);
                            }
                            else if (TS)
                            {
                                ok(TestContext->Buffer >= (PVOID)0xC1000000 && TestContext->Buffer < (PVOID)0xDCFFFFFF,
                                   "Buffer %p not mapped in system space\n", TestContext->Buffer);
                            }
                            else
                            {
                                ok(TestContext->Buffer >= (PVOID)0xC1000000 && TestContext->Buffer < (PVOID)0xDBFFFFFF,
                                   "Buffer %p not mapped in system space\n", TestContext->Buffer);
                            }
#elif defined(_M_AMD64)
                            ok(TestContext->Buffer >= (PVOID)0xFFFFF98000000000 && TestContext->Buffer < (PVOID)0xFFFFFA8000000000,
                               "Buffer %p not mapped in system space\n", TestContext->Buffer);
#else
                            skip(FALSE, "System space mapping not defined\n");
#endif

                            TestContext->Length = FileSizes.FileSize.QuadPart - Offset.QuadPart;
                            ThreadHandle = KmtStartThread(PinInAnotherThread, TestContext);
                            KmtFinishThread(ThreadHandle, NULL);

                            TestContext->Length = FileSizes.FileSize.QuadPart - 2 * Offset.QuadPart;
                            ThreadHandle = KmtStartThread(PinInAnotherThread, TestContext);
                            KmtFinishThread(ThreadHandle, NULL);

                            CcUnpinData(TestContext->Bcb);
                        }

                        KmtStartSeh();
                        Ret = CcPinRead(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT | PIN_EXCLUSIVE, &TestContext->Bcb, &TestContext->Buffer);
                        KmtEndSeh(STATUS_SUCCESS);

                        if (!skip(Ret == TRUE, "CcPinRead failed\n"))
                        {
                            PKTHREAD ThreadHandle;

                            ok_bcb(TestContext->Bcb, 12288, Offset.QuadPart);

                            TestContext->Length = FileSizes.FileSize.QuadPart - Offset.QuadPart;
                            ThreadHandle = KmtStartThread(PinInAnotherThreadExclusive, TestContext);
                            KmtFinishThread(ThreadHandle, NULL);

                            CcUnpinData(TestContext->Bcb);
                        }

                        ExFreePool(TestContext);
                    }
                }
                else if (TestId == 4)
                {
                    Ret = FALSE;<--- Ret is assigned
                    Offset.QuadPart = 0x1000;
                    KmtStartSeh();
                    Ret = CcPinRead(TestFileObject, &Offset, FileSizes.FileSize.QuadPart - Offset.QuadPart, PIN_WAIT, &Bcb, (PVOID *)&Buffer);<--- Ret is overwritten
                    KmtEndSeh(STATUS_SUCCESS);

                    if (!skip(Ret == TRUE, "CcPinRead failed\n"))
                    {
                        ok_bcb(Bcb, 12288, Offset.QuadPart);
                        ok_eq_ulong(Buffer[0x2000 / sizeof(ULONG)], 0);

                        CcUnpinData(Bcb);
                    }
                }
                else if (TestId == 5)
                {
                    /* Pin after EOF */
                    Ret = FALSE;<--- Ret is assigned
                    Offset.QuadPart = FileSizes.FileSize.QuadPart + 0x1000;

                    KmtStartSeh();
                    Ret = CcPinRead(TestFileObject, &Offset, 0x1000, 0, &Bcb, (PVOID *)&Buffer);<--- Ret is overwritten
                    KmtEndSeh(STATUS_SUCCESS);
                    ok(Ret == FALSE, "CcPinRead succeed\n");

                    if (Ret)
                    {
                        CcUnpinData(Bcb);
                    }

                    /* Pin a VACB after EOF */
                    Ret = FALSE;<--- Ret is assigned
                    Offset.QuadPart = FileSizes.FileSize.QuadPart + 0x1000 + VACB_MAPPING_GRANULARITY;

                    KmtStartSeh();
                    Ret = CcPinRead(TestFileObject, &Offset, 0x1000, 0, &Bcb, (PVOID *)&Buffer);<--- Ret is overwritten
                    KmtEndSeh(STATUS_ACCESS_VIOLATION);
                    ok(Ret == FALSE, "CcPinRead succeed\n");

                    if (Ret)
                    {
                        CcUnpinData(Bcb);
                    }

                    /* Pin more than a VACB */
                    Ret = FALSE;<--- Ret is assigned
                    Offset.QuadPart = 0x0;

                    KmtStartSeh();
                    Ret = CcPinRead(TestFileObject, &Offset, 0x1000 + VACB_MAPPING_GRANULARITY, 0, &Bcb, (PVOID *)&Buffer);<--- Ret is overwritten
                    KmtEndSeh(STATUS_SUCCESS);
                    ok(Ret == FALSE, "CcPinRead succeed\n");

                    if (Ret)
                    {
                        CcUnpinData(Bcb);
                    }
                }
                else if (TestId == 6)
                {
                    Ret = FALSE;<--- Ret is assigned
                    Offset.QuadPart = 0;

                    KmtStartSeh();
                    Ret = CcPinRead(TestFileObject, &Offset, FileSizes.FileSize.QuadPart, PIN_WAIT, &Bcb, (PVOID *)&Buffer);<--- Ret is overwritten
                    KmtEndSeh(STATUS_SUCCESS);

                    if (!skip(Ret == TRUE, "CcPinRead failed\n"))
                    {
                        ok_bcb(Bcb, PAGE_SIZE * 4, Offset.QuadPart);
                        RtlFillMemory(Buffer, 0xbd, FileSizes.FileSize.LowPart);
                        CcSetDirtyPinnedData(Bcb, NULL);

                        CcUnpinData(Bcb);
                    }
                }
            }
        }
    }
}


static
VOID
CleanupTest(
    ULONG TestId,
    PDEVICE_OBJECT DeviceObject)
{
    LARGE_INTEGER Zero = RTL_CONSTANT_LARGE_INTEGER(0LL);
    CACHE_UNINITIALIZE_EVENT CacheUninitEvent;

    ok_eq_pointer(TestDeviceObject, DeviceObject);
    ok_eq_ulong(TestTestId, TestId);

    if (!skip(TestFileObject != NULL, "No test FO\n"))
    {
        if (CcIsFileCached(TestFileObject))
        {
            KeInitializeEvent(&CacheUninitEvent.Event, NotificationEvent, FALSE);
            CcUninitializeCacheMap(TestFileObject, &Zero, &CacheUninitEvent);
            KeWaitForSingleObject(&CacheUninitEvent.Event, Executive, KernelMode, FALSE, NULL);
        }

        if (TestFileObject->FsContext != NULL)
        {
            ExFreePool(TestFileObject->FsContext);
            TestFileObject->FsContext = NULL;
            TestFileObject->SectionObjectPointer = NULL;
        }

        if (TestTestId == 6)
        {
            ok_bool_true(TestWriteCalled, "Write was not called!\n");
        }
        else
        {
            ok_bool_false(TestWriteCalled, "Write was unexpectedly called\n");
        }

        ObDereferenceObject(TestFileObject);
    }

    TestFileObject = NULL;
    TestDeviceObject = NULL;
    TestTestId = -1;
    TestWriteCalled = FALSE;
}


static
NTSTATUS
TestMessageHandler(
    _In_ PDEVICE_OBJECT DeviceObject,
    _In_ ULONG ControlCode,
    _In_opt_ PVOID Buffer,
    _In_ SIZE_T InLength,
    _Inout_ PSIZE_T OutLength)
{
    NTSTATUS Status = STATUS_SUCCESS;

    FsRtlEnterFileSystem();

    switch (ControlCode)
    {
        case IOCTL_START_TEST:
            ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
            PerformTest(*(PULONG)Buffer, DeviceObject);
            break;
            
        case IOCTL_FINISH_TEST:
            ok_eq_ulong((ULONG)InLength, sizeof(ULONG));
            CleanupTest(*(PULONG)Buffer, DeviceObject);
            break;

        default:
            Status = STATUS_NOT_IMPLEMENTED;
            break;
    }

    FsRtlExitFileSystem();

    return Status;
}

static
NTSTATUS
TestIrpHandler(
    _In_ PDEVICE_OBJECT DeviceObject,
    _In_ PIRP Irp,
    _In_ PIO_STACK_LOCATION IoStack)
{
    NTSTATUS Status;

    PAGED_CODE();

    DPRINT("IRP %x/%x\n", IoStack->MajorFunction, IoStack->MinorFunction);
    ASSERT(IoStack->MajorFunction == IRP_MJ_READ ||
           IoStack->MajorFunction == IRP_MJ_WRITE);

    FsRtlEnterFileSystem();

    Status = STATUS_NOT_SUPPORTED;
    Irp->IoStatus.Information = 0;

    if (IoStack->MajorFunction == IRP_MJ_READ)
    {
        PMDL Mdl;
        ULONG Length;
        PVOID Buffer;
        LARGE_INTEGER Offset;

        Offset = IoStack->Parameters.Read.ByteOffset;
        Length = IoStack->Parameters.Read.Length;

        ok_eq_pointer(DeviceObject, TestDeviceObject);
        ok_eq_pointer(IoStack->FileObject, TestFileObject);

        ok(FlagOn(Irp->Flags, IRP_NOCACHE), "Not coming from Cc\n");

        ok_irql(APC_LEVEL);
        ok((Offset.QuadPart % PAGE_SIZE == 0 || Offset.QuadPart == 0), "Offset is not aligned: %I64i\n", Offset.QuadPart);
        ok(Length % PAGE_SIZE == 0, "Length is not aligned: %I64i\n", Length);

        ok(Irp->AssociatedIrp.SystemBuffer == NULL, "A SystemBuffer was allocated!\n");
        Buffer = MapAndLockUserBuffer(Irp, Length);
        ok(Buffer != NULL, "Null pointer!\n");
        RtlFillMemory(Buffer, Length, 0xBA);

        Status = STATUS_SUCCESS;
        if (Offset.QuadPart <= 0x3000 && Offset.QuadPart + Length > 0x3000)
        {
            *(PULONG)((ULONG_PTR)Buffer + (ULONG_PTR)(0x3000 - Offset.QuadPart)) = 0xDEADBABE;
        }

        Mdl = Irp->MdlAddress;
        ok(Mdl != NULL, "Null pointer for MDL!\n");
        ok((Mdl->MdlFlags & MDL_PAGES_LOCKED) != 0, "MDL not locked\n");
        ok((Mdl->MdlFlags & MDL_SOURCE_IS_NONPAGED_POOL) == 0, "MDL from non paged\n");
        ok((Mdl->MdlFlags & MDL_IO_PAGE_READ) != 0, "Non paging IO\n");
        ok((Irp->Flags & IRP_PAGING_IO) != 0, "Non paging IO\n");

        Irp->IoStatus.Information = Length;
    }
    else if (IoStack->MajorFunction == IRP_MJ_WRITE)
    {
        PMDL Mdl;
        ULONG Length;
        PVOID Buffer;
        LARGE_INTEGER Offset;

        Offset = IoStack->Parameters.Write.ByteOffset;
        Length = IoStack->Parameters.Write.Length;

        ok(TestTestId == 6, "Unexpected test id: %d\n", TestTestId);
        ok_eq_pointer(DeviceObject, TestDeviceObject);
        ok_eq_pointer(IoStack->FileObject, TestFileObject);

        ok(FlagOn(Irp->Flags, IRP_NOCACHE), "Not coming from Cc\n");

        ok_irql(PASSIVE_LEVEL);
        ok(Offset.QuadPart == 0, "Offset is not null: %I64i\n", Offset.QuadPart);
        ok(Length % PAGE_SIZE == 0, "Length is not aligned: %I64i\n", Length);
        ok(Length == PAGE_SIZE * 4, "Length is not MappedLength-sized: %I64i\n", Length);

        Buffer = MapAndLockUserBuffer(Irp, Length);
        ok(Buffer != NULL, "Null pointer!\n");

        Mdl = Irp->MdlAddress;
        ok(Mdl != NULL, "Null pointer for MDL!\n");
        ok((Mdl->MdlFlags & MDL_PAGES_LOCKED) != 0, "MDL not locked\n");
        ok((Mdl->MdlFlags & MDL_SOURCE_IS_NONPAGED_POOL) == 0, "MDL from non paged\n");
        ok((Irp->Flags & IRP_PAGING_IO) != 0, "Non paging IO\n");

        ok_bool_false(TestWriteCalled, "Write has been unexpectedly called twice!\n");
        TestWriteCalled = TRUE;

        Status = STATUS_SUCCESS;
        Irp->IoStatus.Information = Length;
    }

    if (Status == STATUS_PENDING)
    {
        IoMarkIrpPending(Irp);
        IoCompleteRequest(Irp, IO_NO_INCREMENT);
        Status = STATUS_PENDING;
    }
    else
    {
        Irp->IoStatus.Status = Status;
        IoCompleteRequest(Irp, IO_NO_INCREMENT);
    }

    FsRtlExitFileSystem();

    return Status;
}