-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathgcinfodecoder.cpp
More file actions
2299 lines (1926 loc) · 80.1 KB
/
Copy pathgcinfodecoder.cpp
File metadata and controls
2299 lines (1926 loc) · 80.1 KB
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.
#ifndef SOS_INCLUDE
#include "common.h"
#endif
#include "gcinfodecoder.h"
#ifdef FEATURE_INTERPRETER
#include "interpexec.h"
#endif // FEATURE_INTERPRETER
#ifdef USE_GC_INFO_DECODER
#ifndef CHECK_APP_DOMAIN
#define CHECK_APP_DOMAIN 0
#endif
#ifndef GCINFODECODER_CONTRACT
#define GCINFODECODER_CONTRACT LIMITED_METHOD_CONTRACT
#endif // !GCINFODECODER_CONTRACT
#ifndef GET_CALLER_SP
inline size_t GET_CALLER_SP(PREGDISPLAY pREGDISPLAY)
{
_ASSERTE(false);
return 0;
}
#endif // !GET_CALLER_SP
#ifndef VALIDATE_OBJECTREF
#if defined(DACCESS_COMPILE)
#define VALIDATE_OBJECTREF(objref, fDeep)
#else // DACCESS_COMPILE
#define VALIDATE_OBJECTREF(objref, fDeep) \
do { \
Object* objPtr = OBJECTREF_TO_UNCHECKED_OBJECTREF(objref); \
if (objPtr) \
{ \
objPtr->Validate(fDeep); \
} \
} while(0)
#endif // DACCESS_COMPILE
#endif // !VALIDATE_OBJECTREF
#ifndef VALIDATE_ROOT
#include "gcenv.h"
#define VALIDATE_ROOT(isInterior, hCallBack, pObjRef) \
do { \
/* Only call Object::Validate() with bDeep == TRUE if we are in the promote phase. */ \
/* We should call Validate() with bDeep == FALSE if we are in the relocation phase. */ \
/* Actually with the introduction of the POPO feature, we cannot validate during */ \
/* relocate because POPO might have written over the object. It will require non */ \
/* trivial amount of work to make this work.*/ \
\
GCCONTEXT* pGCCtx = (GCCONTEXT*)(hCallBack); \
\
if (!(isInterior) && !(m_Flags & DECODE_NO_VALIDATION) && (pGCCtx->sc->promotion)) { \
VALIDATE_OBJECTREF(*(pObjRef), pGCCtx->sc->promotion == TRUE); \
} \
} while (0)
#endif // !VALIDATE_ROOT
#ifndef LOG_PIPTR
#define LOG_PIPTR(pObjRef, gcFlags, hCallBack) \
{ \
GCCONTEXT* pGCCtx = (GCCONTEXT*)(hCallBack); \
if (pGCCtx->sc->promotion) \
{ \
LOG((LF_GCROOTS, LL_INFO1000, /* Part Three */ \
LOG_PIPTR_OBJECT_CLASS(OBJECTREF_TO_UNCHECKED_OBJECTREF(*pObjRef), (gcFlags & GC_CALL_PINNED), (gcFlags & GC_CALL_INTERIOR)))); \
} \
else \
{ \
LOG((LF_GCROOTS, LL_INFO1000, /* Part Three */ \
LOG_PIPTR_OBJECT(OBJECTREF_TO_UNCHECKED_OBJECTREF(*pObjRef), (gcFlags & GC_CALL_PINNED), (gcFlags & GC_CALL_INTERIOR)))); \
} \
}
#endif // !LOG_PIPTR
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::SetIsInterruptibleCB (UINT32 startOffset, UINT32 stopOffset, void * hCallback)
{
TGcInfoDecoder<GcInfoEncoding> *pThis = (TGcInfoDecoder<GcInfoEncoding>*)hCallback;
bool fStop = pThis->m_InstructionOffset >= startOffset && pThis->m_InstructionOffset < stopOffset;
if (fStop)
pThis->m_IsInterruptible = true;
return fStop;
}
// returns true if we decoded all that was asked;
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::PredecodeFatHeader(int remainingFlags)
{
m_headerFlags = (GcInfoHeaderFlags)m_Reader.Read(GC_INFO_FLAGS_BIT_SIZE);
#ifdef DECODE_OLD_FORMATS
if (Version() < 4)
{
m_ReturnKind = (ReturnKind)((UINT32)m_Reader.Read(GcInfoEncoding::SIZE_OF_RETURN_KIND_IN_FAT_HEADER));
}
#endif
remainingFlags &= ~(DECODE_RETURN_KIND | DECODE_VARARG);
#if defined(TARGET_ARM) || defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
remainingFlags &= ~DECODE_HAS_TAILCALLS;
#endif
if (remainingFlags == 0)
{
// Bail, if we've decoded enough,
return true;
}
m_CodeLength = GcInfoEncoding::DENORMALIZE_CODE_LENGTH((UINT32)m_Reader.DecodeVarLengthUnsigned(GcInfoEncoding::CODE_LENGTH_ENCBASE));
remainingFlags &= ~DECODE_CODE_LENGTH;
if (remainingFlags == 0)
{
// Bail, if we've decoded enough,
return true;
}
if (m_headerFlags & GC_INFO_HAS_GS_COOKIE)
{
// Note that normalization as a code offset can be different than
// normalization as code length
UINT32 normCodeLength = NormalizeCodeOffset(m_CodeLength);
// Decode prolog/epilog information
UINT32 normPrologSize = (UINT32)m_Reader.DecodeVarLengthUnsigned(GcInfoEncoding::NORM_PROLOG_SIZE_ENCBASE) + 1;
UINT32 normEpilogSize = (UINT32)m_Reader.DecodeVarLengthUnsigned(GcInfoEncoding::NORM_EPILOG_SIZE_ENCBASE);
m_ValidRangeStart = DenormalizeCodeOffset(normPrologSize);
m_ValidRangeEnd = DenormalizeCodeOffset(normCodeLength - normEpilogSize);
_ASSERTE(m_ValidRangeStart < m_ValidRangeEnd);
}
else if ((m_headerFlags & GC_INFO_HAS_GENERICS_INST_CONTEXT_MASK) != GC_INFO_HAS_GENERICS_INST_CONTEXT_NONE)
{
// Decode prolog information
UINT32 normPrologSize = (UINT32)m_Reader.DecodeVarLengthUnsigned(GcInfoEncoding::NORM_PROLOG_SIZE_ENCBASE) + 1;
m_ValidRangeStart = DenormalizeCodeOffset(normPrologSize);
// satisfy asserts that assume m_GSCookieValidRangeStart != 0 ==> m_GSCookieValidRangeStart < m_GSCookieValidRangeEnd
m_ValidRangeEnd = m_ValidRangeStart + 1;
}
else
{
m_ValidRangeStart = m_ValidRangeEnd = 0;
}
remainingFlags &= ~DECODE_PROLOG_LENGTH;
if (remainingFlags == 0)
{
// Bail, if we've decoded enough,
return true;
}
// Decode the offset to the GS cookie.
if (m_headerFlags & GC_INFO_HAS_GS_COOKIE)
{
m_GSCookieStackSlot = GcInfoEncoding::DENORMALIZE_STACK_SLOT((INT32)m_Reader.DecodeVarLengthSigned(GcInfoEncoding::GS_COOKIE_STACK_SLOT_ENCBASE));
}
else
{
m_GSCookieStackSlot = NO_GS_COOKIE;
}
remainingFlags &= ~DECODE_GS_COOKIE;
if (remainingFlags == 0)
{
// Bail, if we've decoded enough,
return true;
}
#ifdef DECODE_OLD_FORMATS
// Decode the offset to the PSPSym.
// The PSPSym is relative to the caller SP on IA64 and the initial stack pointer before any stack allocation on X64 (InitialSP).
if (Version() < 4 && (m_headerFlags & GC_INFO_HAS_PSP_SYM))
{
m_PSPSymStackSlot = GcInfoEncoding::DENORMALIZE_STACK_SLOT((INT32)m_Reader.DecodeVarLengthSigned(GcInfoEncoding::PSP_SYM_STACK_SLOT_ENCBASE));
}
else
#endif
{
m_PSPSymStackSlot = NO_PSP_SYM;
}
remainingFlags &= ~DECODE_PSP_SYM;
if (remainingFlags == 0)
{
// Bail, if we've decoded enough,
return true;
}
// Decode the offset to the generics type context.
if ((m_headerFlags & GC_INFO_HAS_GENERICS_INST_CONTEXT_MASK) != GC_INFO_HAS_GENERICS_INST_CONTEXT_NONE)
{
m_GenericsInstContextStackSlot = GcInfoEncoding::DENORMALIZE_STACK_SLOT((INT32)m_Reader.DecodeVarLengthSigned(GcInfoEncoding::GENERICS_INST_CONTEXT_STACK_SLOT_ENCBASE));
}
else
{
m_GenericsInstContextStackSlot = NO_GENERICS_INST_CONTEXT;
}
remainingFlags &= ~DECODE_GENERICS_INST_CONTEXT;
if (remainingFlags == 0)
{
// Bail, if we've decoded enough,
return true;
}
if (m_headerFlags & GC_INFO_HAS_STACK_BASE_REGISTER)
{
m_StackBaseRegister = GcInfoEncoding::DENORMALIZE_STACK_BASE_REGISTER((UINT32)m_Reader.DecodeVarLengthUnsigned(GcInfoEncoding::STACK_BASE_REGISTER_ENCBASE));
}
else
{
m_StackBaseRegister = NO_STACK_BASE_REGISTER;
}
if (m_headerFlags & GC_INFO_HAS_EDIT_AND_CONTINUE_INFO)
{
m_SizeOfEditAndContinuePreservedArea = (UINT32)m_Reader.DecodeVarLengthUnsigned(GcInfoEncoding::SIZE_OF_EDIT_AND_CONTINUE_PRESERVED_AREA_ENCBASE);
#ifdef TARGET_ARM64
m_SizeOfEditAndContinueFixedStackFrame = (UINT32)m_Reader.DecodeVarLengthUnsigned(GcInfoEncoding::SIZE_OF_EDIT_AND_CONTINUE_FIXED_STACK_FRAME_ENCBASE);
#endif
}
else
{
m_SizeOfEditAndContinuePreservedArea = NO_SIZE_OF_EDIT_AND_CONTINUE_PRESERVED_AREA;
#ifdef TARGET_ARM64
m_SizeOfEditAndContinueFixedStackFrame = 0;
#endif
}
remainingFlags &= ~DECODE_EDIT_AND_CONTINUE;
if (remainingFlags == 0)
{
// Bail, if we've decoded enough,
return true;
}
if (m_headerFlags & GC_INFO_REVERSE_PINVOKE_FRAME)
{
m_ReversePInvokeFrameStackSlot = GcInfoEncoding::DENORMALIZE_STACK_SLOT((INT32)m_Reader.DecodeVarLengthSigned(GcInfoEncoding::REVERSE_PINVOKE_FRAME_ENCBASE));
}
else
{
m_ReversePInvokeFrameStackSlot = NO_REVERSE_PINVOKE_FRAME;
}
remainingFlags &= ~DECODE_REVERSE_PINVOKE_VAR;
if (remainingFlags == 0)
{
// Bail, if we've decoded enough,
return true;
}
if constexpr (GcInfoEncoding::HAS_FIXED_STACK_PARAMETER_SCRATCH_AREA)
{
m_SizeOfStackOutgoingAndScratchArea = GcInfoEncoding::DENORMALIZE_SIZE_OF_STACK_AREA((UINT32)m_Reader.DecodeVarLengthUnsigned(GcInfoEncoding::SIZE_OF_STACK_AREA_ENCBASE));
}
return false;
}
template <typename GcInfoEncoding>
TGcInfoDecoder<GcInfoEncoding>::TGcInfoDecoder(
GCInfoToken gcInfoToken,
GcInfoDecoderFlags flags,
UINT32 breakOffset
)
: m_Reader(dac_cast<PTR_CBYTE>(gcInfoToken.Info))
, m_InstructionOffset(breakOffset)
, m_IsInterruptible(false)
, m_ReturnKind(RT_Illegal)
#ifdef _DEBUG
, m_Flags( flags )
, m_GcInfoAddress(dac_cast<PTR_CBYTE>(gcInfoToken.Info))
#endif
, m_Version(gcInfoToken.Version)
{
_ASSERTE( (flags & (DECODE_INTERRUPTIBILITY | DECODE_GC_LIFETIMES)) || (0 == breakOffset) );
// The current implementation doesn't support the two flags together
_ASSERTE(
((flags & (DECODE_INTERRUPTIBILITY | DECODE_GC_LIFETIMES)) != (DECODE_INTERRUPTIBILITY | DECODE_GC_LIFETIMES))
);
//--------------------------------------------
// Pre-decode information
//--------------------------------------------
bool slimHeader = (m_Reader.ReadOneFast() == 0);
// Use flag mask to bail out early if we already decoded all the pieces that caller requested
int remainingFlags = flags == DECODE_EVERYTHING ? ~0 : flags;
if (!slimHeader)
{
if (PredecodeFatHeader(remainingFlags))
return;
}
else
{
if (m_Reader.ReadOneFast())
{
m_headerFlags = GC_INFO_HAS_STACK_BASE_REGISTER;
m_StackBaseRegister = GcInfoEncoding::DENORMALIZE_STACK_BASE_REGISTER(0);
}
else
{
m_headerFlags = (GcInfoHeaderFlags)0;
m_StackBaseRegister = NO_STACK_BASE_REGISTER;
}
#ifdef DECODE_OLD_FORMATS
if (Version() < 4)
{
m_ReturnKind = (ReturnKind)((UINT32)m_Reader.Read(GcInfoEncoding::SIZE_OF_RETURN_KIND_IN_SLIM_HEADER));
}
#endif
remainingFlags &= ~(DECODE_RETURN_KIND | DECODE_VARARG);
#if defined(TARGET_ARM) || defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
remainingFlags &= ~DECODE_HAS_TAILCALLS;
#endif
if (remainingFlags == 0)
{
// Bail, if we've decoded enough,
return;
}
m_CodeLength = GcInfoEncoding::DENORMALIZE_CODE_LENGTH((UINT32)m_Reader.DecodeVarLengthUnsigned(GcInfoEncoding::CODE_LENGTH_ENCBASE));
//
// predecoding the rest of slim header does not require any reading.
//
m_ValidRangeStart = m_ValidRangeEnd = 0;
m_GSCookieStackSlot = NO_GS_COOKIE;
m_PSPSymStackSlot = NO_PSP_SYM;
m_GenericsInstContextStackSlot = NO_GENERICS_INST_CONTEXT;
m_SizeOfEditAndContinuePreservedArea = NO_SIZE_OF_EDIT_AND_CONTINUE_PRESERVED_AREA;
#ifdef TARGET_ARM64
m_SizeOfEditAndContinueFixedStackFrame = 0;
#endif
m_ReversePInvokeFrameStackSlot = NO_REVERSE_PINVOKE_FRAME;
if constexpr (GcInfoEncoding::HAS_FIXED_STACK_PARAMETER_SCRATCH_AREA)
{
m_SizeOfStackOutgoingAndScratchArea = 0;
}
remainingFlags &= ~(DECODE_CODE_LENGTH
| DECODE_PROLOG_LENGTH
| DECODE_GS_COOKIE
| DECODE_PSP_SYM
| DECODE_GENERICS_INST_CONTEXT
| DECODE_EDIT_AND_CONTINUE
| DECODE_REVERSE_PINVOKE_VAR
);
if (remainingFlags == 0)
{
// Bail, if we've decoded enough,
return;
}
}
#ifdef PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
m_NumSafePoints = (UINT32) m_Reader.DecodeVarLengthUnsigned(GcInfoEncoding::NUM_SAFE_POINTS_ENCBASE);
m_SafePointIndex = m_NumSafePoints;
#endif
if (slimHeader)
{
m_NumInterruptibleRanges = 0;
}
else
{
m_NumInterruptibleRanges = (UINT32) m_Reader.DecodeVarLengthUnsigned(GcInfoEncoding::NUM_INTERRUPTIBLE_RANGES_ENCBASE);
}
#ifdef PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
if(flags & (DECODE_GC_LIFETIMES | DECODE_INTERRUPTIBILITY))
{
if(m_NumSafePoints)
{
UINT32 offset = m_InstructionOffset;
#ifdef DECODE_OLD_FORMATS
if (Version() < 4 && (flags & DECODE_INTERRUPTIBILITY))
{
// Safepoints are encoded with a -1 adjustment
// DECODE_GC_LIFETIMES adjusts the offset accordingly, but DECODE_INTERRUPTIBILITY does not
// adjust here
offset--;
}
#endif
m_SafePointIndex = FindSafePoint(offset);
}
}
else if(flags & DECODE_FOR_RANGES_CALLBACK)
{
// Note that normalization as a code offset can be different than
// normalization as code length
UINT32 normCodeLength = NormalizeCodeOffset(m_CodeLength);
UINT32 numBitsPerOffset = CeilOfLog2(normCodeLength);
m_Reader.Skip(m_NumSafePoints * numBitsPerOffset);
}
#endif
// we do not support both DECODE_INTERRUPTIBILITY and DECODE_FOR_RANGES_CALLBACK at the same time
// as both will enumerate and consume interruptible ranges.
_ASSERTE((flags & (DECODE_INTERRUPTIBILITY | DECODE_FOR_RANGES_CALLBACK)) !=
(DECODE_INTERRUPTIBILITY | DECODE_FOR_RANGES_CALLBACK));
_ASSERTE(!m_IsInterruptible);
if(flags & DECODE_INTERRUPTIBILITY)
{
EnumerateInterruptibleRanges(&SetIsInterruptibleCB, this);
}
}
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::IsInterruptible()
{
_ASSERTE( m_Flags & DECODE_INTERRUPTIBILITY );
return m_IsInterruptible;
}
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::HasInterruptibleRanges()
{
_ASSERTE(m_Flags & (DECODE_INTERRUPTIBILITY | DECODE_GC_LIFETIMES));
return m_NumInterruptibleRanges > 0;
}
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::IsSafePoint()
{
_ASSERTE(m_Flags & (DECODE_INTERRUPTIBILITY | DECODE_GC_LIFETIMES));
return m_SafePointIndex != m_NumSafePoints;
}
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::CouldBeSafePoint()
{
// This is used in asserts. Ideally it would return false
// if current location canot possibly be a safepoint.
// However in some cases we optimize away "boring" callsites when no variables are tracked.
// So there is no way to tell precisely that a point is indeed not a safe point.
// Thus we do what we can here, but this could be better if we could have more data
return m_NumInterruptibleRanges == 0;
}
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::HasMethodDescGenericsInstContext()
{
_ASSERTE( m_Flags & DECODE_GENERICS_INST_CONTEXT );
return (m_headerFlags & GC_INFO_HAS_GENERICS_INST_CONTEXT_MASK) == GC_INFO_HAS_GENERICS_INST_CONTEXT_MD;
}
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::HasMethodTableGenericsInstContext()
{
_ASSERTE( m_Flags & DECODE_GENERICS_INST_CONTEXT );
return (m_headerFlags & GC_INFO_HAS_GENERICS_INST_CONTEXT_MASK) == GC_INFO_HAS_GENERICS_INST_CONTEXT_MT;
}
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::HasStackBaseRegister()
{
return (m_headerFlags & GC_INFO_HAS_STACK_BASE_REGISTER) == GC_INFO_HAS_STACK_BASE_REGISTER;
}
#ifdef PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
// This is used for gcinfodumper: is the given offset
// a call-return offset with partially-interruptible GC info?
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::IsSafePoint(UINT32 codeOffset)
{
_ASSERTE(m_Flags == DECODE_EVERYTHING && m_InstructionOffset == 0);
if(m_NumSafePoints == 0)
return false;
#ifdef DECODE_OLD_FORMATS
if (Version() < 4)
{
// Safepoints are encoded with a -1 adjustment, adjust before searching.
codeOffset--;
}
#endif
size_t savedPos = m_Reader.GetCurrentPos();
UINT32 safePointIndex = FindSafePoint(codeOffset);
m_Reader.SetCurrentPos(savedPos);
return (bool) (safePointIndex != m_NumSafePoints);
}
// Repositioning within a bit stream is an involved operation, compared to sequential read,
// so we prefer linear search unless the number of safepoints is too high.
// The limit is not very significant as most methods will have just a few safe points.
// At 32, even if a single point is 16bit encoded (64K method length),
// the whole run will be under 64 bytes, so likely we will stay in the same cache line.
#define MAX_LINEAR_SEARCH 32
template <typename GcInfoEncoding>
NOINLINE
UINT32 TGcInfoDecoder<GcInfoEncoding>::NarrowSafePointSearch(size_t savedPos, UINT32 breakOffset, UINT32* searchEnd)
{
INT32 low = 0;
INT32 high = (INT32)m_NumSafePoints;
const UINT32 numBitsPerOffset = CeilOfLog2(NormalizeCodeOffset(m_CodeLength));
while (high - low > MAX_LINEAR_SEARCH)
{
const INT32 mid = (low + high) / 2;
_ASSERTE(mid >= 0 && mid < (INT32)m_NumSafePoints);
m_Reader.SetCurrentPos(savedPos + (UINT32)mid * numBitsPerOffset);
UINT32 midSpOffset = (UINT32)m_Reader.Read(numBitsPerOffset);
if (breakOffset < midSpOffset)
high = mid;
else
low = mid;
}
m_Reader.SetCurrentPos(savedPos +(UINT32)low * numBitsPerOffset);
*searchEnd = high;
return low;
}
template <typename GcInfoEncoding> UINT32 TGcInfoDecoder<GcInfoEncoding>::FindSafePoint(UINT32 breakOffset)
{
_ASSERTE(m_NumSafePoints > 0);
UINT32 result = m_NumSafePoints;
const size_t savedPos = m_Reader.GetCurrentPos();
const UINT32 numBitsPerOffset = CeilOfLog2(NormalizeCodeOffset(m_CodeLength));
const UINT32 normBreakOffset = NormalizeCodeOffset(breakOffset);
UINT32 linearSearchStart = 0;
UINT32 linearSearchEnd = m_NumSafePoints;
if (linearSearchEnd - linearSearchStart > MAX_LINEAR_SEARCH)
{
linearSearchStart = NarrowSafePointSearch(savedPos, normBreakOffset, &linearSearchEnd);
}
for (UINT32 i = linearSearchStart; i < linearSearchEnd; i++)
{
UINT32 spOffset = (UINT32)m_Reader.Read(numBitsPerOffset);
if (spOffset == normBreakOffset)
{
result = i;
break;
}
if (spOffset > normBreakOffset)
{
break;
}
}
// Cannot just set the "savedPos + m_NumSafePoints * numBitsPerOffset" as
// there could be no more data if method tracks no variables of any kind.
// Must use Skip, which handles potential stream end.
m_Reader.Skip(savedPos + m_NumSafePoints * numBitsPerOffset - m_Reader.GetCurrentPos());
return result;
}
template <typename GcInfoEncoding> void TGcInfoDecoder<GcInfoEncoding>::EnumerateSafePoints(EnumerateSafePointsCallback *pCallback, void * hCallback)
{
if(m_NumSafePoints == 0)
return;
const UINT32 numBitsPerOffset = CeilOfLog2(NormalizeCodeOffset(m_CodeLength));
for(UINT32 i = 0; i < m_NumSafePoints; i++)
{
UINT32 normOffset = (UINT32)m_Reader.Read(numBitsPerOffset);
UINT32 offset = DenormalizeCodeOffset(normOffset);
#ifdef DECODE_OLD_FORMATS
if (Version() < 4)
{
// Safepoints are encoded with a -1 adjustment, adjust before reporting
offset++;
}
#endif
pCallback(this, offset, hCallback);
}
}
#endif
template <typename GcInfoEncoding> void TGcInfoDecoder<GcInfoEncoding>::EnumerateInterruptibleRanges (
EnumerateInterruptibleRangesCallback *pCallback,
void * hCallback)
{
// If no info is found for the call site, we default to fully-interruptible
LOG((LF_GCROOTS, LL_INFO1000000, "No GC info found for call site at offset %x. Defaulting to fully-interruptible information.\n", (int) m_InstructionOffset));
UINT32 lastInterruptibleRangeStopOffsetNormalized = 0;
for(UINT32 i=0; i<m_NumInterruptibleRanges; i++)
{
UINT32 normStartDelta = (UINT32) m_Reader.DecodeVarLengthUnsigned( GcInfoEncoding::INTERRUPTIBLE_RANGE_DELTA1_ENCBASE );
UINT32 normStopDelta = (UINT32) m_Reader.DecodeVarLengthUnsigned( GcInfoEncoding::INTERRUPTIBLE_RANGE_DELTA2_ENCBASE ) + 1;
UINT32 rangeStartOffsetNormalized = lastInterruptibleRangeStopOffsetNormalized + normStartDelta;
UINT32 rangeStopOffsetNormalized = rangeStartOffsetNormalized + normStopDelta;
UINT32 rangeStartOffset = DenormalizeCodeOffset(rangeStartOffsetNormalized);
UINT32 rangeStopOffset = DenormalizeCodeOffset(rangeStopOffsetNormalized);
bool fStop = pCallback(rangeStartOffset, rangeStopOffset, hCallback);
if (fStop)
return;
lastInterruptibleRangeStopOffsetNormalized = rangeStopOffsetNormalized;
}
}
template <typename GcInfoEncoding> INT32 TGcInfoDecoder<GcInfoEncoding>::GetGSCookieStackSlot()
{
_ASSERTE( m_Flags & DECODE_GS_COOKIE );
return m_GSCookieStackSlot;
}
template <typename GcInfoEncoding> INT32 TGcInfoDecoder<GcInfoEncoding>::GetReversePInvokeFrameStackSlot()
{
_ASSERTE(m_Flags & DECODE_REVERSE_PINVOKE_VAR);
return m_ReversePInvokeFrameStackSlot;
}
template <typename GcInfoEncoding> UINT32 TGcInfoDecoder<GcInfoEncoding>::GetGSCookieValidRangeStart()
{
_ASSERTE( m_Flags & DECODE_GS_COOKIE );
return m_ValidRangeStart;
}
template <typename GcInfoEncoding> UINT32 TGcInfoDecoder<GcInfoEncoding>::GetGSCookieValidRangeEnd()
{
_ASSERTE( m_Flags & DECODE_GS_COOKIE );
return m_ValidRangeEnd;
}
template <typename GcInfoEncoding> UINT32 TGcInfoDecoder<GcInfoEncoding>::GetPrologSize()
{
_ASSERTE( m_Flags & DECODE_PROLOG_LENGTH );
return m_ValidRangeStart;
}
template <typename GcInfoEncoding> INT32 TGcInfoDecoder<GcInfoEncoding>::GetGenericsInstContextStackSlot()
{
_ASSERTE( m_Flags & DECODE_GENERICS_INST_CONTEXT );
return m_GenericsInstContextStackSlot;
}
template <typename GcInfoEncoding> INT32 TGcInfoDecoder<GcInfoEncoding>::GetPSPSymStackSlot()
{
_ASSERTE( m_Flags & DECODE_PSP_SYM );
return m_PSPSymStackSlot;
}
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::GetIsVarArg()
{
_ASSERTE( m_Flags & DECODE_VARARG );
return m_headerFlags & GC_INFO_IS_VARARG;
}
#if defined(TARGET_ARM) || defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::HasTailCalls()
{
_ASSERTE( m_Flags & DECODE_HAS_TAILCALLS );
return ((m_headerFlags & GC_INFO_HAS_TAILCALLS) != 0);
}
#endif // TARGET_ARM || TARGET_ARM64 || TARGET_LOONGARCH64 || TARGET_RISCV64
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::WantsReportOnlyLeaf()
{
// Only AMD64 with JIT64 can return false here.
#if defined(TARGET_AMD64) && defined(DECODE_OLD_FORMATS)
return ((m_headerFlags & GC_INFO_WANTS_REPORT_ONLY_LEAF) != 0);
#else
return true;
#endif
}
template <typename GcInfoEncoding> UINT32 TGcInfoDecoder<GcInfoEncoding>::GetCodeLength()
{
// SUPPORTS_DAC;
_ASSERTE( m_Flags & DECODE_CODE_LENGTH );
return m_CodeLength;
}
template <typename GcInfoEncoding> ReturnKind TGcInfoDecoder<GcInfoEncoding>::GetReturnKind()
{
// SUPPORTS_DAC;
_ASSERTE(m_Flags & DECODE_RETURN_KIND);
return m_ReturnKind;
}
template <typename GcInfoEncoding> UINT32 TGcInfoDecoder<GcInfoEncoding>::GetStackBaseRegister()
{
return m_StackBaseRegister;
}
template <typename GcInfoEncoding> UINT32 TGcInfoDecoder<GcInfoEncoding>::GetSizeOfEditAndContinuePreservedArea()
{
_ASSERTE( m_Flags & DECODE_EDIT_AND_CONTINUE );
return m_SizeOfEditAndContinuePreservedArea;
}
#ifdef TARGET_ARM64
template <typename GcInfoEncoding> UINT32 TGcInfoDecoder<GcInfoEncoding>::GetSizeOfEditAndContinueFixedStackFrame()
{
_ASSERTE( m_Flags & DECODE_EDIT_AND_CONTINUE );
return m_SizeOfEditAndContinueFixedStackFrame;
}
#endif
template <typename GcInfoEncoding> size_t TGcInfoDecoder<GcInfoEncoding>::GetNumBytesRead()
{
return (m_Reader.GetCurrentPos() + 7) / 8;
}
template <typename GcInfoEncoding> UINT32 TGcInfoDecoder<GcInfoEncoding>::GetSizeOfStackParameterArea()
{
return m_SizeOfStackOutgoingAndScratchArea;
}
template <typename GcInfoEncoding> bool TGcInfoDecoder<GcInfoEncoding>::EnumerateLiveSlots(
PREGDISPLAY pRD,
bool reportScratchSlots,
unsigned inputFlags,
GCEnumCallback pCallBack,
void * hCallBack
)
{
unsigned executionAborted = (inputFlags & ExecutionAborted);
// In order to make ARM more x86-like we only ever report the leaf frame
// of any given function. We accomplish this by having the stackwalker
// pass a flag whenever walking the frame of a method where it has
// previously visited a child funclet
if (WantsReportOnlyLeaf() && (inputFlags & ParentOfFuncletStackFrame))
{
LOG((LF_GCROOTS, LL_INFO100000, "Not reporting this frame because it was already reported via another funclet.\n"));
return true;
}
_ASSERTE(GC_SLOT_INTERIOR == GC_CALL_INTERIOR);
_ASSERTE(GC_SLOT_PINNED == GC_CALL_PINNED);
_ASSERTE( m_Flags & DECODE_GC_LIFETIMES );
GcSlotDecoder<GcInfoEncoding> slotDecoder;
UINT32 normBreakOffset = NormalizeCodeOffset(m_InstructionOffset);
// Normalized break offset
// Relative to interruptible ranges #if PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
#ifdef PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
UINT32 pseudoBreakOffset = 0;
UINT32 numInterruptibleLength = 0;
#else
UINT32 pseudoBreakOffset = normBreakOffset;
UINT32 numInterruptibleLength = NORMALIZE_CODE_OFFSET(m_CodeLength);
#endif
#ifdef PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
if(m_SafePointIndex < m_NumSafePoints && !executionAborted)
{
// Skip interruptibility information
for(UINT32 i=0; i<m_NumInterruptibleRanges; i++)
{
m_Reader.DecodeVarLengthUnsigned( GcInfoEncoding::INTERRUPTIBLE_RANGE_DELTA1_ENCBASE );
m_Reader.DecodeVarLengthUnsigned( GcInfoEncoding::INTERRUPTIBLE_RANGE_DELTA2_ENCBASE );
}
}
else
{
//
// We didn't find the break offset in the list of call sites
// or we are in an executionAborted frame
// So either we have fully-interruptible information,
// or execution will not resume at the current method
// and nothing should be reported
//
int countIntersections = 0;
UINT32 lastNormStop = 0;
for(UINT32 i=0; i<m_NumInterruptibleRanges; i++)
{
UINT32 normStartDelta = (UINT32) m_Reader.DecodeVarLengthUnsigned( GcInfoEncoding::INTERRUPTIBLE_RANGE_DELTA1_ENCBASE );
UINT32 normStopDelta = (UINT32) m_Reader.DecodeVarLengthUnsigned( GcInfoEncoding::INTERRUPTIBLE_RANGE_DELTA2_ENCBASE ) + 1;
UINT32 normStart = lastNormStop + normStartDelta;
UINT32 normStop = normStart + normStopDelta;
if(normBreakOffset >= normStart && normBreakOffset < normStop)
{
_ASSERTE(pseudoBreakOffset == 0);
countIntersections++;
pseudoBreakOffset = numInterruptibleLength + normBreakOffset - normStart;
}
numInterruptibleLength += normStopDelta;
lastNormStop = normStop;
}
_ASSERTE(countIntersections <= 1);
if(countIntersections == 0 && executionAborted)
{
LOG((LF_GCROOTS, LL_INFO100000, "Not reporting this frame because it is aborted and not fully interruptible.\n"));
goto ExitSuccess;
}
}
#else // !PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
// Skip interruptibility information
for(UINT32 i=0; i<m_NumInterruptibleRanges; i++)
{
m_Reader.DecodeVarLengthUnsigned( INTERRUPTIBLE_RANGE_DELTA1_ENCBASE );
m_Reader.DecodeVarLengthUnsigned( INTERRUPTIBLE_RANGE_DELTA2_ENCBASE );
}
#endif
//------------------------------------------------------------------------------
// Read the slot table
//------------------------------------------------------------------------------
slotDecoder.DecodeSlotTable(m_Reader);
{
UINT32 numSlots = slotDecoder.GetNumTracked();
if(!numSlots)
goto ReportUntracked;
#ifdef PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
UINT32 numBitsPerOffset = 0;
// Duplicate the encoder's heuristic to determine if we have indirect live
// slot table (similar to the chunk pointers)
if ((m_NumSafePoints > 0) && m_Reader.ReadOneFast())
{
numBitsPerOffset = (UINT32) m_Reader.DecodeVarLengthUnsigned(GcInfoEncoding::POINTER_SIZE_ENCBASE) + 1;
_ASSERTE(numBitsPerOffset != 0);
}
//------------------------------------------------------------------------------
// Try partially interruptible first
//------------------------------------------------------------------------------
if( !executionAborted && m_SafePointIndex != m_NumSafePoints )
{
if (numBitsPerOffset)
{
const size_t offsetTablePos = m_Reader.GetCurrentPos();
m_Reader.Skip(m_SafePointIndex * numBitsPerOffset);
const size_t liveStatesOffset = m_Reader.Read(numBitsPerOffset);
const size_t liveStatesStart = ((offsetTablePos + m_NumSafePoints * numBitsPerOffset + 7) & (~7));
m_Reader.SetCurrentPos(liveStatesStart + liveStatesOffset);
if (m_Reader.ReadOneFast()) {
// RLE encoded
bool fSkip = (m_Reader.ReadOneFast() == 0);
bool fReport = true;
UINT32 readSlots = (UINT32)m_Reader.DecodeVarLengthUnsigned( fSkip ? GcInfoEncoding::LIVESTATE_RLE_SKIP_ENCBASE : GcInfoEncoding::LIVESTATE_RLE_RUN_ENCBASE );
fSkip = !fSkip;
while (readSlots < numSlots)
{
UINT32 cnt = (UINT32)m_Reader.DecodeVarLengthUnsigned( fSkip ? GcInfoEncoding::LIVESTATE_RLE_SKIP_ENCBASE : GcInfoEncoding::LIVESTATE_RLE_RUN_ENCBASE ) + 1;
if (fReport)
{
for(UINT32 slotIndex = readSlots; slotIndex < readSlots + cnt; slotIndex++)
{
ReportSlotToGC(slotDecoder,
slotIndex,
pRD,
reportScratchSlots,
inputFlags,
pCallBack,
hCallBack
);
}
}
readSlots += cnt;
fSkip = !fSkip;
fReport = !fReport;
}
_ASSERTE(readSlots == numSlots);
goto ReportUntracked;
}
// Just a normal live state (1 bit per slot), so use the normal decoding loop
}
else
{
m_Reader.Skip(m_SafePointIndex * numSlots);
}
for(UINT32 slotIndex = 0; slotIndex < numSlots; slotIndex++)
{
if(m_Reader.ReadOneFast())
{
ReportSlotToGC(
slotDecoder,
slotIndex,
pRD,
reportScratchSlots,
inputFlags,
pCallBack,
hCallBack
);
}
}
goto ReportUntracked;
}
else
{
m_Reader.Skip(m_NumSafePoints * numSlots);
if(m_NumInterruptibleRanges == 0)
goto ReportUntracked;
}
#endif // PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
_ASSERTE(m_NumInterruptibleRanges);
_ASSERTE(numInterruptibleLength);
// If no info is found for the call site, we default to fully-interruptible
LOG((LF_GCROOTS, LL_INFO1000000, "No GC info found for call site at offset %x. Defaulting to fully-interruptible information.\n", (int) m_InstructionOffset));
UINT32 numChunks = (numInterruptibleLength + GcInfoEncoding::NUM_NORM_CODE_OFFSETS_PER_CHUNK - 1) / GcInfoEncoding::NUM_NORM_CODE_OFFSETS_PER_CHUNK;
UINT32 breakChunk = pseudoBreakOffset / GcInfoEncoding::NUM_NORM_CODE_OFFSETS_PER_CHUNK;
_ASSERTE(breakChunk < numChunks);
UINT32 numBitsPerPointer = (UINT32) m_Reader.DecodeVarLengthUnsigned(GcInfoEncoding::POINTER_SIZE_ENCBASE);
if(!numBitsPerPointer)
goto ReportUntracked;
size_t pointerTablePos = m_Reader.GetCurrentPos();
size_t chunkPointer;
UINT32 chunk = breakChunk;
for(;;)
{
m_Reader.SetCurrentPos(pointerTablePos + chunk * numBitsPerPointer);
chunkPointer = m_Reader.Read(numBitsPerPointer);
if(chunkPointer)
break;
if(chunk-- == 0)
goto ReportUntracked;
}
size_t chunksStartPos = ((pointerTablePos + numChunks * numBitsPerPointer + 7) & (~7));
size_t chunkPos = chunksStartPos + chunkPointer - 1;
m_Reader.SetCurrentPos(chunkPos);
{
BitStreamReader couldBeLiveReader(m_Reader);
UINT32 numCouldBeLiveSlots = 0;
// A potentially compressed bit vector of which slots have any lifetimes
if (m_Reader.ReadOneFast())
{
// RLE encoded
bool fSkip = (m_Reader.ReadOneFast() == 0);
bool fReport = true;
UINT32 readSlots = (UINT32)m_Reader.DecodeVarLengthUnsigned( fSkip ? GcInfoEncoding::LIVESTATE_RLE_SKIP_ENCBASE : GcInfoEncoding::LIVESTATE_RLE_RUN_ENCBASE );
fSkip = !fSkip;
while (readSlots < numSlots)
{
UINT32 cnt = (UINT32)m_Reader.DecodeVarLengthUnsigned( fSkip ? GcInfoEncoding::LIVESTATE_RLE_SKIP_ENCBASE : GcInfoEncoding::LIVESTATE_RLE_RUN_ENCBASE ) + 1;
if (fReport)
{
numCouldBeLiveSlots += cnt;
}
readSlots += cnt;
fSkip = !fSkip;
fReport = !fReport;
}
_ASSERTE(readSlots == numSlots);
}
else
{
for(UINT32 i = 0; i < numSlots; i++)
{
if(m_Reader.ReadOneFast())
numCouldBeLiveSlots++;
}
}
_ASSERTE(numCouldBeLiveSlots > 0);
BitStreamReader finalStateReader(m_Reader);
m_Reader.Skip(numCouldBeLiveSlots);
int lifetimeTransitionsCount = 0;
UINT32 slotIndex = 0;