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
|
/*++
Copyright (c) Microsoft Corporation
Module Name:
sampletest.cpp
Abstract:
This file demonstrates I/O to TrEE secure services.
Environment:
User mode
--*/
#pragma warning(disable:4127)
#include <initguid.h>
#include <ntstatus.h>
#define WIN32_NO_STATUS
#include <Windows.h>
#include <winioctl.h>
#include <cfgmgr32.h>
#include <stdio.h>
#include <stdlib.h>
typedef LONG NTSTATUS;
#include <TrustedRT.h>
#include <SampleSecureService.h>
#include <SampleOSService.h>
#define TEST_ASSERT(_Assertion, _Message, ...) do { \
if (!(_Assertion)) { \
wprintf(_Message L"\n", __VA_ARGS__); \
Success = FALSE; \
goto End; \
}\
} while (0)
#define TEST_COMMENT(_Message, ...) wprintf(_Message L"\n", __VA_ARGS__)
#define TEST_STRING L"This is a test string.\n"
HANDLE
OpenServiceHandleByInterface(
_In_ LPCGUID ServiceGuid
)
/*++
Routine Description:
This routine opens a handle to the service device specified the GUID using
PnP device interface. The device interface gives a symbolic link to the
service device that can be fed to CreateFile function.
Arguments:
ServiceGuid - Supplies the GUID of the service to open.
Return Value:
HANDLE to the secure service, NULL if error occurred.
--*/
{
CONFIGRET ConfigRet;
WCHAR InterfaceSymlink[1024];
HANDLE ServiceHandle;
BOOL Success;
ServiceHandle = INVALID_HANDLE_VALUE;
ConfigRet = CM_Get_Device_Interface_ListW(
const_cast<LPGUID>(ServiceGuid),
NULL,
InterfaceSymlink,
_countof(InterfaceSymlink),
CM_GET_DEVICE_INTERFACE_LIST_PRESENT
);
TEST_ASSERT(ConfigRet == CR_SUCCESS, L"GetDeviceInterface failed, configret=%d", ConfigRet);
TEST_COMMENT(L"Symlink=%ws", InterfaceSymlink);
ServiceHandle = CreateFileW(
InterfaceSymlink,
FILE_READ_DATA | FILE_WRITE_DATA,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
TEST_ASSERT(ServiceHandle != INVALID_HANDLE_VALUE, L"Service open failed");
End:
return ServiceHandle;
}
HANDLE
OpenServiceHandleByFilename(
_In_ LPCGUID ServiceGuid
)
/*++
Routine Description:
This routine opens a handle to the service device specified the GUID using
TrEE-namespace filename. The filename has format \\.\WindowsTrustedRT\{GUID}.
The filename is parsed in IRP_MJ_CREATE handler in TrEE class extension.
Then request is forwarded to corresponding service device.
Arguments:
ServiceGuid - Supplies the GUID of the service to open.
Return Value:
HANDLE to the secure service, NULL if error occurred.
--*/
{
WCHAR InterfaceGuid[1024];
WCHAR InterfaceSymlink[1024];
HANDLE ServiceHandle;
BOOL Success;
ServiceHandle = INVALID_HANDLE_VALUE;
swprintf_s(InterfaceGuid,
L"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
ServiceGuid->Data1,
ServiceGuid->Data2,
ServiceGuid->Data3,
ServiceGuid->Data4[0],
ServiceGuid->Data4[1],
ServiceGuid->Data4[2],
ServiceGuid->Data4[3],
ServiceGuid->Data4[4],
ServiceGuid->Data4[5],
ServiceGuid->Data4[6],
ServiceGuid->Data4[7]
);
swprintf_s(InterfaceSymlink, L"\\\\.\\WindowsTrustedRT\\%ws", InterfaceGuid);
TEST_COMMENT(L"Guid=%ws", InterfaceGuid);
TEST_COMMENT(L"Symlink=%ws", InterfaceSymlink);
ServiceHandle = CreateFileW(
InterfaceSymlink,
FILE_READ_DATA | FILE_WRITE_DATA,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
TEST_ASSERT(ServiceHandle != INVALID_HANDLE_VALUE, L"Service open failed");
End:
return ServiceHandle;
}
BOOL
CallTrEEService(
_In_ HANDLE ServiceHandle,
_In_ ULONG FunctionCode,
_In_reads_bytes_(InputBufferLength) PVOID InputBuffer,
_In_ ULONG InputBufferLength,
_Out_writes_bytes_to_(OutputBufferLength, *BytesWritten) PVOID OutputBuffer,
_In_ ULONG OutputBufferLength,
_Out_ PULONG BytesWritten
)
/*++
Routine Description:
This routine is a wrapper for DeviceIoControl to conveniently send TrEE
secure function requests. Input and output buffers are packed to TrEE
request structs and number of bytes written is unpacked from response
struct.
Arguments:
ServiceHandle - Supplies a handle to the secure service on which the
request is to be sent.
FunctionCode - Supplies the function code of the request.
InputBuffer - Supplies a pointer to the input buffer that contains the data
required to perform the operation
InputBufferLength - Supplies the size of the input buffer, in bytes.
OutputBuffer - Supplies a pointer to the output buffer that is to receive
the data returned by the operation.
OutputBufferLength - Supplies the size of the output buffer, in bytes.
BytesWritten - Supplies a pointer to a variable that receives the size of
the data stored in the output buffer, in bytes.
Return Value:
TRUE if successful.
--*/
{
TR_SERVICE_REQUEST Request;
TR_SERVICE_REQUEST_RESPONSE Response;
BOOL Success;
ULONG ResponseBytesWritten;
Request.FunctionCode = FunctionCode;
Request.InputBuffer = InputBuffer;
Request.InputBufferSize = InputBufferLength;
Request.OutputBuffer = OutputBuffer;
Request.OutputBufferSize = OutputBufferLength;
Success = DeviceIoControl(ServiceHandle,
IOCTL_TR_EXECUTE_FUNCTION,
&Request,
sizeof(Request),
&Response,
sizeof(Response),
&ResponseBytesWritten,
NULL);
if (ResponseBytesWritten >= sizeof(Response)) {
*BytesWritten = (ULONG)Response.BytesWritten;
return Success;
} else {
*BytesWritten = 0;
return FALSE;
}
}
typedef struct _ASYNC_TREE_CALL {
OVERLAPPED Overlapped;
TR_SERVICE_REQUEST Request;
TR_SERVICE_REQUEST_RESPONSE Response;
} ASYNC_TREE_CALL, *PASYNC_TREE_CALL;
BOOL
CallTrEEServiceEx(
_In_ HANDLE ServiceHandle,
_In_ ULONG FunctionCode,
_In_reads_bytes_(InputBufferLength) PVOID InputBuffer,
_In_ ULONG InputBufferLength,
_Out_writes_bytes_to_(OutputBufferLength, *BytesWritten) PVOID OutputBuffer,
_In_ ULONG OutputBufferLength,
_Out_ PULONG BytesWritten,
_Deref_out_ PASYNC_TREE_CALL* pAsyncContext
)
/*++
Routine Description:
This routine is a wrapper for DeviceIoControl similar to CallTrEEService,
except that it performs asynchronous I/O.
Arguments:
ServiceHandle - Supplies a handle to the secure service on which the
request is to be sent. The handle must have been opened
with FILE_FLAG_OVERLAPPED flag.
FunctionCode - Supplies the function code of the request.
InputBuffer - Supplies a pointer to the input buffer that contains the data
required to perform the operation
InputBufferLength - Supplies the size of the input buffer, in bytes.
OutputBuffer - Supplies a pointer to the output buffer that is to receive
the data returned by the operation.
OutputBufferLength - Supplies the size of the output buffer, in bytes.
BytesWritten - Supplies a pointer to a variable that receives the size of
the data stored in the output buffer, in bytes.
pAsyncContext - Supplies a pointer to a variable that receives the pointer
to ASYNC_TREE_CALL which contains necessary input and
output buffer for the request and an OVERLAPPED struct to
track the progress of the request.
Return Value:
TRUE if the operation was completed synchronously and successfully.
FALSE if the operation is undergoing asynchronously or has failed. Check
the value of GetLastError to distinguish the case.
--*/
{
PASYNC_TREE_CALL AsyncContext;
BOOL Success;
ULONG ResponseBytesWritten;
AsyncContext = new ASYNC_TREE_CALL;
memset(AsyncContext, 0, sizeof(ASYNC_TREE_CALL));
AsyncContext->Request.FunctionCode = FunctionCode;
AsyncContext->Request.InputBuffer = InputBuffer;
AsyncContext->Request.InputBufferSize = InputBufferLength;
AsyncContext->Request.OutputBuffer = OutputBuffer;
AsyncContext->Request.OutputBufferSize = OutputBufferLength;
AsyncContext->Overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
*pAsyncContext = AsyncContext;
Success = DeviceIoControl(ServiceHandle,
IOCTL_TR_EXECUTE_FUNCTION,
&AsyncContext->Request,
sizeof(TR_SERVICE_REQUEST),
&AsyncContext->Response,
sizeof(TR_SERVICE_REQUEST_RESPONSE),
&ResponseBytesWritten,
&AsyncContext->Overlapped);
if (ResponseBytesWritten >= sizeof(TR_SERVICE_REQUEST_RESPONSE)) {
*BytesWritten = (ULONG)AsyncContext->Response.BytesWritten;
return Success;
} else {
*BytesWritten = 0;
return FALSE;
}
}
VOID
CleanupAsyncTrEECallContext(
_In_ PASYNC_TREE_CALL AsyncContext
)
/*++
Routine Description:
This routine cleans up the memory and resources used by ASYNC_TREE_CALL
struct returned from CallTrEEServiceEx function.
Arguments:
AsyncContext - Supplies a pointer to ASYNC_TREE_CALL struct returned from
CallTrEEServiceEx function.
Return Value:
None.
--*/
{
CloseHandle(AsyncContext->Overlapped.hEvent);
delete AsyncContext;
}
int
__cdecl
wmain(
int argc,
const wchar_t** argv
)
{
DWORD BytesWritten;
HANDLE TestServiceHandle;
HANDLE Test2ServiceHandle;
HANDLE MasterDeviceHandle;
BOOL Success;
ULONG64 TestFlag;
Success = TRUE;
TestServiceHandle = INVALID_HANDLE_VALUE;
Test2ServiceHandle = INVALID_HANDLE_VALUE;
MasterDeviceHandle = INVALID_HANDLE_VALUE;
if (argc > 1) {
swscanf_s(argv[1], L"%I64x", &TestFlag);
} else {
TestFlag = ~0UI64;
}
//
// Open the service using TrEE-namespace filename.
//
TestServiceHandle = OpenServiceHandleByFilename(&GUID_SAMPLE_TEST_SERVICE);
TEST_ASSERT(TestServiceHandle != INVALID_HANDLE_VALUE, L"Test service open failed");
if ((TestFlag & 0x1) != 0) {
TEST_COMMENT(L"\n::::: TEST_SERVICE_HELLO_WORLD");
wchar_t* HelloWorld;
//
// First call the service with no output buffer to detect how large
// output buffer should be.
//
Success = CallTrEEService(TestServiceHandle,
TEST_SERVICE_HELLO_WORLD,
NULL, 0,
NULL, 0,
&BytesWritten);
TEST_ASSERT(!Success, L"CallTrEEService (get size) unexpectedly succeeded");
TEST_ASSERT(BytesWritten > 0, L"CallTrEEService (get size) returned 0 bytes required size");
//
// The request fails, but BytesWritten variable will contain the
// required size of output buffer.
//
HelloWorld = (wchar_t*)malloc(BytesWritten);
Success = CallTrEEService(TestServiceHandle,
TEST_SERVICE_HELLO_WORLD,
NULL, 0,
HelloWorld, BytesWritten,
&BytesWritten);
TEST_ASSERT(Success, L"CallTrEEService (read data) failed (err=%d)", GetLastError());
TEST_ASSERT(BytesWritten > 0, L"CallTrEEService (read data) returned 0 bytes written");
//
// The service will return classic "Hello, world!" message.
//
TEST_COMMENT(L"TEST_SERVICE_HELLO_WORLD returned: %ws", HelloWorld);
}
if ((TestFlag & 0x2) != 0) {
TEST_COMMENT(L"\n::::: TEST_SERVICE_GET_INTERRUPT_TIME");
ULONG64 InterruptTime;
Success = CallTrEEService(TestServiceHandle,
TEST_SERVICE_GET_INTERRUPT_TIME,
NULL, 0,
&InterruptTime, sizeof(InterruptTime),
&BytesWritten);
TEST_ASSERT(Success, L"CallTrEEService (read data) failed (err=%d)", GetLastError());
TEST_ASSERT(BytesWritten == sizeof(ULONG64),
L"CallTrEEService (read data) returned unexpected number of bytes (BytesWritten=%d)",
BytesWritten);
TEST_COMMENT(L"TEST_SERVICE_GET_INTERRUPT_TIME returned: %I64x", InterruptTime);
}
if ((TestFlag & 0x4) != 0) {
TEST_COMMENT(L"\n::::: TEST_SERVICE_KERNEL_ONLY");
DWORD Error;
//
// This call is available only from kernel mode. The service will fail
// the request if it came from usermode.
//
Success = CallTrEEService(TestServiceHandle,
TEST_SERVICE_KERNEL_ONLY,
NULL, 0,
NULL, 0,
&BytesWritten);
Error = GetLastError();
TEST_ASSERT(!Success, L"CallTrEEService unexpectedly succeeded");
TEST_COMMENT(L"TEST_SERVICE_KERNEL_ONLY failed with err=%d", Error);
}
{
DWORD Error;
ULONG Input;
ULONG Output;
PASYNC_TREE_CALL AsyncContext;
if ((TestFlag & 0x8) != 0) {
TEST_COMMENT(L"\n::::: TEST_SERVICE_DELAYED_COMPLETION(1500ms, synchronous wait)");
Input = 1500;
Output = 0;
Success = CallTrEEService(TestServiceHandle,
TEST_SERVICE_DELAYED_COMPLETION,
&Input, sizeof(Input),
&Output, sizeof(Output),
&BytesWritten);
TEST_ASSERT(Success, L"CallTrEEService failed (err=%d)", GetLastError());
TEST_ASSERT(BytesWritten == sizeof(Output),
L"CallTrEEService returned unexpected number of bytes (BytesWritten=%d)",
BytesWritten);
TEST_COMMENT(L"Output buffer contains: %08x", Output);
}
if ((TestFlag & 0x10) != 0) {
TEST_COMMENT(L"\n::::: TEST_SERVICE_DELAYED_COMPLETION(5000ms, asynchronous wait)");
Input = 5000;
Output = 0;
//
// CallTrEEServiceEx is called to demonstrate asynchronous operation.
//
Success = CallTrEEServiceEx(TestServiceHandle,
TEST_SERVICE_DELAYED_COMPLETION,
&Input, sizeof(Input),
&Output, sizeof(Output),
&BytesWritten,
&AsyncContext);
Error = GetLastError();
TEST_ASSERT(!Success, L"CallTrEEServiceEx unexpectedly succeeded");
TEST_ASSERT(GetLastError() == ERROR_IO_PENDING,
L"CallTrEEServiceEx returned unexpected error code (err=%d)",
Error);
while (true) {
TEST_COMMENT(L"Waiting for completion...");
Error = WaitForSingleObject(AsyncContext->Overlapped.hEvent, 500);
TEST_ASSERT((Error == STATUS_TIMEOUT) || (Error == STATUS_WAIT_0),
L"WaitForSingleObject returned unexpected value %d",
Error);
if (Error == STATUS_WAIT_0) {
break;
}
}
TEST_ASSERT(AsyncContext->Overlapped.Internal == STATUS_SUCCESS,
L"Request failed (NTSTATUS=%08x)",
(NTSTATUS)AsyncContext->Overlapped.Internal);
//
// IOCTL's output buffer points to TR_SERVICE_REQUEST_RESPONSE. So this
// request's number of bytes written is always sizeof(TR_SERVICE_REQUEST_RESPONSE).
//
TEST_ASSERT(AsyncContext->Overlapped.InternalHigh == sizeof(TR_SERVICE_REQUEST_RESPONSE),
L"Request response size incorrect (BytesWritten=%u)",
(ULONG)AsyncContext->Overlapped.InternalHigh);
//
// Number of bytes written to secure request's output buffer is
// returned in TR_SERVICE_REQUEST_RESPONSE::BytesWritten.
//
TEST_ASSERT(AsyncContext->Response.BytesWritten == sizeof(ULONG),
L"Number of bytes written to output buffer incorrect (BytesWritten=%u)",
(ULONG)AsyncContext->Response.BytesWritten);
TEST_COMMENT(L"Output buffer contains: %08x", Output);
CleanupAsyncTrEECallContext(AsyncContext);
}
if ((TestFlag & 0x20) != 0) {
TEST_COMMENT(L"\n::::: TEST_SERVICE_DELAYED_COMPLETION(5000ms, asynchronous wait + cancel)");
Input = 5000;
Output = 0;
Success = CallTrEEServiceEx(TestServiceHandle,
TEST_SERVICE_DELAYED_COMPLETION,
&Input, sizeof(Input),
&Output, sizeof(Output),
&BytesWritten,
&AsyncContext);
Error = GetLastError();
TEST_ASSERT(!Success, L"CallTrEEServiceEx unexpectedly succeeded");
TEST_ASSERT(GetLastError() == ERROR_IO_PENDING,
L"CallTrEEServiceEx returned unexpected error code (err=%d)",
Error);
Sleep(1000);
TEST_COMMENT(L"Cancelling...");
//
// The request is cancelled before it can be completed.
//
Success = CancelIoEx(TestServiceHandle, &AsyncContext->Overlapped);
TEST_ASSERT(Success, L"CancelIoEx failed (err=%d)", GetLastError());
//
// When the cancellation is handled by the miniport driver, the request
// will have status STATUS_CANCELLED.
//
for (int i = 0; i < 10; ++i) {
TEST_COMMENT(L"Request status = %08x",
(NTSTATUS)AsyncContext->Overlapped.Internal);
if ((NTSTATUS)AsyncContext->Overlapped.Internal == STATUS_CANCELLED) {
break;
}
Sleep(20);
}
TEST_ASSERT((NTSTATUS)AsyncContext->Overlapped.Internal == STATUS_CANCELLED,
L"Request not cancelled (NTSTATUS=%08x)",
(NTSTATUS)AsyncContext->Overlapped.Internal);
CleanupAsyncTrEECallContext(AsyncContext);
}
}
{
DWORD Error;
ULONG Input;
ULONG Output;
OVERLAPPED Overlapped;
if ((TestFlag & 0x40) != 0) {
TEST_COMMENT(L"\n::::: IOCTL_TEST_DELAYED_COMPLETION(3000ms + async wait)");
memset(&Overlapped, 0, sizeof(Overlapped));
Overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
//
// Other I/O doesn't use TR_SERVICE_REQUEST structs. Use
// DeviceIocontrol and specify input and output buffers directly in
// parameters.
//
Input = 3000;
Output = 0;
Success = DeviceIoControl(TestServiceHandle,
IOCTL_TEST_DELAYED_COMPLETION,
&Input, sizeof(Input),
&Output, sizeof(Output),
&BytesWritten,
&Overlapped);
Error = GetLastError();
TEST_ASSERT(!Success, L"CallTrEEServiceEx unexpectedly succeeded");
TEST_ASSERT(GetLastError() == ERROR_IO_PENDING,
L"CallTrEEServiceEx returned unexpected error code (err=%d)",
Error);
while (true) {
TEST_COMMENT(L"Waiting for completion...");
Error = WaitForSingleObject(Overlapped.hEvent, 500);
TEST_ASSERT((Error == STATUS_TIMEOUT) || (Error == STATUS_WAIT_0),
L"WaitForSingleObject returned unexpected value %d",
Error);
if (Error == STATUS_WAIT_0) {
break;
}
}
TEST_ASSERT(Overlapped.Internal == STATUS_SUCCESS,
L"Request failed (NTSTATUS=%08x)",
(NTSTATUS)Overlapped.Internal);
//
// IOCTL's output buffer points to ULONG Output.
//
TEST_ASSERT(Overlapped.InternalHigh == sizeof(ULONG),
L"Request response size incorrect (BytesWritten=%u)",
(ULONG)Overlapped.InternalHigh);
TEST_COMMENT(L"Output buffer contains: %08x", Output);
}
if ((TestFlag & 0x80) != 0) {
TEST_COMMENT(L"\n::::: IOCTL_TEST_DELAYED_COMPLETION(5000ms, asynchronous wait + cancel)");
Overlapped.Internal = 0;
Overlapped.InternalHigh = 0;
Overlapped.Offset = 0;
Overlapped.OffsetHigh = 0;
Overlapped.Pointer = 0;
Input = 5000;
Output = 0;
Success = DeviceIoControl(TestServiceHandle,
IOCTL_TEST_DELAYED_COMPLETION,
&Input, sizeof(Input),
&Output, sizeof(Output),
&BytesWritten,
&Overlapped);
Error = GetLastError();
TEST_ASSERT(!Success, L"CallTrEEServiceEx unexpectedly succeeded");
TEST_ASSERT(GetLastError() == ERROR_IO_PENDING,
L"CallTrEEServiceEx returned unexpected error code (err=%d)",
Error);
Sleep(1000);
TEST_COMMENT(L"Cancelling...");
//
// The request is cancelled before it can be completed.
//
Success = CancelIoEx(TestServiceHandle, &Overlapped);
TEST_ASSERT(Success, L"CancelIoEx failed (err=%d)", GetLastError());
//
// When the cancellation is handled by the miniport driver, the request
// will have status STATUS_CANCELLED.
//
for (int i = 0; i < 10; ++i) {
TEST_COMMENT(L"Request status = %08x",
(NTSTATUS)Overlapped.Internal);
if ((NTSTATUS)Overlapped.Internal == STATUS_CANCELLED) {
break;
}
Sleep(20);
}
TEST_ASSERT((NTSTATUS)Overlapped.Internal == STATUS_CANCELLED,
L"Request not cancelled (NTSTATUS=%08x)",
(NTSTATUS)Overlapped.Internal);
CloseHandle(Overlapped.hEvent);
}
}
//
// Open the service using PnP device interface.
//
Test2ServiceHandle = OpenServiceHandleByInterface(&GUID_SAMPLE_TEST2_SERVICE);
TEST_ASSERT(Test2ServiceHandle != INVALID_HANDLE_VALUE, L"Test2 service open failed");
{
char Input[64];
size_t InputLength;
char Output[128];
if ((TestFlag & 0x100) != 0)
{
//
// This service talks to another driversin OS. Refer to
// miniport\TestService.c to see how the miniport driver sends requests
// to other drivers.
//
TEST_COMMENT(L"\n::::: TEST2_SERVICE_ECHO");
strcpy_s(Input, "Hello from usermode");
InputLength = strlen(Input);
memset(Output, 0, sizeof(Output));
Success = CallTrEEService(Test2ServiceHandle,
TEST2_SERVICE_ECHO,
Input, (ULONG)InputLength,
Output, sizeof(Output),
&BytesWritten);
TEST_ASSERT(Success, L"CallTrEEService failed (err=%d)", GetLastError());
TEST_ASSERT(BytesWritten == InputLength,
L"CallTrEEService returned unexpected number of bytes (BytesWritten=%d)",
BytesWritten);
TEST_COMMENT(L"Output buffer contains: %hs", Output);
}
if ((TestFlag & 0x200) != 0)
{
TEST_COMMENT(L"\n::::: TEST2_SERVICE_TWICE_REVERSED");
strcpy_s(Input, "Hello from usermode");
InputLength = strlen(Input);
memset(Output, 0, sizeof(Output));
Success = CallTrEEService(Test2ServiceHandle,
TEST2_SERVICE_ECHO_TWICE_REVERSED,
Input, (ULONG)InputLength,
Output, sizeof(Output),
&BytesWritten);
TEST_ASSERT(Success, L"CallTrEEService failed (err=%d)", GetLastError());
TEST_ASSERT(BytesWritten == InputLength * 2,
L"CallTrEEService returned unexpected number of bytes (BytesWritten=%d)",
BytesWritten);
TEST_COMMENT(L"Output buffer contains: %hs", Output);
}
}
//
// Open the master device using PnP device interface.
//
MasterDeviceHandle = CreateFileW(L"\\\\.\\SampleTrEEDriver",
FILE_READ_DATA | FILE_WRITE_DATA,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
TEST_ASSERT(MasterDeviceHandle != INVALID_HANDLE_VALUE, L"Master device open failed");
if ((TestFlag & 0x400) != 0)
{
TEST_COMMENT(L"\n::::: IOCTL_SAMPLE_DBGPRINT");
Success = DeviceIoControl(MasterDeviceHandle,
IOCTL_SAMPLE_DBGPRINT,
TEST_STRING, sizeof(TEST_STRING),
NULL, 0,
&BytesWritten,
NULL);
TEST_ASSERT(Success, L"CallTrEEService failed (err=%d)", GetLastError());
}
End:
if (TestServiceHandle != INVALID_HANDLE_VALUE) {
CloseHandle(TestServiceHandle);
}
if (Test2ServiceHandle != INVALID_HANDLE_VALUE) {
CloseHandle(Test2ServiceHandle);
}
if (MasterDeviceHandle != INVALID_HANDLE_VALUE) {
CloseHandle(MasterDeviceHandle);
}
return 0;
}
|