-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathframe.cpp
2253 lines (1986 loc) · 79.7 KB
/
frame.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
/***
*frame.cxx - The frame handler and everything associated with it.
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* The frame handler and everything associated with it.
*
* Entry points:
* _CxxFrameHandler - the frame handler.
*
* Open issues:
* Handling re-throw from dynamicly nested scope.
* Fault-tolerance (checking for data structure validity).
****/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <windows.h>
#include <internal.h>
#include <mtdll.h> // CRT internal header file
#include <ehassert.h> // This project's versions of standard assert macros
#include <ehdata.h> // Declarations of all types used for EH
#include <ehstate.h> // Declarations of state management stuff
#include <eh.h> // User-visible routines for eh
#include <ehhooks.h> // Declarations of hook variables and callbacks
#include <trnsctrl.h> // Routines to handle transfer of control (trnsctrl.asm)
#include <typeinfo.h>
#include <unknwn.h>
#define cxxReThrow (_getptd()->_cxxReThrow)
// We define CRTIMP2 to remove dependency on msvcprt.dll.
#if defined(CRTIMP2)
#undef CRTIMP2
#endif
#define CRTIMP2
#include <exception> // we need to get unexpected, and std::bad_exception from there
// Pre-V4 managed exception code
#define MANAGED_EXCEPTION_CODE 0XE0434F4D
// V4 and later managed exception code
#define MANAGED_EXCEPTION_CODE_V4 0XE0434352
////////////////////////////////////////////////////////////////////////////////
//
// Intel x86-specific definitions
//
#if defined(_M_IX86)
#define __GetRangeOfTrysToCheck(a, b, c, d, e, f, g) \
_GetRangeOfTrysToCheck(b, c, d, e, f)
#define __FrameUnwindToEmptyState(a, b, c) \
__FrameUnwindToState(a, b, c, EH_EMPTY_STATE);
#define __CallSETranslator(a, b, c, d, e, f, g, h) \
_CallSETranslator(a, b, c, d, e, f, g)
#define __GetUnwindState(a, b, c) \
GetCurrentState(a, b, c)
#define __OffsetToAddress(a, b, c) \
OffsetToAddress(a, b)
#define __GetAddress(a, b) \
(a)
#define REAL_FP(a, b) \
(a)
#define __ResetException(a)
////////////////////////////////////////////////////////////////////////////////
//
// ARM-specific definitions
//
#elif (defined(_M_ARM) && !defined(_M_ARM_NT)) /*IFSTRIP=IGN*/
#define __GetRangeOfTrysToCheck(a, b, c, d, e, f, g) \
_GetRangeOfTrysToCheck(a, b, c, d, e, f, g)
#define __CallSETranslator(a, b, c, d, e, f, g, h) \
_CallSETranslator(a, b, c, d, e, f, g)
#define __GetUnwindState(a, b, c) \
GetCurrentState(a, b, c)
#define __OffsetToAddress(a, b, c) \
OffsetToAddress(a, b)
#define __GetAddress(a, b) \
(void*)(a)
#define REAL_FP(a, b) \
(a)
#define __ResetException(a)
#ifdef _MT
#define pExitContext (*((CONTEXT **)&(_getptd()->_pExitContext)))
#else
static CONTEXT *pExitContext = NULL; // context to assist the return to the continuation point
EHExceptionRecord *_pForeignExcept = NULL;
#endif // _MT
// The throw site
#undef abnormal_termination
#define abnormal_termination() FALSE
////////////////////////////////////////////////////////////////////////////////
//
// MIPS-specific definitions
//
#elif defined(_M_X64) || defined(_M_ARM_NT) /*IFSTRIP=IGN*/
#define __GetRangeOfTrysToCheck(a, b, c, d, e, f, g) \
_GetRangeOfTrysToCheck(a, b, c, d, e, f, g)
#define __CallSETranslator(a, b, c, d, e, f, g, h) \
_CallSETranslator(a, b, c, d, e, f, g)
#define __GetUnwindState(a, b, c) \
GetCurrentState(a, b, c)
#define __OffsetToAddress(a, b, c) \
OffsetToAddress(a, b)
#define __GetAddress(a, b) \
(void*)(a)
#define REAL_FP(a, b) \
(a)
#define __ResetException(a)
#define pExitContext (*((CONTEXT **)&(_getptd()->_pExitContext)))
#define _pForeignExcept (*((EHExceptionRecord **)&(_getptd()->_pForeignException)))
#define EST_ARRAY(x,n) EST_ARRAY_IB(x, _GetImageBase(), n)
#define FUNC_ESTYPES(fi) ((fi).dispESTypeList ? FUNC_ESTYPES_IB(fi, _GetImageBase()) : NULL )
#define FUNC_PESTYPES(fi) ((*(fi)).dispESTypeList ? FUNC_PESTYPES_IB((fi), _GetImageBase()) : NULL )
// The throw site
#undef CT_PTD
#define CT_PTD(ct) (CT_PTD_IB(ct, _GetThrowImageBase()))
#undef CT_COPYFUNC
#define CT_COPYFUNC(ct) ((ct).copyFunction? CT_COPYFUNC_IB(ct, _GetThrowImageBase()):NULL)
#define CT_NAME_IB(ct,ib) (TD_NAME(*CT_PTD_IB(ct, ib)))
#undef THROW_FORWARDCOMPAT
#define THROW_FORWARDCOMPAT(ti) ((ti).pForwardCompat? THROW_FORWARDCOMPAT_IB(ti, _GetThrowImageBase()):NULL)
#undef THROW_COUNT
#define THROW_COUNT(ti) THROW_COUNT_IB(ti, _GetThrowImageBase())
#undef THROW_CTLIST
#define THROW_CTLIST(ti) THROW_CTLIST_IB(ti, _GetThrowImageBase())
// The catch site
#undef HT_HANDLER
#define HT_HANDLER(ht) (HT_HANDLER_IB(ht, _GetImageBase()))
#undef UWE_ACTION
#define UWE_ACTION(uwe) ((uwe).action? UWE_ACTION_IB(uwe, _GetImageBase()):NULL)
#undef FUNC_UNWIND
#define FUNC_UNWIND(fi,st) (FUNC_PUNWINDMAP(fi,_GetImageBase())[st])
#undef TBME_CATCH
#define TBME_CATCH(hm,n) (TBME_PLIST(hm,_GetImageBase())[n])
#undef TBME_PCATCH
#define TBME_PCATCH(hm,n) (&(TBME_PLIST(hm,_GetImageBase())[n]))
#undef HT_PTD
#define HT_PTD(ht) ((TypeDescriptor*)((ht).dispType? HT_PTD_IB(ht,_GetImageBase()):NULL))
#undef abnormal_termination
#define abnormal_termination() FALSE
#else
#error Unrecognized platform
#endif
extern "C" {
typedef struct {
unsigned long dwSig;
unsigned long uoffDestination;
unsigned long dwCode;
unsigned long uoffFramePointer;
} _NLG_INFO;
extern _NLG_INFO _NLG_Destination;
}
////////////////////////////////////////////////////////////////////////////////
//
// Forward declaration of local functions:
//
// The local unwinder must be external (see __CxxLongjmpUnwind in trnsctrl.cpp)
extern "C" void __FrameUnwindToState(
EHRegistrationNode *,
DispatcherContext *,
FuncInfo *,
__ehstate_t
);
static void FindHandler(
EHExceptionRecord *,
EHRegistrationNode *,
CONTEXT *,
DispatcherContext *,
FuncInfo *,
BOOLEAN,
int,
EHRegistrationNode*
);
static void CatchIt(
EHExceptionRecord *,
EHRegistrationNode *,
CONTEXT *,
DispatcherContext *,
FuncInfo *,
HandlerType *,
CatchableType *,
TryBlockMapEntry *,
int,
EHRegistrationNode *,
BOOLEAN
#if defined (_M_X64) || defined(_M_ARM_NT)
, BOOLEAN
#endif
);
static void * CallCatchBlock(
EHExceptionRecord *,
EHRegistrationNode *,
CONTEXT *,
FuncInfo *,
void *,
int,
unsigned long
);
extern "C" _CRTIMP void __cdecl __BuildCatchObject(
EHExceptionRecord *,
void *,
HandlerType *,
CatchableType *
);
extern "C" _CRTIMP int __cdecl __TypeMatch(
HandlerType *,
CatchableType *,
ThrowInfo *
);
extern "C" _CRTIMP void * __AdjustPointer(
void *,
const PMD&
);
static void FindHandlerForForeignException(
EHExceptionRecord *,
EHRegistrationNode *, CONTEXT *,
DispatcherContext *,
FuncInfo *,
__ehstate_t,
int,
EHRegistrationNode *
);
extern "C" _CRTIMP int __cdecl __FrameUnwindFilter(
EXCEPTION_POINTERS *
);
static int ExFilterRethrow(
EXCEPTION_POINTERS *
#if defined(_M_X64) || defined(_M_ARM)
,EHExceptionRecord *,
int *
#endif
);
extern "C" _CRTIMP void __cdecl __DestructExceptionObject(
EHExceptionRecord *,
BOOLEAN
);
// If we call DestructExceptionObject directly from C_Specific_Handler/
// _except_handler3, then frame.obj will be pulled in by the linker
// even in programs which do not have C++ exceptions. So we call it using a
// function pointer _pDestructExceptionObject which gets initialized to 0 by
// default, but, if frame.obj is pulled in naturally, it then points to
// __DestructExceptionObject.
extern "C" void (__cdecl * const _pDestructExceptionObject)
(EHExceptionRecord *,BOOLEAN) = &__DestructExceptionObject;
static BOOLEAN IsInExceptionSpec(
EHExceptionRecord *pExcept, // Information for this (logical)
// exception
ESTypeList *pFuncInfo // Static information for subject frame
);
static void CallUnexpected(ESTypeList* pESTypeList);
static BOOLEAN Is_bad_exception_allowed(ESTypeList *pExceptionSpec);
//
// This describes the most recently handled exception, in case of a rethrow:
//
#define _pCurrentException (*((EHExceptionRecord **)&(_getptd()->_curexception)))
#define _pCurrentExContext (*((CONTEXT **)&(_getptd()->_curcontext)))
#define __ProcessingThrow _getptd()->_ProcessingThrow
#define _pCurrentFuncInfo (*((ESTypeList **)&(_getptd()->_curexcspec)))
////////////////////////////////////////////////////////////////////////////////
//
// __InternalCxxFrameHandler - the frame handler for all functions with C++ EH
// information.
//
// If exception is handled, this doesn't return; otherwise, it returns
// ExceptionContinueSearch.
//
// Note that this is called three ways:
// From __CxxFrameHandler: primary usage, called to inspect whole function.
// CatchDepth == 0, pMarkerRN == NULL
// From CatchGuardHandler: If an exception occurred within a catch, this is
// called to check for try blocks within that catch only, and does not
// handle unwinds.
// From TranslatorGuardHandler: Called to handle the translation of a
// non-C++ EH exception. Context considered is that of parent.
extern "C" EXCEPTION_DISPOSITION __cdecl __InternalCxxFrameHandler(
EHExceptionRecord *pExcept, // Information for this exception
EHRegistrationNode *pRN, // Dynamic information for this frame
CONTEXT *pContext, // Context info
DispatcherContext *pDC, // Context within subject frame
FuncInfo *pFuncInfo, // Static information for this frame
int CatchDepth, // How deeply nested are we?
EHRegistrationNode *pMarkerRN, // Marker node for when checking inside
// catch block
BOOLEAN recursive // Are we handling a translation?
) {
EHTRACE_ENTER_FMT2("%s, pRN = 0x%p",
IS_UNWINDING(PER_FLAGS(pExcept)) ? "Unwinding" : "Searching",
pRN);
if ((cxxReThrow == false) && (PER_CODE(pExcept) != EH_EXCEPTION_NUMBER) &&
#if defined(_M_X64) || defined(_M_ARM) /*IFSTRIP=IGN*/
/* On the 64 bit/ARM platforms, ExceptionCode maybe set to STATUS_UNWIND_CONSOLIDATE
when called from _UnwindNestedFrames during Logical Unwind. _UnwindNestedFrames
will also set EH_MAGIC_NUMBER1 in the 8 element */
(!((PER_CODE(pExcept) == STATUS_UNWIND_CONSOLIDATE) && (PER_NPARAMS(pExcept) == 15) && (PER_EXCEPTINFO(pExcept)[8] == EH_MAGIC_NUMBER1))) &&
#endif
(PER_CODE(pExcept) != STATUS_LONGJUMP) &&
(FUNC_MAGICNUM(*pFuncInfo) >= EH_MAGIC_NUMBER3) &&
((FUNC_FLAGS(*pFuncInfo) & FI_EHS_FLAG) != 0))
{
/*
* This function was compiled /EHs so we don't need to do anything in
* this handler.
*/
return ExceptionContinueSearch;
}
if (IS_UNWINDING(PER_FLAGS(pExcept)))
{
// We're at the unwinding stage of things. Don't care about the
// exception itself. (Check this first because it's easier)
if (FUNC_MAXSTATE(*pFuncInfo) != 0 && CatchDepth == 0)
{
// Only unwind if there's something to unwind
// AND we're being called through the primary RN.
#if defined(_M_X64) || defined(_M_ARM) /*IFSTRIP=IGN*/
if (IS_TARGET_UNWIND(PER_FLAGS(pExcept)) && PER_CODE(pExcept) == STATUS_LONGJUMP) {
__ehstate_t target_state = __StateFromIp(pFuncInfo,
pDC,
#if defined(_M_X64)
pContext->Rip
#elif defined(_M_ARM)
pContext->Pc
#endif
);
DASSERT(target_state >= EH_EMPTY_STATE
&& target_state < FUNC_MAXSTATE(*pFuncInfo));
__FrameUnwindToState(pRN, pDC, pFuncInfo, target_state);
EHTRACE_HANDLER_EXIT(ExceptionContinueSearch);
return ExceptionContinueSearch;
} else if(IS_TARGET_UNWIND(PER_FLAGS(pExcept)) &&
PER_CODE(pExcept) == STATUS_UNWIND_CONSOLIDATE)
{
PEXCEPTION_RECORD pSehExcept = (PEXCEPTION_RECORD)pExcept;
__ehstate_t target_state = (__ehstate_t)pSehExcept->ExceptionInformation[3];
DASSERT(target_state >= EH_EMPTY_STATE
&& target_state < FUNC_MAXSTATE(*pFuncInfo));
__FrameUnwindToState((EHRegistrationNode *)pSehExcept->ExceptionInformation[1],
pDC,
pFuncInfo,
target_state);
EHTRACE_HANDLER_EXIT(ExceptionContinueSearch);
return ExceptionContinueSearch;
}
#endif // defined(_M_X64) || defined(_M_ARM)
__FrameUnwindToEmptyState(pRN, pDC, pFuncInfo);
}
EHTRACE_HANDLER_EXIT(ExceptionContinueSearch);
return ExceptionContinueSearch; // I don't think this value matters
} else if (FUNC_NTRYBLOCKS(*pFuncInfo) != 0
//
// If the function has no try block, we still want to call the
// frame handler if there is an exception specification
//
|| (FUNC_MAGICNUM(*pFuncInfo) >= EH_MAGIC_NUMBER2 && FUNC_PESTYPES(pFuncInfo) != NULL)) {
// NT is looking for handlers. We've got handlers.
// Let's check this puppy out. Do we recognize it?
int (__cdecl *pfn)(...);
if (PER_CODE(pExcept) == EH_EXCEPTION_NUMBER
&& PER_NPARAMS(pExcept) >= 3
&& PER_MAGICNUM(pExcept) > EH_MAGIC_NUMBER3
&& (pfn = THROW_FORWARDCOMPAT(*PER_PTHROW(pExcept))) != NULL) {
// Forward compatibility: The thrown object appears to have been
// created by a newer version of our compiler. Let that version's
// frame handler do the work (if one was specified).
#if defined(_DEBUG) || defined(_SYSCRT_DEBUG)
if (_ValidateExecute((FARPROC)pfn)) {
#endif
EXCEPTION_DISPOSITION result =
(EXCEPTION_DISPOSITION)pfn(pExcept, pRN, pContext, pDC,
pFuncInfo, CatchDepth,
pMarkerRN, recursive);
EHTRACE_HANDLER_EXIT(result);
return result;
#if defined(_DEBUG) || defined(_SYSCRT_DEBUG)
} else {
_inconsistency(); // Does not return; TKB
}
#endif
} else {
// Anything else: we'll handle it here.
FindHandler(pExcept, pRN, pContext, pDC, pFuncInfo, recursive,
CatchDepth, pMarkerRN);
}
// If it returned, we didn't have any matches.
} // NT was looking for a handler
// We had nothing to do with it or it was rethrown. Keep searching.
EHTRACE_HANDLER_EXIT(ExceptionContinueSearch);
return ExceptionContinueSearch;
} // InternalCxxFrameHandler
////////////////////////////////////////////////////////////////////////////////
//
// FindHandler - find a matching handler on this frame, using all means
// available.
//
// Description:
// If the exception thrown was an MSC++ EH, search handlers for match.
// Otherwise, if we haven't already recursed, try to translate.
// If we have recursed (ie we're handling the translator's exception), and
// it isn't a typed exception, call _inconsistency.
//
// Returns:
// Returns iff exception was not handled.
//
// Assumptions:
// Only called if there are handlers in this function.
static void FindHandler(
EHExceptionRecord *pExcept, // Information for this (logical)
// exception
EHRegistrationNode *pRN, // Dynamic information for subject frame
CONTEXT *pContext, // Context info
DispatcherContext *pDC, // Context within subject frame
FuncInfo *pFuncInfo, // Static information for subject frame
BOOLEAN recursive, // TRUE if we're handling the
// translation
int CatchDepth, // Level of nested catch that is being
// checked
EHRegistrationNode *pMarkerRN // Extra marker RN for nested catch
// handling
)
{
EHTRACE_ENTER;
BOOLEAN IsRethrow = FALSE;
BOOLEAN gotMatch = FALSE;
// Get the current state (machine-dependent)
#if defined(_M_X64) || defined(_M_ARM_NT) /*IFSTRIP=IGN*/
__ehstate_t curState = __StateFromControlPc(pFuncInfo, pDC);
EHRegistrationNode EstablisherFrame;
/*
* Here We find what is the actual State of current function. The way we
* do this is first get State from ControlPc.
*
* Remember we have __GetUnwindTryBlock to remember the last State for which
* Exception was handled and __GetCurrentState for retriving the current
* state of the function. Please Note that __GetCurrentState is used
* primarily for unwinding purpose.
*
* Also remember that all the catch blocks act as funclets. This means that
* ControlPc for all the catch blocks are different from ControlPc of parent
* catch block or function.
*
* take a look at this example
* try {
* // STATE1 = 1
* try {
* // STATE2
* // THROW
* } catch (...) { // CatchB1
* // STATE3
* // RETHROW OR NEW THROW
* }
* } catch (...) { // CatchB2
* }
*
* If we have an exception comming from STATE3, the FindHandler will be
* called for CatchB1, at this point we do the test which State is our
* real state, curState from ControlPc or state from __GetUnwindTryBlock.
* Since curState from ControlPc is greater, we know that real State is
* curState from ControlPc and thus we update the UnwindTryBlockState.
*
* On further examination, we found out that there is no handler within
* this catch block, we return without handling the exception. For more
* info on how we determine if we have handler, have a look at
* __GetRangeOfTrysToCheck.
*
* Now FindHandler will again be called for parent function. Here again
* we test which is real State, state from ControlPc or State from
* __GetUnwindTryBlock. This time state from __GetUnwindTryBlock is correct.
*
* Also look at code in __CxxCallCatchBlock, you will se that as soon as we get
* out of last catch block, we reset __GetUnwindTryBlock state to -1.
*/
_GetEstablisherFrame(pRN, pDC, pFuncInfo, &EstablisherFrame);
if (curState > __GetUnwindTryBlock(pRN, pDC, pFuncInfo)) {
__SetState(&EstablisherFrame, pDC, pFuncInfo, curState);
__SetUnwindTryBlock(pRN, pDC, pFuncInfo, /*curTry*/ curState);
} else {
curState = __GetUnwindTryBlock(pRN, pDC, pFuncInfo);
}
#else
__ehstate_t curState = GetCurrentState(pRN, pDC, pFuncInfo);
#endif
DASSERT(curState >= EH_EMPTY_STATE && curState < FUNC_MAXSTATE(*pFuncInfo));
// Check if it's a re-throw. Use the exception we stashed away if it is.
if (PER_IS_MSVC_EH(pExcept) && PER_PTHROW(pExcept) == NULL) {
if (_pCurrentException == NULL) {
// Oops! User re-threw a non-existant exception! Let it propogate.
EHTRACE_EXIT;
return;
}
pExcept = _pCurrentException;
pContext = _pCurrentExContext;
IsRethrow = TRUE;
#if _EH_RELATIVE_OFFSETS /*IFSTRIP=IGN*/
_SetThrowImageBase((ptrdiff_t)pExcept->params.pThrowImageBase);
#endif
DASSERT(_ValidateRead(pExcept));
DASSERT(!PER_IS_MSVC_EH(pExcept) || PER_PTHROW(pExcept) != NULL);
//
// We know it is a rethrow -- did we come here as a result of an
// exception re-thrown from CallUnexpected() ?
//
if( _pCurrentFuncInfo != NULL )
{
ESTypeList* pCurrentFuncInfo = _pCurrentFuncInfo; // remember it in a local variable
_pCurrentFuncInfo = NULL; // and reset it immediately -- so we don't forget to do it later
// Does the exception thrown by CallUnexpected belong to the exception specification?
if( IsInExceptionSpec(pExcept, pCurrentFuncInfo) )
{
// Yes it does -- so "continue the search for another handler at the call of the function
// whose exception-specification was violated"
;
}
else
{
// Nope, it does not. Is std::bad_exception allowed by the spec?
if( Is_bad_exception_allowed(pCurrentFuncInfo) )
{
// yup -- so according to the standard, we need to replace the thrown
// exception by an implementation-defined object of the type std::bad_exception
// and continue the search for another handler at the call of the function
// whose exception-specification was violated.
// Just throw bad_exception -- we will then come into FindHandler for the third time --
// but make sure we will not get here again
__DestructExceptionObject(pExcept, TRUE); // destroy the original object
throw std::bad_exception();
}
else
{
terminate();
}
}
}
}
if (PER_IS_MSVC_EH(pExcept)) {
// Looks like it's ours. Let's see if we have a match:
//
// First, determine range of try blocks to consider:
// Only try blocks which are at the current catch depth are of interest.
unsigned curTry;
unsigned end;
if( FUNC_NTRYBLOCKS(*pFuncInfo) > 0 )
{
TryBlockMapEntry *pEntry = __GetRangeOfTrysToCheck(pRN,
pFuncInfo,
CatchDepth,
curState,
&curTry,
&end,
pDC);
// Scan the try blocks in the function:
for (; curTry < end; curTry++, pEntry++) {
HandlerType *pCatch;
#if _EH_RELATIVE_OFFSETS
__int32 const *ppCatchable;
#else
CatchableType * const *ppCatchable;
#endif
CatchableType *pCatchable;
int catches;
int catchables;
if (TBME_LOW(*pEntry) > curState || curState > TBME_HIGH(*pEntry)) {
continue;
}
// Try block was in scope for current state. Scan catches for this
// try:
pCatch = TBME_PCATCH(*pEntry, 0);
for (catches = TBME_NCATCHES(*pEntry); catches > 0; catches--,
pCatch++) {
// Scan all types that thrown object can be converted to:
ppCatchable = THROW_CTLIST(*PER_PTHROW(pExcept));
for (catchables = THROW_COUNT(*PER_PTHROW(pExcept));
catchables > 0; catchables--, ppCatchable++) {
#if _EH_RELATIVE_OFFSETS
pCatchable = (CatchableType *)(_GetThrowImageBase() + *ppCatchable);
#else
pCatchable = *ppCatchable;
#endif
if (!__TypeMatch(pCatch, pCatchable, PER_PTHROW(pExcept))) {
continue;
}
// OK. We finally found a match. Activate the catch. If
// control gets back here, the catch did a re-throw, so
// keep searching.
gotMatch = TRUE;
CatchIt(pExcept,
pRN,
pContext,
pDC,
pFuncInfo,
pCatch,
pCatchable,
pEntry,
CatchDepth,
pMarkerRN,
IsRethrow
#if defined (_M_X64) || defined(_M_ARM_NT)
, recursive
#endif
);
goto NextTryBlock;
} // Scan posible conversions
} // Scan catch clauses
NextTryBlock: ;
} // Scan try blocks
} // if FUNC_NTRYBLOCKS( pFuncInfo ) > 0
#if defined(_DEBUG) || defined(_SYSCRT_DEBUG)
else
{
//
// This can only happen if the function has an exception specification
// but no try/catch blocks
//
DASSERT( FUNC_MAGICNUM(*pFuncInfo) >= EH_MAGIC_NUMBER2 );
DASSERT( FUNC_PESTYPES(pFuncInfo) != NULL );
}
#endif
#if defined(_M_IX86)
if (recursive) {
//
// A translation was provided, but this frame didn't catch it.
// Destruct the translated object before returning; if destruction
// raises an exception, terminate.
//
// This is not done for Win64 platforms. On those, the translated
// object is destructed in __CxxCallCatchBlock.
//
__DestructExceptionObject(pExcept, TRUE);
}
#endif
#if (!defined(_M_ARM) || defined(_M_ARM_NT))
//
// We haven't found the match -- let's look at the exception spec and see if our try
// matches one of the listed types.
//
if( !gotMatch && FUNC_MAGICNUM(*pFuncInfo) >= EH_MAGIC_HAS_ES && FUNC_PESTYPES(pFuncInfo) != NULL )
{
if( !IsInExceptionSpec(pExcept, FUNC_PESTYPES(pFuncInfo)) )
{
// Nope, it does not. Call unexpected
//
// We must unwind the stack before calling unexpected -- this makes it work
// as if it were inside catch(...) clause
//
#if defined (_M_X64) || defined(_M_ARM_NT) /*IFSTRIP=IGN*/
EHRegistrationNode *pEstablisher = pRN;
EHRegistrationNode EstablisherFramePointers;
pEstablisher = _GetEstablisherFrame(pRN, pDC, pFuncInfo, &EstablisherFramePointers);
PVOID pExceptionObjectDestroyed = NULL;
_UnwindNestedFrames(pRN,
pExcept,
pContext,
pEstablisher,
NULL,
-1,
pFuncInfo,
pDC,
recursive
);
#else
EHExceptionRecord *pSaveException = _pCurrentException;
CONTEXT *pSaveExContext = _pCurrentExContext;
_pCurrentException = pExcept;
_pCurrentExContext = pContext;
if (pMarkerRN == NULL) {
_UnwindNestedFrames(pRN, pExcept);
} else {
_UnwindNestedFrames(pMarkerRN, pExcept);
}
__FrameUnwindToEmptyState(pRN, pDC, pFuncInfo);
CallUnexpected(FUNC_PESTYPES(pFuncInfo));
_pCurrentException = pExcept;
_pCurrentExContext = pContext;
#endif
}
}
#endif // !defined(_M_ARM)
} // It was a C++ EH exception
else {
// Not ours. But maybe someone told us how to make it ours.
if( FUNC_NTRYBLOCKS(*pFuncInfo) > 0 ) {
if (!recursive) {
FindHandlerForForeignException(pExcept, pRN, pContext, pDC,
pFuncInfo, curState, CatchDepth, pMarkerRN);
} else {
// We're recursive, and the exception wasn't a C++ EH!
// Translator threw something uninteligable.
// Two choices here: we could let the new exception take over, or we could abort. We abort.
terminate();
}
}
} // It wasn't our exception
DASSERT( _pCurrentFuncInfo == NULL ); // never leave it initialized with something
EHTRACE_EXIT;
}
////////////////////////////////////////////////////////////////////////////////
//
// FindHandlerForForeignException - We've got an exception which wasn't ours.
// Try to translate it into C++ EH, and also check for match with ellipsis.
//
// Description:
// If an SE-to-EH translator has been installed, call it. The translator
// must throw the appropriate typed exception or return. If the translator
// throws, we invoke FindHandler again as the exception filter.
//
// Returns:
// Returns if exception was not fully handled.
// No return value.
//
// Assumptions:
// Only called if there are handlers in this function.
static void FindHandlerForForeignException(
EHExceptionRecord *pExcept, // Information for this (logical)
// exception
EHRegistrationNode *pRN, // Dynamic information for subject frame
CONTEXT *pContext, // Context info
DispatcherContext *pDC, // Context within subject frame
FuncInfo *pFuncInfo, // Static information for subject frame
__ehstate_t curState, // Current state
int CatchDepth, // Level of nested catch that is being
// checked
EHRegistrationNode *pMarkerRN // Extra marker RN for nested catch
// handling
)
{
EHTRACE_ENTER;
unsigned curTry;
unsigned end;
TryBlockMapEntry *pEntry;
// We don't want to touch BreakPoint generated Exception.
if (PER_CODE(pExcept) == STATUS_BREAKPOINT) {
EHTRACE_EXIT;
return;
}
if (__pSETranslator != NULL && __pSETranslator != EncodePointer(NULL) &&
pExcept->ExceptionCode != MANAGED_EXCEPTION_CODE &&
pExcept->ExceptionCode != MANAGED_EXCEPTION_CODE_V4) {
// Call the translator. If the translator knows what to
// make of it, it will throw an appropriate C++ exception.
// We intercept it and use it (recursively) for this
// frame. Don't recurse more than once.
if (__CallSETranslator(pExcept, pRN, pContext, pDC, pFuncInfo,
CatchDepth, pMarkerRN, TDTransOffset)) {
EHTRACE_EXIT;
return;
}
}
DASSERT( FUNC_NTRYBLOCKS(*pFuncInfo) != 0 );
// Didn't have a translator, or the translator returned normally (i.e.
// didn't translate it). Still need to check for match with ellipsis:
pEntry = __GetRangeOfTrysToCheck(pRN, pFuncInfo, CatchDepth, curState,
&curTry, &end, pDC);
// Scan the try blocks in the function:
for (; curTry < end; curTry++, pEntry++) {
// If the try-block was in scope *and* the last catch in that try is an
// ellipsis (no other can be)
if (curState < TBME_LOW(*pEntry) || curState > TBME_HIGH(*pEntry)
|| !((HT_IS_TYPE_ELLIPSIS(TBME_CATCH(*pEntry, TBME_NCATCHES(*pEntry) - 1)))
&& !(HT_IS_STD_DOTDOT(TBME_CATCH(*pEntry, TBME_NCATCHES(*pEntry) - 1))))) {
continue;
}
// Found an ellipsis. Handle exception.
CatchIt(pExcept,
pRN,
pContext,
pDC,
pFuncInfo,
TBME_PCATCH(*pEntry, TBME_NCATCHES(*pEntry) - 1),
NULL,
pEntry,
CatchDepth,
pMarkerRN,
TRUE
#if defined(_M_X64) || defined(_M_ARM_NT)
,FALSE
#endif
);
// If it returns, handler re-threw. Keep searching.
} // Search for try
EHTRACE_EXIT;
// If we got here, that means we didn't have anything to do with the
// exception. Continue search.
}
////////////////////////////////////////////////////////////////////////////////
//
// __TypeMatch - Check if the catch type matches the given throw conversion.
//
// Returns:
// TRUE if the catch can catch using this throw conversion, FALSE otherwise.
extern "C" _CRTIMP int __cdecl __TypeMatch(
HandlerType *pCatch, // Type of the 'catch' clause
CatchableType *pCatchable, // Type conversion under consideration
ThrowInfo *pThrow // General information about the thrown
// type.
) {
// First, check for match with ellipsis:
if (HT_IS_TYPE_ELLIPSIS(*pCatch)) {
return TRUE;
}
// Not ellipsis; the basic types match if it's the same record *or* the
// names are identical.
if (HT_PTD(*pCatch) != CT_PTD(*pCatchable)
&& strcmp(HT_NAME(*pCatch), CT_NAME(*pCatchable)) != 0) {
return FALSE;
}
// Basic types match. The actual conversion is valid if:
// caught by ref if ref required *and*
// the qualifiers are compatible *and*
// the alignments match *and*
// the volatility matches
return (!CT_BYREFONLY(*pCatchable) || HT_ISREFERENCE(*pCatch))
&& (!THROW_ISCONST(*pThrow) || HT_ISCONST(*pCatch))
#if defined(_M_X64) || defined(_M_ARM_NT) /*IFSTRIP=IGN*/
&& (!THROW_ISUNALIGNED(*pThrow) || HT_ISUNALIGNED(*pCatch))
#endif
&& (!THROW_ISVOLATILE(*pThrow) || HT_ISVOLATILE(*pCatch));
}
////////////////////////////////////////////////////////////////////////////////
//
// __FrameUnwindFilter - Allows possibility of continuing through SEH during
// unwind.
//
extern "C" _CRTIMP int __cdecl __FrameUnwindFilter(
EXCEPTION_POINTERS *pExPtrs
) {
EHTRACE_ENTER;
EHExceptionRecord *pExcept = (EHExceptionRecord *)pExPtrs->ExceptionRecord;
switch (PER_CODE(pExcept)) {
case EH_EXCEPTION_NUMBER:
__ProcessingThrow = 0;
terminate();
#ifdef ALLOW_UNWIND_ABORT
case EH_ABORT_FRAME_UNWIND_PART:
EHTRACE_EXIT;
return EXCEPTION_EXECUTE_HANDLER;
#endif
case MANAGED_EXCEPTION_CODE:
case MANAGED_EXCEPTION_CODE_V4:
/*
See VSW#544593 for more details. __ProcessingThrow is used to implement
std::uncaught_exception(). The interaction between C++, SEH and managed
exception wrt __ProcessingThrow is unspec'ed. From code inspection, it
looks like that __ProcessingThrow works ok with all C++ exceptions.
In this case, when we encounter a managed exception thrown from a destructor
during unwind, we choose to decrement the count. This means that the previous
C++ exception which incremented the count won't be considered any longer.
In fact, the managed exception will be thrown, and the native C++ one will
not have any possibility to be catched any longer.
We should revisit std::uncaught_exception() and SEH/managed exception in the
next version.
*/
EHTRACE_EXIT;
if (__ProcessingThrow > 0)
{
--__ProcessingThrow;
}
return EXCEPTION_CONTINUE_SEARCH;
default:
EHTRACE_EXIT;
return EXCEPTION_CONTINUE_SEARCH;
}
}
////////////////////////////////////////////////////////////////////////////////
//
// __FrameUnwindToState - Unwind this frame until specified state is reached.
//
// Returns:
// No return value.
//
// Side Effects: