forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackwalk.cpp
3404 lines (2928 loc) · 137 KB
/
stackwalk.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.
// STACKWALK.CPP
#include "common.h"
#include "frames.h"
#include "threads.h"
#include "stackwalk.h"
#include "excep.h"
#include "eetwain.h"
#include "codeman.h"
#include "eeconfig.h"
#include "dbginterface.h"
#include "generics.h"
#ifdef FEATURE_INTERPRETER
#include "interpreter.h"
#endif // FEATURE_INTERPRETER
#include "gcinfodecoder.h"
#ifdef FEATURE_EH_FUNCLETS
#define PROCESS_EXPLICIT_FRAME_BEFORE_MANAGED_FRAME
#endif
#include "exinfo.h"
CrawlFrame::CrawlFrame()
{
LIMITED_METHOD_DAC_CONTRACT;
pCurGSCookie = NULL;
pFirstGSCookie = NULL;
}
Assembly* CrawlFrame::GetAssembly()
{
WRAPPER_NO_CONTRACT;
Assembly *pAssembly = NULL;
Frame *pF = GetFrame();
if (pF != NULL)
pAssembly = pF->GetAssembly();
if (pAssembly == NULL && pFunc != NULL)
pAssembly = pFunc->GetModule()->GetAssembly();
return pAssembly;
}
BOOL CrawlFrame::IsInCalleesFrames(LPVOID stackPointer)
{
LIMITED_METHOD_CONTRACT;
#ifdef FEATURE_INTERPRETER
Frame* pFrm = GetFrame();
if (pFrm != NULL && pFrm->GetVTablePtr() == InterpreterFrame::GetMethodFrameVPtr())
{
#ifdef DACCESS_COMPILE
// TBD: DACize the interpreter.
return NULL;
#else
return dac_cast<PTR_InterpreterFrame>(pFrm)->GetInterpreter()->IsInCalleesFrames(stackPointer);
#endif
}
else if (pFunc != NULL)
{
return ::IsInCalleesFrames(GetRegisterSet(), stackPointer);
}
else
{
return FALSE;
}
#else
return ::IsInCalleesFrames(GetRegisterSet(), stackPointer);
#endif
}
#ifdef FEATURE_INTERPRETER
MethodDesc* CrawlFrame::GetFunction()
{
LIMITED_METHOD_DAC_CONTRACT;
if (pFunc != NULL)
{
return pFunc;
}
else
{
Frame* pFrm = GetFrame();
if (pFrm != NULL && pFrm->GetVTablePtr() == InterpreterFrame::GetMethodFrameVPtr())
{
#ifdef DACCESS_COMPILE
// TBD: DACize the interpreter.
return NULL;
#else
return dac_cast<PTR_InterpreterFrame>(pFrm)->GetInterpreter()->GetMethodDesc();
#endif
}
else
{
return NULL;
}
}
}
#endif // FEATURE_INTERPRETER
OBJECTREF CrawlFrame::GetThisPointer()
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
MODE_COOPERATIVE;
SUPPORTS_DAC;
} CONTRACTL_END;
if (!pFunc || pFunc->IsStatic() || pFunc->GetMethodTable()->IsValueType())
return NULL;
// As discussed in the specification comment at the declaration, the precondition, unfortunately,
// differs by architecture. @TODO: fix this.
#if defined(TARGET_X86)
_ASSERTE_MSG((pFunc->IsSharedByGenericInstantiations() && pFunc->AcquiresInstMethodTableFromThis())
|| pFunc->IsSynchronized(),
"Precondition");
#else
_ASSERTE_MSG(pFunc->IsSharedByGenericInstantiations() && pFunc->AcquiresInstMethodTableFromThis(), "Precondition");
#endif
if (isFrameless)
{
return GetCodeManager()->GetInstance(pRD,
&codeInfo);
}
else
{
_ASSERTE(pFrame);
_ASSERTE(pFunc);
/*ISSUE: we already know that we have (at least) a method */
/* might need adjustment as soon as we solved the
jit-helper frame question
*/
//<TODO>@TODO: What about other calling conventions?
// _ASSERT(pFunc()->GetCallSig()->CALLING CONVENTION);</TODO>
#ifdef TARGET_AMD64
// @TODO: PORT: we need to find the this pointer without triggering a GC
// or find a way to make this method GC_TRIGGERS
return NULL;
#else
return (dac_cast<PTR_FramedMethodFrame>(pFrame))->GetThis();
#endif // TARGET_AMD64
}
}
//-----------------------------------------------------------------------------
// Get the "Ambient SP" from a CrawlFrame.
// This will be null if there is no Ambient SP (eg, in the prolog / epilog,
// or on certain platforms),
//-----------------------------------------------------------------------------
TADDR CrawlFrame::GetAmbientSPFromCrawlFrame()
{
SUPPORTS_DAC;
#if defined(TARGET_X86)
// we set nesting level to zero because it won't be used for esp-framed methods,
// and zero is at least valid for ebp based methods (where we won't use the ambient esp anyways)
DWORD nestingLevel = 0;
return GetCodeManager()->GetAmbientSP(
GetRegisterSet(),
GetCodeInfo(),
GetRelOffset(),
nestingLevel,
GetCodeManState()
);
#elif defined(TARGET_ARM)
return GetRegisterSet()->pCurrentContext->Sp;
#else
return NULL;
#endif
}
PTR_VOID CrawlFrame::GetParamTypeArg()
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
SUPPORTS_DAC;
} CONTRACTL_END;
if (isFrameless)
{
return GetCodeManager()->GetParamTypeArg(pRD,
&codeInfo);
}
else
{
#ifdef FEATURE_INTERPRETER
if (pFrame != NULL && pFrame->GetVTablePtr() == InterpreterFrame::GetMethodFrameVPtr())
{
#ifdef DACCESS_COMPILE
// TBD: DACize the interpreter.
return NULL;
#else
return dac_cast<PTR_InterpreterFrame>(pFrame)->GetInterpreter()->GetParamTypeArg();
#endif
}
// Otherwise...
#endif // FEATURE_INTERPRETER
if (!pFunc || !pFunc->RequiresInstArg())
{
return NULL;
}
#ifdef HOST_64BIT
if (!pFunc->IsSharedByGenericInstantiations() ||
!(pFunc->RequiresInstMethodTableArg() || pFunc->RequiresInstMethodDescArg()))
{
// win64 can only return the param type arg if the method is shared code
// and actually has a param type arg
return NULL;
}
#endif // HOST_64BIT
_ASSERTE(pFrame);
_ASSERTE(pFunc);
return (dac_cast<PTR_FramedMethodFrame>(pFrame))->GetParamTypeArg();
}
}
// [pClassInstantiation] : Always filled in, though may be set to NULL if no inst.
// [pMethodInst] : Always filled in, though may be set to NULL if no inst.
void CrawlFrame::GetExactGenericInstantiations(Instantiation *pClassInst,
Instantiation *pMethodInst)
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(CheckPointer(pClassInst));
PRECONDITION(CheckPointer(pMethodInst));
} CONTRACTL_END;
TypeHandle specificClass;
MethodDesc* specificMethod;
BOOL ret = Generics::GetExactInstantiationsOfMethodAndItsClassFromCallInformation(
GetFunction(),
GetExactGenericArgsToken(),
&specificClass,
&specificMethod);
if (!ret)
{
_ASSERTE(!"Cannot return exact class instantiation when we are requested to.");
}
*pClassInst = specificMethod->GetExactClassInstantiation(specificClass);
*pMethodInst = specificMethod->GetMethodInstantiation();
}
PTR_VOID CrawlFrame::GetExactGenericArgsToken()
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
SUPPORTS_DAC;
} CONTRACTL_END;
MethodDesc* pFunc = GetFunction();
if (!pFunc || !pFunc->IsSharedByGenericInstantiations())
return NULL;
if (pFunc->AcquiresInstMethodTableFromThis())
{
OBJECTREF obj = GetThisPointer();
if (obj == NULL)
return NULL;
return obj->GetMethodTable();
}
else
{
_ASSERTE(pFunc->RequiresInstArg());
return GetParamTypeArg();
}
}
/* Is this frame at a safe spot for GC?
*/
bool CrawlFrame::IsGcSafe()
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
SUPPORTS_DAC;
} CONTRACTL_END;
return GetCodeManager()->IsGcSafe(&codeInfo, GetRelOffset());
}
#if defined(TARGET_ARM) || defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
bool CrawlFrame::HasTailCalls()
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
SUPPORTS_DAC;
} CONTRACTL_END;
return GetCodeManager()->HasTailCalls(&codeInfo);
}
#endif // TARGET_ARM || TARGET_ARM64 || TARGET_LOONGARCH64 || TARGET_RISCV64
inline void CrawlFrame::GotoNextFrame()
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
SUPPORTS_DAC;
} CONTRACTL_END;
//
// Update app domain if this frame caused a transition
//
pFrame = pFrame->Next();
if (pFrame != FRAME_TOP)
{
SetCurGSCookie(Frame::SafeGetGSCookiePtr(pFrame));
}
}
//******************************************************************************
// For asynchronous stackwalks, the thread being walked may not be suspended.
// It could cause a buffer-overrun while the stack-walk is in progress.
// To detect this, we can only use data that is guarded by a GSCookie
// that has been recently checked.
// This function should be called after doing any time-consuming activity
// during stack-walking to reduce the window in which a buffer-overrun
// could cause an problems.
//
// To keep things simple, we do this checking even for synchronous stack-walks.
void CrawlFrame::CheckGSCookies()
{
WRAPPER_NO_CONTRACT;
SUPPORTS_DAC;
#if !defined(DACCESS_COMPILE)
if (pFirstGSCookie == NULL)
return;
if (*pFirstGSCookie != GetProcessGSCookie())
DoJITFailFast();
if(*pCurGSCookie != GetProcessGSCookie())
DoJITFailFast();
#endif // !DACCESS_COMPILE
}
void CrawlFrame::SetCurGSCookie(GSCookie * pGSCookie)
{
WRAPPER_NO_CONTRACT;
SUPPORTS_DAC;
#if !defined(DACCESS_COMPILE)
if (pGSCookie == NULL)
DoJITFailFast();
pCurGSCookie = pGSCookie;
if (pFirstGSCookie == NULL)
pFirstGSCookie = pGSCookie;
CheckGSCookies();
#endif // !DACCESS_COMPILE
}
#if defined(FEATURE_EH_FUNCLETS)
bool CrawlFrame::IsFilterFunclet()
{
WRAPPER_NO_CONTRACT;
if (!IsFrameless())
{
return false;
}
if (!isFilterFuncletCached)
{
isFilterFunclet = GetJitManager()->IsFilterFunclet(&codeInfo) != 0;
isFilterFuncletCached = true;
}
return isFilterFunclet;
}
#endif // FEATURE_EH_FUNCLETS
//******************************************************************************
#if defined(ELIMINATE_FEF)
//******************************************************************************
// Advance to the next ExInfo. Typically done when an ExInfo has been used and
// should not be used again.
//******************************************************************************
void ExInfoWalker::WalkOne()
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
if (m_pExInfo)
{
LOG((LF_EH, LL_INFO10000, "ExInfoWalker::WalkOne: advancing ExInfo chain: pExInfo:%p, pContext:%p; prev:%p, pContext:%p\n",
m_pExInfo, m_pExInfo->m_pContext, m_pExInfo->m_pPrevNestedInfo, m_pExInfo->m_pPrevNestedInfo?m_pExInfo->m_pPrevNestedInfo->m_pContext:0));
m_pExInfo = m_pExInfo->m_pPrevNestedInfo;
}
} // void ExInfoWalker::WalkOne()
//******************************************************************************
// Attempt to find an ExInfo with a pContext that is higher (older) than
// a given minimum location. (It is the pContext's SP that is relevant.)
//******************************************************************************
void ExInfoWalker::WalkToPosition(
TADDR taMinimum, // Starting point of stack walk.
BOOL bPopFrames) // If true, ResetUseExInfoForStackwalk on each exinfo.
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
while (m_pExInfo &&
((GetSPFromContext() < taMinimum) ||
(GetSPFromContext() == NULL)) )
{
// Try the next ExInfo, if there is one.
LOG((LF_EH, LL_INFO10000,
"ExInfoWalker::WalkToPosition: searching ExInfo chain: m_pExInfo:%p, pContext:%p; \
prev:%p, pContext:%p; pStartFrame:%p\n",
m_pExInfo,
m_pExInfo->m_pContext,
m_pExInfo->m_pPrevNestedInfo,
(m_pExInfo->m_pPrevNestedInfo ? m_pExInfo->m_pPrevNestedInfo->m_pContext : 0),
taMinimum));
if (bPopFrames)
{ // If caller asked for it, reset the bit which indicates that this ExInfo marks a fault from managed code.
// This is done so that the fault can be effectively "unwound" from the stack, similarly to how Frames
// are unlinked from the Frame chain.
m_pExInfo->m_ExceptionFlags.ResetUseExInfoForStackwalk();
}
m_pExInfo = m_pExInfo->m_pPrevNestedInfo;
}
// At this point, m_pExInfo is NULL, or points to a pContext that is greater than taMinimum.
} // void ExInfoWalker::WalkToPosition()
//******************************************************************************
// Attempt to find an ExInfo with a pContext that has an IP in managed code.
//******************************************************************************
void ExInfoWalker::WalkToManaged()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
SUPPORTS_DAC;
}
CONTRACTL_END;
while (m_pExInfo)
{
// See if the current ExInfo has a CONTEXT that "returns" to managed code, and, if so, exit the loop.
if (m_pExInfo->m_ExceptionFlags.UseExInfoForStackwalk() &&
GetContext() &&
ExecutionManager::IsManagedCode(GetIP(GetContext())))
{
break;
}
// No, so skip to next, if any.
LOG((LF_EH, LL_INFO1000, "ExInfoWalker::WalkToManaged: searching for ExInfo->managed: m_pExInfo:%p, pContext:%p, sp:%p; prev:%p, pContext:%p\n",
m_pExInfo,
GetContext(),
GetSPFromContext(),
m_pExInfo->m_pPrevNestedInfo,
m_pExInfo->m_pPrevNestedInfo?m_pExInfo->m_pPrevNestedInfo->m_pContext:0));
m_pExInfo = m_pExInfo->m_pPrevNestedInfo;
}
// At this point, m_pExInfo is NULL, or points to a pContext that has an IP in managed code.
} // void ExInfoWalker::WalkToManaged()
#endif // defined(ELIMINATE_FEF)
#ifdef FEATURE_EH_FUNCLETS
// static
UINT_PTR Thread::VirtualUnwindCallFrame(PREGDISPLAY pRD, EECodeInfo* pCodeInfo /*= NULL*/)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(GetControlPC(pRD) == GetIP(pRD->pCurrentContext));
}
CONTRACTL_END;
if (pRD->IsCallerContextValid)
{
// We already have the caller's frame context
// We just switch the pointers
PT_CONTEXT temp = pRD->pCurrentContext;
pRD->pCurrentContext = pRD->pCallerContext;
pRD->pCallerContext = temp;
PT_KNONVOLATILE_CONTEXT_POINTERS tempPtrs = pRD->pCurrentContextPointers;
pRD->pCurrentContextPointers = pRD->pCallerContextPointers;
pRD->pCallerContextPointers = tempPtrs;
}
else
{
VirtualUnwindCallFrame(pRD->pCurrentContext, pRD->pCurrentContextPointers, pCodeInfo);
}
SyncRegDisplayToCurrentContext(pRD);
pRD->IsCallerContextValid = FALSE;
pRD->IsCallerSPValid = FALSE; // Don't add usage of this field. This is only temporary.
return pRD->ControlPC;
}
// static
PCODE Thread::VirtualUnwindCallFrame(T_CONTEXT* pContext,
T_KNONVOLATILE_CONTEXT_POINTERS* pContextPointers /*= NULL*/,
EECodeInfo * pCodeInfo /*= NULL*/)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(CheckPointer(pContext, NULL_NOT_OK));
PRECONDITION(CheckPointer(pContextPointers, NULL_OK));
SUPPORTS_DAC;
}
CONTRACTL_END;
PCODE uControlPc = GetIP(pContext);
#if !defined(DACCESS_COMPILE)
UINT_PTR uImageBase;
PT_RUNTIME_FUNCTION pFunctionEntry;
if (pCodeInfo == NULL)
{
#ifndef TARGET_UNIX
pFunctionEntry = RtlLookupFunctionEntry(uControlPc,
ARM_ONLY((DWORD*))(&uImageBase),
NULL);
#else // !TARGET_UNIX
EECodeInfo codeInfo;
codeInfo.Init(uControlPc);
pFunctionEntry = codeInfo.GetFunctionEntry();
uImageBase = (UINT_PTR)codeInfo.GetModuleBase();
#endif // !TARGET_UNIX
}
else
{
pFunctionEntry = pCodeInfo->GetFunctionEntry();
uImageBase = (UINT_PTR)pCodeInfo->GetModuleBase();
// RUNTIME_FUNCTION of cold code just points to the RUNTIME_FUNCTION of hot code. The unwinder
// expects this indirection to be resolved, so we use RUNTIME_FUNCTION of the hot code even
// if we are in cold code.
#if defined(_DEBUG) && !defined(TARGET_UNIX)
UINT_PTR uImageBaseFromOS;
PT_RUNTIME_FUNCTION pFunctionEntryFromOS;
pFunctionEntryFromOS = RtlLookupFunctionEntry(uControlPc,
ARM_ONLY((DWORD*))(&uImageBaseFromOS),
NULL);
// Note that he address returned from the OS is different from the one we have computed
// when unwind info is registered using RtlAddGrowableFunctionTable. Compare RUNTIME_FUNCTION content.
_ASSERTE( (uImageBase == uImageBaseFromOS) && (memcmp(pFunctionEntry, pFunctionEntryFromOS, sizeof(RUNTIME_FUNCTION)) == 0) );
#endif // _DEBUG && !TARGET_UNIX
}
if (pFunctionEntry)
{
uControlPc = VirtualUnwindNonLeafCallFrame(pContext, pContextPointers, pFunctionEntry, uImageBase);
}
else
{
uControlPc = VirtualUnwindLeafCallFrame(pContext);
}
#else // DACCESS_COMPILE
// We can't use RtlVirtualUnwind() from out-of-process. Instead, we call code:DacUnwindStackFrame,
// which is similar to StackWalk64().
if (DacUnwindStackFrame(pContext, pContextPointers) == TRUE)
{
uControlPc = GetIP(pContext);
}
else
{
ThrowHR(CORDBG_E_TARGET_INCONSISTENT);
}
#endif // !DACCESS_COMPILE
return uControlPc;
}
#ifndef DACCESS_COMPILE
// static
PCODE Thread::VirtualUnwindLeafCallFrame(T_CONTEXT* pContext)
{
PCODE uControlPc;
#if defined(_DEBUG) && !defined(TARGET_UNIX)
UINT_PTR uImageBase;
PT_RUNTIME_FUNCTION pFunctionEntry = RtlLookupFunctionEntry((UINT_PTR)GetIP(pContext),
ARM_ONLY((DWORD*))(&uImageBase),
NULL);
CONSISTENCY_CHECK(NULL == pFunctionEntry);
#endif // _DEBUG && !TARGET_UNIX
#if defined(TARGET_AMD64)
uControlPc = *(ULONGLONG*)pContext->Rsp;
pContext->Rsp += sizeof(ULONGLONG);
#ifdef TARGET_WINDOWS
DWORD64 ssp = GetSSP(pContext);
if (ssp != 0)
{
SetSSP(pContext, ssp + sizeof(ULONGLONG));
}
#endif // TARGET_WINDOWS
#elif defined(TARGET_ARM) || defined(TARGET_ARM64)
uControlPc = TADDR(pContext->Lr);
#elif defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
uControlPc = TADDR(pContext->Ra);
#else
PORTABILITY_ASSERT("Thread::VirtualUnwindLeafCallFrame");
uControlPc = NULL;
#endif
SetIP(pContext, uControlPc);
return uControlPc;
}
// static
PCODE Thread::VirtualUnwindNonLeafCallFrame(T_CONTEXT* pContext, KNONVOLATILE_CONTEXT_POINTERS* pContextPointers,
PT_RUNTIME_FUNCTION pFunctionEntry, UINT_PTR uImageBase)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(CheckPointer(pContext, NULL_NOT_OK));
PRECONDITION(CheckPointer(pContextPointers, NULL_OK));
PRECONDITION(CheckPointer(pFunctionEntry, NULL_OK));
}
CONTRACTL_END;
PCODE uControlPc = GetIP(pContext);
#ifdef HOST_64BIT
UINT64 EstablisherFrame;
#else // HOST_64BIT
DWORD EstablisherFrame;
#endif // HOST_64BIT
PVOID HandlerData;
if (NULL == pFunctionEntry)
{
#ifndef TARGET_UNIX
pFunctionEntry = RtlLookupFunctionEntry(uControlPc,
ARM_ONLY((DWORD*))(&uImageBase),
NULL);
#endif
if (NULL == pFunctionEntry)
{
return NULL;
}
}
RtlVirtualUnwind(NULL,
uImageBase,
uControlPc,
pFunctionEntry,
pContext,
&HandlerData,
&EstablisherFrame,
pContextPointers);
uControlPc = GetIP(pContext);
return uControlPc;
}
extern void* g_hostingApiReturnAddress;
// static
UINT_PTR Thread::VirtualUnwindToFirstManagedCallFrame(T_CONTEXT* pContext)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
PCODE uControlPc = GetIP(pContext);
// unwind out of this function and out of our caller to
// get our caller's PSP, or our caller's caller's SP.
while (!ExecutionManager::IsManagedCode(uControlPc))
{
if (IsIPInWriteBarrierCodeCopy(uControlPc))
{
// Pretend we were executing the barrier function at its original location so that the unwinder can unwind the frame
uControlPc = AdjustWriteBarrierIP(uControlPc);
SetIP(pContext, uControlPc);
}
#ifndef TARGET_UNIX
uControlPc = VirtualUnwindCallFrame(pContext);
#else // !TARGET_UNIX
if (AdjustContextForVirtualStub(NULL, pContext))
{
uControlPc = GetIP(pContext);
break;
}
BOOL success = PAL_VirtualUnwind(pContext, NULL);
if (!success)
{
_ASSERTE(!"Thread::VirtualUnwindToFirstManagedCallFrame: PAL_VirtualUnwind failed");
EEPOLICY_HANDLE_FATAL_ERROR(COR_E_EXECUTIONENGINE);
}
uControlPc = GetIP(pContext);
if ((uControlPc == 0) || (uControlPc == (PCODE)g_hostingApiReturnAddress))
{
uControlPc = 0;
break;
}
#endif // !TARGET_UNIX
}
return uControlPc;
}
#endif // !DACCESS_COMPILE
#endif // FEATURE_EH_FUNCLETS
#ifdef _DEBUG
void Thread::DebugLogStackWalkInfo(CrawlFrame* pCF, _In_z_ LPCSTR pszTag, UINT32 uFramesProcessed)
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
if (pCF->isFrameless)
{
LPCSTR pszType = "";
#ifdef FEATURE_EH_FUNCLETS
if (pCF->IsFunclet())
{
pszType = "[funclet]";
}
else
#endif // FEATURE_EH_FUNCLETS
if (pCF->pFunc->IsNoMetadata())
{
pszType = "[no metadata]";
}
LOG((LF_GCROOTS, LL_INFO10000, "STACKWALK: [%03x] %s: FRAMELESS: PC=" FMT_ADDR " SP=" FMT_ADDR " method=%s %s\n",
uFramesProcessed,
pszTag,
DBG_ADDR(GetControlPC(pCF->pRD)),
DBG_ADDR(GetRegdisplaySP(pCF->pRD)),
pCF->pFunc->m_pszDebugMethodName,
pszType));
}
else if (pCF->isNativeMarker)
{
LOG((LF_GCROOTS, LL_INFO10000, "STACKWALK: [%03x] %s: NATIVE : PC=" FMT_ADDR " SP=" FMT_ADDR "\n",
uFramesProcessed,
pszTag,
DBG_ADDR(GetControlPC(pCF->pRD)),
DBG_ADDR(GetRegdisplaySP(pCF->pRD))));
}
else if (pCF->isNoFrameTransition)
{
LOG((LF_GCROOTS, LL_INFO10000, "STACKWALK: [%03x] %s: NO_FRAME : PC=" FMT_ADDR " SP=" FMT_ADDR "\n",
uFramesProcessed,
pszTag,
DBG_ADDR(GetControlPC(pCF->pRD)),
DBG_ADDR(GetRegdisplaySP(pCF->pRD))));
}
else
{
LOG((LF_GCROOTS, LL_INFO10000, "STACKWALK: [%03x] %s: EXPLICIT : PC=" FMT_ADDR " SP=" FMT_ADDR " Frame=" FMT_ADDR" vtbl=" FMT_ADDR "\n",
uFramesProcessed,
pszTag,
DBG_ADDR(GetControlPC(pCF->pRD)),
DBG_ADDR(GetRegdisplaySP(pCF->pRD)),
DBG_ADDR(pCF->pFrame),
DBG_ADDR((pCF->pFrame != FRAME_TOP) ? pCF->pFrame->GetVTablePtr() : NULL)));
}
}
#endif // _DEBUG
StackWalkAction Thread::MakeStackwalkerCallback(
CrawlFrame* pCF,
PSTACKWALKFRAMESCALLBACK pCallback,
VOID* pData
DEBUG_ARG(UINT32 uFramesProcessed))
{
INDEBUG(DebugLogStackWalkInfo(pCF, "CALLBACK", uFramesProcessed));
// Since we may be asynchronously walking another thread's stack,
// check (frequently) for stack-buffer-overrun corruptions
pCF->CheckGSCookies();
// Since the stackwalker callback may execute arbitrary managed code and possibly
// not even return (in the case of exception unwinding), explicitly clear the
// stackwalker thread state indicator around the callback.
CLEAR_THREAD_TYPE_STACKWALKER();
StackWalkAction swa = pCallback(pCF, (VOID*)pData);
SET_THREAD_TYPE_STACKWALKER(this);
pCF->CheckGSCookies();
#ifdef _DEBUG
if (swa == SWA_ABORT)
{
LOG((LF_GCROOTS, LL_INFO10000, "STACKWALK: SWA_ABORT: callback aborted the stackwalk\n"));
}
#endif // _DEBUG
return swa;
}
#if !defined(DACCESS_COMPILE) && defined(TARGET_X86) && !defined(FEATURE_EH_FUNCLETS)
#define STACKWALKER_MAY_POP_FRAMES
#endif
StackWalkAction Thread::StackWalkFramesEx(
PREGDISPLAY pRD, // virtual register set at crawl start
PSTACKWALKFRAMESCALLBACK pCallback,
VOID *pData,
unsigned flags,
PTR_Frame pStartFrame
)
{
// Note: there are cases (i.e., exception handling) where we may never return from this function. This means
// that any C++ destructors pushed in this function will never execute, and it means that this function can
// never have a dynamic contract.
STATIC_CONTRACT_WRAPPER;
SCAN_IGNORE_THROW; // see contract above
SCAN_IGNORE_TRIGGER; // see contract above
_ASSERTE(pRD);
_ASSERTE(pCallback);
// when POPFRAMES we don't want to allow GC trigger.
// The only method that guarantees this now is COMPlusUnwindCallback
#ifdef STACKWALKER_MAY_POP_FRAMES
ASSERT(!(flags & POPFRAMES) || pCallback == (PSTACKWALKFRAMESCALLBACK) COMPlusUnwindCallback);
ASSERT(!(flags & POPFRAMES) || pRD->pContextForUnwind != NULL);
ASSERT(!(flags & POPFRAMES) || (this == GetThread() && PreemptiveGCDisabled()));
#else // STACKWALKER_MAY_POP_FRAMES
ASSERT(!(flags & POPFRAMES));
#endif // STACKWALKER_MAY_POP_FRAMES
// We haven't set the stackwalker thread type flag yet, so it shouldn't be set. Only
// exception to this is if the current call is made by a hijacking profiler which
// redirected this thread while it was previously in the middle of another stack walk
#ifdef PROFILING_SUPPORTED
_ASSERTE(CORProfilerStackSnapshotEnabled() || !IsStackWalkerThread());
#else
_ASSERTE(!IsStackWalkerThread());
#endif
StackWalkAction retVal = SWA_FAILED;
{
// SCOPE: Remember that we're walking the stack.
//
// Normally, we'd use a StackWalkerWalkingThreadHolder to temporarily set this
// flag in the thread state, but we can't in this function, since C++ destructors
// are forbidden when this is called for exception handling (which causes
// MakeStackwalkerCallback() not to return). Note that in exception handling
// cases, we will have already cleared the stack walker thread state indicator inside
// MakeStackwalkerCallback(), so we will be properly cleaned up.
#if !defined(DACCESS_COMPILE)
Thread* pStackWalkThreadOrig = t_pStackWalkerWalkingThread;
#endif
SET_THREAD_TYPE_STACKWALKER(this);
StackFrameIterator iter;
if (iter.Init(this, pStartFrame, pRD, flags) == TRUE)
{
while (iter.IsValid())
{
retVal = MakeStackwalkerCallback(&iter.m_crawl, pCallback, pData DEBUG_ARG(iter.m_uFramesProcessed));
if (retVal == SWA_ABORT)
{
break;
}
retVal = iter.Next();
if (retVal == SWA_FAILED)
{
break;
}
}
}
SET_THREAD_TYPE_STACKWALKER(pStackWalkThreadOrig);
}
return retVal;
} // StackWalkAction Thread::StackWalkFramesEx()
StackWalkAction Thread::StackWalkFrames(PSTACKWALKFRAMESCALLBACK pCallback,
VOID *pData,
unsigned flags,
PTR_Frame pStartFrame)
{
// Note: there are cases (i.e., exception handling) where we may never return from this function. This means
// that any C++ destructors pushed in this function will never execute, and it means that this function can
// never have a dynamic contract.
STATIC_CONTRACT_WRAPPER;
_ASSERTE((flags & THREAD_IS_SUSPENDED) == 0 || (flags & ALLOW_ASYNC_STACK_WALK));
T_CONTEXT ctx;
REGDISPLAY rd;
bool fUseInitRegDisplay;
#ifndef DACCESS_COMPILE
_ASSERTE(GetThreadNULLOk() == this || (flags & ALLOW_ASYNC_STACK_WALK));
BOOL fDebuggerHasInitialContext = (GetFilterContext() != NULL);
BOOL fProfilerHasInitialContext = (GetProfilerFilterContext() != NULL);
// If this walk is seeded by a profiler, then the walk better be done by the profiler
_ASSERTE(!fProfilerHasInitialContext || (flags & PROFILER_DO_STACK_SNAPSHOT));
fUseInitRegDisplay = fDebuggerHasInitialContext || fProfilerHasInitialContext;
#else
fUseInitRegDisplay = true;
#endif
if(fUseInitRegDisplay)
{
if (GetProfilerFilterContext() != NULL)
{
if (!InitRegDisplay(&rd, GetProfilerFilterContext(), TRUE))
{
LOG((LF_CORPROF, LL_INFO100, "**PROF: InitRegDisplay(&rd, GetProfilerFilterContext() failure leads to SWA_FAILED.\n"));
return SWA_FAILED;
}
}
else
{
if (!InitRegDisplay(&rd, &ctx, FALSE))
{
LOG((LF_CORPROF, LL_INFO100, "**PROF: InitRegDisplay(&rd, &ctx, FALSE) failure leads to SWA_FAILED.\n"));
return SWA_FAILED;
}
}
}
else
{
// Initialize the context
memset(&ctx, 0x00, sizeof(T_CONTEXT));
LOG((LF_GCROOTS, LL_INFO100000, "STACKWALK starting with partial context\n"));
FillRegDisplay(&rd, &ctx, !!(flags & LIGHTUNWIND));
}