forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackFrameIterator.cpp
2365 lines (2028 loc) · 104 KB
/
StackFrameIterator.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.
#include "common.h"
#ifdef HOST_WINDOWS
#include <windows.h>
#endif
#include "gcenv.h"
#include "CommonTypes.h"
#include "CommonMacros.h"
#include "daccess.h"
#include "PalRedhawkCommon.h"
#include "PalRedhawk.h"
#include "RedhawkWarnings.h"
#include "rhassert.h"
#include "slist.h"
#include "varint.h"
#include "regdisplay.h"
#include "StackFrameIterator.h"
#include "thread.h"
#include "holder.h"
#include "Crst.h"
#include "event.h"
#include "threadstore.h"
#include "threadstore.inl"
#include "thread.inl"
#include "stressLog.h"
#include "CommonMacros.inl"
#include "shash.h"
#include "RuntimeInstance.h"
#include "rhbinder.h"
#include "NativeContext.h"
// warning C4061: enumerator '{blah}' in switch of enum '{blarg}' is not explicitly handled by a case label
#pragma warning(disable:4061)
#if !defined(USE_PORTABLE_HELPERS) // @TODO: these are (currently) only implemented in assembly helpers
#if defined(FEATURE_DYNAMIC_CODE)
EXTERN_C CODE_LOCATION ReturnFromUniversalTransition;
EXTERN_C CODE_LOCATION ReturnFromUniversalTransition_DebugStepTailCall;
#endif
EXTERN_C CODE_LOCATION RhpCallCatchFunclet2;
EXTERN_C CODE_LOCATION RhpCallFinallyFunclet2;
EXTERN_C CODE_LOCATION RhpCallFilterFunclet2;
EXTERN_C CODE_LOCATION RhpThrowEx2;
EXTERN_C CODE_LOCATION RhpThrowHwEx2;
EXTERN_C CODE_LOCATION RhpRethrow2;
#endif // !defined(USE_PORTABLE_HELPERS)
// Addresses of functions in the DAC won't match their runtime counterparts so we
// assign them to globals. However it is more performant in the runtime to compare
// against immediates than to fetch the global. This macro hides the difference.
#ifdef DACCESS_COMPILE
#define EQUALS_RETURN_ADDRESS(x, func_name) ((x) == g_ ## func_name ## Addr)
#else
#define EQUALS_RETURN_ADDRESS(x, func_name) ((x) == &func_name)
#endif
#ifdef DACCESS_COMPILE
#define FAILFAST_OR_DAC_FAIL(x) if(!(x)) { DacError(E_FAIL); }
#define FAILFAST_OR_DAC_FAIL_MSG(x, msg) if(!(x)) { DacError(E_FAIL); }
#define FAILFAST_OR_DAC_FAIL_UNCONDITIONALLY(msg) DacError(E_FAIL)
#else
#define FAILFAST_OR_DAC_FAIL(x) if(!(x)) { ASSERT_UNCONDITIONALLY(#x); RhFailFast(); }
#define FAILFAST_OR_DAC_FAIL_MSG(x, msg) if(!(x)) { ASSERT_MSG((x), msg); ASSERT_UNCONDITIONALLY(#x); RhFailFast(); }
#define FAILFAST_OR_DAC_FAIL_UNCONDITIONALLY(msg) { ASSERT_UNCONDITIONALLY(msg); RhFailFast(); }
#endif
StackFrameIterator::StackFrameIterator(Thread * pThreadToWalk, PInvokeTransitionFrame* pInitialTransitionFrame)
{
STRESS_LOG0(LF_STACKWALK, LL_INFO10000, "----Init---- [ GC ]\n");
ASSERT(!pThreadToWalk->IsHijacked());
if (pInitialTransitionFrame == INTERRUPTED_THREAD_MARKER)
{
InternalInit(pThreadToWalk, pThreadToWalk->GetInterruptedContext(), GcStackWalkFlags | ActiveStackFrame);
}
else if (pInitialTransitionFrame == TOP_OF_STACK_MARKER)
{
InternalInit(pThreadToWalk, pInitialTransitionFrame, GcStackWalkFlags);
}
else
{
uint32_t flags = (pInitialTransitionFrame->m_Flags & PTFF_THREAD_HIJACK) == 0 ?
GcStackWalkFlags :
GcStackWalkFlags | ActiveStackFrame;
InternalInit(pThreadToWalk, pInitialTransitionFrame, flags);
}
PrepareToYieldFrame();
}
StackFrameIterator::StackFrameIterator(Thread * pThreadToWalk, PTR_PAL_LIMITED_CONTEXT pCtx)
{
STRESS_LOG0(LF_STACKWALK, LL_INFO10000, "----Init with limited ctx---- [ hijack ]\n");
InternalInit(pThreadToWalk, pCtx, 0);
PrepareToYieldFrame();
}
StackFrameIterator::StackFrameIterator(Thread* pThreadToWalk, NATIVE_CONTEXT* pCtx)
{
STRESS_LOG0(LF_STACKWALK, LL_INFO10000, "----Init with native ctx---- [ hijack ]\n");
InternalInit(pThreadToWalk, pCtx, 0);
PrepareToYieldFrame();
}
void StackFrameIterator::ResetNextExInfoForSP(uintptr_t SP)
{
while (m_pNextExInfo && (SP > (uintptr_t)dac_cast<TADDR>(m_pNextExInfo)))
m_pNextExInfo = m_pNextExInfo->m_pPrevExInfo;
}
void StackFrameIterator::EnterInitialInvalidState(Thread * pThreadToWalk)
{
m_pThread = pThreadToWalk;
m_pInstance = GetRuntimeInstance();
m_pCodeManager = NULL;
#ifdef TARGET_X86
m_pHijackedReturnValue = NULL;
m_HijackedReturnValueKind = GCRK_Unknown;
#endif
m_pConservativeStackRangeLowerBound = NULL;
m_pConservativeStackRangeUpperBound = NULL;
m_pendingFuncletFramePointer = NULL;
m_pNextExInfo = pThreadToWalk->GetCurExInfo();
m_pPreviousTransitionFrame = NULL;
SetControlPC(0);
}
// Prepare to start a stack walk from the context listed in the supplied PInvokeTransitionFrame.
// The supplied frame can be TOP_OF_STACK_MARKER to indicate that there are no more managed
// frames on the stack. Otherwise, the context in the frame always describes a callsite
// where control transitioned from managed to unmanaged code.
// NOTE: When a return address hijack is executed, the PC in the generated PInvokeTransitionFrame
// matches the hijacked return address. This PC is not guaranteed to be in managed code
// since the hijacked return address may refer to a location where an assembly thunk called
// into managed code.
// NOTE: When the PC is in an assembly thunk, this function will unwind to the next managed
// frame and may publish a conservative stack range (if and only if any of the unwound
// thunks report a conservative range).
void StackFrameIterator::InternalInit(Thread * pThreadToWalk, PInvokeTransitionFrame* pFrame, uint32_t dwFlags)
{
// EH stackwalks are always required to unwind non-volatile floating point state. This
// state is never carried by PInvokeTransitionFrames, implying that they can never be used
// as the initial state for an EH stackwalk.
ASSERT_MSG(!(dwFlags & ApplyReturnAddressAdjustment),
"PInvokeTransitionFrame content is not sufficient to seed an EH stackwalk");
EnterInitialInvalidState(pThreadToWalk);
if (pFrame == TOP_OF_STACK_MARKER)
{
// There are no managed frames on the stack. Leave the iterator in its initial invalid state.
return;
}
m_dwFlags = dwFlags;
m_pPreviousTransitionFrame = pFrame;
// We need to walk the ExInfo chain in parallel with the stackwalk so that we know when we cross over
// exception throw points. So we must find our initial point in the ExInfo chain here so that we can
// properly walk it in parallel.
ResetNextExInfoForSP((uintptr_t)dac_cast<TADDR>(pFrame));
#if !defined(USE_PORTABLE_HELPERS) // @TODO: no portable version of regdisplay
memset(&m_RegDisplay, 0, sizeof(m_RegDisplay));
m_RegDisplay.SetIP((PCODE)PCODEToPINSTR((PCODE)pFrame->m_RIP));
SetControlPC(dac_cast<PTR_VOID>(m_RegDisplay.GetIP()));
PTR_uintptr_t pPreservedRegsCursor = (PTR_uintptr_t)PTR_HOST_MEMBER_TADDR(PInvokeTransitionFrame, pFrame, m_PreservedRegs);
#ifdef TARGET_ARM
m_RegDisplay.pLR = (PTR_uintptr_t)PTR_HOST_MEMBER_TADDR(PInvokeTransitionFrame, pFrame, m_RIP);
if (pFrame->m_Flags & PTFF_SAVE_R4) { m_RegDisplay.pR4 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R5) { m_RegDisplay.pR5 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R6) { m_RegDisplay.pR6 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R7) { m_RegDisplay.pR7 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R8) { m_RegDisplay.pR8 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R9) { m_RegDisplay.pR9 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R10) { m_RegDisplay.pR10 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_SP) { m_RegDisplay.SP = *pPreservedRegsCursor++; }
m_RegDisplay.pR11 = (PTR_uintptr_t) PTR_HOST_MEMBER_TADDR(PInvokeTransitionFrame, pFrame, m_FramePointer);
if (pFrame->m_Flags & PTFF_SAVE_R0) { m_RegDisplay.pR0 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R1) { m_RegDisplay.pR1 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R2) { m_RegDisplay.pR2 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R3) { m_RegDisplay.pR3 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_LR) { m_RegDisplay.pLR = pPreservedRegsCursor++; }
#elif defined(TARGET_ARM64)
m_RegDisplay.pFP = (PTR_uintptr_t)PTR_HOST_MEMBER_TADDR(PInvokeTransitionFrame, pFrame, m_FramePointer);
m_RegDisplay.pLR = (PTR_uintptr_t)PTR_HOST_MEMBER_TADDR(PInvokeTransitionFrame, pFrame, m_RIP);
ASSERT(!(pFrame->m_Flags & PTFF_SAVE_FP)); // FP should never contain a GC ref
if (pFrame->m_Flags & PTFF_SAVE_X19) { m_RegDisplay.pX19 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X20) { m_RegDisplay.pX20 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X21) { m_RegDisplay.pX21 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X22) { m_RegDisplay.pX22 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X23) { m_RegDisplay.pX23 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X24) { m_RegDisplay.pX24 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X25) { m_RegDisplay.pX25 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X26) { m_RegDisplay.pX26 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X27) { m_RegDisplay.pX27 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X28) { m_RegDisplay.pX28 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_SP) { m_RegDisplay.SP = *pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X0) { m_RegDisplay.pX0 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X1) { m_RegDisplay.pX1 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X2) { m_RegDisplay.pX2 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X3) { m_RegDisplay.pX3 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X4) { m_RegDisplay.pX4 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X5) { m_RegDisplay.pX5 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X6) { m_RegDisplay.pX6 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X7) { m_RegDisplay.pX7 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X8) { m_RegDisplay.pX8 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X9) { m_RegDisplay.pX9 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X10) { m_RegDisplay.pX10 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X11) { m_RegDisplay.pX11 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X12) { m_RegDisplay.pX12 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X13) { m_RegDisplay.pX13 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X14) { m_RegDisplay.pX14 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X15) { m_RegDisplay.pX15 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X16) { m_RegDisplay.pX16 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X17) { m_RegDisplay.pX17 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_X18) { m_RegDisplay.pX18 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_LR) { m_RegDisplay.pLR = pPreservedRegsCursor++; }
#elif defined(TARGET_LOONGARCH64)
m_RegDisplay.pFP = (PTR_uintptr_t)PTR_HOST_MEMBER_TADDR(PInvokeTransitionFrame, pFrame, m_FramePointer);
m_RegDisplay.pRA = (PTR_uintptr_t)PTR_HOST_MEMBER_TADDR(PInvokeTransitionFrame, pFrame, m_RIP);
ASSERT(!(pFrame->m_Flags & PTFF_SAVE_FP)); // FP should never contain a GC ref
if (pFrame->m_Flags & PTFF_SAVE_R23) { m_RegDisplay.pR23 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R24) { m_RegDisplay.pR24 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R25) { m_RegDisplay.pR25 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R26) { m_RegDisplay.pR26 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R27) { m_RegDisplay.pR27 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R28) { m_RegDisplay.pR28 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R29) { m_RegDisplay.pR29 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R30) { m_RegDisplay.pR30 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R31) { m_RegDisplay.pR31 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_SP) { m_RegDisplay.SP = *pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R0) { m_RegDisplay.pR0 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R4) { m_RegDisplay.pR4 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R5) { m_RegDisplay.pR5 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R6) { m_RegDisplay.pR6 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R7) { m_RegDisplay.pR7 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R8) { m_RegDisplay.pR8 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R9) { m_RegDisplay.pR9 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R10) { m_RegDisplay.pR10 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R11) { m_RegDisplay.pR11 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R12) { m_RegDisplay.pR12 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R13) { m_RegDisplay.pR13 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R14) { m_RegDisplay.pR14 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R15) { m_RegDisplay.pR15 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R16) { m_RegDisplay.pR16 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R17) { m_RegDisplay.pR17 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R18) { m_RegDisplay.pR18 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R19) { m_RegDisplay.pR19 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R20) { m_RegDisplay.pR20 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R21) { m_RegDisplay.pR21 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_RA) { m_RegDisplay.pRA = pPreservedRegsCursor++; }
#elif defined(TARGET_RISCV64)
m_RegDisplay.pFP = (PTR_uintptr_t)PTR_HOST_MEMBER_TADDR(PInvokeTransitionFrame, pFrame, m_FramePointer);
m_RegDisplay.pRA = (PTR_uintptr_t)PTR_HOST_MEMBER_TADDR(PInvokeTransitionFrame, pFrame, m_RIP);
ASSERT(!(pFrame->m_Flags & PTFF_SAVE_FP)); // FP should never contain a GC ref
if (pFrame->m_Flags & PTFF_SAVE_S1) { m_RegDisplay.pS1 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_S2) { m_RegDisplay.pS2 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_S3) { m_RegDisplay.pS3 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_S4) { m_RegDisplay.pS4 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_S5) { m_RegDisplay.pS5 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_S6) { m_RegDisplay.pS6 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_S7) { m_RegDisplay.pS7 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_S8) { m_RegDisplay.pS8 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_S9) { m_RegDisplay.pS9 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_S10) { m_RegDisplay.pS10 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_S11) { m_RegDisplay.pS11 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_SP) { m_RegDisplay.SP = *pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R0) { m_RegDisplay.pR0 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_GP) { m_RegDisplay.pGP = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_A0) { m_RegDisplay.pA0 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_A1) { m_RegDisplay.pA1 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_A2) { m_RegDisplay.pA2 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_A3) { m_RegDisplay.pA3 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_A4) { m_RegDisplay.pA4 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_A5) { m_RegDisplay.pA5 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_A6) { m_RegDisplay.pA6 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_A7) { m_RegDisplay.pA7 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_T0) { m_RegDisplay.pT0 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_T1) { m_RegDisplay.pT1 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_T2) { m_RegDisplay.pT2 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_T3) { m_RegDisplay.pT3 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_T4) { m_RegDisplay.pT4 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_T5) { m_RegDisplay.pT5 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_T6) { m_RegDisplay.pT6 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_RA) { m_RegDisplay.pRA = pPreservedRegsCursor++; }
#else // TARGET_ARM
if (pFrame->m_Flags & PTFF_SAVE_RBX) { m_RegDisplay.pRbx = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_RSI) { m_RegDisplay.pRsi = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_RDI) { m_RegDisplay.pRdi = pPreservedRegsCursor++; }
ASSERT(!(pFrame->m_Flags & PTFF_SAVE_RBP)); // RBP should never contain a GC ref because we require
// a frame pointer for methods with pinvokes
#ifdef TARGET_AMD64
if (pFrame->m_Flags & PTFF_SAVE_R12) { m_RegDisplay.pR12 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R13) { m_RegDisplay.pR13 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R14) { m_RegDisplay.pR14 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R15) { m_RegDisplay.pR15 = pPreservedRegsCursor++; }
#endif // TARGET_AMD64
m_RegDisplay.pRbp = (PTR_uintptr_t) PTR_HOST_MEMBER_TADDR(PInvokeTransitionFrame, pFrame, m_FramePointer);
if (pFrame->m_Flags & PTFF_SAVE_RSP) { m_RegDisplay.SP = *pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_RAX) { m_RegDisplay.pRax = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_RCX) { m_RegDisplay.pRcx = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_RDX) { m_RegDisplay.pRdx = pPreservedRegsCursor++; }
#ifdef TARGET_AMD64
if (pFrame->m_Flags & PTFF_SAVE_R8 ) { m_RegDisplay.pR8 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R9 ) { m_RegDisplay.pR9 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R10) { m_RegDisplay.pR10 = pPreservedRegsCursor++; }
if (pFrame->m_Flags & PTFF_SAVE_R11) { m_RegDisplay.pR11 = pPreservedRegsCursor++; }
#endif // TARGET_AMD64
#ifdef TARGET_X86
GCRefKind retValueKind = TransitionFrameFlagsToReturnKind(pFrame->m_Flags);
if (retValueKind != GCRK_Scalar)
{
m_pHijackedReturnValue = (PTR_OBJECTREF)m_RegDisplay.pRax;
m_HijackedReturnValueKind = retValueKind;
}
#endif
#endif // TARGET_ARM
// adjust for thunks, if needed
EnsureInitializedToManagedFrame();
#endif // !defined(USE_PORTABLE_HELPERS)
STRESS_LOG1(LF_STACKWALK, LL_INFO10000, " %p\n", m_ControlPC);
}
#ifndef DACCESS_COMPILE
void StackFrameIterator::InternalInitForEH(Thread * pThreadToWalk, PAL_LIMITED_CONTEXT * pCtx, bool instructionFault)
{
STRESS_LOG0(LF_STACKWALK, LL_INFO10000, "----Init---- [ EH ]\n");
InternalInit(pThreadToWalk, pCtx, EHStackWalkFlags);
if (instructionFault)
{
// We treat the IP as a return-address and adjust backward when doing EH-related things. The faulting
// instruction IP here will be the start of the faulting instruction and so we have the right IP for
// EH-related things already.
m_dwFlags &= ~ApplyReturnAddressAdjustment;
PrepareToYieldFrame();
m_dwFlags |= ApplyReturnAddressAdjustment;
}
else
{
PrepareToYieldFrame();
}
STRESS_LOG1(LF_STACKWALK, LL_INFO10000, " %p\n", m_ControlPC);
}
void StackFrameIterator::InternalInitForStackTrace()
{
STRESS_LOG0(LF_STACKWALK, LL_INFO10000, "----Init---- [ StackTrace ]\n");
Thread * pThreadToWalk = ThreadStore::GetCurrentThread();
PInvokeTransitionFrame* pFrame = pThreadToWalk->GetTransitionFrameForStackTrace();
InternalInit(pThreadToWalk, pFrame, StackTraceStackWalkFlags);
PrepareToYieldFrame();
}
#endif //!DACCESS_COMPILE
// Prepare to start a stack walk from the context listed in the supplied PAL_LIMITED_CONTEXT.
// The supplied context can describe a location in either managed or unmanaged code. In the
// latter case the iterator is left in an invalid state when this function returns.
void StackFrameIterator::InternalInit(Thread * pThreadToWalk, PTR_PAL_LIMITED_CONTEXT pCtx, uint32_t dwFlags)
{
ASSERT((dwFlags & MethodStateCalculated) == 0);
EnterInitialInvalidState(pThreadToWalk);
m_dwFlags = dwFlags;
// We need to walk the ExInfo chain in parallel with the stackwalk so that we know when we cross over
// exception throw points. So we must find our initial point in the ExInfo chain here so that we can
// properly walk it in parallel.
ResetNextExInfoForSP(pCtx->GetSp());
// This codepath is used by the hijack stackwalk and we can get arbitrary ControlPCs from there. If this
// context has a non-managed control PC, then we're done.
if (!m_pInstance->IsManaged(dac_cast<PTR_VOID>(pCtx->GetIp())))
return;
//
// control state
//
m_RegDisplay.SP = pCtx->GetSp();
m_RegDisplay.IP = PCODEToPINSTR(pCtx->GetIp());
SetControlPC(dac_cast<PTR_VOID>(m_RegDisplay.GetIP()));
#ifdef TARGET_ARM
//
// preserved regs
//
m_RegDisplay.pR4 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R4);
m_RegDisplay.pR5 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R5);
m_RegDisplay.pR6 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R6);
m_RegDisplay.pR7 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R7);
m_RegDisplay.pR8 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R8);
m_RegDisplay.pR9 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R9);
m_RegDisplay.pR10 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R10);
m_RegDisplay.pR11 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R11);
m_RegDisplay.pLR = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, LR);
//
// preserved vfp regs
//
for (int32_t i = 0; i < 16 - 8; i++)
{
m_RegDisplay.D[i] = pCtx->D[i];
}
//
// scratch regs
//
m_RegDisplay.pR0 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R0);
#elif defined(TARGET_ARM64)
//
// preserved regs
//
m_RegDisplay.pX19 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, X19);
m_RegDisplay.pX20 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, X20);
m_RegDisplay.pX21 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, X21);
m_RegDisplay.pX22 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, X22);
m_RegDisplay.pX23 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, X23);
m_RegDisplay.pX24 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, X24);
m_RegDisplay.pX25 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, X25);
m_RegDisplay.pX26 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, X26);
m_RegDisplay.pX27 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, X27);
m_RegDisplay.pX28 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, X28);
m_RegDisplay.pFP = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, FP);
m_RegDisplay.pLR = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, LR);
//
// preserved vfp regs
//
for (int32_t i = 0; i < 16 - 8; i++)
{
m_RegDisplay.D[i] = pCtx->D[i];
}
//
// scratch regs
//
m_RegDisplay.pX0 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, X0);
m_RegDisplay.pX1 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, X1);
// TODO: Copy X2-X7 when we start supporting HVA's
#elif defined(TARGET_LOONGARCH64)
//
// preserved regs
//
m_RegDisplay.pR23 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R23);
m_RegDisplay.pR24 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R24);
m_RegDisplay.pR25 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R25);
m_RegDisplay.pR26 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R26);
m_RegDisplay.pR27 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R27);
m_RegDisplay.pR28 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R28);
m_RegDisplay.pR29 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R29);
m_RegDisplay.pR30 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R30);
m_RegDisplay.pR31 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R31);
m_RegDisplay.pFP = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, FP);
m_RegDisplay.pRA = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, RA);
//
// preserved vfp regs
//
for (int32_t i = 0; i < 16 - 8; i++)
{
m_RegDisplay.F[i] = pCtx->F[i];
}
//
// scratch regs
//
m_RegDisplay.pR4 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R4);
m_RegDisplay.pR5 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R5);
#elif defined(TARGET_RISCV64)
//
// preserved regs
//
m_RegDisplay.pS1 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, S1);
m_RegDisplay.pS2 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, S2);
m_RegDisplay.pS3 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, S3);
m_RegDisplay.pS4 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, S4);
m_RegDisplay.pS5 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, S5);
m_RegDisplay.pS6 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, S6);
m_RegDisplay.pS7 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, S7);
m_RegDisplay.pS8 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, S8);
m_RegDisplay.pS9 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, S9);
m_RegDisplay.pS10 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, S10);
m_RegDisplay.pS11 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, S11);
m_RegDisplay.pFP = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, FP);
m_RegDisplay.pRA = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, RA);
//
// preserved floating-point registers
//
int32_t preservedFpIndices[] = {8, 9, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27};
for (int i = 0; i < ARRAY_SIZE(preservedFpIndices); i++)
{
m_RegDisplay.F[preservedFpIndices[i]] = pCtx->F[preservedFpIndices[i]];
}
//
// scratch regs
//
m_RegDisplay.pA0 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, A0);
m_RegDisplay.pA1 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, A1);
#elif defined(UNIX_AMD64_ABI)
//
// preserved regs
//
m_RegDisplay.pRbp = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, Rbp);
m_RegDisplay.pRbx = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, Rbx);
m_RegDisplay.pR12 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R12);
m_RegDisplay.pR13 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R13);
m_RegDisplay.pR14 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R14);
m_RegDisplay.pR15 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R15);
//
// scratch regs
//
m_RegDisplay.pRax = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, Rax);
m_RegDisplay.pRcx = NULL;
m_RegDisplay.pRdx = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, Rdx);
m_RegDisplay.pRsi = NULL;
m_RegDisplay.pRdi = NULL;
m_RegDisplay.pR8 = NULL;
m_RegDisplay.pR9 = NULL;
m_RegDisplay.pR10 = NULL;
m_RegDisplay.pR11 = NULL;
#elif defined(TARGET_X86) || defined(TARGET_AMD64)
//
// preserved regs
//
m_RegDisplay.pRbp = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, Rbp);
m_RegDisplay.pRsi = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, Rsi);
m_RegDisplay.pRdi = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, Rdi);
m_RegDisplay.pRbx = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, Rbx);
#ifdef TARGET_AMD64
m_RegDisplay.pR12 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R12);
m_RegDisplay.pR13 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R13);
m_RegDisplay.pR14 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R14);
m_RegDisplay.pR15 = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, R15);
#if defined(TARGET_WINDOWS)
//
// SSP, we only need the value
//
m_RegDisplay.SSP = pCtx->SSP;
#endif
//
// preserved xmm regs
//
memcpy(m_RegDisplay.Xmm, &pCtx->Xmm6, sizeof(m_RegDisplay.Xmm));
#endif // TARGET_AMD64
//
// scratch regs
//
m_RegDisplay.pRax = (PTR_uintptr_t)PTR_TO_MEMBER_TADDR(PAL_LIMITED_CONTEXT, pCtx, Rax);
m_RegDisplay.pRcx = NULL;
m_RegDisplay.pRdx = NULL;
#ifdef TARGET_AMD64
m_RegDisplay.pR8 = NULL;
m_RegDisplay.pR9 = NULL;
m_RegDisplay.pR10 = NULL;
m_RegDisplay.pR11 = NULL;
#endif // TARGET_AMD64
#else
PORTABILITY_ASSERT("StackFrameIterator::InternalInit");
#endif // TARGET_ARM
}
// Prepare to start a stack walk from the context listed in the supplied NATIVE_CONTEXT.
// NOTE: When a return address hijack is executed, the PC in the NATIVE_CONTEXT
// matches the hijacked return address. This PC is not guaranteed to be in managed code
// since the hijacked return address may refer to a location where an assembly thunk called
// into managed code.
// NOTE: When the PC is in an assembly thunk, this function will unwind to the next managed
// frame and may publish a conservative stack range (if and only if any of the unwound
// thunks report a conservative range).
void StackFrameIterator::InternalInit(Thread * pThreadToWalk, NATIVE_CONTEXT* pCtx, uint32_t dwFlags)
{
ASSERT((dwFlags & MethodStateCalculated) == 0);
EnterInitialInvalidState(pThreadToWalk);
m_dwFlags = dwFlags;
// We need to walk the ExInfo chain in parallel with the stackwalk so that we know when we cross over
// exception throw points. So we must find our initial point in the ExInfo chain here so that we can
// properly walk it in parallel.
ResetNextExInfoForSP(pCtx->GetSp());
// This codepath is used by the hijack stackwalk. The IP must be in managed code
// or in a conservatively reported assembly thunk.
ASSERT(IsValidReturnAddress((void*)pCtx->GetIp()));
//
// control state
//
SetControlPC(dac_cast<PTR_VOID>(pCtx->GetIp()));
m_RegDisplay.SP = pCtx->GetSp();
m_RegDisplay.IP = pCtx->GetIp();
#ifdef TARGET_UNIX
#define PTR_TO_REG(ptr, reg) (&((ptr)->reg()))
#else
#define PTR_TO_REG(ptr, reg) (&((ptr)->ctx.reg))
#endif
#ifdef TARGET_ARM64
//
// preserved regs
//
m_RegDisplay.pX19 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X19);
m_RegDisplay.pX20 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X20);
m_RegDisplay.pX21 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X21);
m_RegDisplay.pX22 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X22);
m_RegDisplay.pX23 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X23);
m_RegDisplay.pX24 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X24);
m_RegDisplay.pX25 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X25);
m_RegDisplay.pX26 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X26);
m_RegDisplay.pX27 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X27);
m_RegDisplay.pX28 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X28);
m_RegDisplay.pFP = (PTR_uintptr_t)PTR_TO_REG(pCtx, Fp);
m_RegDisplay.pLR = (PTR_uintptr_t)PTR_TO_REG(pCtx, Lr);
//
// scratch regs
//
m_RegDisplay.pX0 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X0);
m_RegDisplay.pX1 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X1);
m_RegDisplay.pX2 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X2);
m_RegDisplay.pX3 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X3);
m_RegDisplay.pX4 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X4);
m_RegDisplay.pX5 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X5);
m_RegDisplay.pX6 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X6);
m_RegDisplay.pX7 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X7);
m_RegDisplay.pX8 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X8);
m_RegDisplay.pX9 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X9);
m_RegDisplay.pX10 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X10);
m_RegDisplay.pX11 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X11);
m_RegDisplay.pX12 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X12);
m_RegDisplay.pX13 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X13);
m_RegDisplay.pX14 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X14);
m_RegDisplay.pX15 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X15);
m_RegDisplay.pX16 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X16);
m_RegDisplay.pX17 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X17);
m_RegDisplay.pX18 = (PTR_uintptr_t)PTR_TO_REG(pCtx, X18);
#elif defined(TARGET_AMD64)
//
// preserved regs
//
m_RegDisplay.pRbp = (PTR_uintptr_t)PTR_TO_REG(pCtx, Rbp);
m_RegDisplay.pRsi = (PTR_uintptr_t)PTR_TO_REG(pCtx, Rsi);
m_RegDisplay.pRdi = (PTR_uintptr_t)PTR_TO_REG(pCtx, Rdi);
m_RegDisplay.pRbx = (PTR_uintptr_t)PTR_TO_REG(pCtx, Rbx);
m_RegDisplay.pR12 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R12);
m_RegDisplay.pR13 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R13);
m_RegDisplay.pR14 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R14);
m_RegDisplay.pR15 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R15);
#if defined(TARGET_WINDOWS)
//
// SSP, not needed. Unwind from native context is never for EH.
//
m_RegDisplay.SSP = 0;
#endif
//
// scratch regs
//
m_RegDisplay.pRax = (PTR_uintptr_t)PTR_TO_REG(pCtx, Rax);
m_RegDisplay.pRcx = (PTR_uintptr_t)PTR_TO_REG(pCtx, Rcx);
m_RegDisplay.pRdx = (PTR_uintptr_t)PTR_TO_REG(pCtx, Rdx);
m_RegDisplay.pR8 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R8);
m_RegDisplay.pR9 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R9);
m_RegDisplay.pR10 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R10);
m_RegDisplay.pR11 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R11);
#elif defined(TARGET_X86)
//
// preserved regs
//
m_RegDisplay.pRbp = (PTR_uintptr_t)PTR_TO_REG(pCtx, Ebp);
m_RegDisplay.pRsi = (PTR_uintptr_t)PTR_TO_REG(pCtx, Esi);
m_RegDisplay.pRdi = (PTR_uintptr_t)PTR_TO_REG(pCtx, Edi);
m_RegDisplay.pRbx = (PTR_uintptr_t)PTR_TO_REG(pCtx, Ebx);
//
// scratch regs
//
m_RegDisplay.pRax = (PTR_uintptr_t)PTR_TO_REG(pCtx, Eax);
m_RegDisplay.pRcx = (PTR_uintptr_t)PTR_TO_REG(pCtx, Ecx);
m_RegDisplay.pRdx = (PTR_uintptr_t)PTR_TO_REG(pCtx, Edx);
#elif defined(TARGET_ARM)
m_RegDisplay.pR0 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R0);
m_RegDisplay.pR1 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R1);
m_RegDisplay.pR2 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R2);
m_RegDisplay.pR3 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R3);
m_RegDisplay.pR4 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R4);
m_RegDisplay.pR5 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R5);
m_RegDisplay.pR6 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R6);
m_RegDisplay.pR7 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R7);
m_RegDisplay.pR8 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R8);
m_RegDisplay.pR9 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R9);
m_RegDisplay.pR10 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R10);
m_RegDisplay.pR11 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R11);
m_RegDisplay.pR12 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R12);
m_RegDisplay.pLR = (PTR_uintptr_t)PTR_TO_REG(pCtx, Lr);
#elif defined(TARGET_LOONGARCH64)
//
// preserved regs
//
m_RegDisplay.pR23 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R23);
m_RegDisplay.pR24 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R24);
m_RegDisplay.pR25 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R25);
m_RegDisplay.pR26 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R26);
m_RegDisplay.pR27 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R27);
m_RegDisplay.pR28 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R28);
m_RegDisplay.pR29 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R29);
m_RegDisplay.pR30 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R30);
m_RegDisplay.pR31 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R31);
m_RegDisplay.pFP = (PTR_uintptr_t)PTR_TO_REG(pCtx, Fp);
m_RegDisplay.pRA = (PTR_uintptr_t)PTR_TO_REG(pCtx, Ra);
//
// scratch regs
//
m_RegDisplay.pR0 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R0);
m_RegDisplay.pR2 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R2);
m_RegDisplay.pR4 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R4);
m_RegDisplay.pR5 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R5);
m_RegDisplay.pR6 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R6);
m_RegDisplay.pR7 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R7);
m_RegDisplay.pR8 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R8);
m_RegDisplay.pR9 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R9);
m_RegDisplay.pR10 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R10);
m_RegDisplay.pR11 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R11);
m_RegDisplay.pR12 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R12);
m_RegDisplay.pR13 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R13);
m_RegDisplay.pR14 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R14);
m_RegDisplay.pR15 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R15);
m_RegDisplay.pR16 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R16);
m_RegDisplay.pR17 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R17);
m_RegDisplay.pR18 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R18);
m_RegDisplay.pR19 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R19);
m_RegDisplay.pR20 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R20);
m_RegDisplay.pR21 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R21);
#elif defined(TARGET_RISCV64)
//
// preserved regs
//
m_RegDisplay.pS1 = (PTR_uintptr_t)PTR_TO_REG(pCtx, S1);
m_RegDisplay.pS2 = (PTR_uintptr_t)PTR_TO_REG(pCtx, S2);
m_RegDisplay.pS3 = (PTR_uintptr_t)PTR_TO_REG(pCtx, S3);
m_RegDisplay.pS4 = (PTR_uintptr_t)PTR_TO_REG(pCtx, S4);
m_RegDisplay.pS5 = (PTR_uintptr_t)PTR_TO_REG(pCtx, S5);
m_RegDisplay.pS6 = (PTR_uintptr_t)PTR_TO_REG(pCtx, S6);
m_RegDisplay.pS7 = (PTR_uintptr_t)PTR_TO_REG(pCtx, S7);
m_RegDisplay.pS8 = (PTR_uintptr_t)PTR_TO_REG(pCtx, S8);
m_RegDisplay.pS9 = (PTR_uintptr_t)PTR_TO_REG(pCtx, S9);
m_RegDisplay.pS10 = (PTR_uintptr_t)PTR_TO_REG(pCtx, S10);
m_RegDisplay.pS11 = (PTR_uintptr_t)PTR_TO_REG(pCtx, S11);
m_RegDisplay.pFP = (PTR_uintptr_t)PTR_TO_REG(pCtx, Fp);
m_RegDisplay.pRA = (PTR_uintptr_t)PTR_TO_REG(pCtx, Ra);
//
// scratch regs
//
m_RegDisplay.pR0 = (PTR_uintptr_t)PTR_TO_REG(pCtx, R0);
m_RegDisplay.pA0 = (PTR_uintptr_t)PTR_TO_REG(pCtx, A0);
m_RegDisplay.pA1 = (PTR_uintptr_t)PTR_TO_REG(pCtx, A1);
m_RegDisplay.pA2 = (PTR_uintptr_t)PTR_TO_REG(pCtx, A2);
m_RegDisplay.pA3 = (PTR_uintptr_t)PTR_TO_REG(pCtx, A3);
m_RegDisplay.pA4 = (PTR_uintptr_t)PTR_TO_REG(pCtx, A4);
m_RegDisplay.pA5 = (PTR_uintptr_t)PTR_TO_REG(pCtx, A5);
m_RegDisplay.pA6 = (PTR_uintptr_t)PTR_TO_REG(pCtx, A6);
m_RegDisplay.pA7 = (PTR_uintptr_t)PTR_TO_REG(pCtx, A7);
m_RegDisplay.pT0 = (PTR_uintptr_t)PTR_TO_REG(pCtx, T0);
m_RegDisplay.pT1 = (PTR_uintptr_t)PTR_TO_REG(pCtx, T1);
m_RegDisplay.pT2 = (PTR_uintptr_t)PTR_TO_REG(pCtx, T2);
m_RegDisplay.pT3 = (PTR_uintptr_t)PTR_TO_REG(pCtx, T3);
m_RegDisplay.pT4 = (PTR_uintptr_t)PTR_TO_REG(pCtx, T4);
m_RegDisplay.pT5 = (PTR_uintptr_t)PTR_TO_REG(pCtx, T5);
m_RegDisplay.pT6 = (PTR_uintptr_t)PTR_TO_REG(pCtx, T6);
#else
PORTABILITY_ASSERT("StackFrameIterator::InternalInit");
#endif // TARGET_ARM
#undef PTR_TO_REG
// adjust for thunks, if needed
EnsureInitializedToManagedFrame();
}
void StackFrameIterator::EnsureInitializedToManagedFrame()
{
// This function guarantees that the final initialized context will refer to a managed
// frame. In the rare case where the PC does not refer to managed code (and refers to an
// assembly thunk instead), unwind through the thunk sequence to find the nearest managed
// frame.
// NOTE: When thunks are present, the thunk sequence may report a conservative GC reporting
// lower bound that must be applied when processing the managed frame.
ReturnAddressCategory category = CategorizeUnadjustedReturnAddress(m_ControlPC);
if (category == InManagedCode)
{
ASSERT(m_pInstance->IsManaged(m_ControlPC));
}
else if (IsNonEHThunk(category))
{
UnwindNonEHThunkSequence();
ASSERT(m_pInstance->IsManaged(m_ControlPC));
}
else
{
FAILFAST_OR_DAC_FAIL_UNCONDITIONALLY("Unadjusted initial PC points to an unexpected assembly thunk kind.");
}
}
PTR_VOID StackFrameIterator::HandleExCollide(PTR_ExInfo pExInfo)
{
STRESS_LOG3(LF_STACKWALK, LL_INFO10000, " [ ex collide ] kind = %d, pass = %d, idxCurClause = %d\n",
pExInfo->m_kind, pExInfo->m_passNumber, pExInfo->m_idxCurClause);
PTR_VOID collapsingTargetFrame = NULL;
uint32_t curFlags = m_dwFlags;
// Capture and clear the pending funclet frame pointer (if any). This field is only set
// when stack walks collide with active exception dispatch, and only exists to save the
// funclet frame pointer until the next ExInfo collision (which has now occurred).
PTR_VOID activeFuncletFramePointer = m_pendingFuncletFramePointer;
m_pendingFuncletFramePointer = NULL;
// If we aren't invoking a funclet (i.e. idxCurClause == -1), and we're doing a GC stackwalk, we don't
// want the 2nd-pass collided behavior because that behavior assumes that the previous frame was a
// funclet, which isn't the case when taking a GC at some points in the EH dispatch code. So we treat it
// as if the 2nd pass hasn't actually started yet.
if ((pExInfo->m_passNumber == 1) ||
(pExInfo->m_idxCurClause == 0xFFFFFFFF))
{
FAILFAST_OR_DAC_FAIL_MSG(!(curFlags & ApplyReturnAddressAdjustment),
"did not expect to collide with a 1st-pass ExInfo during a EH stackwalk");
InternalInit(m_pThread, pExInfo->m_pExContext, curFlags);
m_pNextExInfo = pExInfo->m_pPrevExInfo;
CalculateCurrentMethodState();
ASSERT(IsValid());
if ((pExInfo->m_kind & EK_HardwareFault) && (curFlags & RemapHardwareFaultsToSafePoint))
m_effectiveSafePointAddress = GetCodeManager()->RemapHardwareFaultToGCSafePoint(&m_methodInfo, m_ControlPC);
}
else
{
ASSERT_MSG(activeFuncletFramePointer != NULL,
"collided with an active funclet invoke but the funclet frame pointer is unknown");
//
// Copy our state from the previous StackFrameIterator
//
this->UpdateFromExceptionDispatch((PTR_StackFrameIterator)&pExInfo->m_frameIter);
// Sync our 'current' ExInfo with the updated state (we may have skipped other dispatches)
ResetNextExInfoForSP(m_RegDisplay.GetSP());
m_dwFlags = curFlags;
// The iterator has been moved to the "owner frame" (either a parent funclet or the main
// code body) of the funclet being invoked by this ExInfo. As a result, both the active
// funclet and the current frame must be "part of the same function" and therefore must
// have identical frame pointer values.
CalculateCurrentMethodState();
ASSERT(IsValid());
ASSERT(m_FramePointer == activeFuncletFramePointer);
if ((m_ControlPC != 0) && // the dispatch in ExInfo could have gone unhandled
(m_dwFlags & CollapseFunclets))
{
// GC stack walks must skip the owner frame since GC information for the entire function
// has already been reported by the leafmost active funclet. In general, the GC stack walk
// must skip all parent frames that are "part of the same function" (i.e., have the same
// frame pointer).
collapsingTargetFrame = activeFuncletFramePointer;
}
}
return collapsingTargetFrame;
}
void StackFrameIterator::UpdateFromExceptionDispatch(PTR_StackFrameIterator pSourceIterator)
{
ASSERT(m_pendingFuncletFramePointer == NULL);
PreservedRegPtrs thisFuncletPtrs = this->m_funcletPtrs;
// Blast over 'this' with everything from the 'source'.
*this = *pSourceIterator;
// Clear the funclet frame pointer (if any) that was loaded from the previous iterator.
// This field does not relate to the transferrable state of the previous iterator (it
// instead tracks the frame-by-frame progression of a particular iterator instance) and
// therefore has no meaning in the context of the current stack walk.
m_pendingFuncletFramePointer = NULL;
// Then, put back the pointers to the funclet's preserved registers (since those are the correct values
// until the funclet completes, at which point the values will be copied back to the ExInfo's REGDISPLAY).
#ifdef TARGET_ARM
m_RegDisplay.pR4 = thisFuncletPtrs.pR4 ;
m_RegDisplay.pR5 = thisFuncletPtrs.pR5 ;
m_RegDisplay.pR6 = thisFuncletPtrs.pR6 ;
m_RegDisplay.pR7 = thisFuncletPtrs.pR7 ;
m_RegDisplay.pR8 = thisFuncletPtrs.pR8 ;
m_RegDisplay.pR9 = thisFuncletPtrs.pR9 ;
m_RegDisplay.pR10 = thisFuncletPtrs.pR10;
m_RegDisplay.pR11 = thisFuncletPtrs.pR11;
#elif defined(TARGET_ARM64)
m_RegDisplay.pX19 = thisFuncletPtrs.pX19;
m_RegDisplay.pX20 = thisFuncletPtrs.pX20;
m_RegDisplay.pX21 = thisFuncletPtrs.pX21;
m_RegDisplay.pX22 = thisFuncletPtrs.pX22;
m_RegDisplay.pX23 = thisFuncletPtrs.pX23;
m_RegDisplay.pX24 = thisFuncletPtrs.pX24;
m_RegDisplay.pX25 = thisFuncletPtrs.pX25;
m_RegDisplay.pX26 = thisFuncletPtrs.pX26;
m_RegDisplay.pX27 = thisFuncletPtrs.pX27;
m_RegDisplay.pX28 = thisFuncletPtrs.pX28;
m_RegDisplay.pFP = thisFuncletPtrs.pFP;
#elif defined(TARGET_LOONGARCH64)
m_RegDisplay.pR23 = thisFuncletPtrs.pR23;
m_RegDisplay.pR24 = thisFuncletPtrs.pR24;
m_RegDisplay.pR25 = thisFuncletPtrs.pR25;
m_RegDisplay.pR26 = thisFuncletPtrs.pR26;
m_RegDisplay.pR27 = thisFuncletPtrs.pR27;
m_RegDisplay.pR28 = thisFuncletPtrs.pR28;
m_RegDisplay.pR29 = thisFuncletPtrs.pR29;
m_RegDisplay.pR30 = thisFuncletPtrs.pR30;
m_RegDisplay.pR31 = thisFuncletPtrs.pR31;
m_RegDisplay.pFP = thisFuncletPtrs.pFP;
#elif defined(TARGET_RISCV64)
m_RegDisplay.pS1 = thisFuncletPtrs.pS1;
m_RegDisplay.pS2 = thisFuncletPtrs.pS2;
m_RegDisplay.pS3 = thisFuncletPtrs.pS3;
m_RegDisplay.pS4 = thisFuncletPtrs.pS4;
m_RegDisplay.pS5 = thisFuncletPtrs.pS5;
m_RegDisplay.pS6 = thisFuncletPtrs.pS6;
m_RegDisplay.pS7 = thisFuncletPtrs.pS7;
m_RegDisplay.pS8 = thisFuncletPtrs.pS8;
m_RegDisplay.pS9 = thisFuncletPtrs.pS9;