forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeman.cpp
6652 lines (5461 loc) · 221 KB
/
codeman.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.
//
// codeman.cpp - a managment class for handling multiple code managers
//
#include "common.h"
#include "jitinterface.h"
#include "corjit.h"
#include "jithost.h"
#include "eetwain.h"
#include "eeconfig.h"
#include "excep.h"
#include "appdomain.hpp"
#include "codeman.h"
#include "nibblemapmacros.h"
#include "generics.h"
#include "dynamicmethod.h"
#include "eventtrace.h"
#include "threadsuspend.h"
#include "exceptionhandling.h"
#include "rtlfunctions.h"
#include "shimload.h"
#include "debuginfostore.h"
#include "strsafe.h"
#include "configuration.h"
#ifdef HOST_64BIT
#define CHECK_DUPLICATED_STRUCT_LAYOUTS
#include "../debug/daccess/fntableaccess.h"
#endif // HOST_64BIT
#ifdef FEATURE_PERFMAP
#include "perfmap.h"
#endif
// Default number of jump stubs in a jump stub block
#define DEFAULT_JUMPSTUBS_PER_BLOCK 32
SPTR_IMPL(EECodeManager, ExecutionManager, m_pDefaultCodeMan);
SPTR_IMPL(EEJitManager, ExecutionManager, m_pEEJitManager);
#ifdef FEATURE_READYTORUN
SPTR_IMPL(ReadyToRunJitManager, ExecutionManager, m_pReadyToRunJitManager);
#endif
VOLATILE_SPTR_IMPL_INIT(RangeSection, ExecutionManager, m_CodeRangeList, NULL);
VOLATILE_SVAL_IMPL_INIT(LONG, ExecutionManager, m_dwReaderCount, 0);
VOLATILE_SVAL_IMPL_INIT(LONG, ExecutionManager, m_dwWriterLock, 0);
#ifndef DACCESS_COMPILE
CrstStatic ExecutionManager::m_JumpStubCrst;
CrstStatic ExecutionManager::m_RangeCrst;
unsigned ExecutionManager::m_normal_JumpStubLookup;
unsigned ExecutionManager::m_normal_JumpStubUnique;
unsigned ExecutionManager::m_normal_JumpStubBlockAllocCount;
unsigned ExecutionManager::m_normal_JumpStubBlockFullCount;
unsigned ExecutionManager::m_LCG_JumpStubLookup;
unsigned ExecutionManager::m_LCG_JumpStubUnique;
unsigned ExecutionManager::m_LCG_JumpStubBlockAllocCount;
unsigned ExecutionManager::m_LCG_JumpStubBlockFullCount;
#endif // DACCESS_COMPILE
#if defined(TARGET_AMD64) && !defined(DACCESS_COMPILE) // We don't do this on ARM just amd64
// Support for new style unwind information (to allow OS to stack crawl JIT compiled code).
typedef NTSTATUS (WINAPI* RtlAddGrowableFunctionTableFnPtr) (
PVOID *DynamicTable, PRUNTIME_FUNCTION FunctionTable, ULONG EntryCount,
ULONG MaximumEntryCount, ULONG_PTR rangeStart, ULONG_PTR rangeEnd);
typedef VOID (WINAPI* RtlGrowFunctionTableFnPtr) (PVOID DynamicTable, ULONG NewEntryCount);
typedef VOID (WINAPI* RtlDeleteGrowableFunctionTableFnPtr) (PVOID DynamicTable);
// OS entry points (only exist on Win8 and above)
static RtlAddGrowableFunctionTableFnPtr pRtlAddGrowableFunctionTable;
static RtlGrowFunctionTableFnPtr pRtlGrowFunctionTable;
static RtlDeleteGrowableFunctionTableFnPtr pRtlDeleteGrowableFunctionTable;
static Volatile<bool> RtlUnwindFtnsInited;
// statics for UnwindInfoTable
Crst* UnwindInfoTable::s_pUnwindInfoTableLock = NULL;
Volatile<bool> UnwindInfoTable::s_publishingActive = false;
#if _DEBUG
// Fake functions on Win7 checked build to excercize the code paths, they are no-ops
NTSTATUS WINAPI FakeRtlAddGrowableFunctionTable (
PVOID *DynamicTable, PT_RUNTIME_FUNCTION FunctionTable, ULONG EntryCount,
ULONG MaximumEntryCount, ULONG_PTR rangeStart, ULONG_PTR rangeEnd) { *DynamicTable = (PVOID) 1; return 0; }
VOID WINAPI FakeRtlGrowFunctionTable (PVOID DynamicTable, ULONG NewEntryCount) { }
VOID WINAPI FakeRtlDeleteGrowableFunctionTable (PVOID DynamicTable) {}
#endif
/****************************************************************************/
// initialize the entry points for new win8 unwind info publishing functions.
// return true if the initialize is successful (the functions exist)
bool InitUnwindFtns()
{
CONTRACTL {
NOTHROW;
} CONTRACTL_END;
#ifndef TARGET_UNIX
if (!RtlUnwindFtnsInited)
{
HINSTANCE hNtdll = WszGetModuleHandle(W("ntdll.dll"));
if (hNtdll != NULL)
{
void* growFunctionTable = GetProcAddress(hNtdll, "RtlGrowFunctionTable");
void* deleteGrowableFunctionTable = GetProcAddress(hNtdll, "RtlDeleteGrowableFunctionTable");
void* addGrowableFunctionTable = GetProcAddress(hNtdll, "RtlAddGrowableFunctionTable");
// All or nothing AddGroableFunctionTable is last (marker)
if (growFunctionTable != NULL &&
deleteGrowableFunctionTable != NULL &&
addGrowableFunctionTable != NULL)
{
pRtlGrowFunctionTable = (RtlGrowFunctionTableFnPtr) growFunctionTable;
pRtlDeleteGrowableFunctionTable = (RtlDeleteGrowableFunctionTableFnPtr) deleteGrowableFunctionTable;
pRtlAddGrowableFunctionTable = (RtlAddGrowableFunctionTableFnPtr) addGrowableFunctionTable;
}
// Don't call FreeLibrary(hNtdll) because GetModuleHandle did *NOT* increment the reference count!
}
else
{
#if _DEBUG
pRtlGrowFunctionTable = FakeRtlGrowFunctionTable;
pRtlDeleteGrowableFunctionTable = FakeRtlDeleteGrowableFunctionTable;
pRtlAddGrowableFunctionTable = FakeRtlAddGrowableFunctionTable;
#endif
}
RtlUnwindFtnsInited = true;
}
return (pRtlAddGrowableFunctionTable != NULL);
#else // !TARGET_UNIX
return false;
#endif // !TARGET_UNIX
}
/****************************************************************************/
UnwindInfoTable::UnwindInfoTable(ULONG_PTR rangeStart, ULONG_PTR rangeEnd, ULONG size)
{
STANDARD_VM_CONTRACT;
_ASSERTE(s_pUnwindInfoTableLock->OwnedByCurrentThread());
_ASSERTE((rangeEnd - rangeStart) <= 0x7FFFFFFF);
cTableCurCount = 0;
cTableMaxCount = size;
cDeletedEntries = 0;
iRangeStart = rangeStart;
iRangeEnd = rangeEnd;
hHandle = NULL;
pTable = new T_RUNTIME_FUNCTION[cTableMaxCount];
}
/****************************************************************************/
UnwindInfoTable::~UnwindInfoTable()
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
} CONTRACTL_END;
_ASSERTE(s_publishingActive);
// We do this lock free to because too many places still want no-trigger. It should be OK
// It would be cleaner if we could take the lock (we did not have to be GC_NOTRIGGER)
UnRegister();
delete[] pTable;
}
/*****************************************************************************/
void UnwindInfoTable::Register()
{
_ASSERTE(s_pUnwindInfoTableLock->OwnedByCurrentThread());
EX_TRY
{
hHandle = NULL;
NTSTATUS ret = pRtlAddGrowableFunctionTable(&hHandle, pTable, cTableCurCount, cTableMaxCount, iRangeStart, iRangeEnd);
if (ret != STATUS_SUCCESS)
{
_ASSERTE(!"Failed to publish UnwindInfo (ignorable)");
hHandle = NULL;
STRESS_LOG3(LF_JIT, LL_ERROR, "UnwindInfoTable::Register ERROR %x creating table [%p, %p]\n", ret, iRangeStart, iRangeEnd);
}
else
{
STRESS_LOG3(LF_JIT, LL_INFO100, "UnwindInfoTable::Register Handle: %p [%p, %p]\n", hHandle, iRangeStart, iRangeEnd);
}
}
EX_CATCH
{
hHandle = NULL;
STRESS_LOG2(LF_JIT, LL_ERROR, "UnwindInfoTable::Register Exception while creating table [%p, %p]\n",
iRangeStart, iRangeEnd);
_ASSERTE(!"Failed to publish UnwindInfo (ignorable)");
}
EX_END_CATCH(SwallowAllExceptions)
}
/*****************************************************************************/
void UnwindInfoTable::UnRegister()
{
PVOID handle = hHandle;
hHandle = 0;
if (handle != 0)
{
STRESS_LOG3(LF_JIT, LL_INFO100, "UnwindInfoTable::UnRegister Handle: %p [%p, %p]\n", handle, iRangeStart, iRangeEnd);
pRtlDeleteGrowableFunctionTable(handle);
}
}
/*****************************************************************************/
// Add 'data' to the linked list whose head is pointed at by 'unwindInfoPtr'
//
/* static */
void UnwindInfoTable::AddToUnwindInfoTable(UnwindInfoTable** unwindInfoPtr, PT_RUNTIME_FUNCTION data,
TADDR rangeStart, TADDR rangeEnd)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
}
CONTRACTL_END;
_ASSERTE(data->BeginAddress <= RUNTIME_FUNCTION__EndAddress(data, rangeStart));
_ASSERTE(RUNTIME_FUNCTION__EndAddress(data, rangeStart) <= (rangeEnd-rangeStart));
_ASSERTE(unwindInfoPtr != NULL);
if (!s_publishingActive)
return;
CrstHolder ch(s_pUnwindInfoTableLock);
UnwindInfoTable* unwindInfo = *unwindInfoPtr;
// was the original list null, If so lazy initialize.
if (unwindInfo == NULL)
{
// We can choose the average method size estimate dynamically based on past experience
// 128 is the estimated size of an average method, so we can accurately predict
// how many RUNTIME_FUNCTION entries are in each chunk we allocate.
ULONG size = (ULONG) ((rangeEnd - rangeStart) / 128) + 1;
// To insure the test the growing logic in debug code make the size much smaller.
INDEBUG(size = size / 4 + 1);
unwindInfo = (PTR_UnwindInfoTable)new UnwindInfoTable(rangeStart, rangeEnd, size);
unwindInfo->Register();
*unwindInfoPtr = unwindInfo;
}
_ASSERTE(unwindInfo != NULL); // If new had failed, we would have thrown OOM
_ASSERTE(unwindInfo->cTableCurCount <= unwindInfo->cTableMaxCount);
_ASSERTE(unwindInfo->iRangeStart == rangeStart);
_ASSERTE(unwindInfo->iRangeEnd == rangeEnd);
// Means we had a failure publishing to the OS, in this case we give up
if (unwindInfo->hHandle == NULL)
return;
// Check for the fast path: we are adding the end of an UnwindInfoTable with space
if (unwindInfo->cTableCurCount < unwindInfo->cTableMaxCount)
{
if (unwindInfo->cTableCurCount == 0 ||
unwindInfo->pTable[unwindInfo->cTableCurCount-1].BeginAddress < data->BeginAddress)
{
// Yeah, we can simply add to the end of table and we are done!
unwindInfo->pTable[unwindInfo->cTableCurCount] = *data;
unwindInfo->cTableCurCount++;
// Add to the function table
pRtlGrowFunctionTable(unwindInfo->hHandle, unwindInfo->cTableCurCount);
STRESS_LOG5(LF_JIT, LL_INFO1000, "AddToUnwindTable Handle: %p [%p, %p] ADDING 0x%p TO END, now 0x%x entries\n",
unwindInfo->hHandle, unwindInfo->iRangeStart, unwindInfo->iRangeEnd,
data->BeginAddress, unwindInfo->cTableCurCount);
return;
}
}
// OK we need to rellocate the table and reregister. First figure out our 'desiredSpace'
// We could imagine being much more efficient for 'bulk' updates, but we don't try
// because we assume that this is rare and we want to keep the code simple
ULONG usedSpace = unwindInfo->cTableCurCount - unwindInfo->cDeletedEntries;
ULONG desiredSpace = usedSpace * 5 / 4 + 1; // Increase by 20%
// Be more aggressive if we used all of our space;
if (usedSpace == unwindInfo->cTableMaxCount)
desiredSpace = usedSpace * 3 / 2 + 1; // Increase by 50%
STRESS_LOG7(LF_JIT, LL_INFO100, "AddToUnwindTable Handle: %p [%p, %p] SLOW Realloc Cnt 0x%x Max 0x%x NewMax 0x%x, Adding %x\n",
unwindInfo->hHandle, unwindInfo->iRangeStart, unwindInfo->iRangeEnd,
unwindInfo->cTableCurCount, unwindInfo->cTableMaxCount, desiredSpace, data->BeginAddress);
UnwindInfoTable* newTab = new UnwindInfoTable(unwindInfo->iRangeStart, unwindInfo->iRangeEnd, desiredSpace);
// Copy in the entries, removing deleted entries and adding the new entry wherever it belongs
int toIdx = 0;
bool inserted = false; // Have we inserted 'data' into the table
for(ULONG fromIdx = 0; fromIdx < unwindInfo->cTableCurCount; fromIdx++)
{
if (!inserted && data->BeginAddress < unwindInfo->pTable[fromIdx].BeginAddress)
{
STRESS_LOG1(LF_JIT, LL_INFO100, "AddToUnwindTable Inserted at MID position 0x%x\n", toIdx);
newTab->pTable[toIdx++] = *data;
inserted = true;
}
if (unwindInfo->pTable[fromIdx].UnwindData != 0) // A 'non-deleted' entry
newTab->pTable[toIdx++] = unwindInfo->pTable[fromIdx];
}
if (!inserted)
{
STRESS_LOG1(LF_JIT, LL_INFO100, "AddToUnwindTable Inserted at END position 0x%x\n", toIdx);
newTab->pTable[toIdx++] = *data;
}
newTab->cTableCurCount = toIdx;
STRESS_LOG2(LF_JIT, LL_INFO100, "AddToUnwindTable New size 0x%x max 0x%x\n",
newTab->cTableCurCount, newTab->cTableMaxCount);
_ASSERTE(newTab->cTableCurCount <= newTab->cTableMaxCount);
// Unregister the old table
*unwindInfoPtr = 0;
unwindInfo->UnRegister();
// Note that there is a short time when we are not publishing...
// Register the new table
newTab->Register();
*unwindInfoPtr = newTab;
delete unwindInfo;
}
/*****************************************************************************/
/* static */ void UnwindInfoTable::RemoveFromUnwindInfoTable(UnwindInfoTable** unwindInfoPtr, TADDR baseAddress, TADDR entryPoint)
{
CONTRACTL {
NOTHROW;
GC_TRIGGERS;
} CONTRACTL_END;
_ASSERTE(unwindInfoPtr != NULL);
if (!s_publishingActive)
return;
CrstHolder ch(s_pUnwindInfoTableLock);
UnwindInfoTable* unwindInfo = *unwindInfoPtr;
if (unwindInfo != NULL)
{
DWORD relativeEntryPoint = (DWORD)(entryPoint - baseAddress);
STRESS_LOG3(LF_JIT, LL_INFO100, "RemoveFromUnwindInfoTable Removing %p BaseAddress %p rel %x\n",
entryPoint, baseAddress, relativeEntryPoint);
for(ULONG i = 0; i < unwindInfo->cTableCurCount; i++)
{
if (unwindInfo->pTable[i].BeginAddress <= relativeEntryPoint &&
relativeEntryPoint < RUNTIME_FUNCTION__EndAddress(&unwindInfo->pTable[i], unwindInfo->iRangeStart))
{
if (unwindInfo->pTable[i].UnwindData != 0)
unwindInfo->cDeletedEntries++;
unwindInfo->pTable[i].UnwindData = 0; // Mark the entry for deletion
STRESS_LOG1(LF_JIT, LL_INFO100, "RemoveFromUnwindInfoTable Removed entry 0x%x\n", i);
return;
}
}
}
STRESS_LOG2(LF_JIT, LL_WARNING, "RemoveFromUnwindInfoTable COULD NOT FIND %p BaseAddress %p\n",
entryPoint, baseAddress);
}
/****************************************************************************/
// Publish the stack unwind data 'data' which is relative 'baseAddress'
// to the operating system in a way ETW stack tracing can use.
/* static */ void UnwindInfoTable::PublishUnwindInfoForMethod(TADDR baseAddress, PT_RUNTIME_FUNCTION unwindInfo, int unwindInfoCount)
{
STANDARD_VM_CONTRACT;
if (!s_publishingActive)
return;
TADDR entry = baseAddress + unwindInfo->BeginAddress;
RangeSection * pRS = ExecutionManager::FindCodeRange(entry, ExecutionManager::GetScanFlags());
_ASSERTE(pRS != NULL);
if (pRS != NULL)
{
for(int i = 0; i < unwindInfoCount; i++)
AddToUnwindInfoTable(&pRS->pUnwindInfoTable, &unwindInfo[i], pRS->LowAddress, pRS->HighAddress);
}
}
/*****************************************************************************/
/* static */ void UnwindInfoTable::UnpublishUnwindInfoForMethod(TADDR entryPoint)
{
CONTRACTL {
NOTHROW;
GC_TRIGGERS;
} CONTRACTL_END;
if (!s_publishingActive)
return;
RangeSection * pRS = ExecutionManager::FindCodeRange(entryPoint, ExecutionManager::GetScanFlags());
_ASSERTE(pRS != NULL);
if (pRS != NULL)
{
_ASSERTE(pRS->pjit->GetCodeType() == (miManaged | miIL));
if (pRS->pjit->GetCodeType() == (miManaged | miIL))
{
// This cast is justified because only EEJitManager's have the code type above.
EEJitManager* pJitMgr = (EEJitManager*)(pRS->pjit);
CodeHeader * pHeader = pJitMgr->GetCodeHeaderFromStartAddress(entryPoint);
for(ULONG i = 0; i < pHeader->GetNumberOfUnwindInfos(); i++)
RemoveFromUnwindInfoTable(&pRS->pUnwindInfoTable, pRS->LowAddress, pRS->LowAddress + pHeader->GetUnwindInfo(i)->BeginAddress);
}
}
}
#ifdef STUBLINKER_GENERATES_UNWIND_INFO
extern StubUnwindInfoHeapSegment *g_StubHeapSegments;
#endif // STUBLINKER_GENERATES_UNWIND_INFO
extern CrstStatic g_StubUnwindInfoHeapSegmentsCrst;
/*****************************************************************************/
// Publish all existing JIT compiled methods by iterating through the code heap
// Note that because we need to keep the entries in order we have to hold
// s_pUnwindInfoTableLock so that all entries get inserted in the correct order.
// (we rely on heapIterator walking the methods in a heap section in order).
/* static */ void UnwindInfoTable::PublishUnwindInfoForExistingMethods()
{
STANDARD_VM_CONTRACT;
{
// CodeHeapIterator holds the m_CodeHeapCritSec, which insures code heaps don't get deallocated while being walked
EEJitManager::CodeHeapIterator heapIterator(NULL);
// Currently m_CodeHeapCritSec is given the CRST_UNSAFE_ANYMODE flag which allows it to be taken in a GC_NOTRIGGER
// region but also disallows GC_TRIGGERS. We need GC_TRIGGERS because we take another lock. Ideally we would
// fix m_CodeHeapCritSec to not have the CRST_UNSAFE_ANYMODE flag, but I currently reached my threshold for fixing
// contracts.
CONTRACT_VIOLATION(GCViolation);
while(heapIterator.Next())
{
MethodDesc *pMD = heapIterator.GetMethod();
if(pMD)
{
PCODE methodEntry =(PCODE) heapIterator.GetMethodCode();
RangeSection * pRS = ExecutionManager::FindCodeRange(methodEntry, ExecutionManager::GetScanFlags());
_ASSERTE(pRS != NULL);
_ASSERTE(pRS->pjit->GetCodeType() == (miManaged | miIL));
if (pRS != NULL && pRS->pjit->GetCodeType() == (miManaged | miIL))
{
// This cast is justified because only EEJitManager's have the code type above.
EEJitManager* pJitMgr = (EEJitManager*)(pRS->pjit);
CodeHeader * pHeader = pJitMgr->GetCodeHeaderFromStartAddress(methodEntry);
int unwindInfoCount = pHeader->GetNumberOfUnwindInfos();
for(int i = 0; i < unwindInfoCount; i++)
AddToUnwindInfoTable(&pRS->pUnwindInfoTable, pHeader->GetUnwindInfo(i), pRS->LowAddress, pRS->HighAddress);
}
}
}
}
#ifdef STUBLINKER_GENERATES_UNWIND_INFO
// Enumerate all existing stubs
CrstHolder crst(&g_StubUnwindInfoHeapSegmentsCrst);
for (StubUnwindInfoHeapSegment* pStubHeapSegment = g_StubHeapSegments; pStubHeapSegment; pStubHeapSegment = pStubHeapSegment->pNext)
{
// The stubs are in reverse order, so we reverse them so they are in memory order
CQuickArrayList<StubUnwindInfoHeader*> list;
for (StubUnwindInfoHeader *pHeader = pStubHeapSegment->pUnwindHeaderList; pHeader; pHeader = pHeader->pNext)
list.Push(pHeader);
for(int i = (int) list.Size()-1; i >= 0; --i)
{
StubUnwindInfoHeader *pHeader = list[i];
AddToUnwindInfoTable(&pStubHeapSegment->pUnwindInfoTable, &pHeader->FunctionEntry,
(TADDR) pStubHeapSegment->pbBaseAddress, (TADDR) pStubHeapSegment->pbBaseAddress + pStubHeapSegment->cbSegment);
}
}
#endif // STUBLINKER_GENERATES_UNWIND_INFO
}
/*****************************************************************************/
// turn on the publishing of unwind info. Called when the ETW rundown provider
// is turned on.
/* static */ void UnwindInfoTable::PublishUnwindInfo(bool publishExisting)
{
CONTRACTL {
NOTHROW;
GC_TRIGGERS;
} CONTRACTL_END;
if (s_publishingActive)
return;
// If we don't have the APIs we need, give up
if (!InitUnwindFtns())
return;
EX_TRY
{
// Create the lock
Crst* newCrst = new Crst(CrstUnwindInfoTableLock);
if (InterlockedCompareExchangeT(&s_pUnwindInfoTableLock, newCrst, NULL) == NULL)
{
s_publishingActive = true;
if (publishExisting)
PublishUnwindInfoForExistingMethods();
}
else
delete newCrst; // we were in a race and failed, throw away the Crst we made.
} EX_CATCH {
STRESS_LOG1(LF_JIT, LL_ERROR, "Exception happened when doing unwind Info rundown. EIP of last AV = %p\n", g_LastAccessViolationEIP);
_ASSERTE(!"Exception thrown while publishing 'catchup' ETW unwind information");
s_publishingActive = false; // Try to minimize damage.
} EX_END_CATCH(SwallowAllExceptions);
}
#endif // defined(TARGET_AMD64) && !defined(DACCESS_COMPILE)
/*-----------------------------------------------------------------------------
This is a listing of which methods uses which synchronization mechanism
in the EEJitManager.
//-----------------------------------------------------------------------------
Setters of EEJitManager::m_CodeHeapCritSec
-----------------------------------------------
allocCode
allocGCInfo
allocEHInfo
allocJumpStubBlock
ResolveEHClause
RemoveJitData
Unload
ReleaseReferenceToHeap
JitCodeToMethodInfo
Need EEJitManager::m_CodeHeapCritSec to be set
-----------------------------------------------
NewCodeHeap
allocCodeRaw
GetCodeHeapList
RemoveCodeHeapFromDomainList
DeleteCodeHeap
AddRangeToJitHeapCache
DeleteJitHeapCache
*/
#if !defined(DACCESS_COMPILE)
EEJitManager::CodeHeapIterator::CodeHeapIterator(LoaderAllocator *pLoaderAllocatorFilter)
: m_lockHolder(&(ExecutionManager::GetEEJitManager()->m_CodeHeapCritSec)), m_Iterator(NULL, 0, NULL, 0)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
m_pHeapList = NULL;
m_pLoaderAllocator = pLoaderAllocatorFilter;
m_pHeapList = ExecutionManager::GetEEJitManager()->GetCodeHeapList();
if(m_pHeapList)
new (&m_Iterator) MethodSectionIterator((const void *)m_pHeapList->mapBase, (COUNT_T)m_pHeapList->maxCodeHeapSize, m_pHeapList->pHdrMap, (COUNT_T)HEAP2MAPSIZE(ROUND_UP_TO_PAGE(m_pHeapList->maxCodeHeapSize)));
};
EEJitManager::CodeHeapIterator::~CodeHeapIterator()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
}
BOOL EEJitManager::CodeHeapIterator::Next()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
if(!m_pHeapList)
return FALSE;
while(1)
{
if(!m_Iterator.Next())
{
m_pHeapList = m_pHeapList->GetNext();
if(!m_pHeapList)
return FALSE;
new (&m_Iterator) MethodSectionIterator((const void *)m_pHeapList->mapBase, (COUNT_T)m_pHeapList->maxCodeHeapSize, m_pHeapList->pHdrMap, (COUNT_T)HEAP2MAPSIZE(ROUND_UP_TO_PAGE(m_pHeapList->maxCodeHeapSize)));
}
else
{
BYTE * code = m_Iterator.GetMethodCode();
CodeHeader * pHdr = (CodeHeader *)(code - sizeof(CodeHeader));
m_pCurrent = !pHdr->IsStubCodeBlock() ? pHdr->GetMethodDesc() : NULL;
// LoaderAllocator filter
if (m_pLoaderAllocator && m_pCurrent)
{
LoaderAllocator *pCurrentLoaderAllocator = m_pCurrent->GetLoaderAllocator();
if(pCurrentLoaderAllocator != m_pLoaderAllocator)
continue;
}
return TRUE;
}
}
}
#endif // !DACCESS_COMPILE
#ifndef DACCESS_COMPILE
//---------------------------------------------------------------------------------------
//
// ReaderLockHolder::ReaderLockHolder takes the reader lock, checks for the writer lock
// and either aborts if the writer lock is held, or yields until the writer lock is released,
// keeping the reader lock. This is normally called in the constructor for the
// ReaderLockHolder.
//
// The writer cannot be taken if there are any readers. The WriterLockHolder functions take the
// writer lock and check for any readers. If there are any, the WriterLockHolder functions
// release the writer and yield to wait for the readers to be done.
ExecutionManager::ReaderLockHolder::ReaderLockHolder(HostCallPreference hostCallPreference /*=AllowHostCalls*/)
{
CONTRACTL {
NOTHROW;
if (hostCallPreference == AllowHostCalls) { HOST_CALLS; } else { HOST_NOCALLS; }
GC_NOTRIGGER;
CAN_TAKE_LOCK;
} CONTRACTL_END;
IncCantAllocCount();
InterlockedIncrement(&m_dwReaderCount);
EE_LOCK_TAKEN(GetPtrForLockContract());
if (VolatileLoad(&m_dwWriterLock) != 0)
{
if (hostCallPreference != AllowHostCalls)
{
// Rats, writer lock is held. Gotta bail. Since the reader count was already
// incremented, we're technically still blocking writers at the moment. But
// the holder who called us is about to call DecrementReader in its
// destructor and unblock writers.
return;
}
YIELD_WHILE ((VolatileLoad(&m_dwWriterLock) != 0));
}
}
//---------------------------------------------------------------------------------------
//
// See code:ExecutionManager::ReaderLockHolder::ReaderLockHolder. This just decrements the reader count.
ExecutionManager::ReaderLockHolder::~ReaderLockHolder()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
InterlockedDecrement(&m_dwReaderCount);
DecCantAllocCount();
EE_LOCK_RELEASED(GetPtrForLockContract());
}
//---------------------------------------------------------------------------------------
//
// Returns whether the reader lock is acquired
BOOL ExecutionManager::ReaderLockHolder::Acquired()
{
LIMITED_METHOD_CONTRACT;
return VolatileLoad(&m_dwWriterLock) == 0;
}
ExecutionManager::WriterLockHolder::WriterLockHolder()
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
CAN_TAKE_LOCK;
} CONTRACTL_END;
_ASSERTE(m_dwWriterLock == 0);
// Signal to a debugger that this thread cannot stop now
IncCantStopCount();
IncCantAllocCount();
DWORD dwSwitchCount = 0;
while (TRUE)
{
// While this thread holds the writer lock, we must not try to suspend it
// or allow a profiler to walk its stack
Thread::IncForbidSuspendThread();
InterlockedIncrement(&m_dwWriterLock);
if (m_dwReaderCount == 0)
break;
InterlockedDecrement(&m_dwWriterLock);
// Before we loop and retry, it's safe to suspend or hijack and inspect
// this thread
Thread::DecForbidSuspendThread();
__SwitchToThread(0, ++dwSwitchCount);
}
EE_LOCK_TAKEN(GetPtrForLockContract());
}
ExecutionManager::WriterLockHolder::~WriterLockHolder()
{
LIMITED_METHOD_CONTRACT;
InterlockedDecrement(&m_dwWriterLock);
// Writer lock released, so it's safe again for this thread to be
// suspended or have its stack walked by a profiler
Thread::DecForbidSuspendThread();
DecCantAllocCount();
// Signal to a debugger that it's again safe to stop this thread
DecCantStopCount();
EE_LOCK_RELEASED(GetPtrForLockContract());
}
#else
// For DAC builds, we only care whether the writer lock is held.
// If it is, we will assume the locked data is in an inconsistent
// state and throw. We never actually take the lock.
// Note: Throws
ExecutionManager::ReaderLockHolder::ReaderLockHolder(HostCallPreference hostCallPreference /*=AllowHostCalls*/)
{
SUPPORTS_DAC;
if (m_dwWriterLock != 0)
{
ThrowHR(CORDBG_E_PROCESS_NOT_SYNCHRONIZED);
}
}
ExecutionManager::ReaderLockHolder::~ReaderLockHolder()
{
}
#endif // DACCESS_COMPILE
/*-----------------------------------------------------------------------------
This is a listing of which methods uses which synchronization mechanism
in the ExecutionManager
//-----------------------------------------------------------------------------
==============================================================================
ExecutionManger::ReaderLockHolder and ExecutionManger::WriterLockHolder
Protects the callers of ExecutionManager::GetRangeSection from heap deletions
while walking RangeSections. You need to take a reader lock before reading the
values: m_CodeRangeList and hold it while walking the lists
Uses ReaderLockHolder (allows multiple reeaders with no writers)
-----------------------------------------
ExecutionManager::FindCodeRange
ExecutionManager::FindZapModule
ExecutionManager::EnumMemoryRegions
Uses WriterLockHolder (allows single writer and no readers)
-----------------------------------------
ExecutionManager::AddRangeHelper
ExecutionManager::DeleteRangeHelper
*/
//-----------------------------------------------------------------------------
#if defined(TARGET_ARM) || defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64)
#define EXCEPTION_DATA_SUPPORTS_FUNCTION_FRAGMENTS
#endif
#if defined(EXCEPTION_DATA_SUPPORTS_FUNCTION_FRAGMENTS)
// The function fragments can be used in Hot/Cold splitting, expressing Large Functions or in 'ShrinkWrapping', which is
// delaying saving and restoring some callee-saved registers later inside the body of the method.
// (It's assumed that JIT will not emit any ShrinkWrapping-style methods)
// For these cases multiple RUNTIME_FUNCTION entries (a.k.a function fragments) are used to define
// all the regions of the function or funclet. And one of these function fragments cover the beginning of the function/funclet,
// including the prolog section and is referred as the 'Host Record'.
// This function returns TRUE if the inspected RUNTIME_FUNCTION entry is NOT a host record
BOOL IsFunctionFragment(TADDR baseAddress, PTR_RUNTIME_FUNCTION pFunctionEntry)
{
LIMITED_METHOD_DAC_CONTRACT;
_ASSERTE((pFunctionEntry->UnwindData & 3) == 0); // The unwind data must be an RVA; we don't support packed unwind format
DWORD unwindHeader = *(PTR_DWORD)(baseAddress + pFunctionEntry->UnwindData);
_ASSERTE((0 == ((unwindHeader >> 18) & 3)) || !"unknown unwind data format, version != 0");
#if defined(TARGET_ARM)
// On ARM, It's assumed that the prolog is always at the beginning of the function and cannot be split.
// Given that, there are 4 possible ways to fragment a function:
// 1. Prolog only:
// 2. Prolog and some epilogs:
// 3. Epilogs only:
// 4. No Prolog or epilog
//
// Function fragments describing 1 & 2 are host records, 3 & 4 are not.
// for 3 & 4, the .xdata record's F bit is set to 1, marking clearly what is NOT a host record
_ASSERTE((pFunctionEntry->BeginAddress & THUMB_CODE) == THUMB_CODE); // Sanity check: it's a thumb address
DWORD Fbit = (unwindHeader >> 22) & 0x1; // F "fragment" bit
return (Fbit == 1);
#elif defined(TARGET_ARM64)
// ARM64 is a little bit more flexible, in the sense that it supports partial prologs. However only one of the
// prolog regions are allowed to alter SP and that's the Host Record. Partial prologs are used in ShrinkWrapping
// scenarios which is not supported, hence we don't need to worry about them. discarding partial prologs
// simplifies identifying a host record a lot.
//
// 1. Prolog only: The host record. Epilog Count and E bit are all 0.
// 2. Prolog and some epilogs: The host record with accompanying epilog-only records
// 3. Epilogs only: First unwind code is Phantom prolog (Starting with an end_c, indicating an empty prolog)
// 4. No prologs or epilogs: First unwind code is Phantom prolog (Starting with an end_c, indicating an empty prolog)
//
int EpilogCount = (int)(unwindHeader >> 22) & 0x1F;
int CodeWords = unwindHeader >> 27;
PTR_DWORD pUnwindCodes = (PTR_DWORD)(baseAddress + pFunctionEntry->UnwindData);
// Skip header.
pUnwindCodes++;
// Skip extended header.
if ((CodeWords == 0) && (EpilogCount == 0))
{
EpilogCount = (*pUnwindCodes) & 0xFFFF;
pUnwindCodes++;
}
// Skip epilog scopes.
BOOL Ebit = (unwindHeader >> 21) & 0x1;
if (!Ebit && (EpilogCount != 0))
{
// EpilogCount is the number of exception scopes defined right after the unwindHeader
pUnwindCodes += EpilogCount;
}
return ((*pUnwindCodes & 0xFF) == 0xE5);
#elif defined(TARGET_LOONGARCH64)
// LOONGARCH64 is a little bit more flexible, in the sense that it supports partial prologs. However only one of the
// prolog regions are allowed to alter SP and that's the Host Record. Partial prologs are used in ShrinkWrapping
// scenarios which is not supported, hence we don't need to worry about them. discarding partial prologs
// simplifies identifying a host record a lot.
//
// 1. Prolog only: The host record. Epilog Count and E bit are all 0.
// 2. Prolog and some epilogs: The host record with accompanying epilog-only records
// 3. Epilogs only: First unwind code is Phantom prolog (Starting with an end_c, indicating an empty prolog)
// 4. No prologs or epilogs: First unwind code is Phantom prolog (Starting with an end_c, indicating an empty prolog)
//
int EpilogCount = (int)(unwindHeader >> 22) & 0x1F;
int CodeWords = unwindHeader >> 27;
PTR_DWORD pUnwindCodes = (PTR_DWORD)(baseAddress + pFunctionEntry->UnwindData);
// Skip header.
pUnwindCodes++;
// Skip extended header.
if ((CodeWords == 0) && (EpilogCount == 0))
{
EpilogCount = (*pUnwindCodes) & 0xFFFF;
pUnwindCodes++;
}
// Skip epilog scopes.
BOOL Ebit = (unwindHeader >> 21) & 0x1;
if (!Ebit && (EpilogCount != 0))
{
// EpilogCount is the number of exception scopes defined right after the unwindHeader
pUnwindCodes += EpilogCount;
}
return ((*pUnwindCodes & 0xFF) == 0xE5);
#else
PORTABILITY_ASSERT("IsFunctionFragnent - NYI on this platform");
#endif
}
// When we have fragmented unwind we usually want to refer to the
// unwind record that includes the prolog. We can find it by searching
// back in the sequence of unwind records.
PTR_RUNTIME_FUNCTION FindRootEntry(PTR_RUNTIME_FUNCTION pFunctionEntry, TADDR baseAddress)
{
LIMITED_METHOD_DAC_CONTRACT;
PTR_RUNTIME_FUNCTION pRootEntry = pFunctionEntry;
if (pRootEntry != NULL)
{
// Walk backwards in the RUNTIME_FUNCTION array until we find a non-fragment.
// We're guaranteed to find one, because we require that a fragment live in a function or funclet
// that has a prolog, which will have non-fragment .xdata.
while (true)
{
if (!IsFunctionFragment(baseAddress, pRootEntry))
{
// This is not a fragment; we're done
break;
}
--pRootEntry;
}
}
return pRootEntry;
}
#endif // EXCEPTION_DATA_SUPPORTS_FUNCTION_FRAGMENTS
#ifndef DACCESS_COMPILE
//**********************************************************************************
// IJitManager
//**********************************************************************************
IJitManager::IJitManager()
{
LIMITED_METHOD_CONTRACT;
m_runtimeSupport = ExecutionManager::GetDefaultCodeManager();
}
#endif // #ifndef DACCESS_COMPILE
// When we unload an appdomain, we need to make sure that any threads that are crawling through
// our heap or rangelist are out. For cooperative-mode threads, we know that they will have
// been stopped when we suspend the EE so they won't be touching an element that is about to be deleted.
// However for pre-emptive mode threads, they could be stalled right on top of the element we want
// to delete, so we need to apply the reader lock to them and wait for them to drain.
ExecutionManager::ScanFlag ExecutionManager::GetScanFlags()
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
HOST_NOCALLS;
SUPPORTS_DAC;
} CONTRACTL_END;
#if !defined(DACCESS_COMPILE)
Thread *pThread = GetThreadNULLOk();
if (!pThread)
return ScanNoReaderLock;
// If this thread is hijacked by a profiler and crawling its own stack,
// we do need to take the lock
if (pThread->GetProfilerFilterContext() != NULL)
return ScanReaderLock;
if (pThread->PreemptiveGCDisabled() || (pThread == ThreadSuspend::GetSuspensionThread()))
return ScanNoReaderLock;
return ScanReaderLock;
#else
return ScanNoReaderLock;
#endif
}