-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathcodeversion.cpp
2287 lines (2016 loc) · 70.7 KB
/
codeversion.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.
// ===========================================================================
// File: CodeVersion.cpp
//
// ===========================================================================
#include "common.h"
#include "codeversion.h"
#include "patchpointinfo.h"
#ifdef FEATURE_CODE_VERSIONING
#include "threadsuspend.h"
#include "methoditer.h"
#include "../debug/ee/debugger.h"
#include "../debug/ee/walker.h"
#include "../debug/ee/controller.h"
#endif // FEATURE_CODE_VERSIONING
#ifndef FEATURE_CODE_VERSIONING
//
// When not using code versioning we've got a minimal implementation of
// NativeCodeVersion that simply wraps a MethodDesc* with no additional
// versioning information
//
NativeCodeVersion::NativeCodeVersion(PTR_MethodDesc pMethod) : m_pMethodDesc(pMethod) {}
BOOL NativeCodeVersion::IsDefaultVersion() const { return TRUE; }
PCODE NativeCodeVersion::GetNativeCode() const { return m_pMethodDesc->GetNativeCode(); }
#ifndef DACCESS_COMPILE
BOOL NativeCodeVersion::SetNativeCodeInterlocked(PCODE pCode, PCODE pExpected) { return m_pMethodDesc->SetNativeCodeInterlocked(pCode, pExpected); }
#endif
#ifdef HAVE_GCCOVER
PTR_GCCoverageInfo NativeCodeVersion::GetGCCoverageInfo() const { return GetMethodDesc()->m_GcCover; }
void NativeCodeVersion::SetGCCoverageInfo(PTR_GCCoverageInfo gcCover)
{
MethodDesc *pMD = GetMethodDesc();
_ASSERTE(gcCover == NULL || pMD->m_GcCover == NULL);
pMD->m_GcCover = gcCover;
}
#endif
#else // FEATURE_CODE_VERSIONING
// This is just used as a unique id. Overflow is OK. If we happen to have more than 4+Billion rejits
// and somehow manage to not run out of memory, we'll just have to redefine ReJITID as size_t.
/* static */
static ReJITID s_GlobalReJitId = 1;
#ifndef DACCESS_COMPILE
NativeCodeVersionNode::NativeCodeVersionNode(
NativeCodeVersionId id,
MethodDesc* pMethodDesc,
ReJITID parentId,
NativeCodeVersion::OptimizationTier optimizationTier,
PatchpointInfo* patchpointInfo,
unsigned ilOffset)
:
m_pNativeCode{},
m_pMethodDesc(pMethodDesc),
m_parentId(parentId),
m_pNextMethodDescSibling(NULL),
m_id(id),
#ifdef FEATURE_TIERED_COMPILATION
m_optTier(optimizationTier),
#endif
#ifdef HAVE_GCCOVER
m_gcCover(PTR_NULL),
#endif
#ifdef FEATURE_ON_STACK_REPLACEMENT
m_patchpointInfo(patchpointInfo),
m_ilOffset(ilOffset),
#endif
m_flags(0)
{}
#endif
PCODE NativeCodeVersionNode::GetNativeCode() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_pNativeCode;
}
ReJITID NativeCodeVersionNode::GetILVersionId() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_parentId;
}
ILCodeVersion NativeCodeVersionNode::GetILCodeVersion() const
{
LIMITED_METHOD_DAC_CONTRACT;
PTR_MethodDesc pMD = GetMethodDesc();
return pMD->GetCodeVersionManager()->GetILCodeVersion(pMD, GetILVersionId());
}
NativeCodeVersionId NativeCodeVersionNode::GetVersionId() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_id;
}
#ifndef DACCESS_COMPILE
BOOL NativeCodeVersionNode::SetNativeCodeInterlocked(PCODE pCode, PCODE pExpected)
{
LIMITED_METHOD_CONTRACT;
return InterlockedCompareExchangeT(&m_pNativeCode,
(TADDR&)pCode, (TADDR&)pExpected) == (TADDR&)pExpected;
}
#endif
BOOL NativeCodeVersionNode::IsActiveChildVersion() const
{
LIMITED_METHOD_DAC_CONTRACT;
_ASSERTE(CodeVersionManager::IsLockOwnedByCurrentThread());
return (m_flags & IsActiveChildFlag) != 0;
}
#ifndef DACCESS_COMPILE
void NativeCodeVersionNode::SetActiveChildFlag(BOOL isActive)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(CodeVersionManager::IsLockOwnedByCurrentThread());
if (isActive)
{
m_flags |= IsActiveChildFlag;
}
else
{
m_flags &= ~IsActiveChildFlag;
}
}
#endif
#ifdef FEATURE_TIERED_COMPILATION
NativeCodeVersion::OptimizationTier NativeCodeVersionNode::GetOptimizationTier() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_optTier;
}
#ifndef DACCESS_COMPILE
void NativeCodeVersionNode::SetOptimizationTier(NativeCodeVersion::OptimizationTier tier)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(
tier == m_optTier ||
(m_optTier != NativeCodeVersion::OptimizationTier::OptimizationTier1 &&
m_optTier != NativeCodeVersion::OptimizationTier::OptimizationTierOptimized));
m_optTier = tier;
}
#endif
#endif // FEATURE_TIERED_COMPILATION
#ifdef FEATURE_ON_STACK_REPLACEMENT
PatchpointInfo* NativeCodeVersionNode::GetOSRInfo(unsigned * ilOffset) const
{
LIMITED_METHOD_DAC_CONTRACT;
*ilOffset = m_ilOffset;
return m_patchpointInfo;
}
#endif // FEATURE_ON_STACK_REPLACEMENT
#ifdef HAVE_GCCOVER
PTR_GCCoverageInfo NativeCodeVersionNode::GetGCCoverageInfo() const
{
LIMITED_METHOD_CONTRACT;
return m_gcCover;
}
void NativeCodeVersionNode::SetGCCoverageInfo(PTR_GCCoverageInfo gcCover)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(gcCover == NULL || m_gcCover == NULL);
m_gcCover = gcCover;
}
#endif // HAVE_GCCOVER
NativeCodeVersion::NativeCodeVersion(PTR_NativeCodeVersionNode pVersionNode) :
m_storageKind(pVersionNode != NULL ? StorageKind::Explicit : StorageKind::Unknown),
m_pVersionNode(pVersionNode)
{}
NativeCodeVersion::NativeCodeVersion(PTR_MethodDesc pMethod) :
m_storageKind(pMethod != NULL ? StorageKind::Synthetic : StorageKind::Unknown)
{
LIMITED_METHOD_DAC_CONTRACT;
m_synthetic.m_pMethodDesc = pMethod;
}
BOOL NativeCodeVersion::IsDefaultVersion() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_storageKind == StorageKind::Synthetic;
}
PCODE NativeCodeVersion::GetNativeCode() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->GetNativeCode();
}
else
{
return GetMethodDesc()->GetNativeCode();
}
}
ReJITID NativeCodeVersion::GetILCodeVersionId() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->GetILVersionId();
}
else
{
return 0;
}
}
ILCodeVersion NativeCodeVersion::GetILCodeVersion() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->GetILCodeVersion();
}
else
{
PTR_MethodDesc pMethod = GetMethodDesc();
return ILCodeVersion(dac_cast<PTR_Module>(pMethod->GetModule()), pMethod->GetMemberDef());
}
}
#ifndef DACCESS_COMPILE
BOOL NativeCodeVersion::SetNativeCodeInterlocked(PCODE pCode, PCODE pExpected)
{
LIMITED_METHOD_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->SetNativeCodeInterlocked(pCode, pExpected);
}
else
{
return GetMethodDesc()->SetNativeCodeInterlocked(pCode, pExpected);
}
}
#endif
BOOL NativeCodeVersion::IsActiveChildVersion() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->IsActiveChildVersion();
}
else
{
MethodDescVersioningState* pMethodVersioningState = GetMethodDescVersioningState();
if (pMethodVersioningState == NULL)
{
return TRUE;
}
return pMethodVersioningState->IsDefaultVersionActiveChild();
}
}
PTR_MethodDescVersioningState NativeCodeVersion::GetMethodDescVersioningState() const
{
LIMITED_METHOD_DAC_CONTRACT;
PTR_MethodDesc pMethodDesc = GetMethodDesc();
CodeVersionManager* pCodeVersionManager = pMethodDesc->GetCodeVersionManager();
return pCodeVersionManager->GetMethodDescVersioningState(pMethodDesc);
}
#ifndef DACCESS_COMPILE
void NativeCodeVersion::SetActiveChildFlag(BOOL isActive)
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
if (isActive &&
!CodeVersionManager::InitialNativeCodeVersionMayNotBeTheDefaultNativeCodeVersion() &&
GetMethodDesc()->GetNativeCode() == (PCODE)NULL)
{
CodeVersionManager::SetInitialNativeCodeVersionMayNotBeTheDefaultNativeCodeVersion();
}
AsNode()->SetActiveChildFlag(isActive);
}
else
{
MethodDescVersioningState* pMethodVersioningState = GetMethodDescVersioningState();
pMethodVersioningState->SetDefaultVersionActiveChildFlag(isActive);
}
}
MethodDescVersioningState* NativeCodeVersion::GetMethodDescVersioningState()
{
LIMITED_METHOD_DAC_CONTRACT;
MethodDesc* pMethodDesc = GetMethodDesc();
CodeVersionManager* pCodeVersionManager = pMethodDesc->GetCodeVersionManager();
return pCodeVersionManager->GetMethodDescVersioningState(pMethodDesc);
}
#endif
#ifdef FEATURE_TIERED_COMPILATION
NativeCodeVersion::OptimizationTier NativeCodeVersion::GetOptimizationTier() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->GetOptimizationTier();
}
else
{
return TieredCompilationManager::GetInitialOptimizationTier(GetMethodDesc());
}
}
bool NativeCodeVersion::IsFinalTier() const
{
LIMITED_METHOD_DAC_CONTRACT;
OptimizationTier tier = GetOptimizationTier();
return tier == OptimizationTier1 || tier == OptimizationTierOptimized;
}
#ifndef DACCESS_COMPILE
void NativeCodeVersion::SetOptimizationTier(OptimizationTier tier)
{
WRAPPER_NO_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
AsNode()->SetOptimizationTier(tier);
}
else
{
// State changes should have been made previously such that the initial tier is the new tier
_ASSERTE(TieredCompilationManager::GetInitialOptimizationTier(GetMethodDesc()) == tier);
}
}
#endif
#endif
#ifdef FEATURE_ON_STACK_REPLACEMENT
PatchpointInfo * NativeCodeVersion::GetOSRInfo(unsigned * ilOffset)
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->GetOSRInfo(ilOffset);
}
else
{
return NULL;
}
}
#endif
#ifdef HAVE_GCCOVER
PTR_GCCoverageInfo NativeCodeVersion::GetGCCoverageInfo() const
{
WRAPPER_NO_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->GetGCCoverageInfo();
}
else
{
return GetMethodDesc()->m_GcCover;
}
}
void NativeCodeVersion::SetGCCoverageInfo(PTR_GCCoverageInfo gcCover)
{
WRAPPER_NO_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
AsNode()->SetGCCoverageInfo(gcCover);
}
else
{
MethodDesc *pMD = GetMethodDesc();
_ASSERTE(gcCover == NULL || pMD->m_GcCover == NULL);
pMD->m_GcCover = gcCover;
}
}
#endif // HAVE_GCCOVER
PTR_NativeCodeVersionNode NativeCodeVersion::AsNode() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return m_pVersionNode;
}
else
{
return NULL;
}
}
#ifndef DACCESS_COMPILE
PTR_NativeCodeVersionNode NativeCodeVersion::AsNode()
{
LIMITED_METHOD_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return m_pVersionNode;
}
else
{
return NULL;
}
}
#endif
NativeCodeVersionCollection::NativeCodeVersionCollection(PTR_MethodDesc pMethodDescFilter, ILCodeVersion ilCodeFilter) :
m_pMethodDescFilter(pMethodDescFilter),
m_ilCodeFilter(ilCodeFilter)
{
}
NativeCodeVersionIterator NativeCodeVersionCollection::Begin()
{
LIMITED_METHOD_DAC_CONTRACT;
return NativeCodeVersionIterator(this);
}
NativeCodeVersionIterator NativeCodeVersionCollection::End()
{
LIMITED_METHOD_DAC_CONTRACT;
return NativeCodeVersionIterator(NULL);
}
NativeCodeVersionIterator::NativeCodeVersionIterator(NativeCodeVersionCollection* pNativeCodeVersionCollection) :
m_stage(IterationStage::Initial),
m_pCollection(pNativeCodeVersionCollection),
m_pLinkedListCur(dac_cast<PTR_NativeCodeVersionNode>(nullptr))
{
LIMITED_METHOD_DAC_CONTRACT;
First();
}
void NativeCodeVersionIterator::First()
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_pCollection == NULL)
{
m_stage = IterationStage::End;
}
Next();
}
void NativeCodeVersionIterator::Next()
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_stage == IterationStage::Initial)
{
ILCodeVersion ilCodeFilter = m_pCollection->m_ilCodeFilter;
m_stage = IterationStage::ImplicitCodeVersion;
if (ilCodeFilter.IsNull() || ilCodeFilter.IsDefaultVersion())
{
m_cur = NativeCodeVersion(m_pCollection->m_pMethodDescFilter);
return;
}
}
if (m_stage == IterationStage::ImplicitCodeVersion)
{
m_stage = IterationStage::LinkedList;
CodeVersionManager* pCodeVersionManager = m_pCollection->m_pMethodDescFilter->GetCodeVersionManager();
MethodDescVersioningState* pMethodDescVersioningState = pCodeVersionManager->GetMethodDescVersioningState(m_pCollection->m_pMethodDescFilter);
if (pMethodDescVersioningState == NULL)
{
m_pLinkedListCur = NULL;
}
else
{
ILCodeVersion ilCodeFilter = m_pCollection->m_ilCodeFilter;
m_pLinkedListCur = pMethodDescVersioningState->GetFirstVersionNode();
while (m_pLinkedListCur != NULL && !ilCodeFilter.IsNull() && ilCodeFilter.GetVersionId() != m_pLinkedListCur->GetILVersionId())
{
m_pLinkedListCur = m_pLinkedListCur->m_pNextMethodDescSibling;
}
}
if (m_pLinkedListCur != NULL)
{
m_cur = NativeCodeVersion(m_pLinkedListCur);
return;
}
}
if (m_stage == IterationStage::LinkedList)
{
if (m_pLinkedListCur != NULL)
{
ILCodeVersion ilCodeFilter = m_pCollection->m_ilCodeFilter;
do
{
m_pLinkedListCur = m_pLinkedListCur->m_pNextMethodDescSibling;
} while (m_pLinkedListCur != NULL && !ilCodeFilter.IsNull() && ilCodeFilter.GetVersionId() != m_pLinkedListCur->GetILVersionId());
}
if (m_pLinkedListCur != NULL)
{
m_cur = NativeCodeVersion(m_pLinkedListCur);
return;
}
else
{
m_stage = IterationStage::End;
m_cur = NativeCodeVersion();
}
}
}
const NativeCodeVersion & NativeCodeVersionIterator::Get() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_cur;
}
bool NativeCodeVersionIterator::Equal(const NativeCodeVersionIterator &i) const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_cur == i.m_cur;
}
ILCodeVersionNode::ILCodeVersionNode() :
m_pModule(dac_cast<PTR_Module>(nullptr)),
m_methodDef(0),
m_rejitId(0),
m_pNextILVersionNode(dac_cast<PTR_ILCodeVersionNode>(nullptr)),
m_rejitState(RejitFlags::kStateRequested),
m_pIL(),
m_jitFlags(0),
m_deoptimized(FALSE)
{
m_pIL.Store(dac_cast<PTR_COR_ILMETHOD>(nullptr));
}
#ifndef DACCESS_COMPILE
ILCodeVersionNode::ILCodeVersionNode(Module* pModule, mdMethodDef methodDef, ReJITID id, BOOL isDeoptimized) :
m_pModule(pModule),
m_methodDef(methodDef),
m_rejitId(id),
m_pNextILVersionNode(dac_cast<PTR_ILCodeVersionNode>(nullptr)),
m_rejitState(RejitFlags::kStateRequested),
m_pIL(nullptr),
m_jitFlags(0),
m_deoptimized(isDeoptimized)
{}
#endif
PTR_Module ILCodeVersionNode::GetModule() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_pModule;
}
mdMethodDef ILCodeVersionNode::GetMethodDef() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_methodDef;
}
ReJITID ILCodeVersionNode::GetVersionId() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_rejitId;
}
RejitFlags ILCodeVersionNode::GetRejitState() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_rejitState.Load() & RejitFlags::kStateMask;
}
BOOL ILCodeVersionNode::GetEnableReJITCallback() const
{
LIMITED_METHOD_DAC_CONTRACT;
return (m_rejitState.Load() & RejitFlags::kSuppressParams) == RejitFlags::kSuppressParams;
}
PTR_COR_ILMETHOD ILCodeVersionNode::GetIL() const
{
LIMITED_METHOD_DAC_CONTRACT;
return dac_cast<PTR_COR_ILMETHOD>(m_pIL.Load());
}
DWORD ILCodeVersionNode::GetJitFlags() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_jitFlags.Load();
}
const InstrumentedILOffsetMapping* ILCodeVersionNode::GetInstrumentedILMap() const
{
LIMITED_METHOD_DAC_CONTRACT;
return &m_instrumentedILMap;
}
PTR_ILCodeVersionNode ILCodeVersionNode::GetNextILVersionNode() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_pNextILVersionNode;
}
BOOL ILCodeVersionNode::IsDeoptimized() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_deoptimized;
}
#ifndef DACCESS_COMPILE
void ILCodeVersionNode::SetRejitState(RejitFlags newState)
{
LIMITED_METHOD_CONTRACT;
// We're doing a non thread safe modification to m_rejitState
_ASSERTE(CodeVersionManager::IsLockOwnedByCurrentThread());
RejitFlags oldNonMaskFlags = m_rejitState.Load() & ~RejitFlags::kStateMask;
m_rejitState.Store(static_cast<RejitFlags>(newState | oldNonMaskFlags));
}
void ILCodeVersionNode::SetEnableReJITCallback(BOOL state)
{
LIMITED_METHOD_CONTRACT;
// We're doing a non thread safe modification to m_rejitState
_ASSERTE(CodeVersionManager::IsLockOwnedByCurrentThread());
RejitFlags oldFlags = m_rejitState.Load();
if (state)
{
m_rejitState.Store(oldFlags | RejitFlags::kSuppressParams);
}
else
{
m_rejitState.Store(oldFlags & ~RejitFlags::kSuppressParams);
}
}
void ILCodeVersionNode::SetIL(COR_ILMETHOD* pIL)
{
LIMITED_METHOD_CONTRACT;
m_pIL.Store(pIL);
}
void ILCodeVersionNode::SetJitFlags(DWORD flags)
{
LIMITED_METHOD_CONTRACT;
m_jitFlags.Store(flags);
}
void ILCodeVersionNode::SetInstrumentedILMap(SIZE_T cMap, COR_IL_MAP * rgMap)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(CodeVersionManager::IsLockOwnedByCurrentThread());
m_instrumentedILMap.SetMappingInfo(cMap, rgMap);
}
void ILCodeVersionNode::SetNextILVersionNode(ILCodeVersionNode* pNextILVersionNode)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(CodeVersionManager::IsLockOwnedByCurrentThread());
m_pNextILVersionNode = pNextILVersionNode;
}
#endif
ILCodeVersion::ILCodeVersion() :
m_storageKind(StorageKind::Unknown)
{}
ILCodeVersion::ILCodeVersion(const ILCodeVersion & ilCodeVersion) :
m_storageKind(ilCodeVersion.m_storageKind)
{
if(m_storageKind == StorageKind::Explicit)
{
m_pVersionNode = ilCodeVersion.m_pVersionNode;
}
else if(m_storageKind == StorageKind::Synthetic)
{
m_synthetic = ilCodeVersion.m_synthetic;
}
}
ILCodeVersion::ILCodeVersion(PTR_ILCodeVersionNode pILCodeVersionNode) :
m_storageKind(pILCodeVersionNode != NULL ? StorageKind::Explicit : StorageKind::Unknown),
m_pVersionNode(pILCodeVersionNode)
{}
ILCodeVersion::ILCodeVersion(PTR_Module pModule, mdMethodDef methodDef) :
m_storageKind(pModule != NULL ? StorageKind::Synthetic : StorageKind::Unknown)
{
LIMITED_METHOD_DAC_CONTRACT;
m_synthetic.m_pModule = pModule;
m_synthetic.m_methodDef = methodDef;
}
bool ILCodeVersion::operator==(const ILCodeVersion & rhs) const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return (rhs.m_storageKind == StorageKind::Explicit) &&
(AsNode() == rhs.AsNode());
}
else if (m_storageKind == StorageKind::Synthetic)
{
return (rhs.m_storageKind == StorageKind::Synthetic) &&
(m_synthetic.m_pModule == rhs.m_synthetic.m_pModule) &&
(m_synthetic.m_methodDef == rhs.m_synthetic.m_methodDef);
}
else
{
return rhs.m_storageKind == StorageKind::Unknown;
}
}
BOOL ILCodeVersion::HasDefaultIL() const
{
LIMITED_METHOD_CONTRACT;
return (m_storageKind == StorageKind::Synthetic) || (AsNode()->GetIL() == NULL);
}
BOOL ILCodeVersion::IsNull() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_storageKind == StorageKind::Unknown;
}
BOOL ILCodeVersion::IsDefaultVersion() const
{
LIMITED_METHOD_DAC_CONTRACT;
return m_storageKind == StorageKind::Synthetic;
}
PTR_Module ILCodeVersion::GetModule() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->GetModule();
}
else
{
return m_synthetic.m_pModule;
}
}
mdMethodDef ILCodeVersion::GetMethodDef() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->GetMethodDef();
}
else
{
return m_synthetic.m_methodDef;
}
}
ReJITID ILCodeVersion::GetVersionId() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->GetVersionId();
}
else
{
return 0;
}
}
NativeCodeVersionCollection ILCodeVersion::GetNativeCodeVersions(PTR_MethodDesc pClosedMethodDesc) const
{
LIMITED_METHOD_DAC_CONTRACT;
return NativeCodeVersionCollection(pClosedMethodDesc, *this);
}
NativeCodeVersion ILCodeVersion::GetActiveNativeCodeVersion(PTR_MethodDesc pClosedMethodDesc) const
{
LIMITED_METHOD_DAC_CONTRACT;
_ASSERTE(CodeVersionManager::IsLockOwnedByCurrentThread());
NativeCodeVersionCollection versions = GetNativeCodeVersions(pClosedMethodDesc);
for (NativeCodeVersionIterator cur = versions.Begin(), end = versions.End(); cur != end; cur++)
{
if (cur->IsActiveChildVersion())
{
return *cur;
}
}
return NativeCodeVersion();
}
#if defined(FEATURE_TIERED_COMPILATION) && !defined(DACCESS_COMPILE)
bool ILCodeVersion::HasAnyOptimizedNativeCodeVersion(NativeCodeVersion tier0NativeCodeVersion) const
{
WRAPPER_NO_CONTRACT;
_ASSERTE(CodeVersionManager::IsLockOwnedByCurrentThread());
_ASSERTE(!tier0NativeCodeVersion.IsNull());
_ASSERTE(tier0NativeCodeVersion.GetILCodeVersion() == *this);
_ASSERTE(tier0NativeCodeVersion.GetMethodDesc()->IsEligibleForTieredCompilation());
_ASSERTE(!tier0NativeCodeVersion.IsFinalTier());
NativeCodeVersionCollection nativeCodeVersions = GetNativeCodeVersions(tier0NativeCodeVersion.GetMethodDesc());
for (auto itEnd = nativeCodeVersions.End(), it = nativeCodeVersions.Begin(); it != itEnd; ++it)
{
NativeCodeVersion nativeCodeVersion = *it;
// The tier 0 native code version is often the default code version and this is much faster than below
if (nativeCodeVersion == tier0NativeCodeVersion)
{
continue;
}
NativeCodeVersion::OptimizationTier optimizationTier = nativeCodeVersion.GetOptimizationTier();
if (optimizationTier == NativeCodeVersion::OptimizationTier1 ||
optimizationTier == NativeCodeVersion::OptimizationTierOptimized)
{
return true;
}
}
return false;
}
#endif
RejitFlags ILCodeVersion::GetRejitState() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->GetRejitState();
}
else
{
return RejitFlags::kStateActive;
}
}
BOOL ILCodeVersion::GetEnableReJITCallback() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->GetEnableReJITCallback();
}
else
{
return FALSE;
}
}
PTR_COR_ILMETHOD ILCodeVersion::GetIL() const
{
CONTRACTL
{
THROWS; //GetILHeader throws
GC_NOTRIGGER;
FORBID_FAULT;
MODE_ANY;
}
CONTRACTL_END
PTR_COR_ILMETHOD pIL = NULL;
if (m_storageKind == StorageKind::Explicit)
{
pIL = AsNode()->GetIL();
}
// For the default code version we always fetch the globally stored default IL for a method
//
// In the non-default code version we assume NULL is the equivalent of explicitly requesting to
// re-use the default IL. Ideally there would be no reason to create a new version that re-uses
// the default IL (just use the default code version for that) but we do it here for compat. We've
// got some profilers that use ReJIT to create a new code version and then instead of calling
// ICorProfilerFunctionControl::SetILFunctionBody they call ICorProfilerInfo::SetILFunctionBody.
// This mutates the default IL so that it is now correct for their new code version. Of course this
// also overwrote the previous default IL so now the default code version GetIL() is out of sync
// with the jitted code. In the majority of cases we never re-read the IL after the initial
// jitting so this issue goes unnoticed.
//
// If changing the default IL after it is in use becomes more problematic in the future we would
// need to add enforcement that prevents profilers from using ICorProfilerInfo::SetILFunctionBody
// that way + coordinate with them because it is a breaking change for any profiler currently doing it.
if(pIL == NULL)
{
PTR_Module pModule = GetModule();
PTR_MethodDesc pMethodDesc = dac_cast<PTR_MethodDesc>(pModule->LookupMethodDef(GetMethodDef()));
if (pMethodDesc != NULL)
{
pIL = dac_cast<PTR_COR_ILMETHOD>(pMethodDesc->GetILHeader());
}
}
return pIL;
}
DWORD ILCodeVersion::GetJitFlags() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->GetJitFlags();
}
else
{
return 0;
}
}
const InstrumentedILOffsetMapping* ILCodeVersion::GetInstrumentedILMap() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->GetInstrumentedILMap();
}
else
{
return NULL;
}
}
BOOL ILCodeVersion::IsDeoptimized() const
{
LIMITED_METHOD_DAC_CONTRACT;
if (m_storageKind == StorageKind::Explicit)
{
return AsNode()->IsDeoptimized();
}
else
{
return FALSE;
}
}
#ifndef DACCESS_COMPILE
void ILCodeVersion::SetRejitState(RejitFlags newState)
{
LIMITED_METHOD_CONTRACT;
AsNode()->SetRejitState(newState);
}
void ILCodeVersion::SetEnableReJITCallback(BOOL state)
{
LIMITED_METHOD_CONTRACT;
return AsNode()->SetEnableReJITCallback(state);
}
void ILCodeVersion::SetIL(COR_ILMETHOD* pIL)
{
LIMITED_METHOD_CONTRACT;
AsNode()->SetIL(pIL);
}
void ILCodeVersion::SetJitFlags(DWORD flags)
{
LIMITED_METHOD_CONTRACT;
AsNode()->SetJitFlags(flags);
}
void ILCodeVersion::SetInstrumentedILMap(SIZE_T cMap, COR_IL_MAP * rgMap)
{
LIMITED_METHOD_CONTRACT;
AsNode()->SetInstrumentedILMap(cMap, rgMap);
}
HRESULT ILCodeVersion::AddNativeCodeVersion(
MethodDesc* pClosedMethodDesc,
NativeCodeVersion::OptimizationTier optimizationTier,
NativeCodeVersion* pNativeCodeVersion,
PatchpointInfo* patchpointInfo,
unsigned ilOffset
)
{