00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include <nt.h>
00022
#include <ntrtl.h>
00023
#include <nturtl.h>
00024
#include <ntdbg.h>
00025
00026 HANDLE
DebugPort;
00027
00028
00029
NTSTATUS
00030 ThreadThatExits (
00031 IN PVOID ThreadParameter
00032 )
00033 {
00034
NtTerminateThread(NtCurrentThread(),(
NTSTATUS) ThreadParameter );
00035 }
00036
00037 ULONG
00038 foo(PULONG l)
00039 {
00040
00041
00042
00043
00044
return *l;
00045
00046 }
00047
00048
NTSTATUS
00049 ThreadThatExcepts (
00050 IN PVOID ThreadParameter
00051 )
00052 {
00053
foo((PULONG)0x00000001);
00054
NtTerminateThread(NtCurrentThread(),(
NTSTATUS) ThreadParameter );
00055 }
00056
00057
00058
00059
NTSTATUS
00060 ThreadThatSpins (
00061 IN PVOID ThreadParameter
00062 )
00063 {
00064
for(;;);
00065
NtTerminateThread(NtCurrentThread(),STATUS_SUCCESS);
00066 }
00067
00068
00069 UdbgTest1()
00070 {
00071
NTSTATUS st;
00072 HANDLE ExitThread, SpinThread, DebugProcess;
00073 CLIENT_ID ExitClientId, SpinClientId;
00074 DBGKM_APIMSG m;
00075 PDBGKM_CREATE_THREAD CreateThreadArgs;
00076 PDBGKM_CREATE_PROCESS CreateProcessArgs;
00077 PDBGKM_EXIT_THREAD ExitThreadArgs;
00078 PDBGKM_EXIT_PROCESS ExitProcessArgs;
00079 ULONG Psp;
00080
00081
DbgPrint(
"UdbgTest1: (1)...\n");
00082
00083
00084
00085
00086
00087
00088 st =
NtCreateProcess(
00089 &DebugProcess,
00090 PROCESS_ALL_ACCESS,
00091
NULL,
00092 NtCurrentProcess(),
00093
FALSE,
00094
NULL,
00095
DebugPort,
00096
NULL
00097 );
00098
ASSERT(
NT_SUCCESS(st));
00099
00100 st =
RtlCreateUserThread(
00101 DebugProcess,
00102
NULL,
00103
TRUE,
00104 0
L,
00105 0
L,
00106 0
L,
00107
ThreadThatExits,
00108 (PVOID) STATUS_ABANDONED,
00109 &ExitThread,
00110 &ExitClientId
00111 );
00112
ASSERT(
NT_SUCCESS(st));
00113
00114 st =
RtlCreateUserThread(
00115 DebugProcess,
00116
NULL,
00117
TRUE,
00118 0
L,
00119 0
L,
00120 0
L,
00121
ThreadThatSpins,
00122
NULL,
00123 &SpinThread,
00124 &SpinClientId
00125 );
00126
ASSERT(
NT_SUCCESS(st));
00127
00128
DbgPrint(
"UdbgTest1: (2)...\n");
00129
00130
00131
00132
00133
00134
00135 st =
NtResumeThread(SpinThread,
NULL);
00136
ASSERT(
NT_SUCCESS(st));
00137
00138 st =
NtReplyWaitReceivePort(
00139
DebugPort,
00140
NULL,
00141
NULL,
00142 (PPORT_MESSAGE)&m
00143 );
00144
ASSERT(
NT_SUCCESS(st));
00145
ASSERT(m.ApiNumber == DbgKmCreateProcessApi);
00146
00147 CreateThreadArgs = &m.u.CreateProcess.InitialThread;
00148 CreateProcessArgs = &m.u.CreateProcess;
00149
ASSERT( CreateThreadArgs->SubSystemKey == 0 && CreateThreadArgs->StartAddress == (PVOID)
ThreadThatSpins );
00150
ASSERT( CreateProcessArgs->SubSystemKey == 0);
00151
00152
DbgPrint(
"UdbgTest1: (3)...\n");
00153
00154
00155
00156
00157
00158 st =
NtSuspendThread(ExitThread,&Psp);
00159
ASSERT(
NT_SUCCESS(st) && Psp == 2);
00160
00161 st =
NtResumeThread(ExitThread,&Psp);
00162
ASSERT(
NT_SUCCESS(st) && Psp == 3);
00163
00164 st =
NtReplyPort(
DebugPort,(PPORT_MESSAGE)&m);
00165
ASSERT(
NT_SUCCESS(st));
00166
00167
00168
DbgPrint(
"UdbgTest1: (4)...\n");
00169
00170
00171
00172
00173
00174
00175 st =
NtResumeThread(ExitThread,&Psp);
00176
ASSERT(
NT_SUCCESS(st));
00177
00178 st =
NtReplyWaitReceivePort(
00179
DebugPort,
00180
NULL,
00181
NULL,
00182 (PPORT_MESSAGE)&m
00183 );
00184
ASSERT(
NT_SUCCESS(st));
00185
ASSERT(m.ApiNumber == DbgKmCreateThreadApi);
00186
00187 CreateThreadArgs = &m.u.CreateThread;
00188
ASSERT( CreateThreadArgs->SubSystemKey == 0 && CreateThreadArgs->StartAddress == (PVOID)
ThreadThatExits );
00189
00190 st =
NtReplyPort(
DebugPort,(PPORT_MESSAGE)&m);
00191
ASSERT(
NT_SUCCESS(st));
00192
00193
DbgPrint(
"UdbgTest1: (5)...\n");
00194
00195
00196
00197
00198
00199
00200 st =
NtReplyWaitReceivePort(
00201
DebugPort,
00202
NULL,
00203
NULL,
00204 (PPORT_MESSAGE)&m
00205 );
00206
ASSERT(
NT_SUCCESS(st));
00207
ASSERT(m.ApiNumber == DbgKmExitThreadApi);
00208
00209 ExitThreadArgs = &m.u.ExitThread;
00210
ASSERT( ExitThreadArgs->ExitStatus == STATUS_ABANDONED );
00211
00212 st =
NtReplyPort(
DebugPort,(PPORT_MESSAGE)&m);
00213
ASSERT(
NT_SUCCESS(st));
00214
00215 st =
NtWaitForSingleObject(ExitThread,
FALSE,
NULL);
00216
ASSERT(
NT_SUCCESS(st));
00217
00218
DbgPrint(
"UdbgTest1: (6)...\n");
00219
00220
00221
00222
00223
00224
00225 st =
NtTerminateProcess(DebugProcess,STATUS_REPARSE);
00226
ASSERT(
NT_SUCCESS(st));
00227
00228 st =
NtReplyWaitReceivePort(
00229
DebugPort,
00230
NULL,
00231
NULL,
00232 (PPORT_MESSAGE)&m
00233 );
00234
ASSERT(
NT_SUCCESS(st));
00235
ASSERT(m.ApiNumber == DbgKmExitThreadApi);
00236
00237 ExitThreadArgs = &m.u.ExitThread;
00238
ASSERT( ExitThreadArgs->ExitStatus == STATUS_REPARSE );
00239
00240 st =
NtReplyPort(
DebugPort,(PPORT_MESSAGE)&m);
00241
ASSERT(
NT_SUCCESS(st));
00242
00243
DbgPrint(
"UdbgTest1: (7)...\n");
00244
00245
00246
00247
00248
00249
00250 st =
NtReplyWaitReceivePort(
00251
DebugPort,
00252
NULL,
00253
NULL,
00254 (PPORT_MESSAGE)&m
00255 );
00256
ASSERT(
NT_SUCCESS(st));
00257
ASSERT(m.ApiNumber == DbgKmExitProcessApi);
00258
00259 ExitProcessArgs = &m.u.ExitProcess;
00260
ASSERT( ExitProcessArgs->ExitStatus == STATUS_REPARSE );
00261
00262 st =
NtReplyPort(
DebugPort,(PPORT_MESSAGE)&m);
00263
ASSERT(
NT_SUCCESS(st));
00264
00265
00266 st =
NtWaitForSingleObject(ExitThread,
FALSE,
NULL);
00267
ASSERT(
NT_SUCCESS(st));
00268
00269 st =
NtWaitForSingleObject(DebugProcess,
FALSE,
NULL);
00270
ASSERT(
NT_SUCCESS(st));
00271
00272
NtClose(ExitThread);
00273
NtClose(SpinThread);
00274
NtClose(DebugProcess);
00275
00276
DbgPrint(
"UdbgTest1: END OF TEST ***\n");
00277
00278 }
00279
00280 UdbgTest2()
00281 {
00282
NTSTATUS st;
00283 HANDLE ExceptionThread, DebugProcess;
00284 DBGKM_APIMSG m;
00285 PDBGKM_CREATE_THREAD CreateThreadArgs;
00286 PDBGKM_CREATE_PROCESS CreateProcessArgs;
00287 PDBGKM_EXIT_THREAD ExitThreadArgs;
00288 PDBGKM_EXIT_PROCESS ExitProcessArgs;
00289 PDBGKM_EXCEPTION ExceptionArgs;
00290 ULONG Psp;
00291
00292
DbgPrint(
"UdbgTest2: (1)...\n");
00293
00294
00295
00296
00297
00298
00299 st =
NtCreateProcess(
00300 &DebugProcess,
00301 PROCESS_ALL_ACCESS,
00302
NULL,
00303 NtCurrentProcess(),
00304
FALSE,
00305
NULL,
00306
DebugPort,
00307
NULL
00308 );
00309
ASSERT(
NT_SUCCESS(st));
00310
00311 st =
RtlCreateUserThread(
00312 DebugProcess,
00313
NULL,
00314
TRUE,
00315 0
L,
00316 0
L,
00317 0
L,
00318
ThreadThatExcepts,
00319 (PVOID) STATUS_ABANDONED,
00320 &ExceptionThread,
00321
NULL
00322 );
00323
ASSERT(
NT_SUCCESS(st));
00324
00325
DbgPrint(
"UdbgTest2: (2)...\n");
00326
00327
00328
00329
00330
00331
00332 st =
NtResumeThread(ExceptionThread,
NULL);
00333
ASSERT(
NT_SUCCESS(st));
00334
00335 st =
NtReplyWaitReceivePort(
00336
DebugPort,
00337
NULL,
00338
NULL,
00339 (PPORT_MESSAGE)&m
00340 );
00341
ASSERT(
NT_SUCCESS(st));
00342
ASSERT(m.ApiNumber == DbgKmCreateProcessApi);
00343
00344 CreateThreadArgs = &m.u.CreateProcess.InitialThread;
00345 CreateProcessArgs = &m.u.CreateProcess;
00346
ASSERT( CreateThreadArgs->SubSystemKey == 0 && CreateThreadArgs->StartAddress == (PVOID)
ThreadThatExcepts );
00347
ASSERT( CreateProcessArgs->SubSystemKey == 0);
00348
00349 st =
NtReplyPort(
DebugPort,(PPORT_MESSAGE)&m);
00350
ASSERT(
NT_SUCCESS(st));
00351
00352
DbgPrint(
"UdbgTest2: (3)...\n");
00353
00354
00355
00356
00357
00358
00359 st =
NtReplyWaitReceivePort(
00360
DebugPort,
00361
NULL,
00362
NULL,
00363 (PPORT_MESSAGE)&m
00364 );
00365
ASSERT(
NT_SUCCESS(st));
00366
ASSERT(m.ApiNumber == DbgKmExceptionApi);
00367
00368 ExceptionArgs = &m.u.Exception;
00369
ASSERT( ExceptionArgs->FirstChance ==
TRUE );
00370
00371 m.ReturnedStatus = DBG_EXCEPTION_NOT_HANDLED;
00372
00373 st =
NtReplyPort(
DebugPort,(PPORT_MESSAGE)&m);
00374
ASSERT(
NT_SUCCESS(st));
00375
00376
DbgPrint(
"UdbgTest2: (4)...\n");
00377
00378
00379
00380
00381
00382
00383 st =
NtReplyWaitReceivePort(
00384
DebugPort,
00385
NULL,
00386
NULL,
00387 (PPORT_MESSAGE)&m
00388 );
00389
ASSERT(
NT_SUCCESS(st));
00390
ASSERT(m.ApiNumber == DbgKmExceptionApi);
00391
00392 ExceptionArgs = &m.u.Exception;
00393
ASSERT( ExceptionArgs->FirstChance ==
FALSE );
00394
00395 m.ReturnedStatus = DBG_EXCEPTION_HANDLED;
00396 skip4:
00397 st =
NtTerminateProcess(DebugProcess,STATUS_REPARSE);
00398
ASSERT(
NT_SUCCESS(st));
00399
00400 st =
NtReplyPort(
DebugPort,(PPORT_MESSAGE)&m);
00401
ASSERT(
NT_SUCCESS(st));
00402
00403 st =
NtReplyWaitReceivePort(
00404
DebugPort,
00405
NULL,
00406
NULL,
00407 (PPORT_MESSAGE)&m
00408 );
00409
ASSERT(
NT_SUCCESS(st));
00410
ASSERT(m.ApiNumber == DbgKmExitThreadApi);
00411
00412 ExitThreadArgs = &m.u.ExitThread;
00413
ASSERT( ExitThreadArgs->ExitStatus == STATUS_REPARSE );
00414
00415 st =
NtReplyPort(
DebugPort,(PPORT_MESSAGE)&m);
00416
ASSERT(
NT_SUCCESS(st));
00417
00418
DbgPrint(
"UdbgTest2: (5)...\n");
00419
00420
00421
00422
00423
00424
00425 st =
NtReplyWaitReceivePort(
00426
DebugPort,
00427
NULL,
00428
NULL,
00429 (PPORT_MESSAGE)&m
00430 );
00431
ASSERT(
NT_SUCCESS(st));
00432
ASSERT(m.ApiNumber == DbgKmExitProcessApi);
00433
00434 ExitProcessArgs = &m.u.ExitProcess;
00435
ASSERT( ExitProcessArgs->ExitStatus == STATUS_REPARSE );
00436
00437 st =
NtReplyPort(
DebugPort,(PPORT_MESSAGE)&m);
00438
ASSERT(
NT_SUCCESS(st));
00439
00440
00441 st =
NtWaitForSingleObject(ExceptionThread,
FALSE,
NULL);
00442
ASSERT(
NT_SUCCESS(st));
00443
00444 st =
NtWaitForSingleObject(DebugProcess,
FALSE,
NULL);
00445
ASSERT(
NT_SUCCESS(st));
00446
00447
NtClose(ExceptionThread);
00448
NtClose(DebugProcess);
00449
00450
DbgPrint(
"UdbgTest2: END OF TEST ***\n");
00451 }
00452
00453 main()
00454 {
00455
NTSTATUS st;
00456 OBJECT_ATTRIBUTES Obja;
00457
00458 InitializeObjectAttributes(&Obja,
NULL, 0,
NULL,
NULL);
00459
00460 st =
NtCreatePort(
00461 &
DebugPort,
00462 &Obja,
00463 0
L,
00464 256,
00465 256 * 16
00466 );
00467
ASSERT(
NT_SUCCESS(st));
00468
00469
UdbgTest2();
00470
UdbgTest1();
00471
00472 }