This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
clrex.cpp
2626 lines (2208 loc) · 79.5 KB
/
clrex.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
//
// ---------------------------------------------------------------------------
// Clrex.cpp
// ---------------------------------------------------------------------------
#include "common.h"
#include "clrex.h"
#include "field.h"
#include "eetoprofinterfacewrapper.inl"
#include "typestring.h"
#include "sigformat.h"
#include "eeconfig.h"
#include "frameworkexceptionloader.h"
#ifdef FEATURE_EH_FUNCLETS
#include "exceptionhandling.h"
#endif // FEATURE_EH_FUNCLETS
#ifdef FEATURE_COMINTEROP
#include "interoputil.inl"
#endif // FEATURE_COMINTEROP
// ---------------------------------------------------------------------------
// CLRException methods
// ---------------------------------------------------------------------------
CLRException::~CLRException()
{
CONTRACTL
{
GC_NOTRIGGER;
NOTHROW;
MODE_ANY;
if (GetThrowableHandle() == NULL)
{
CANNOT_TAKE_LOCK;
}
else
{
CAN_TAKE_LOCK; // because of DestroyHandle
}
}
CONTRACTL_END;
#ifndef CROSSGEN_COMPILE
OBJECTHANDLE throwableHandle = GetThrowableHandle();
if (throwableHandle != NULL)
{
STRESS_LOG1(LF_EH, LL_INFO100, "CLRException::~CLRException destroying throwable: obj = %x\n", GetThrowableHandle());
// clear the handle first, so if we SO on destroying it, we don't have a dangling reference
SetThrowableHandle(NULL);
DestroyHandle(throwableHandle);
}
#endif
}
OBJECTREF CLRException::GetThrowable()
{
CONTRACTL
{
GC_TRIGGERS;
NOTHROW;
MODE_COOPERATIVE;
FORBID_FAULT;
}
CONTRACTL_END;
#ifdef CROSSGEN_COMPILE
_ASSERTE(false);
return NULL;
#else
OBJECTREF throwable = NULL;
if (NingenEnabled())
{
return NULL;
}
Thread *pThread = GetThread();
if (pThread->IsRudeAbortInitiated()) {
return GetPreallocatedRudeThreadAbortException();
}
if ((IsType(CLRLastThrownObjectException::GetType()) &&
pThread->LastThrownObject() == GetPreallocatedStackOverflowException()))
{
return GetPreallocatedStackOverflowException();
}
OBJECTHANDLE oh = GetThrowableHandle();
if (oh != NULL)
{
return ObjectFromHandle(oh);
}
Exception *pLastException = pThread->m_pCreatingThrowableForException;
if (pLastException != NULL)
{
if (IsSameInstanceType(pLastException))
{
#if defined(_DEBUG)
static int BreakOnExceptionInGetThrowable = -1;
if (BreakOnExceptionInGetThrowable == -1)
{
BreakOnExceptionInGetThrowable = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_BreakOnExceptionInGetThrowable);
}
if (BreakOnExceptionInGetThrowable)
{
_ASSERTE(!"BreakOnExceptionInGetThrowable");
}
LOG((LF_EH, LL_INFO100, "GetThrowable: Exception in GetThrowable, translating to a preallocated exception.\n"));
#endif // _DEBUG
// Look at the type of GET_EXCEPTION() and see if it is OOM or SO.
if (IsPreallocatedOOMException())
{
throwable = GetPreallocatedOutOfMemoryException();
}
else if (GetInstanceType() == EEException::GetType() && GetHR() == COR_E_THREADABORTED)
{
// If creating a normal ThreadAbortException fails, due to OOM or StackOverflow,
// use a pre-created one.
// We do not won't to change a ThreadAbortException into OOM or StackOverflow, because
// it will cause recursive call when escalation policy is on:
// Creating ThreadAbortException fails, we throw OOM. Escalation leads to ThreadAbort.
// The cycle repeats.
throwable = GetPreallocatedThreadAbortException();
}
else
{
// I am not convinced if this case is actually a fatal error in the runtime.
// There have been two bugs in early 2006 (VSW 575647 and 575650) that came in here,
// both because of OOM and resulted in the ThreadAbort clause above being added since
// we were creating a ThreadAbort throwable that, due to OOM, got us on a path
// which came here. Both were valid execution paths and scenarios and not a fatal condition.
//
// I am tempted to return preallocated OOM from here but my concern is that it *may*
// result in fake OOM exceptions being thrown that could break valid scenarios.
//
// Hence, we return preallocated System.Exception instance. Lossy information is better
// than wrong or no information (or even FailFast).
_ASSERTE (!"Recursion in CLRException::GetThrowable");
// We didn't recognize it, so use the preallocated System.Exception instance.
STRESS_LOG0(LF_EH, LL_INFO100, "CLRException::GetThrowable: Recursion! Translating to preallocated System.Exception.\n");
throwable = GetPreallocatedBaseException();
}
}
}
GCPROTECT_BEGIN(throwable);
if (throwable == NULL)
{
class RestoreLastException
{
Thread *m_pThread;
Exception *m_pLastException;
public:
RestoreLastException(Thread *pThread, Exception *pException)
{
m_pThread = pThread;
m_pLastException = m_pThread->m_pCreatingThrowableForException;
m_pThread->m_pCreatingThrowableForException = pException;
}
~RestoreLastException()
{
m_pThread->m_pCreatingThrowableForException = m_pLastException;
}
};
RestoreLastException restore(pThread, this);
EX_TRY
{
FAULT_NOT_FATAL();
throwable = CreateThrowable();
}
EX_CATCH
{
// This code used to be this line:
// throwable = GET_THROWABLE();
// GET_THROWABLE() expands to CLRException::GetThrowable(GET_EXCEPTION()),
// (where GET_EXCEPTION() refers to the exception that was thrown from
// CreateThrowable() and is being caught in this EX_TRY/EX_CATCH.)
// If that exception is the same as the one for which this GetThrowable()
// was called, we're in a recursive situation.
// Since the CreateThrowable() call should return a type from mscorlib,
// there really shouldn't be much opportunity for error. We could be
// out of memory, we could overflow the stack, or the runtime could
// be in a weird state(the thread could be aborted as well).
// Because we've seen a number of recursive death bugs here, just look
// explicitly for OOM and SO, and otherwise use ExecutionEngineException.
// Check whether the exception from CreateThrowable() is the same as the current
// exception. If not, call GetThrowable(), otherwise, settle for a
// preallocated exception.
Exception *pException = GET_EXCEPTION();
if (GetHR() == COR_E_THREADABORTED)
{
// If creating a normal ThreadAbortException fails, due to OOM or StackOverflow,
// use a pre-created one.
// We do not won't to change a ThreadAbortException into OOM or StackOverflow, because
// it will cause recursive call when escalation policy is on:
// Creating ThreadAbortException fails, we throw OOM. Escalation leads to ThreadAbort.
// The cycle repeats.
throwable = GetPreallocatedThreadAbortException();
}
else
{
throwable = CLRException::GetThrowableFromException(pException);
}
}
EX_END_CATCH(SwallowAllExceptions)
}
{
if (throwable == NULL)
{
STRESS_LOG0(LF_EH, LL_INFO100, "CLRException::GetThrowable: We have failed to track exceptions accurately through the system.\n");
// There's no reason to believe that it is an OOM. A better choice is ExecutionEngineException.
// We have failed to track exceptions accurately through the system. However, it's arguably
// better to give the wrong exception object than it is to rip the process. So let's leave
// it as an Assert for now and convert it to ExecutionEngineException in the next release.
// SQL Stress is hitting the assert. We want to remove it, so that we can see if there are further errors
// masked by the assert.
// _ASSERTE(FALSE);
throwable = GetPreallocatedOutOfMemoryException();
}
EX_TRY
{
SetThrowableHandle(GetAppDomain()->CreateHandle(throwable));
if (m_innerException != NULL && !CLRException::IsPreallocatedExceptionObject(throwable))
{
// Only set inner exception if the exception is not preallocated.
FAULT_NOT_FATAL();
// If inner exception is not empty, then set the managed exception's
// _innerException field properly
OBJECTREF throwableValue = CLRException::GetThrowableFromException(m_innerException);
((EXCEPTIONREF)throwable)->SetInnerException(throwableValue);
}
}
EX_CATCH
{
// No matter... we just don't get to cache the throwable.
}
EX_END_CATCH(SwallowAllExceptions)
}
GCPROTECT_END();
return throwable;
#endif
}
HRESULT CLRException::GetHR()
{
CONTRACTL
{
DISABLED(NOTHROW);
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
GCX_COOP();
return GetExceptionHResult(GetThrowable());
}
#ifdef FEATURE_COMINTEROP
HRESULT CLRException::SetErrorInfo()
{
CONTRACTL
{
GC_TRIGGERS;
NOTHROW;
MODE_ANY;
}
CONTRACTL_END;
HRESULT hr = S_OK;
IErrorInfo *pErrorInfo = NULL;
// Try to get IErrorInfo
EX_TRY
{
pErrorInfo = GetErrorInfo();
}
EX_CATCH
{
// Since there was an exception getting IErrorInfo get the exception's HR so
// that we return it back to the caller as the new exception.
hr = GET_EXCEPTION()->GetHR();
pErrorInfo = NULL;
LOG((LF_EH, LL_INFO100, "CLRException::SetErrorInfo: caught exception (hr = %08X) while trying to get IErrorInfo\n", hr));
}
EX_END_CATCH(SwallowAllExceptions)
if (!pErrorInfo)
{
// Return the HR to the caller if we dont get IErrorInfo - if the HR is E_NOINTERFACE, then
// there was no IErrorInfo available. If its anything else, it implies we failed to get the
// interface and have the HR corresponding to the exception we took while trying to get IErrorInfo.
return hr;
}
else
{
GCX_PREEMP();
EX_TRY
{
::SetErrorInfo(0, pErrorInfo);
pErrorInfo->Release();
// Success in setting the ErrorInfo on the thread
hr = S_OK;
}
EX_CATCH
{
hr = GET_EXCEPTION()->GetHR();
// Log the failure
LOG((LF_EH, LL_INFO100, "CLRException::SetErrorInfo: caught exception (hr = %08X) while trying to set IErrorInfo\n", hr));
}
EX_END_CATCH(SwallowAllExceptions)
}
return hr;
}
IErrorInfo *CLRException::GetErrorInfo()
{
CONTRACTL
{
GC_TRIGGERS;
THROWS;
MODE_ANY;
}
CONTRACTL_END;
IErrorInfo *pErrorInfo = NULL;
#ifndef CROSSGEN_COMPILE
// Attempt to get IErrorInfo only if COM is initialized.
// Not all codepaths expect to have it initialized (e.g. hosting APIs).
if (g_fComStarted)
{
// Get errorinfo only when our SO probe succeeds
{
// Switch to coop mode since GetComIPFromObjectRef requires that
// and we could be here in any mode...
GCX_COOP();
OBJECTREF e = NULL;
GCPROTECT_BEGIN(e);
e = GetThrowable();
if (e != NULL)
{
pErrorInfo = (IErrorInfo *)GetComIPFromObjectRef(&e, IID_IErrorInfo);
}
GCPROTECT_END();
}
}
else
{
// Write to the log incase COM isnt initialized.
LOG((LF_EH, LL_INFO100, "CLRException::GetErrorInfo: exiting since COM is not initialized.\n"));
}
#endif //CROSSGEN_COMPILE
// return the IErrorInfo we got...
return pErrorInfo;
}
#else // FEATURE_COMINTEROP
IErrorInfo *CLRException::GetErrorInfo()
{
LIMITED_METHOD_CONTRACT;
return NULL;
}
HRESULT CLRException::SetErrorInfo()
{
LIMITED_METHOD_CONTRACT;
return S_OK;
}
#endif // FEATURE_COMINTEROP
void CLRException::GetMessage(SString &result)
{
CONTRACTL
{
GC_TRIGGERS;
THROWS;
MODE_ANY;
}
CONTRACTL_END;
#ifndef CROSSGEN_COMPILE
GCX_COOP();
OBJECTREF e = GetThrowable();
if (e != NULL)
{
_ASSERTE(IsException(e->GetMethodTable()));
GCPROTECT_BEGIN (e);
STRINGREF message = ((EXCEPTIONREF)e)->GetMessage();
if (!message)
result.Clear();
else
message->GetSString(result);
GCPROTECT_END ();
}
#endif
}
#ifndef CROSSGEN_COMPILE
OBJECTREF CLRException::GetPreallocatedBaseException()
{
WRAPPER_NO_CONTRACT;
_ASSERTE(g_pPreallocatedBaseException != NULL);
return ObjectFromHandle(g_pPreallocatedBaseException);
}
OBJECTREF CLRException::GetPreallocatedOutOfMemoryException()
{
WRAPPER_NO_CONTRACT;
_ASSERTE(g_pPreallocatedOutOfMemoryException != NULL);
return ObjectFromHandle(g_pPreallocatedOutOfMemoryException);
}
OBJECTREF CLRException::GetPreallocatedStackOverflowException()
{
WRAPPER_NO_CONTRACT;
_ASSERTE(g_pPreallocatedStackOverflowException != NULL);
return ObjectFromHandle(g_pPreallocatedStackOverflowException);
}
OBJECTREF CLRException::GetPreallocatedExecutionEngineException()
{
WRAPPER_NO_CONTRACT;
_ASSERTE(g_pPreallocatedExecutionEngineException != NULL);
return ObjectFromHandle(g_pPreallocatedExecutionEngineException);
}
OBJECTREF CLRException::GetPreallocatedRudeThreadAbortException()
{
WRAPPER_NO_CONTRACT;
// When we are hosted, we pre-create this exception.
// This function should be called only if the exception has been created.
_ASSERTE(g_pPreallocatedRudeThreadAbortException);
return ObjectFromHandle(g_pPreallocatedRudeThreadAbortException);
}
OBJECTREF CLRException::GetPreallocatedThreadAbortException()
{
WRAPPER_NO_CONTRACT;
_ASSERTE(g_pPreallocatedThreadAbortException);
return ObjectFromHandle(g_pPreallocatedThreadAbortException);
}
OBJECTHANDLE CLRException::GetPreallocatedOutOfMemoryExceptionHandle()
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(g_pPreallocatedOutOfMemoryException != NULL);
return g_pPreallocatedOutOfMemoryException;
}
OBJECTHANDLE CLRException::GetPreallocatedThreadAbortExceptionHandle()
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(g_pPreallocatedThreadAbortException != NULL);
return g_pPreallocatedThreadAbortException;
}
OBJECTHANDLE CLRException::GetPreallocatedRudeThreadAbortExceptionHandle()
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(g_pPreallocatedRudeThreadAbortException != NULL);
return g_pPreallocatedRudeThreadAbortException;
}
OBJECTHANDLE CLRException::GetPreallocatedStackOverflowExceptionHandle()
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(g_pPreallocatedStackOverflowException != NULL);
return g_pPreallocatedStackOverflowException;
}
OBJECTHANDLE CLRException::GetPreallocatedExecutionEngineExceptionHandle()
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(g_pPreallocatedExecutionEngineException != NULL);
return g_pPreallocatedExecutionEngineException;
}
//
// Returns TRUE if the given object ref is one of the preallocated exception objects.
//
BOOL CLRException::IsPreallocatedExceptionObject(OBJECTREF o)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_COOPERATIVE;
FORBID_FAULT;
}
CONTRACTL_END;
if ((o == ObjectFromHandle(g_pPreallocatedBaseException)) ||
(o == ObjectFromHandle(g_pPreallocatedOutOfMemoryException)) ||
(o == ObjectFromHandle(g_pPreallocatedStackOverflowException)) ||
(o == ObjectFromHandle(g_pPreallocatedExecutionEngineException)))
{
return TRUE;
}
// The preallocated rude thread abort exception is not always preallocated.
if ((g_pPreallocatedRudeThreadAbortException != NULL) &&
(o == ObjectFromHandle(g_pPreallocatedRudeThreadAbortException)))
{
return TRUE;
}
// The preallocated rude thread abort exception is not always preallocated.
if ((g_pPreallocatedThreadAbortException != NULL) &&
(o == ObjectFromHandle(g_pPreallocatedThreadAbortException)))
{
return TRUE;
}
return FALSE;
}
//
// Returns TRUE if the given object ref is one of the preallocated exception handles
//
BOOL CLRException::IsPreallocatedExceptionHandle(OBJECTHANDLE h)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
FORBID_FAULT;
}
CONTRACTL_END;
if ((h == g_pPreallocatedBaseException) ||
(h == g_pPreallocatedOutOfMemoryException) ||
(h == g_pPreallocatedStackOverflowException) ||
(h == g_pPreallocatedExecutionEngineException) ||
(h == g_pPreallocatedThreadAbortException))
{
return TRUE;
}
// The preallocated rude thread abort exception is not always preallocated.
if ((g_pPreallocatedRudeThreadAbortException != NULL) &&
(h == g_pPreallocatedRudeThreadAbortException))
{
return TRUE;
}
return FALSE;
}
//
// Returns a preallocated handle to match a preallocated exception object, or NULL if the object isn't one of the
// preallocated exception objects.
//
OBJECTHANDLE CLRException::GetPreallocatedHandleForObject(OBJECTREF o)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_COOPERATIVE;
FORBID_FAULT;
}
CONTRACTL_END;
if (o == ObjectFromHandle(g_pPreallocatedBaseException))
{
return g_pPreallocatedBaseException;
}
else if (o == ObjectFromHandle(g_pPreallocatedOutOfMemoryException))
{
return g_pPreallocatedOutOfMemoryException;
}
else if (o == ObjectFromHandle(g_pPreallocatedStackOverflowException))
{
return g_pPreallocatedStackOverflowException;
}
else if (o == ObjectFromHandle(g_pPreallocatedExecutionEngineException))
{
return g_pPreallocatedExecutionEngineException;
}
else if (o == ObjectFromHandle(g_pPreallocatedThreadAbortException))
{
return g_pPreallocatedThreadAbortException;
}
// The preallocated rude thread abort exception is not always preallocated.
if ((g_pPreallocatedRudeThreadAbortException != NULL) &&
(o == ObjectFromHandle(g_pPreallocatedRudeThreadAbortException)))
{
return g_pPreallocatedRudeThreadAbortException;
}
return NULL;
}
// Prefer a new OOM exception if we can make one. If we cannot, then give back the pre-allocated one.
OBJECTREF CLRException::GetBestOutOfMemoryException()
{
CONTRACTL
{
NOTHROW;
MODE_COOPERATIVE;
}
CONTRACTL_END;
OBJECTREF retVal = NULL;
EX_TRY
{
FAULT_NOT_FATAL();
EXCEPTIONREF pOutOfMemory = (EXCEPTIONREF)AllocateObject(g_pOutOfMemoryExceptionClass);
pOutOfMemory->SetHResult(COR_E_OUTOFMEMORY);
pOutOfMemory->SetXCode(EXCEPTION_COMPLUS);
retVal = pOutOfMemory;
}
EX_CATCH
{
retVal = GetPreallocatedOutOfMemoryException();
}
EX_END_CATCH(SwallowAllExceptions)
_ASSERTE(retVal != NULL);
return retVal;
}
// Works on non-CLRExceptions as well
// static function
OBJECTREF CLRException::GetThrowableFromException(Exception *pException)
{
CONTRACTL
{
GC_TRIGGERS;
NOTHROW;
MODE_COOPERATIVE;
}
CONTRACTL_END;
Thread* pThread = GetThread();
// Can't have a throwable without a Thread.
_ASSERTE(pThread != NULL);
if (NULL == pException)
{
return pThread->LastThrownObject();
}
if (pException->IsType(CLRException::GetType()))
return ((CLRException*)pException)->GetThrowable();
if (pException->IsType(EEException::GetType()))
return ((EEException*)pException)->GetThrowable();
// Note: we are creating a throwable on the fly in this case - so
// multiple calls will return different objects. If we really need identity,
// we could store a throwable handle at the catch site, or store it
// on the thread object.
if (pException->IsType(SEHException::GetType()))
{
SEHException *pSEHException = (SEHException*)pException;
switch (pSEHException->m_exception.ExceptionCode)
{
case EXCEPTION_COMPLUS:
// Note: even though the switch compared the exception code,
// we have to call the official IsComPlusException() routine
// for side-by-side correctness. If that check fails, treat
// as an unrelated unmanaged exception.
if (IsComPlusException(&(pSEHException->m_exception)))
{
return pThread->LastThrownObject();
}
else
{
break;
}
case STATUS_NO_MEMORY:
return GetBestOutOfMemoryException();
case STATUS_STACK_OVERFLOW:
return GetPreallocatedStackOverflowException();
}
DWORD exceptionCode =
MapWin32FaultToCOMPlusException(&pSEHException->m_exception);
EEException e((RuntimeExceptionKind)exceptionCode);
OBJECTREF throwable = e.GetThrowable();
GCPROTECT_BEGIN (throwable);
EX_TRY
{
SCAN_IGNORE_FAULT;
if (throwable != NULL && !CLRException::IsPreallocatedExceptionObject(throwable))
{
_ASSERTE(IsException(throwable->GetMethodTable()));
// set the exception code
((EXCEPTIONREF)throwable)->SetXCode(pSEHException->m_exception.ExceptionCode);
}
}
EX_CATCH
{
}
EX_END_CATCH(SwallowAllExceptions)
GCPROTECT_END ();
return throwable;
}
else
{
// We can enter here for HRException, COMException, DelegatingException
// just to name a few.
OBJECTREF oRetVal = NULL;
GCPROTECT_BEGIN(oRetVal);
{
EX_TRY
{
HRESULT hr = pException->GetHR();
if (hr == E_OUTOFMEMORY || hr == HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY))
{
oRetVal = GetBestOutOfMemoryException();
}
else if (hr == COR_E_STACKOVERFLOW)
{
oRetVal = GetPreallocatedStackOverflowException();
}
else
{
SafeComHolder<IErrorInfo> pErrInfo(pException->GetErrorInfo());
if (pErrInfo != NULL)
{
GetExceptionForHR(hr, pErrInfo, &oRetVal);
}
else
{
SString message;
pException->GetMessage(message);
EEMessageException e(hr, IDS_EE_GENERIC, message);
oRetVal = e.CreateThrowable();
}
}
}
EX_CATCH
{
// We have caught an exception trying to get a Throwable for the pException we
// were given. It is tempting to want to get the Throwable for the new
// exception, but that is dangerous, due to infinitely cascading
// exceptions, leading to a stack overflow.
// If we can see that the exception was OOM, return the preallocated OOM,
// if we can see that it is SO, return the preallocated SO,
// if we can see that it is some other managed exception, return that
// exception, otherwise return the preallocated System.Exception.
Exception *pNewException = GET_EXCEPTION();
if (pNewException->IsPreallocatedOOMException())
{ // It definitely was an OOM
STRESS_LOG0(LF_EH, LL_INFO100, "CLRException::GetThrowableFromException: OOM creating throwable; getting pre-alloc'd OOM.\n");
if (oRetVal == NULL)
oRetVal = GetPreallocatedOutOfMemoryException();
}
else
if (pNewException->IsType(CLRLastThrownObjectException::GetType()) &&
(pThread->LastThrownObject() != NULL))
{
STRESS_LOG0(LF_EH, LL_INFO100, "CLRException::GetThrowableFromException: LTO Exception creating throwable; getting LastThrownObject.\n");
if (oRetVal == NULL)
oRetVal = pThread->LastThrownObject();
}
else
{
// We *could* come here if one of the calls in the EX_TRY above throws an exception (e.g. MissingMethodException if we attempt
// to invoke CreateThrowable for a type that does not have a default constructor) that is neither preallocated OOM nor a
// CLRLastThrownObject type.
//
// Like the comment says above, we cannot afford to get the throwable lest we hit SO. In such a case, runtime is not in a bad shape
// but we dont know what to return as well. A reasonable answer is to return something less appropriate than ripping down process
// or returning an incorrect exception (e.g. OOM) that could break execution paths.
//
// Hence, we return preallocated System.Exception instance.
if (oRetVal == NULL)
{
oRetVal = GetPreallocatedBaseException();
STRESS_LOG0(LF_EH, LL_INFO100, "CLRException::GetThrowableFromException: Unknown Exception creating throwable; getting preallocated System.Exception.\n");
}
}
}
EX_END_CATCH(SwallowAllExceptions)
}
GCPROTECT_END();
return oRetVal;
}
} // OBJECTREF CLRException::GetThrowableFromException()
OBJECTREF CLRException::GetThrowableFromExceptionRecord(EXCEPTION_RECORD *pExceptionRecord)
{
CONTRACTL
{
GC_NOTRIGGER;
NOTHROW;
MODE_ANY;
}
CONTRACTL_END;
if (IsComPlusException(pExceptionRecord))
{
return GetThread()->LastThrownObject();
}
return NULL;
}
void CLRException::HandlerState::CleanupTry()
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_MODE_ANY;
if (m_pThread != NULL)
{
BEGIN_GETTHREAD_ALLOWED;
// If there is no frame to unwind, UnwindFrameChain call is just an expensive NOP
// due to setting up and tear down of EH records. So we avoid it if we can.
if (m_pThread->GetFrame() < m_pFrame)
UnwindFrameChain(m_pThread, m_pFrame);
if (m_fPreemptiveGCDisabled != m_pThread->PreemptiveGCDisabled())
{
if (m_fPreemptiveGCDisabled)
m_pThread->DisablePreemptiveGC();
else
m_pThread->EnablePreemptiveGC();
}
END_GETTHREAD_ALLOWED;
}
// Make sure to call the base class's CleanupTry so it can do whatever it wants to do.
Exception::HandlerState::CleanupTry();
}
void CLRException::HandlerState::SetupCatch(INDEBUG_COMMA(__in_z const char * szFile) int lineNum)
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_MODE_ANY;
STATIC_CONTRACT_CANNOT_TAKE_LOCK;
Exception::HandlerState::SetupCatch(INDEBUG_COMMA(szFile) lineNum);
Thread *pThread = NULL;
DWORD exceptionCode = 0;
if (g_fEEStarted)
{
pThread = GetThread();
exceptionCode = GetCurrentExceptionCode();
}
if (!DidCatchCxx())
{
if (exceptionCode == STATUS_STACK_OVERFLOW)
{
// Handle SO exception
//
// We should ensure that a valid Thread object exists before trying to set SO as the LTO.
if (pThread != NULL)
{
// We have a nasty issue with our EX_TRY/EX_CATCH. If EX_CATCH catches SEH exception,
// GET_THROWABLE uses CLRLastThrownObjectException instead, because we don't know
// what exception to use. But for SO, we can use preallocated SO exception.
GCX_COOP();
pThread->SetSOForLastThrownObject();
}
if (exceptionCode == STATUS_STACK_OVERFLOW)
{
// We have called HandleStackOverflow for soft SO through our vectored exception handler.
EEPolicy::HandleStackOverflow(SOD_UnmanagedFrameHandler, FRAME_TOP);
}
}
}
#ifdef FEATURE_EH_FUNCLETS
if (!DidCatchCxx())
{
// this must be done after the second pass has run, it does not
// reference anything on the stack, so it is safe to run in an
// SEH __except clause as well as a C++ catch clause.
ExceptionTracker::PopTrackers(this);
}
#endif // FEATURE_EH_FUNCLETS
}
#ifdef LOGGING
void CLRException::HandlerState::SucceedCatch()
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_MODE_ANY;
STATIC_CONTRACT_CANNOT_TAKE_LOCK;
LOG((LF_EH, LL_INFO100, "EX_CATCH catch succeeded (CLRException::HandlerState)\n"));
//
// At this point, we don't believe we need to do any unwinding of the ExInfo chain after an EX_CATCH. The chain
// is unwound by CPFH_UnwindFrames1() when it detects that the exception is being caught by an unmanaged
// catcher. EX_CATCH looks just like an unmanaged catcher now, so the unwind is already done by the time we get
// into the catch. That's different than before the big switch to the new exeption system, and it effects
// rethrows. Fixing rethrows is a work item for a little later. For now, we're simplying removing the unwind
// from here to avoid the extra unwind, which is harmless in many cases, but is very harmful when a managed
// filter throws an exception.
//
//
Exception::HandlerState::SucceedCatch();
}
#endif
#endif // CROSSGEN_COMPILE
// ---------------------------------------------------------------------------
// EEException methods
// ---------------------------------------------------------------------------
//------------------------------------------------------------------------
// Array that is used to retrieve the right exception for a given HRESULT.
//------------------------------------------------------------------------
#ifdef FEATURE_COMINTEROP
struct WinRtHR_to_ExceptionKind_Map
{
RuntimeExceptionKind reKind;
int cHRs;
const HRESULT *aHRs;
};
enum WinRtOnly_ExceptionKind {
#define DEFINE_EXCEPTION_HR_WINRT_ONLY(ns, reKind, ...) kWinRtEx##reKind,
#define DEFINE_EXCEPTION(ns, reKind, bHRformessage, ...)
#define DEFINE_EXCEPTION_IN_OTHER_FX_ASSEMBLY(ns, reKind, assemblySimpleName, publicKeyToken, bHRformessage, ...)
#include "rexcep.h"
kWinRtExLastException
};
#define DEFINE_EXCEPTION_HR_WINRT_ONLY(ns, reKind, ...) static const HRESULT s_##reKind##WinRtOnlyHRs[] = { __VA_ARGS__ };
#define DEFINE_EXCEPTION(ns, reKind, bHRformessage, ...)
#define DEFINE_EXCEPTION_IN_OTHER_FX_ASSEMBLY(ns, reKind, assemblySimpleName, publicKeyToken, bHRformessage, ...)