00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
#include "ntrtlp.h"
00033
00034
00035
00036
00037
00038 PRUNTIME_FUNCTION
00039
RtlLookupStaticFunctionEntry(
00040 IN ULONG_PTR ControlPc,
00041 OUT PBOOLEAN InImage
00042 );
00043
00044 PRUNTIME_FUNCTION
00045
RtlLookupDynamicFunctionEntry(
00046 IN ULONG_PTR ControlPc,
00047 OUT PULONGLONG ImageBase,
00048 OUT PULONGLONG TargetGp
00049 );
00050
00051
00052 LIST_ENTRY
DynamicFunctionTable;
00053
00054
VOID
00055
RtlRestoreContext (
00056 IN PCONTEXT ContextRecord,
00057 IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL
00058 );
00059
00060
00061
00062
00063
00064
00065
00066
00067 #define IS_HANDLER_DEFINED(f, base) \
00068
(f->UnwindInfoAddress && \
00069
(((PUNWIND_INFO)(base+f->UnwindInfoAddress))->Flags & 0x3))
00070
00071 #define HANDLER(f, base, target) \
00072
(PEXCEPTION_ROUTINE) \
00073
(*(PULONGLONG) ((LONGLONG)target + \
00074
(*(PULONGLONG) (base + f->UnwindInfoAddress + sizeof(UNWIND_INFO) + \
00075
(((PUNWIND_INFO) (base + f->UnwindInfoAddress))->DataLength * sizeof(ULONGLONG))))))
00076
00077 #define RAISE_EXCEPTION(Status, ExceptionRecordt) { \
00078
EXCEPTION_RECORD ExceptionRecordn; \
00079
\
00080
ExceptionRecordn.ExceptionCode = Status; \
00081
ExceptionRecordn.ExceptionFlags = EXCEPTION_NONCONTINUABLE; \
00082
ExceptionRecordn.ExceptionRecord = ExceptionRecordt; \
00083
ExceptionRecordn.NumberParameters = 0; \
00084
RtlRaiseException(&ExceptionRecordn); \
00085
}
00086
00087 #define IS_SAME_FRAME(Frame1, Frame2) \
00088
( (Frame1.MemoryStackFp == Frame2.MemoryStackFp) && \
00089
(Frame1.BackingStoreFp == Frame2.BackingStoreFp) )
00090
00091 #define INITIALIZE_FRAME(Frame) \
00092
Frame.MemoryStackFp = Frame.BackingStoreFp = 0
00093
00094 #define CHECK_MSTACK_FRAME(Establisher, Target) \
00095
((Establisher.MemoryStackFp < LowStackLimit) || \
00096
(Establisher.MemoryStackFp > HighStackLimit) || \
00097
((Target.MemoryStackFp != 0) && \
00098
((ULONG)Target.MemoryStackFp < (ULONG)Establisher.MemoryStackFp)) || \
00099
((Establisher.MemoryStackFp & 0x3) != 0))
00100
00101 #define CHECK_BSTORE_FRAME(Establisher, Target) \
00102
((Establisher.BackingStoreFp < LowBStoreLimit) || \
00103
(Establisher.BackingStoreFp > HighBStoreLimit) || \
00104
((Target.BackingStoreFp != 0) && \
00105
((ULONG)Target.BackingStoreFp > (ULONG)Establisher.BackingStoreFp)) || \
00106
((Establisher.BackingStoreFp & 0x7) != 0))
00107
00108 #define IS_EM_SETJMP_REGISTRATION(ExRegistration) \
00109
( (ExRegistration != NULL) && \
00110
(ExRegistration != EXCEPTION_CHAIN_END) && \
00111
(ExRegistration->Next == (struct _EXCEPTION_REGISTRATION_RECORD *)(LONG_PTR) 1) \
00112
)
00113
00114
00115 ULONGLONG
00116
RtlpVirtualUnwind (
00117 IN ULONGLONG ImageBase,
00118 IN ULONGLONG ControlPc,
00119 IN PRUNTIME_FUNCTION FunctionEntry,
00120 IN PCONTEXT ContextRecord,
00121 OUT PBOOLEAN InFunction,
00122 OUT PFRAME_POINTERS EstablisherFrame,
00123 IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
00124 );
00125
00126
00127 PEXCEPTION_REGISTRATION_RECORD
00128 RtlpGetRegistrationHead (
00129 IN VOID
00130 )
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 {
00150 PTEB CurrentTeb = NtCurrentTeb();
00151
00152
return (CurrentTeb ? CurrentTeb->NtTib.ExceptionList : EXCEPTION_CHAIN_END);
00153 }
00154
00155
00156
VOID
00157 RtlpUnlinkHandler (
00158 PEXCEPTION_REGISTRATION_RECORD UnlinkPointer
00159 )
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 {
00179 NtCurrentTeb()->NtTib.ExceptionList = UnlinkPointer->Next;
00180 }
00181
00182
00183 PRUNTIME_FUNCTION
00184 RtlLookupFunctionEntry (
00185 IN ULONGLONG ControlPc,
00186 OUT PULONGLONG ImageBase,
00187 OUT PULONGLONG TargetGp
00188 )
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 {
00216 PRUNTIME_FUNCTION FunctionEntry;
00217 PRUNTIME_FUNCTION FunctionTable;
00218 ULONG SizeOfExceptionTable;
00219 ULONG
Size;
00220 LONG High;
00221 LONG Middle;
00222 LONG Low;
00223
USHORT i;
00224
00225
00226
00227
00228
00229 *ImageBase = (ULONG_PTR)
RtlPcToFileHeader((PVOID)ControlPc,
00230 (PVOID *)ImageBase);
00231
00232
00233
00234
00235
00236
00237
00238
if ((PVOID)*ImageBase !=
NULL) {
00239
00240 *TargetGp = (ULONG_PTR)(
RtlImageDirectoryEntryToData(
00241 (PVOID)*ImageBase,
00242
TRUE,
00243 IMAGE_DIRECTORY_ENTRY_GLOBALPTR,
00244 &
Size
00245 ));
00246
00247 FunctionTable = (PRUNTIME_FUNCTION)
RtlImageDirectoryEntryToData(
00248 (PVOID)*ImageBase,
00249
TRUE,
00250 IMAGE_DIRECTORY_ENTRY_EXCEPTION,
00251 &SizeOfExceptionTable);
00252
00253
00254
00255
00256
00257
00258
if (FunctionTable !=
NULL) {
00259
00260
00261
00262
00263
00264 Low = 0;
00265 High = (SizeOfExceptionTable /
sizeof(RUNTIME_FUNCTION)) - 1;
00266 ControlPc = ControlPc - *ImageBase;
00267
00268
00269
00270
00271
00272
00273
while (High >= Low) {
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 Middle = (Low + High) >> 1;
00284 FunctionEntry = &FunctionTable[Middle];
00285
00286
if (ControlPc < FunctionEntry->BeginAddress) {
00287 High = Middle - 1;
00288
00289 }
else if (ControlPc >= FunctionEntry->EndAddress) {
00290 Low = Middle + 1;
00291
00292 }
else {
00293
return FunctionEntry;
00294
00295 }
00296 }
00297 }
00298 }
00299
#if !defined(NTOS_KERNEL_RUNTIME)
00300
00301
else
00302
return RtlLookupDynamicFunctionEntry ( ControlPc, ImageBase, TargetGp );
00303
00304
#endif // NTOS_KERNEL_RUNTIME
00305
00306
return NULL;
00307 }
00308
00309
00310
VOID
00311 RtlpRaiseException (
00312 IN PEXCEPTION_RECORD ExceptionRecord
00313 )
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 {
00333 ULONGLONG ImageBase;
00334 ULONGLONG TargetGp;
00335 ULONGLONG ControlPc;
00336 CONTEXT ContextRecord;
00337 FRAME_POINTERS EstablisherFrame;
00338 PRUNTIME_FUNCTION FunctionEntry;
00339 BOOLEAN InFunction;
00340 ULONGLONG NextPc;
00341
NTSTATUS Status;
00342
00343
00344
00345
00346
00347
00348
00349 RtlCaptureContext(&ContextRecord);
00350 ControlPc = RtlIa64InsertIPSlotNumber((ContextRecord.BrRp-16), 2);
00351 FunctionEntry =
RtlLookupFunctionEntry(ControlPc, &ImageBase, &TargetGp);
00352 NextPc =
RtlVirtualUnwind(ImageBase,
00353 ControlPc,
00354 FunctionEntry,
00355 &ContextRecord,
00356 &InFunction,
00357 &EstablisherFrame,
00358
NULL);
00359
00360 ContextRecord.StIIP = NextPc + 8;
00361 ContextRecord.StIPSR &= ~((ULONGLONG) 3 << PSR_RI);
00362 ExceptionRecord->ExceptionAddress = (PVOID)ContextRecord.StIIP;
00363
Status = ZwRaiseException(ExceptionRecord, &ContextRecord,
TRUE);
00364
00365
00366
00367
00368
00369
00370
00371
RtlRaiseStatus(
Status);
00372
return;
00373 }
00374
00375
00376
VOID
00377 RtlRaiseException (
00378 IN PEXCEPTION_RECORD ExceptionRecord
00379 )
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 {
00404
RtlpRaiseException(ExceptionRecord);
00405
return;
00406 }
00407
00408
VOID
00409 RtlpRaiseStatus (
00410 IN NTSTATUS Status
00411 )
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431 {
00432 ULONGLONG ImageBase;
00433 ULONGLONG TargetGp;
00434 ULONGLONG ControlPc;
00435 ULONGLONG NextPc;
00436 CONTEXT ContextRecord;
00437 FRAME_POINTERS EstablisherFrame;
00438 EXCEPTION_RECORD ExceptionRecord;
00439 PRUNTIME_FUNCTION FunctionEntry;
00440 BOOLEAN InFunction;
00441
00442
00443
00444
00445
00446 ExceptionRecord.ExceptionCode =
Status;
00447 ExceptionRecord.ExceptionRecord = (PEXCEPTION_RECORD)
NULL;
00448 ExceptionRecord.NumberParameters = 0;
00449 ExceptionRecord.ExceptionFlags =
EXCEPTION_NONCONTINUABLE;
00450
00451
00452
00453
00454
00455
00456
00457 RtlCaptureContext(&ContextRecord);
00458 ControlPc = RtlIa64InsertIPSlotNumber((ContextRecord.BrRp-16), 2);
00459 FunctionEntry =
RtlLookupFunctionEntry(ControlPc, &ImageBase, &TargetGp);
00460 NextPc =
RtlVirtualUnwind(ImageBase,
00461 ControlPc,
00462 FunctionEntry,
00463 &ContextRecord,
00464 &InFunction,
00465 &EstablisherFrame,
00466
NULL);
00467 ContextRecord.StIIP = NextPc + 8;
00468 ContextRecord.StIPSR &= ~((ULONGLONG) 3 << PSR_RI);
00469 ExceptionRecord.ExceptionAddress = (PVOID)ContextRecord.StIIP;
00470
Status = ZwRaiseException(&ExceptionRecord, &ContextRecord,
TRUE);
00471
00472
00473
00474
00475
00476
00477
00478
RtlRaiseStatus(
Status);
00479
return;
00480 }
00481
00482
00483
VOID
00484 RtlRaiseStatus (
00485 IN NTSTATUS Status
00486 )
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511 {
00512
RtlpRaiseStatus(
Status);
00513
return;
00514 }
00515
00516
00517
VOID
00518 RtlUnwind (
00519 IN PVOID TargetFrame OPTIONAL,
00520 IN PVOID TargetIp OPTIONAL,
00521 IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL,
00522 IN PVOID ReturnValue
00523 )
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565 {
00566 CONTEXT ContextRecord;
00567 FRAME_POINTERS Frame;
00568 PEXCEPTION_REGISTRATION_RECORD RegistrationPointer;
00569
00570 Frame.MemoryStackFp = (ULONG_PTR)TargetFrame;
00571 Frame.BackingStoreFp = 0;
00572 ContextRecord.StIPSR = 0;
00573
00574 RegistrationPointer = (PEXCEPTION_REGISTRATION_RECORD)TargetFrame;
00575
if ((RegistrationPointer !=
NULL) &&
00576
IS_EM_SETJMP_REGISTRATION(RegistrationPointer)) {
00577
00578
00579
00580
00581
00582
00583 PULONGLONG Record = (PULONGLONG)(RegistrationPointer);
00584
00585 Frame.MemoryStackFp = Record[1];
00586 Frame.BackingStoreFp = Record[2];
00587 }
00588
00589
00590
00591
00592
00593
00594
RtlUnwind2(Frame,
00595 TargetIp,
00596 ExceptionRecord,
00597 ReturnValue,
00598 &ContextRecord);
00599
00600
return;
00601 }
00602
00603
00604
VOID
00605 RtlUnwind2 (
00606 IN FRAME_POINTERS TargetFrame OPTIONAL,
00607 IN PVOID TargetIp OPTIONAL,
00608 IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL,
00609 IN PVOID ReturnValue,
00610 IN PCONTEXT ContextRecord
00611 )
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654 {
00655 ULONGLONG TargetGp;
00656 ULONGLONG ImageBase;
00657 ULONGLONG ControlPc;
00658 ULONGLONG NextPc;
00659 ULONG ExceptionFlags;
00660
DISPATCHER_CONTEXT DispatcherContext;
00661 EXCEPTION_DISPOSITION Disposition;
00662 FRAME_POINTERS EstablisherFrame;
00663 EXCEPTION_RECORD ExceptionRecord1;
00664 PRUNTIME_FUNCTION FunctionEntry;
00665 ULONGLONG HighStackLimit;
00666 ULONGLONG LowStackLimit;
00667 ULONGLONG HighBStoreLimit;
00668 ULONGLONG LowBStoreLimit;
00669 ULONG
Size;
00670 BOOLEAN InFunction;
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682 RtlCaptureContext(ContextRecord);
00683
00684
00685
00686
00687
00688
00689
Rtlp64GetStackLimits(&LowStackLimit, &HighStackLimit);
00690
Rtlp64GetBStoreLimits(&LowBStoreLimit, &HighBStoreLimit);
00691
00692 ControlPc = RtlIa64InsertIPSlotNumber((ContextRecord->BrRp-16), 2);
00693 FunctionEntry =
RtlLookupFunctionEntry(ControlPc, &ImageBase, &TargetGp);
00694 NextPc =
RtlVirtualUnwind(ImageBase,
00695 ControlPc,
00696 FunctionEntry,
00697 ContextRecord,
00698 &InFunction,
00699 &EstablisherFrame,
00700
NULL);
00701
00702 ControlPc = NextPc;
00703 ContextRecord->StIIP = (ULONGLONG)TargetIp;
00704
00705
#if defined(NTOS_KERNEL_RUNTIME)
00706
ContextRecord->StIPSR = SANITIZE_PSR(ContextRecord->StIPSR,
KernelMode);
00707
#else
00708
ContextRecord->StIPSR = SANITIZE_PSR(ContextRecord->StIPSR,
UserMode);
00709
#endif // defined(NTOS_KERNEL_RUNTIME)
00710
00711
00712
00713
00714
00715
00716
if (ARGUMENT_PRESENT(ExceptionRecord) ==
FALSE) {
00717 ExceptionRecord = &ExceptionRecord1;
00718 ExceptionRecord1.ExceptionCode = STATUS_UNWIND;
00719 ExceptionRecord1.ExceptionRecord =
NULL;
00720 ExceptionRecord1.ExceptionAddress = (PVOID)ControlPc;
00721 ExceptionRecord1.NumberParameters = 0;
00722 }
00723
00724
00725
00726
00727
00728
00729 ExceptionFlags =
EXCEPTION_UNWINDING;
00730
if (TargetFrame.BackingStoreFp == 0 && TargetFrame.MemoryStackFp == 0) {
00731 ExceptionRecord->ExceptionFlags |=
EXCEPTION_EXIT_UNWIND;
00732 }
00733
00734
00735
00736
00737
00738
00739
do {
00740
00741
00742
00743
00744
00745
00746
00747 FunctionEntry =
RtlLookupFunctionEntry(ControlPc, &ImageBase, &TargetGp);
00748
00749
00750
00751
00752
00753
00754
00755
00756
if (FunctionEntry !=
NULL) {
00757 NextPc =
RtlpVirtualUnwind(ImageBase,
00758 ControlPc,
00759 FunctionEntry,
00760 ContextRecord,
00761 &InFunction,
00762 &EstablisherFrame,
00763
NULL);
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
if (
CHECK_MSTACK_FRAME(EstablisherFrame, TargetFrame)) {
00775
00776
RAISE_EXCEPTION(STATUS_BAD_STACK, ExceptionRecord);
00777
00778 }
else if (
CHECK_BSTORE_FRAME(EstablisherFrame, TargetFrame)) {
00779
00780
RAISE_EXCEPTION(STATUS_BAD_STACK, ExceptionRecord);
00781
00782 }
else if (InFunction &&
IS_HANDLER_DEFINED(FunctionEntry, ImageBase)) {
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797 DispatcherContext.ControlPc = ControlPc;
00798 DispatcherContext.FunctionEntry = FunctionEntry;
00799 DispatcherContext.ImageBase = ImageBase;
00800 DispatcherContext.ContextRecord = ContextRecord;
00801
00802
00803
00804
00805
00806
do {
00807
00808
00809
00810
00811
00812
00813
if (
IS_SAME_FRAME(TargetFrame, EstablisherFrame)) {
00814 ExceptionFlags |=
EXCEPTION_TARGET_UNWIND;
00815 }
00816
00817 ExceptionRecord->ExceptionFlags = ExceptionFlags;
00818
00819
00820
00821
00822
00823
00824 ContextRecord->IntV0 = (ULONGLONG)ReturnValue;
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834 DispatcherContext.EstablisherFrame = EstablisherFrame;
00835 Disposition =
RtlpExecuteEmHandlerForUnwind(
00836 ExceptionRecord,
00837 EstablisherFrame.MemoryStackFp,
00838 EstablisherFrame.BackingStoreFp,
00839 ContextRecord,
00840 &DispatcherContext,
00841 TargetGp,
00842
HANDLER(FunctionEntry, ImageBase, TargetGp));
00843
00844
00845
00846
00847
00848 ExceptionFlags &= ~(
EXCEPTION_COLLIDED_UNWIND |
00849
EXCEPTION_TARGET_UNWIND);
00850
00851
00852
00853
00854
00855
switch (Disposition) {
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
case ExceptionContinueSearch :
00867
00868
if (!
IS_SAME_FRAME(EstablisherFrame, TargetFrame)) {
00869 NextPc =
RtlVirtualUnwind(ImageBase,
00870 ControlPc,
00871 FunctionEntry,
00872 ContextRecord,
00873 &InFunction,
00874 &EstablisherFrame,
00875
NULL);
00876 }
00877
break;
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
case ExceptionCollidedUnwind :
00889
00890 ControlPc = DispatcherContext.ControlPc;
00891 FunctionEntry = DispatcherContext.FunctionEntry;
00892 ImageBase = DispatcherContext.ImageBase;
00893 ContextRecord = DispatcherContext.ContextRecord;
00894 ContextRecord->StIIP = (ULONGLONG)TargetIp;
00895 ExceptionFlags |=
EXCEPTION_COLLIDED_UNWIND;
00896 EstablisherFrame = DispatcherContext.EstablisherFrame;
00897 TargetGp = (ULONG_PTR)(
RtlImageDirectoryEntryToData(
00898 (PVOID)ImageBase,
00899
TRUE,
00900 IMAGE_DIRECTORY_ENTRY_GLOBALPTR,
00901 &
Size
00902 ));
00903
break;
00904
00905
00906
00907
00908
00909
00910
00911
default :
00912
RAISE_EXCEPTION(STATUS_INVALID_DISPOSITION, ExceptionRecord);
00913 }
00914
00915 }
while ((ExceptionFlags &
EXCEPTION_COLLIDED_UNWIND) != 0);
00916
00917 }
else {
00918
00919
00920
00921
00922
00923
00924
00925
if (!
IS_SAME_FRAME(EstablisherFrame, TargetFrame)) {
00926 NextPc =
RtlVirtualUnwind(ImageBase,
00927 ControlPc,
00928 FunctionEntry,
00929 ContextRecord,
00930 &InFunction,
00931 &EstablisherFrame,
00932
NULL);
00933 }
00934 }
00935
00936 }
else {
00937
00938
00939
00940
00941
00942
SHORT BsFrameSize, TempFrameSize;
00943
00944 NextPc = RtlIa64InsertIPSlotNumber((ContextRecord->BrRp-16), 2);
00945 ContextRecord->StIFS = ContextRecord->RsPFS;
00946 BsFrameSize = (
SHORT)(ContextRecord->StIFS >> PFS_SIZE_SHIFT) & PFS_SIZE_MASK;
00947 TempFrameSize = BsFrameSize - (
SHORT)((ContextRecord->RsBSP >> 3) & NAT_BITS_PER_RNAT_REG);
00948
while (TempFrameSize > 0) {
00949 BsFrameSize++;
00950 TempFrameSize -= NAT_BITS_PER_RNAT_REG;
00951 }
00952 ContextRecord->RsBSP -= BsFrameSize *
sizeof(ULONGLONG);
00953 ContextRecord->RsBSPSTORE = ContextRecord->RsBSP;
00954 }
00955
00956
00957
00958
00959
00960 ControlPc = NextPc;
00961
00962 }
while (((EstablisherFrame.MemoryStackFp < HighStackLimit) ||
00963 (EstablisherFrame.BackingStoreFp > LowBStoreLimit)) &&
00964 !(
IS_SAME_FRAME(EstablisherFrame, TargetFrame)));
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
if (
IS_SAME_FRAME(EstablisherFrame, TargetFrame)) {
00975 ContextRecord->IntGp = TargetGp;
00976 ContextRecord->StIPSR &= ~(0x3i64 << PSR_RI);
00977 ContextRecord->IntV0 = (ULONGLONG)ReturnValue;
00978
RtlRestoreContext(ContextRecord, ExceptionRecord);
00979 }
else {
00980 ZwRaiseException(ExceptionRecord, ContextRecord,
FALSE);
00981 }
00982 }
00983
00984 BOOLEAN
00985 RtlDispatchException (
00986 IN PEXCEPTION_RECORD ExceptionRecord,
00987 IN PCONTEXT ContextRecord
00988 )
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023 {
01024 ULONGLONG TargetGp;
01025 ULONGLONG ImageBase;
01026 CONTEXT ContextRecordEm;
01027 ULONGLONG ControlPc;
01028 ULONGLONG NextPc;
01029
DISPATCHER_CONTEXT DispatcherContext;
01030 EXCEPTION_DISPOSITION Disposition;
01031 ULONG ExceptionFlags;
01032 PRUNTIME_FUNCTION FunctionEntry;
01033 FRAME_POINTERS EstablisherFrame;
01034 FRAME_POINTERS TargetFrame;
01035 ULONGLONG HighStackLimit;
01036 ULONGLONG LowStackLimit;
01037 ULONGLONG HighBStoreLimit;
01038 ULONGLONG LowBStoreLimit;
01039 FRAME_POINTERS NestedFrame;
01040 FRAME_POINTERS NullFrame;
01041 ULONG
Index;
01042 ULONG
Size;
01043 BOOLEAN InFunction;
01044
01045
01046
01047
01048
01049
01050
01051
Rtlp64GetStackLimits(&LowStackLimit, &HighStackLimit);
01052
Rtlp64GetBStoreLimits(&LowBStoreLimit, &HighBStoreLimit);
01053
01054 RtlMoveMemory(&ContextRecordEm, ContextRecord,
sizeof(CONTEXT));
01055
01056
if ( (ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION) &&
01057 (ExceptionRecord->NumberParameters == 5) &&
01058 (ExceptionRecord->ExceptionInformation[4] & (1 << ISR_X)) )
01059 {
01060 ControlPc = ExceptionRecord->ExceptionInformation[3];
01061 ControlPc = RtlIa64InsertIPSlotNumber(ControlPc,
01062 ((ContextRecordEm.StIPSR >> PSR_RI) & 0x3));
01063 }
else {
01064 ControlPc = RtlIa64InsertIPSlotNumber(ContextRecordEm.StIIP,
01065 ((ContextRecordEm.StIPSR >> PSR_RI) & 0x3));
01066 }
01067
01068 ExceptionFlags = ExceptionRecord->ExceptionFlags &
EXCEPTION_NONCONTINUABLE;
01069
01070
INITIALIZE_FRAME(NestedFrame);
01071
INITIALIZE_FRAME(NullFrame);
01072
01073
01074
01075
01076
01077
01078
01079
do {
01080
01081
01082
01083
01084
01085
01086
01087 FunctionEntry =
RtlLookupFunctionEntry(ControlPc, &ImageBase, &TargetGp);
01088
01089
01090
01091
01092
01093
01094
01095
01096
if (FunctionEntry !=
NULL) {
01097 NextPc =
RtlVirtualUnwind(ImageBase,
01098 ControlPc,
01099 FunctionEntry,
01100 &ContextRecordEm,
01101 &InFunction,
01102 &EstablisherFrame,
01103
NULL);
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
if (
CHECK_MSTACK_FRAME(EstablisherFrame, NullFrame)) {
01114
01115 ExceptionFlags |=
EXCEPTION_STACK_INVALID;
01116
break;
01117
01118 }
else if (
CHECK_BSTORE_FRAME(EstablisherFrame, NullFrame)) {
01119
01120 ExceptionFlags |=
EXCEPTION_STACK_INVALID;
01121
break;
01122
01123 }
else if ((
IS_HANDLER_DEFINED(FunctionEntry, ImageBase) && InFunction)) {
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134 DispatcherContext.ControlPc = ControlPc;
01135 DispatcherContext.FunctionEntry = FunctionEntry;
01136 DispatcherContext.ImageBase = ImageBase;
01137
01138
do {
01139
01140 ExceptionRecord->ExceptionFlags = ExceptionFlags;
01141
01142
if (
NtGlobalFlag & FLG_ENABLE_EXCEPTION_LOGGING) {
01143
Index =
RtlpLogExceptionHandler(
01144 ExceptionRecord,
01145 ContextRecord,
01146 (ULONG)ControlPc,
01147 FunctionEntry,
01148
sizeof(RUNTIME_FUNCTION));
01149 }
01150
01151 DispatcherContext.EstablisherFrame = EstablisherFrame;
01152 DispatcherContext.ContextRecord = ContextRecord;
01153 Disposition =
RtlpExecuteEmHandlerForException(
01154 ExceptionRecord,
01155 EstablisherFrame.MemoryStackFp,
01156 EstablisherFrame.BackingStoreFp,
01157 ContextRecord,
01158 &DispatcherContext,
01159 TargetGp,
01160
HANDLER(FunctionEntry, ImageBase, TargetGp));
01161
01162
if (
NtGlobalFlag & FLG_ENABLE_EXCEPTION_LOGGING) {
01163
RtlpLogLastExceptionDisposition(
Index, Disposition);
01164 }
01165
01166 ExceptionFlags |=
01167 (ExceptionRecord->ExceptionFlags &
EXCEPTION_NONCONTINUABLE);
01168
01169 ExceptionFlags &= ~
EXCEPTION_COLLIDED_UNWIND;
01170
01171
01172
01173
01174
01175
01176
01177
01178
if (
IS_SAME_FRAME(NestedFrame, EstablisherFrame)) {
01179 ExceptionFlags &= (~
EXCEPTION_NESTED_CALL);
01180
INITIALIZE_FRAME(NestedFrame);
01181 }
01182
01183
01184
01185
01186
01187
switch (Disposition) {
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
case ExceptionContinueExecution:
01198
if ((ExceptionFlags &
EXCEPTION_NONCONTINUABLE) != 0) {
01199
RAISE_EXCEPTION(STATUS_NONCONTINUABLE_EXCEPTION,
01200 ExceptionRecord);
01201 }
else {
01202
return TRUE;
01203 }
01204
01205
01206
01207
01208
01209
01210
01211
case ExceptionContinueSearch:
01212
break;
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
case ExceptionNestedException:
01223 ExceptionFlags |=
EXCEPTION_NESTED_CALL;
01224
if (DispatcherContext.EstablisherFrame.MemoryStackFp > NestedFrame.MemoryStackFp) {
01225 NestedFrame = DispatcherContext.EstablisherFrame;
01226 }
01227
break;
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
case ExceptionCollidedUnwind:
01238 ControlPc = DispatcherContext.ControlPc;
01239 NextPc = ControlPc;
01240 EstablisherFrame = DispatcherContext.EstablisherFrame;
01241 FunctionEntry = DispatcherContext.FunctionEntry;
01242 ImageBase = DispatcherContext.ImageBase;
01243 RtlMoveMemory(&ContextRecordEm,
01244 DispatcherContext.ContextRecord,
01245
sizeof(CONTEXT));
01246 ExceptionFlags |=
EXCEPTION_COLLIDED_UNWIND;
01247 TargetGp = (ULONG_PTR)(
RtlImageDirectoryEntryToData(
01248 (PVOID)ImageBase,
01249
TRUE,
01250 IMAGE_DIRECTORY_ENTRY_GLOBALPTR,
01251 &
Size
01252 ));
01253
break;
01254
01255
01256
01257
01258
01259
01260
01261
default:
01262
RAISE_EXCEPTION(STATUS_INVALID_DISPOSITION, ExceptionRecord);
01263
break;
01264 }
01265
01266 }
while ((ExceptionFlags &
EXCEPTION_COLLIDED_UNWIND) != 0);
01267
01268 }
01269
01270 }
else {
01271
01272
01273
01274
01275
01276
SHORT BsFrameSize, TempFrameSize;
01277
01278 NextPc = RtlIa64InsertIPSlotNumber((ContextRecordEm.BrRp-16), 2);
01279 ContextRecordEm.StIFS = ContextRecordEm.RsPFS;
01280 BsFrameSize = (
SHORT)(ContextRecordEm.StIFS >> PFS_SIZE_SHIFT) & PFS_SIZE_MASK;
01281 TempFrameSize = BsFrameSize - (
SHORT)((ContextRecordEm.RsBSP >> 3) & NAT_BITS_PER_RNAT_REG);
01282
while (TempFrameSize > 0) {
01283 BsFrameSize++;
01284 TempFrameSize -= NAT_BITS_PER_RNAT_REG;
01285 }
01286 ContextRecordEm.RsBSP -= BsFrameSize *
sizeof(ULONGLONG);
01287 ContextRecordEm.RsBSPSTORE = ContextRecordEm.RsBSP;
01288
01289
if (NextPc == ControlPc) {
01290
break;
01291 }
01292 }
01293
01294
01295
01296
01297
01298 ControlPc = NextPc;
01299
01300 }
while ( (ContextRecordEm.IntSp < HighStackLimit) ||
01301 (ContextRecordEm.RsBSP > LowBStoreLimit) );
01302
01303
01304
01305
01306
01307
01308
01309 ExceptionRecord->ExceptionFlags = ExceptionFlags;
01310
return FALSE;
01311 }
01312
01313
01314 ULONGLONG
01315 RtlpVirtualUnwind (
01316 IN ULONGLONG ImageBase,
01317 IN ULONGLONG ControlPc,
01318 IN PRUNTIME_FUNCTION FunctionEntry,
01319 IN PCONTEXT ContextRecord,
01320 OUT PBOOLEAN InFunction,
01321 OUT PFRAME_POINTERS EstablisherFrame,
01322 IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
01323 )
01324
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379 {
01380
01381 CONTEXT LocalContext;
01382
01383
01384
01385
01386
01387
01388
01389 RtlCopyMemory((PVOID)&LocalContext, ContextRecord,
sizeof(CONTEXT));
01390
return RtlVirtualUnwind(ImageBase,
01391 ControlPc,
01392 FunctionEntry,
01393 &LocalContext,
01394 InFunction,
01395 EstablisherFrame,
01396 ContextPointers);
01397 }
01398
01399
#if !defined(NTOS_KERNEL_RUNTIME)
01400
01401 PLIST_ENTRY
01402 RtlGetFunctionTableListHead (
01403 VOID
01404 )
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416
01417 {
01418
return &
DynamicFunctionTable;
01419 }
01420
01421 BOOLEAN
01422 RtlAddFunctionTable(
01423 IN PRUNTIME_FUNCTION FunctionTable,
01424 IN ULONG EntryCount,
01425 IN ULONGLONG BaseAddress,
01426 IN ULONGLONG TargetGp
01427 )
01428
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463 {
01464 PDYNAMIC_FUNCTION_TABLE pNew;
01465 PRUNTIME_FUNCTION FunctionEntry;
01466 ULONG i;
01467
01468
if (EntryCount == 0)
01469
return FALSE;
01470
01471
01472
01473
01474
01475
if (
DynamicFunctionTable.Flink ==
NULL) {
01476 InitializeListHead(&
DynamicFunctionTable);
01477 }
01478
01479
01480
01481
01482
01483 pNew =
RtlAllocateHeap( RtlProcessHeap(), 0,
sizeof(DYNAMIC_FUNCTION_TABLE) );
01484
01485
if (pNew !=
NULL) {
01486 pNew->FunctionTable = FunctionTable;
01487 pNew->EntryCount = EntryCount;
01488 NtQuerySystemTime( &pNew->TimeStamp );
01489
01490
01491
01492
01493
01494
01495 FunctionEntry = FunctionTable;
01496 pNew->MinimumAddress = RF_BEGIN_ADDRESS( BaseAddress, FunctionEntry);
01497 pNew->MaximumAddress = RF_END_ADDRESS(BaseAddress, FunctionEntry);
01498 pNew->Sorted =
TRUE;
01499 FunctionEntry++;
01500
01501
for (i = 1; i < EntryCount; FunctionEntry++, i++) {
01502
if (pNew->Sorted && FunctionEntry->BeginAddress < FunctionTable[i-1].BeginAddress) {
01503 pNew->Sorted =
FALSE;
01504 }
01505
if (RF_BEGIN_ADDRESS(FunctionTable, FunctionEntry) < pNew->MinimumAddress) {
01506 pNew->MinimumAddress = RF_BEGIN_ADDRESS( BaseAddress, FunctionEntry);
01507 }
01508
if (RF_END_ADDRESS( FunctionTable, FunctionEntry) > pNew->MaximumAddress) {
01509 pNew->MaximumAddress = RF_END_ADDRESS( BaseAddress, FunctionEntry);
01510 }
01511 }
01512
01513
01514
01515
01516
01517
01518 pNew->BaseAddress = BaseAddress;
01519 pNew->TargetGp = TargetGp;
01520
01521 RtlEnterCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->
LoaderLock);
01522 InsertTailList((PLIST_ENTRY)&
DynamicFunctionTable, (PLIST_ENTRY)pNew);
01523 RtlLeaveCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->
LoaderLock);
01524
01525
return TRUE;
01526 }
else {
01527
return FALSE;
01528 }
01529 }
01530
01531 BOOLEAN
01532 RtlDeleteFunctionTable (
01533 IN PRUNTIME_FUNCTION FunctionTable
01534 )
01535 {
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551
01552
01553
01554
01555 PDYNAMIC_FUNCTION_TABLE CurrentEntry;
01556 PLIST_ENTRY Head;
01557 PLIST_ENTRY Next;
01558 BOOLEAN
Status =
FALSE;
01559
01560 RtlEnterCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->
LoaderLock);
01561
01562
01563
01564
01565
01566
01567 Head = &
DynamicFunctionTable;
01568
for (Next = Head->Blink; Next != Head; Next = Next->Blink) {
01569 CurrentEntry = CONTAINING_RECORD(Next,DYNAMIC_FUNCTION_TABLE,Links);
01570
if (CurrentEntry->FunctionTable == FunctionTable) {
01571 RemoveEntryList((PLIST_ENTRY)CurrentEntry);
01572
RtlFreeHeap( RtlProcessHeap(), 0, CurrentEntry );
01573
Status =
TRUE;
01574
break;
01575 }
01576 }
01577
01578 RtlLeaveCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->
LoaderLock);
01579
return Status;
01580 }
01581
01582 PRUNTIME_FUNCTION
01583 RtlLookupDynamicFunctionEntry(
01584 IN ULONG_PTR ControlPc,
01585 OUT PULONGLONG ImageBase,
01586 OUT PULONGLONG TargetGp
01587 )
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607
01608
01609
01610
01611
01612
01613 {
01614 PDYNAMIC_FUNCTION_TABLE CurrentEntry;
01615 PLIST_ENTRY Next,Head;
01616 PRUNTIME_FUNCTION FunctionTable;
01617 PRUNTIME_FUNCTION FunctionEntry;
01618 LONG High;
01619 LONG Low;
01620 LONG Middle;
01621 SIZE_T BaseAddress;
01622
01623
01624
if (
DynamicFunctionTable.Flink ==
NULL || ImageBase ==
NULL)
01625
return NULL;
01626
01627
01628
01629
01630
01631
01632
if (RtlTryEnterCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->
LoaderLock) ) {
01633
01634 Head = &
DynamicFunctionTable;
01635
for (Next = Head->Blink; Next != Head; Next = Next->Blink) {
01636 CurrentEntry = CONTAINING_RECORD(Next,DYNAMIC_FUNCTION_TABLE,Links);
01637 FunctionTable = CurrentEntry->FunctionTable;
01638
01639
01640
01641
01642
01643
if ((ControlPc >= CurrentEntry->MinimumAddress) &&
01644 (ControlPc < CurrentEntry->MaximumAddress) ) {
01645
01646
01647
01648
01649 BaseAddress = CurrentEntry->BaseAddress;
01650
if (CurrentEntry->Sorted) {
01651
01652
01653
01654
01655
01656
01657 Low = 0;
01658 High = CurrentEntry->EntryCount -1 ;
01659
01660
while (High >= Low) {
01661
01662
01663
01664
01665
01666
01667
01668
01669
01670
01671 Middle = (Low + High) >> 1;
01672 FunctionEntry = &FunctionTable[Middle];
01673
01674
if (ControlPc < RF_BEGIN_ADDRESS( BaseAddress, FunctionEntry)) {
01675 High = Middle - 1;
01676
01677 }
else if (ControlPc >= RF_END_ADDRESS( BaseAddress, FunctionEntry)) {
01678 Low = Middle + 1;
01679
01680 }
else {
01681
01682 *ImageBase = CurrentEntry->BaseAddress;
01683
01684
if ( TargetGp !=
NULL )
01685 *TargetGp = CurrentEntry->TargetGp;
01686
01687 RtlLeaveCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->
LoaderLock);
01688
return FunctionEntry;
01689 }
01690 }
01691
01692 }
else {
01693
01694 PRUNTIME_FUNCTION LastFunctionEntry = &FunctionTable[CurrentEntry->EntryCount];
01695
01696
01697
for (FunctionEntry = FunctionTable; FunctionEntry < LastFunctionEntry; FunctionEntry++) {
01698
01699
if ((ControlPc >= RF_BEGIN_ADDRESS( BaseAddress, FunctionEntry)) &&
01700 (ControlPc < RF_END_ADDRESS( BaseAddress, FunctionEntry))) {
01701
01702
01703 *ImageBase = CurrentEntry->BaseAddress;
01704
01705
if ( TargetGp !=
NULL )
01706 *TargetGp = CurrentEntry->TargetGp;
01707
01708 RtlLeaveCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->
LoaderLock);
01709
return FunctionEntry;
01710 }
01711 }
01712 }
01713 }
01714 }
01715 RtlLeaveCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->
LoaderLock);
01716 }
01717
01718
return NULL;
01719 }
01720
01721
#endif