/
fxrequestapi.cpp
3813 lines (2906 loc) · 101 KB
/
fxrequestapi.cpp
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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) Microsoft Corporation
Module Name:
FxRequestApi.cpp
Abstract:
This module implements FxRequest object
Author:
Environment:
Both kernel and user mode
Revision History:
--*/
#include "coreprivshared.hpp"
// Tracing support
extern "C" {
#include "FxRequestApi.tmh"
//
// Verifiers
//
// Do not supply Argument names
FX_DECLARE_VF_FUNCTION_P1(
NTSTATUS,
VerifyRequestComplete,
_In_ FxRequest*
);
// Do not supply Argument names
FX_DECLARE_VF_FUNCTION_P1(
NTSTATUS,
VerifyWdfRequestIsCanceled,
_In_ FxRequest*
);
//Do not supply argument names
FX_DECLARE_VF_FUNCTION_P1(
NTSTATUS,
VerifyWdfRequestForwardToIoQueue,
_In_ FxRequest*
);
//Do not supply argument names
FX_DECLARE_VF_FUNCTION_P1(
NTSTATUS,
VerifyWdfRequestForwardToParentDeviceIoQueue,
_In_ FxRequest*
);
_Must_inspect_result_
__drv_maxIRQL(DISPATCH_LEVEL)
NTSTATUS
WDFEXPORT(WdfRequestCreate)(
__in
PWDF_DRIVER_GLOBALS DriverGlobals,
__in_opt
PWDF_OBJECT_ATTRIBUTES RequestAttributes,
__in_opt
WDFIOTARGET Target,
__out
WDFREQUEST* Request
)
/*++
Routine Description:
Creates a WDFREQUEST handle that is suitable to be submitted to the provided
target
Arguments:
RequestAttributes - Attributes associated with the request. If NULL, the
size of the user context associated with the request will be
the default size specified in WdfDriverCreate.
Target - Target for which the request will be sent to. If NULL, then
WdfRequestChangeTarget must be called before the request is formatted
or sent to any target
Request - Pointer which will receive the newly created request
Return Value:
NT_SUCCESS if successful, otherwise appropriate error code
--*/
{
PFX_DRIVER_GLOBALS pFxDriverGlobals;
FxRequest* pRequest;
FxIoTarget* pTarget;
NTSTATUS status;
pFxDriverGlobals = GetFxDriverGlobals(DriverGlobals);
if (Target != NULL) {
FxObjectHandleGetPtrAndGlobals(pFxDriverGlobals,
Target,
FX_TYPE_IO_TARGET,
(PVOID*)&pTarget,
&pFxDriverGlobals);
}
else {
pTarget = NULL;
//
// For class extension support, get globals from parent object.
//
if (RequestAttributes != NULL &&
RequestAttributes->ParentObject != NULL) {
FxObjectHandleGetGlobals(
pFxDriverGlobals,
RequestAttributes->ParentObject,
&pFxDriverGlobals);
}
}
FxPointerNotNull(pFxDriverGlobals, Request);
*Request = NULL;
status = FxRequest::_Create(pFxDriverGlobals,
RequestAttributes,
NULL,
pTarget,
FxRequestOwnsIrp,
FxRequestConstructorCallerIsDriver,
&pRequest);
if (NT_SUCCESS(status)) {
*Request = pRequest->GetHandle();
#if FX_VERBOSE_TRACE
DoTraceLevelMessage(
pFxDriverGlobals, TRACE_LEVEL_VERBOSE, TRACINGREQUEST,
"Created WDFREQUEST %p, %!STATUS!",
*Request, status);
#endif // FX_VERBOSE_TRACE
}
return status;
}
_Must_inspect_result_
__drv_maxIRQL(DISPATCH_LEVEL)
NTSTATUS
WDFEXPORT(WdfRequestCreateFromIrp)(
__in
PWDF_DRIVER_GLOBALS DriverGlobals,
__in_opt
PWDF_OBJECT_ATTRIBUTES RequestAttributes,
__in
MdIrp Irp,
__in
BOOLEAN RequestFreesIrp,
__out
WDFREQUEST* Request
)
/*++
Routine Description:
Creates a request handle that uses an external IRP instead of an internally
allocated irp.
Arguments:
RequestAttributes - Attributes associated with the request. If NULL, the
size of the user context associated with the request will be
the default size specified in WdfDriverCreate.
Irp - The IRP to use
RequestFreesIrp - If TRUE, when the request handle is destroyed, it will
free the IRP with IoFreeIrp. If FALSE, it is the responsibility of the
calller to free the IRP
Request - Pointer which will receive the newly created request
Return Value:
NT_SUCCESS or appropriate error code
--*/
{
FxRequest* pRequest;
NTSTATUS status;
PFX_DRIVER_GLOBALS pFxDriverGlobals;
pFxDriverGlobals = GetFxDriverGlobals(DriverGlobals);
//
// For class extension support, get globals from parent object.
//
if (RequestAttributes != NULL &&
RequestAttributes->ParentObject != NULL) {
FxObjectHandleGetGlobals(
pFxDriverGlobals,
RequestAttributes->ParentObject,
&pFxDriverGlobals);
}
FxPointerNotNull(pFxDriverGlobals, Irp);
FxPointerNotNull(pFxDriverGlobals, Request);
*Request = NULL;
status = FxRequest::_Create(pFxDriverGlobals,
RequestAttributes,
Irp,
NULL,
RequestFreesIrp ? FxRequestOwnsIrp
: FxRequestDoesNotOwnIrp,
FxRequestConstructorCallerIsDriver,
&pRequest);
if (NT_SUCCESS(status)) {
*Request = pRequest->GetHandle();
#if FX_VERBOSE_TRACE
DoTraceLevelMessage(
pFxDriverGlobals, TRACE_LEVEL_VERBOSE, TRACINGREQUEST,
"Irp %p RequestFreesIrp %d WDFREQUEST %p created",
Irp, RequestFreesIrp, *Request);
#endif // FX_VERBOSE_TRACE
}
return status;
}
#define WDF_REQUEST_REUSE_VALID_FLAGS_V1_9 \
(WDF_REQUEST_REUSE_SET_NEW_IRP)
#define WDF_REQUEST_REUSE_VALID_FLAGS \
(WDF_REQUEST_REUSE_SET_NEW_IRP | WDF_REQUEST_REUSE_MUST_COMPLETE)
__drv_maxIRQL(DISPATCH_LEVEL)
NTSTATUS
WDFEXPORT(WdfRequestReuse)(
__in
PWDF_DRIVER_GLOBALS DriverGlobals,
__in
WDFREQUEST Request,
__in
PWDF_REQUEST_REUSE_PARAMS ReuseParams
)
/*++
Routine Description:
Clears out the internal state of the irp, which includes, but is not limited
to:
a) Any internal allocations for the previously formatted request
b) The completion routine and its context
c) The request's intended i/o target
d) All of the internal IRP's stack locations
Arguments:
Request - The request to be reused.
ReuseParams - Parameters controlling the reuse of the request, see comments
for each field in the structure for usage
Return Value:
NT_SUCCESS or appropriate error code
--*/
{
PFX_DRIVER_GLOBALS pFxDriverGlobals;
FxRequest *pRequest;
ULONG validFlags;
FxObjectHandleGetPtrAndGlobals(GetFxDriverGlobals(DriverGlobals),
Request,
FX_TYPE_REQUEST,
(PVOID*)&pRequest,
&pFxDriverGlobals);
FxPointerNotNull(pFxDriverGlobals, ReuseParams);
if (ReuseParams->Size != sizeof(WDF_REQUEST_REUSE_PARAMS)) {
DoTraceLevelMessage(pFxDriverGlobals, TRACE_LEVEL_ERROR, TRACINGREQUEST,
"ReuseParams Size %d, expected %d %!STATUS!",
ReuseParams->Size, sizeof(WDF_REQUEST_REUSE_PARAMS),
STATUS_INVALID_PARAMETER);
return STATUS_INVALID_PARAMETER;
}
if (pFxDriverGlobals->IsVersionGreaterThanOrEqualTo(1,11)) {
validFlags = WDF_REQUEST_REUSE_VALID_FLAGS;
}
else {
validFlags = WDF_REQUEST_REUSE_VALID_FLAGS_V1_9;
}
if (ReuseParams->Flags & ~validFlags) {
DoTraceLevelMessage(pFxDriverGlobals, TRACE_LEVEL_ERROR, TRACINGREQUEST,
"ReuseParams Flags 0x%x, valid mask 0x%x, %!STATUS!",
ReuseParams->Flags,
(ULONG) ~validFlags,
STATUS_INVALID_PARAMETER);
return STATUS_INVALID_PARAMETER;
}
return pRequest->Reuse(ReuseParams);
}
_Must_inspect_result_
__drv_maxIRQL(DISPATCH_LEVEL)
NTSTATUS
WDFEXPORT(WdfRequestChangeTarget)(
__in
PWDF_DRIVER_GLOBALS DriverGlobals,
__in
WDFREQUEST Request,
__in
WDFIOTARGET IoTarget
)
{
PFX_DRIVER_GLOBALS pFxDriverGlobals;
FxRequest* pRequest;
FxIoTarget* pTarget;
FxObjectHandleGetPtrAndGlobals(GetFxDriverGlobals(DriverGlobals),
Request,
FX_TYPE_REQUEST,
(PVOID*)&pRequest,
&pFxDriverGlobals);
FxObjectHandleGetPtr(pFxDriverGlobals,
IoTarget,
FX_TYPE_IO_TARGET,
(PVOID*)&pTarget);
return pRequest->ValidateTarget(pTarget);
}
_Must_inspect_result_
NTSTATUS
FX_VF_FUNCTION(VerifyRequestComplete) (
_In_ PFX_DRIVER_GLOBALS FxDriverGlobals,
_In_ FxRequest* pRequest
)
{
NTSTATUS status;
KIRQL irql;
PAGED_CODE_LOCKED();
pRequest->Lock(&irql);
status = pRequest->VerifyRequestIsDriverOwned(FxDriverGlobals);
if (NT_SUCCESS(status)) {
status = pRequest->VerifyRequestCanBeCompleted(FxDriverGlobals);
}
pRequest->Unlock(irql);
return status;
}
__drv_maxIRQL(DISPATCH_LEVEL)
VOID
WDFAPI
WDFEXPORT(WdfRequestComplete)(
__in
PWDF_DRIVER_GLOBALS DriverGlobals,
__in
WDFREQUEST Request,
__in
NTSTATUS RequestStatus
)
/*++
Routine Description:
Complete the request with supplied status.
Any default reference counts implied by handle are invalid after
completion.
Arguments:
Request - Handle to the Request object
RequestStatus - Wdm Status to complete the request with
Returns:
None
--*/
{
NTSTATUS status;
FxRequest *pRequest;
//
// Validate the request handle, and get the FxRequest*
//
FxObjectHandleGetPtr(GetFxDriverGlobals(DriverGlobals),
Request,
FX_TYPE_REQUEST,
(PVOID*)&pRequest);
#if FX_VERBOSE_TRACE
//
// Use object's globals, not the caller's
//
DoTraceLevelMessage(pRequest->GetDriverGlobals(),
TRACE_LEVEL_VERBOSE, TRACINGREQUEST,
"Completing WDFREQUEST 0x%p, %!STATUS!",
Request, RequestStatus);
#endif
status = VerifyRequestComplete(pRequest->GetDriverGlobals(), pRequest );
if (!NT_SUCCESS(status)) {
return;
}
pRequest->Complete(RequestStatus);
}
__drv_maxIRQL(DISPATCH_LEVEL)
VOID
WDFAPI
WDFEXPORT(WdfRequestCompleteWithPriorityBoost)(
__in
PWDF_DRIVER_GLOBALS DriverGlobals,
__in
WDFREQUEST Request,
__in
NTSTATUS RequestStatus,
__in
CCHAR PriorityBoost
)
/*++
Routine Description:
Complete the request with supplied status.
Any default reference counts implied by handle are invalid after
completion.
Arguments:
Request - Handle to the Request object
RequestStatus - Wdm Status to complete the request with
PriorityBoost - A system-defined constant value by which to increment the
run-time priority of the original thread that requested the operation.
Returns:
None
--*/
{
NTSTATUS status;
FxRequest *pRequest;
//
// Validate the request handle, and get the FxRequest*
//
FxObjectHandleGetPtr(GetFxDriverGlobals(DriverGlobals),
Request,
FX_TYPE_REQUEST,
(PVOID*)&pRequest);
#if FX_VERBOSE_TRACE
//
// Use the object's globals, not the caller's
//
DoTraceLevelMessage(pRequest->GetDriverGlobals(),
TRACE_LEVEL_VERBOSE, TRACINGREQUEST,
"Completing WDFREQUEST 0x%p, %!STATUS!",
Request, RequestStatus);
#endif
status = VerifyRequestComplete(pRequest->GetDriverGlobals(),
pRequest);
if (!NT_SUCCESS(status)) {
return;
}
pRequest->CompleteWithPriority(RequestStatus, PriorityBoost);
}
__drv_maxIRQL(DISPATCH_LEVEL)
VOID
WDFAPI
WDFEXPORT(WdfRequestCompleteWithInformation)(
__in
PWDF_DRIVER_GLOBALS DriverGlobals,
__in
WDFREQUEST Request,
__in
NTSTATUS RequestStatus,
__in
ULONG_PTR Information
)
/*++
Routine Description:
Complete the request with supplied status and information.
Any default reference counts implied by handle are invalid after
completion.
Arguments:
Request - Handle to the Request object
RequestStatus - Wdm Status to complete the request with
Information - Information to complete request with
Returns:
None
--*/
{
FxRequest *pRequest;
NTSTATUS status;
//
// Validate the request handle, and get the FxRequest*
//
FxObjectHandleGetPtr(GetFxDriverGlobals(DriverGlobals),
Request,
FX_TYPE_REQUEST,
(PVOID*)&pRequest);
#if FX_VERBOSE_TRACE
//
// Use the object's globals, not the caller's
//
DoTraceLevelMessage(pRequest->GetDriverGlobals(),
TRACE_LEVEL_VERBOSE, TRACINGREQUEST,
"Completing WDFREQUEST 0x%p, %!STATUS!",
Request, RequestStatus);
#endif
status = VerifyRequestComplete(pRequest->GetDriverGlobals(), pRequest);
if (!NT_SUCCESS(status)) {
return;
}
pRequest->CompleteWithInformation(RequestStatus, Information);
}
__drv_maxIRQL(DISPATCH_LEVEL)
VOID
WDFAPI
WDFEXPORT(WdfRequestSetInformation)(
__in
PWDF_DRIVER_GLOBALS DriverGlobals,
__in
WDFREQUEST Request,
__in
ULONG_PTR Information
)
/*++
Routine Description:
Set the transfer information for the request.
This sets the NT Irp->Status.Information field.
Arguments:
Request - Handle to the Request object
Information - Value to be set
Returns:
None
--*/
{
FxRequest *pRequest;
//
// Validate the request handle, and get the FxRequest*
//
FxObjectHandleGetPtr(GetFxDriverGlobals(DriverGlobals),
Request,
FX_TYPE_REQUEST,
(PVOID*)&pRequest);
#if FX_VERBOSE_TRACE
//
// Use the object's globals, not the caller's
//
DoTraceLevelMessage(pRequest->GetDriverGlobals(),
TRACE_LEVEL_VERBOSE, TRACINGREQUEST,
"Enter: WDFREQUEST 0x%p, Information 0x%p",
Request, (VOID*)Information);
#endif // FX_VERBOSE_TRACE
pRequest->SetInformation(Information);
}
__drv_maxIRQL(DISPATCH_LEVEL)
ULONG_PTR
WDFAPI
WDFEXPORT(WdfRequestGetInformation)(
__in
PWDF_DRIVER_GLOBALS DriverGlobals,
__in
WDFREQUEST Request
)
/*++
Routine Description:
Get the transfer information for the reuqest.
Arguments:
Request - Handle to the Request object
Returns:
Returns Irp->IoStatus.Information value.
--*/
{
FxRequest *pRequest;
//
// Validate the request handle, and get the FxRequest*
//
FxObjectHandleGetPtr(GetFxDriverGlobals(DriverGlobals),
Request,
FX_TYPE_REQUEST,
(PVOID*)&pRequest);
return pRequest->GetInformation();
}
_Must_inspect_result_
__drv_maxIRQL(DISPATCH_LEVEL)
NTSTATUS
WDFAPI
WDFEXPORT(WdfRequestRetrieveInputMemory)(
__in
PWDF_DRIVER_GLOBALS DriverGlobals,
__in
WDFREQUEST Request,
__out
WDFMEMORY *Memory
)
/*++
Routine Description:
Return the WDFMEMORY buffer associated with the request.
The memory buffer is valid in any thread/process context,
and may be accessed at IRQL > PASSIVE_LEVEL.
The memory buffer is automatically released when the request
is completed.
The memory buffers access permissions are validated according
to the command type (IRP_MJ_READ, IRP_MJ_WRITE), and may
only be accessed according to the access semantics of the request.
The memory buffer is not valid for a METHOD_NEITHER IRP_MJ_DEVICE_CONTROL,
or if neither of the DO_BUFFERED_IO or DO_DIRECT_IO flags are
configured for the device object.
The Memory buffer is as follows for each buffering mode:
DO_BUFFERED_IO:
Irp->AssociatedIrp.SystemBuffer
DO_DIRECT_IO:
MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority)
NEITHER flag set:
NULL. Must use WdfDeviceInitSetIoInCallerContextCallback in order
to access the request in the calling threads address space before
it is placed into any I/O Queues.
The buffer is only valid until the request is completed.
Arguments:
Request - Handle to the Request object
Memory - Pointer location to return WDFMEMORY handle
Returns:
NTSTATUS
--*/
{
PFX_DRIVER_GLOBALS pFxDriverGlobals;
NTSTATUS status;
FxRequest *pRequest;
IFxMemory* pMemory;
PVOID pBuffer;
size_t length;
pMemory = NULL;
//
// Validate the request handle, and get the FxRequest*
//
FxObjectHandleGetPtrAndGlobals(GetFxDriverGlobals(DriverGlobals),
Request,
FX_TYPE_REQUEST,
(PVOID*)&pRequest,
&pFxDriverGlobals);
FxPointerNotNull(pFxDriverGlobals, Memory);
#if FX_VERBOSE_TRACE
DoTraceLevelMessage(pFxDriverGlobals, TRACE_LEVEL_VERBOSE, TRACINGREQUEST,
"Enter: WDFREQUEST 0x%p", Request);
#endif // FX_VERBOSE_TRACE
//
// This call is not valid on Read request.
//
if (pRequest->GetFxIrp()->GetMajorFunction() == IRP_MJ_READ) {
status = STATUS_INVALID_DEVICE_REQUEST;
DoTraceLevelMessage(
pFxDriverGlobals, TRACE_LEVEL_VERBOSE, TRACINGREQUEST,
"This call is not valid on the Read request, you should call"
" WdfRequestRetrieveOutputMemory to get the Memory for WDFREQUEST "
"0x%p, %!STATUS!", Request, status);
return status;
}
status = pRequest->GetMemoryObject(&pMemory, &pBuffer, &length);
if (NT_SUCCESS(status)) {
*Memory = pMemory->GetHandle();
}
return status;
}
_Must_inspect_result_
__drv_maxIRQL(DISPATCH_LEVEL)
NTSTATUS
WDFAPI
WDFEXPORT(WdfRequestRetrieveOutputMemory)(
__in
PWDF_DRIVER_GLOBALS DriverGlobals,
__in
WDFREQUEST Request,
__out
WDFMEMORY *Memory
)
/*++
Routine Description:
Return the WDFMEMORY buffer associated with the request.
The memory buffer is valid in any thread/process context,
and may be accessed at IRQL > PASSIVE_LEVEL.
The memory buffer is automatically released when the request
is completed.
The memory buffers access permissions are validated according
to the command type (IRP_MJ_READ, IRP_MJ_WRITE), and may
only be accessed according to the access semantics of the request.
The memory buffer is not valid for a METHOD_NEITHER IRP_MJ_DEVICE_CONTROL,
or if neither of the DO_BUFFERED_IO or DO_DIRECT_IO flags are
configured for the device object.
The Memory buffer is as follows for each buffering mode:
DO_BUFFERED_IO:
Irp->AssociatedIrp.SystemBuffer
DO_DIRECT_IO:
MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority)
NEITHER flag set:
NULL. Must use WdfDeviceInitSetIoInCallerContextCallback in order
to access the request in the calling threads address space before
it is placed into any I/O Queues.
The buffer is only valid until the request is completed.
Arguments:
Request - Handle to the Request object
Memory - Pointer location to return WDFMEMORY handle
Returns:
NTSTATUS
--*/
{
PFX_DRIVER_GLOBALS pFxDriverGlobals;
NTSTATUS status;
FxRequest *pRequest;
IFxMemory* pMemory;
PVOID pBuffer;
size_t length;
UCHAR majorFunction;
pMemory = NULL;
//
// Validate the request handle, and get the FxRequest*
//
FxObjectHandleGetPtrAndGlobals(GetFxDriverGlobals(DriverGlobals),
Request,
FX_TYPE_REQUEST,
(PVOID*)&pRequest,
&pFxDriverGlobals);
FxPointerNotNull(pFxDriverGlobals, Memory);
#if FX_VERBOSE_TRACE
DoTraceLevelMessage(pFxDriverGlobals, TRACE_LEVEL_VERBOSE, TRACINGREQUEST,
"Enter: WDFREQUEST 0x%p", Request);
#endif // FX_VERBOSE_TRACE
//
// This call is not valid on Write request.
//
majorFunction = pRequest->GetFxIrp()->GetMajorFunction();
if (majorFunction == IRP_MJ_WRITE) {
status = STATUS_INVALID_DEVICE_REQUEST;
DoTraceLevelMessage(
pFxDriverGlobals, TRACE_LEVEL_VERBOSE, TRACINGREQUEST,
"This call is not valid on the Write request, you should call"
" WdfRequestRetrieveInputMemory to get the Memory for WDFREQUEST "
"0x%p, %!STATUS!", Request, status);
return status;
}
if( (majorFunction == IRP_MJ_DEVICE_CONTROL) ||
(majorFunction == IRP_MJ_INTERNAL_DEVICE_CONTROL) ) {
status = pRequest->GetDeviceControlOutputMemoryObject(&pMemory, &pBuffer, &length);
}
else {
status = pRequest->GetMemoryObject(&pMemory, &pBuffer, &length);
}
if (NT_SUCCESS(status)) {
*Memory = pMemory->GetHandle();
}
return status;
}
_Must_inspect_result_
__drv_maxIRQL(DISPATCH_LEVEL)
NTSTATUS
WDFAPI
WDFEXPORT(WdfRequestRetrieveInputBuffer)(
__in
PWDF_DRIVER_GLOBALS DriverGlobals,
__in
WDFREQUEST Request,
__in
size_t RequiredLength,
__deref_out_bcount(*Length)
PVOID* Buffer,
__out_opt
size_t* Length
)
/*++
Routine Description:
Return the memory buffer associated with the request along
with its maximum length.
The memory buffer is valid in any thread/process context,
and may be accessed at IRQL > PASSIVE_LEVEL.
The memory buffer is automatically released when the request
is completed.
The memory buffers access permissions are validated according
to the command type (IRP_MJ_READ, IRP_MJ_WRITE), and may
only be accessed according to the access semantics of the request.
The memory buffer is not valid for a METHOD_NEITHER IRP_MJ_DEVICE_CONTROL,
or if neither of the DO_BUFFERED_IO or DO_DIRECT_IO flags are
configured for the device object.
The Memory buffer is as follows for each buffering mode:
DO_BUFFERED_IO:
Irp->AssociatedIrp.SystemBuffer
DO_DIRECT_IO:
MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority)
NEITHER flag set:
NULL. Must use WdfDeviceInitSetIoInCallerContextCallback in order
to access the request in the calling threads address space before
it is placed into any I/O Queues.
The buffer is only valid until the request is completed.
Arguments:
Request - Handle to the Request object
RequiredLength - This is the minimum size expected by the caller
Buffer - Pointer location to return buffer ptr
Length - actual size of the buffer. This is >= to RequiredLength
Returns:
NTSTATUS
--*/
{
PFX_DRIVER_GLOBALS pFxDriverGlobals;
NTSTATUS status;
FxRequest *pRequest;
IFxMemory* pMemory;
PVOID pBuffer;
size_t length;
pMemory = NULL;
//
// Validate the request handle, and get the FxRequest*
//
FxObjectHandleGetPtrAndGlobals(GetFxDriverGlobals(DriverGlobals),
Request,
FX_TYPE_REQUEST,
(PVOID*)&pRequest,
&pFxDriverGlobals);
//
// Validate the pointers and set its content to NULL
//
FxPointerNotNull(pFxDriverGlobals, Buffer);
*Buffer = NULL;
if (Length != NULL) {