forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapBlockMap.cpp
More file actions
1503 lines (1268 loc) · 45.7 KB
/
HeapBlockMap.cpp
File metadata and controls
1503 lines (1268 loc) · 45.7 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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "CommonMemoryPch.h"
#if defined(_M_X64_OR_ARM64)
HeapBlockMap32::HeapBlockMap32(__in char * startAddress) :
startAddress(startAddress),
#else
HeapBlockMap32::HeapBlockMap32() :
#endif
count(0)
{
memset(map, 0, sizeof(map));
#if defined(_M_X64_OR_ARM64)
Assert(((size_t)startAddress) % TotalSize == 0);
#endif
}
HeapBlockMap32::~HeapBlockMap32()
{
for (uint i = 0; i < _countof(map); i++)
{
L2MapChunk * chunk = map[i];
if (chunk)
{
NoMemProtectHeapDelete(chunk);
}
}
}
HeapBlock *
HeapBlockMap32::GetHeapBlock(void * address)
{
uint id1 = GetLevel1Id(address);
L2MapChunk * l2map = map[id1];
if (l2map == nullptr)
{
return nullptr;
}
return l2map->Get(address);
}
bool
HeapBlockMap32::EnsureHeapBlock(void * address, uint pageCount)
{
uint id1 = GetLevel1Id(address);
uint id2 = GetLevel2Id(address);
uint currentPageCount = min(pageCount, L2Count - id2);
while (true)
{
if (map[id1] == nullptr)
{
L2MapChunk * newChunk = NoMemProtectHeapNewNoThrowZ(L2MapChunk);
if (newChunk == nullptr)
{
// Leave any previously allocated L2MapChunks in place --
// the concurrent thread may have already accessed them.
// These will be cleaned up in the Cleanup method, after Sweep is complete.
return false;
}
map[id1] = newChunk;
count++;
}
pageCount -= currentPageCount;
if (pageCount == 0)
{
break;
}
id2 = 0;
id1++;
currentPageCount = min(pageCount, L2Count);
}
return true;
}
void
HeapBlockMap32::SetHeapBlockNoCheck(void * address, uint pageCount, HeapBlock * heapBlock, HeapBlock::HeapBlockType blockType, byte bucketIndex)
{
uint id1 = GetLevel1Id(address);
uint id2 = GetLevel2Id(address);
uint currentPageCount = min(pageCount, L2Count - id2);
while (true)
{
Assert(map[id1] != nullptr);
map[id1]->Set(id2, currentPageCount, heapBlock, blockType, bucketIndex);
pageCount -= currentPageCount;
if (pageCount == 0)
{
return;
}
id2 = 0;
id1++;
currentPageCount = min(pageCount, L2Count);
}
}
bool
HeapBlockMap32::SetHeapBlock(void * address, uint pageCount, HeapBlock * heapBlock, HeapBlock::HeapBlockType blockType, byte bucketIndex)
{
// First, make sure we have all the necessary L2MapChunks we'll need.
// This ensures that in case of failure, the concurrent thread won't see an inconsistent state.
if (!EnsureHeapBlock(address, pageCount))
{
return false;
}
// Now, do the actual set, which cannot fail.
SetHeapBlockNoCheck(address, pageCount, heapBlock, blockType, bucketIndex);
return true;
}
void
HeapBlockMap32::ClearHeapBlock(void * address, uint pageCount)
{
uint id1 = GetLevel1Id(address);
uint id2 = GetLevel2Id(address);
uint currentPageCount = min(pageCount, L2Count - id2);
while (true)
{
Assert(map[id1] != nullptr);
map[id1]->Clear(id2, currentPageCount);
pageCount -= currentPageCount;
if (pageCount == 0)
{
return;
}
id2 = 0;
id1++;
currentPageCount = min(pageCount, L2Count);
}
}
HeapBlockMap32::PageMarkBitVector *
HeapBlockMap32::GetPageMarkBitVector(void * address)
{
uint id1 = GetLevel1Id(address);
L2MapChunk * l2map = map[id1];
if (l2map == nullptr)
{
return nullptr;
}
return l2map->GetPageMarkBitVector(address);
}
template <size_t BitCount>
BVStatic<BitCount>*
HeapBlockMap32::GetMarkBitVectorForPages(void * address)
{
uint id1 = GetLevel1Id(address);
L2MapChunk * l2map = map[id1];
if (l2map == nullptr)
{
return nullptr;
}
return l2map->GetMarkBitVectorForPages<BitCount>(address);
}
template BVStatic<SmallAllocationBlockAttributes::BitVectorCount>* HeapBlockMap32::GetMarkBitVectorForPages<SmallAllocationBlockAttributes::BitVectorCount>(void * address);
template BVStatic<MediumAllocationBlockAttributes::BitVectorCount>* HeapBlockMap32::GetMarkBitVectorForPages<MediumAllocationBlockAttributes::BitVectorCount>(void * address);
uint
HeapBlockMap32::GetMarkCount(void * address, uint pageCount)
{
uint markCount = 0;
ForEachChunkInAddressRange(address, pageCount, [&](L2MapChunk* l2Map, uint chunkId)
{
markCount += l2Map->GetPageMarkBitVector(chunkId /* pageIndex */)->Count();
});
return markCount;
}
template <class Fn>
void
HeapBlockMap32::ForEachChunkInAddressRange(void * address, size_t pageCount, Fn fn)
{
uint id1 = GetLevel1Id(address);
uint id2 = GetLevel2Id(address);
while (true)
{
L2MapChunk * l2map = map[id1];
Assert(l2map != nullptr);
if (l2map != nullptr)
{
while (id2 < L2Count)
{
fn(l2map, id2);
id2++;
pageCount--;
if (pageCount == 0)
{
return;
}
}
id2 = 0;
id1++;
}
}
}
bool
HeapBlockMap32::IsMarked(void * address) const
{
uint id1 = GetLevel1Id(address);
L2MapChunk * chunk = map[id1];
Assert(chunk != nullptr);
return chunk->IsMarked(address);
}
void
HeapBlockMap32::SetMark(void * address)
{
uint id1 = GetLevel1Id(address);
L2MapChunk * chunk = map[id1];
Assert(chunk != nullptr);
return chunk->SetMark(address);
}
bool
HeapBlockMap32::TestAndSetMark(void * address)
{
uint id1 = GetLevel1Id(address);
L2MapChunk * chunk = map[id1];
if (chunk == nullptr)
{
// False reference
return false;
}
uint bitIndex = chunk->GetMarkBitIndex(address);
return (chunk->markBits.TestAndSet(bitIndex) != 0);
}
void
HeapBlockMap32::ResetMarks()
{
for (uint i = 0; i < L1Count; i++)
{
L2MapChunk * chunk = map[i];
if (chunk == nullptr)
{
continue;
}
chunk->markBits.ClearAll();
#ifdef RECYCLER_VERIFY_MARK
chunk->isNewChunk = false;
#endif
#if DBG
for (uint j = 0; j < L2Count; j++)
{
chunk->pageMarkCount[j] = 0;
}
#endif
}
}
#if DBG
ushort
HeapBlockMap32::GetPageMarkCount(void * address) const
{
uint id1 = GetLevel1Id(address);
L2MapChunk * l2map = map[id1];
Assert(l2map != nullptr);
uint id2 = GetLevel2Id(address);
return l2map->pageMarkCount[id2];
}
void
HeapBlockMap32::SetPageMarkCount(void * address, ushort markCount)
{
uint id1 = GetLevel1Id(address);
L2MapChunk * l2map = map[id1];
Assert(l2map != nullptr);
uint id2 = GetLevel2Id(address);
// Callers should already have updated the mark bits by the time they call this,
// so check that the new count is correct for the current mark bits.
// Not true right now, will be true...
Assert(l2map->GetPageMarkBitVector(id2)->Count() == markCount);
l2map->pageMarkCount[id2] = markCount;
}
template void HeapBlockMap32::VerifyMarkCountForPages<SmallAllocationBlockAttributes::BitVectorCount>(void* address, uint pageCount);
template void HeapBlockMap32::VerifyMarkCountForPages<MediumAllocationBlockAttributes::BitVectorCount>(void* address, uint pageCount);
template <uint BitVectorCount>
void
HeapBlockMap32::VerifyMarkCountForPages(void * address, uint pageCount)
{
uint id1 = GetLevel1Id(address);
L2MapChunk * l2map = map[id1];
Assert(l2map != nullptr);
uint id2 = GetLevel2Id(address);
Assert(id2 + pageCount <= L2Count);
for (uint i = id2; i < pageCount + id2; i++)
{
uint markCountForPage = l2map->GetPageMarkBitVector(i)->Count();
Assert(markCountForPage == l2map->pageMarkCount[i]);
}
}
#endif
HeapBlockMap32::L2MapChunk::L2MapChunk()
{
// We are zero-initialized so don't need to actually init.
// Mark bits should be cleared by default
Assert(markBits.Count() == 0);
#ifdef RECYCLER_VERIFY_MARK
this->isNewChunk = true;
#endif
#if DBG
for (uint i = 0; i < L2Count; i++)
{
Assert(pageMarkCount[i] == 0);
}
#endif
}
HeapBlockMap32::L2MapChunk::~L2MapChunk()
{
// In debug builds, we guarantee that the heap block is clear on shutdown.
// In free builds, we skip this to save time.
// So this assert is only true in debug builds.
Assert(IsEmpty());
}
HeapBlock *
HeapBlockMap32::L2MapChunk::Get(void * address)
{
uint id2 = GetLevel2Id(address);
Assert(id2 < L2Count);
__analysis_assume(id2 < L2Count);
return map[id2];
}
void
HeapBlockMap32::L2MapChunk::Set(uint id2, uint pageCount, HeapBlock * heapBlock, HeapBlock::HeapBlockType blockType, byte bucketIndex)
{
uint id2End = id2 + pageCount;
Assert(id2 < L2Count);
Assert(id2End <= L2Count);
for (uint i = id2; i < id2End; i++)
{
__analysis_assume(i < L2Count);
Assert(map[i] == nullptr);
Assert(blockInfo[i].blockType == HeapBlock::HeapBlockType::FreeBlockType);
// Set the blockType last, because we will test this first during marking.
// If it's not FreeBlock, then we expect bucketIndex and heapBlock to be valid.
map[i] = heapBlock;
blockInfo[i].bucketIndex = bucketIndex;
// We need memory barrier here for ARM to ensure that the blockType is set last.
#if defined(_M_ARM32_OR_ARM64)
MemoryBarrier();
#endif
blockInfo[i].blockType = blockType;
}
}
void
HeapBlockMap32::L2MapChunk::Clear(uint id2, uint pageCount)
{
uint id2End = id2 + pageCount;
Assert(id2 < L2Count);
Assert(id2End <= L2Count);
for (uint i = id2; i < id2End; i++)
{
__analysis_assume(i < L2Count);
Assert(map[i] != nullptr);
Assert(blockInfo[i].blockType != HeapBlock::HeapBlockType::FreeBlockType);
// This shouldn't be called when concurrent marking is happening, so order does not matter.
// Regardless, set the blockType first just to be internally consistent.
// We don't actually clear the bucketIndex because it doesn't matter if the blockType is FreeBlock.
blockInfo[i].blockType = HeapBlock::HeapBlockType::FreeBlockType;
map[i] = nullptr;
}
}
bool
HeapBlockMap32::L2MapChunk::IsEmpty() const
{
for (uint i = 0; i < L2Count; i++)
{
if (map[i] != nullptr)
{
return false;
}
}
return true;
}
HeapBlockMap32::PageMarkBitVector *
HeapBlockMap32::L2MapChunk::GetPageMarkBitVector(void * address)
{
uint id2 = GetLevel2Id(address);
Assert(id2 < L2Count);
__analysis_assume(id2 < L2Count);
return GetPageMarkBitVector(id2);
}
HeapBlockMap32::PageMarkBitVector *
HeapBlockMap32::L2MapChunk::GetPageMarkBitVector(uint pageIndex)
{
return markBits.GetRange<PageMarkBitCount>(pageIndex * PageMarkBitCount);
}
template <size_t BitCount>
BVStatic<BitCount> *
HeapBlockMap32::L2MapChunk::GetMarkBitVectorForPages(void * address)
{
uint id2 = GetLevel2Id(address);
Assert(id2 < L2Count);
__analysis_assume(id2 < L2Count);
return GetMarkBitVectorForPages<BitCount>(id2);
}
template <size_t BitCount>
BVStatic<BitCount> *
HeapBlockMap32::L2MapChunk::GetMarkBitVectorForPages(uint pageIndex)
{
return markBits.GetRange<BitCount>(pageIndex * PageMarkBitCount);
}
bool
HeapBlockMap32::L2MapChunk::IsMarked(void * address) const
{
return markBits.Test(GetMarkBitIndex(address)) == TRUE;
}
void
HeapBlockMap32::L2MapChunk::SetMark(void * address)
{
markBits.Set(GetMarkBitIndex(address));
}
#ifdef RECYCLER_STRESS
void
HeapBlockMap32::InduceFalsePositives(Recycler * recycler)
{
for (uint i = 0; i < L1Count; i++)
{
L2MapChunk * chunk = map[i];
if (chunk == nullptr)
{
continue;
}
for (uint j = 0; j < L2Count; j++)
{
HeapBlock * block = chunk->map[j];
if (block == nullptr)
{
// Unallocated block. Try to mark the first offset, in case
// we are simultaneously allocating this block on the main thread.
recycler->TryMarkNonInterior((void *)GetAddressFromIds(i, j), nullptr);
}
else if (!block->IsLargeHeapBlock())
{
((SmallHeapBlock *)block)->InduceFalsePositive(recycler);
}
}
}
}
#endif
#ifdef RECYCLER_VERIFY_MARK
bool
HeapBlockMap32::IsAddressInNewChunk(void * address)
{
uint id1 = GetLevel1Id(address);
L2MapChunk * l2map = map[id1];
Assert(l2map != nullptr);
return l2map->isNewChunk;
}
#endif
#ifdef CONCURRENT_GC_ENABLED
template <class Fn>
void
HeapBlockMap32::ForEachSegment(Recycler * recycler, Fn func)
{
Segment * currentSegment = nullptr;
for (uint i = 0; i < L1Count; i++)
{
L2MapChunk * chunk = map[i];
if (chunk == nullptr)
{
continue;
}
for (uint j = 0; j < L2Count; j++)
{
HeapBlock * block = chunk->map[j];
if (block == nullptr)
{
continue;
}
Assert(block->GetSegment() != nullptr);
if (block->GetSegment() == currentSegment)
{
Assert(currentSegment != nullptr);
Assert(currentSegment->IsInSegment(block->GetAddress()));
continue;
}
// New segment.
Assert(currentSegment == nullptr || !currentSegment->IsInSegment(block->GetAddress()));
currentSegment = block->GetSegment();
AnalysisAssert(currentSegment != nullptr);
char * segmentStart = currentSegment->GetAddress();
size_t segmentLength = currentSegment->GetPageCount() * PageSize;
PageAllocator* segmentPageAllocator = (PageAllocator*)currentSegment->GetAllocator();
Assert(segmentPageAllocator == block->GetPageAllocator(recycler));
#if defined(_M_X64_OR_ARM64)
// On 64 bit, the segment may span multiple HeapBlockMap32 structures.
// Limit the processing to the portion of the segment in this HeapBlockMap32.
// We'll process other portions when we visit the other HeapBlockMap32 structures.
if (segmentStart < this->startAddress)
{
Assert(segmentLength > (size_t)(this->startAddress - segmentStart));
segmentLength -= (this->startAddress - segmentStart);
segmentStart = this->startAddress;
}
if ((segmentStart - this->startAddress) + segmentLength > HeapBlockMap32::TotalSize)
{
segmentLength = HeapBlockMap32::TotalSize - (segmentStart - this->startAddress);
}
#endif
func(segmentStart, segmentLength, currentSegment, segmentPageAllocator);
}
}
}
void
HeapBlockMap32::ResetWriteWatch(Recycler * recycler)
{
this->ForEachSegment(recycler, [=] (char * segmentStart, size_t segmentLength, Segment * segment, PageAllocator * segmentPageAllocator) {
Assert(segmentLength % AutoSystemInfo::PageSize == 0);
if (segmentPageAllocator == recycler->GetRecyclerPageAllocator() ||
segmentPageAllocator == recycler->GetRecyclerLargeBlockPageAllocator())
{
// Call ResetWriteWatch for Small non-leaf and Large segments.
UINT ret = ::ResetWriteWatch(segmentStart, segmentLength);
Assert(ret == 0);
}
#ifdef RECYCLER_WRITE_BARRIER
else if (segmentPageAllocator == recycler->GetRecyclerWithBarrierPageAllocator())
{
// Reset software write barrier for barrier segments.
RecyclerWriteBarrierManager::ResetWriteBarrier(segmentStart, segmentLength / AutoSystemInfo::PageSize);
}
#endif
});
}
bool
HeapBlockMap32::RescanPage(void * dirtyPage, bool* anyObjectsMarkedOnPage, Recycler * recycler)
{
uint id1 = GetLevel1Id(dirtyPage);
L2MapChunk * chunk = map[id1];
if (chunk != nullptr)
{
uint id2 = GetLevel2Id(dirtyPage);
HeapBlock::HeapBlockType blockType = chunk->blockInfo[id2].blockType;
// Determine block type and process as appropriate
switch (blockType)
{
case HeapBlock::HeapBlockType::FreeBlockType:
// We had a false reference to a free block. Do nothing.
break;
case HeapBlock::HeapBlockType::SmallNormalBlockType:
#ifdef RECYCLER_WRITE_BARRIER
case HeapBlock::HeapBlockType::SmallNormalBlockWithBarrierType:
#endif
return RescanHeapBlock<SmallNormalHeapBlock>(dirtyPage, blockType, chunk, id2, anyObjectsMarkedOnPage, recycler);
case HeapBlock::HeapBlockType::SmallFinalizableBlockType:
#ifdef RECYCLER_WRITE_BARRIER
case HeapBlock::HeapBlockType::SmallFinalizableBlockWithBarrierType:
#endif
return RescanHeapBlock<SmallFinalizableHeapBlock>(dirtyPage, blockType, chunk, id2, anyObjectsMarkedOnPage, recycler);
case HeapBlock::HeapBlockType::MediumNormalBlockType:
#ifdef RECYCLER_WRITE_BARRIER
case HeapBlock::HeapBlockType::MediumNormalBlockWithBarrierType:
#endif
return RescanHeapBlock<MediumNormalHeapBlock>(dirtyPage, blockType, chunk, id2, anyObjectsMarkedOnPage, recycler);
case HeapBlock::HeapBlockType::MediumFinalizableBlockType:
#ifdef RECYCLER_WRITE_BARRIER
case HeapBlock::HeapBlockType::MediumFinalizableBlockWithBarrierType:
#endif
return RescanHeapBlock<MediumFinalizableHeapBlock>(dirtyPage, blockType, chunk, id2, anyObjectsMarkedOnPage, recycler);
default:
// Shouldn't be here -- leaf blocks aren't rescanned, and large blocks are handled separately
Assert(false);
break;
}
}
return false;
}
template bool HeapBlockMap32::RescanHeapBlock<SmallNormalHeapBlock>(void * dirtyPage, HeapBlock::HeapBlockType blockType, L2MapChunk* chunk, uint id2, bool* anyObjectsMarkedOnPage, Recycler * recycler);
template bool HeapBlockMap32::RescanHeapBlock<SmallFinalizableHeapBlock>(void * dirtyPage, HeapBlock::HeapBlockType blockType, L2MapChunk* chunk, uint id2, bool* anyObjectsMarkedOnPage, Recycler * recycler);
template bool HeapBlockMap32::RescanHeapBlock<MediumNormalHeapBlock>(void * dirtyPage, HeapBlock::HeapBlockType blockType, L2MapChunk* chunk, uint id2, bool* anyObjectsMarkedOnPage, Recycler * recycler);
template bool HeapBlockMap32::RescanHeapBlock<MediumFinalizableHeapBlock>(void * dirtyPage, HeapBlock::HeapBlockType blockType, L2MapChunk* chunk, uint id2, bool* anyObjectsMarkedOnPage, Recycler * recycler);
template <class TBlockType>
bool
HeapBlockMap32::RescanHeapBlock(void * dirtyPage, HeapBlock::HeapBlockType blockType, L2MapChunk* chunk, uint id2, bool* anyObjectsMarkedOnPage, Recycler * recycler)
{
Assert(chunk != nullptr);
char* heapBlockPageAddress = TBlockType::GetBlockStartAddress((char*) dirtyPage);
typedef TBlockType::HeapBlockAttributes TBlockAttributes;
// We need to check the entire mark bit vector here. It's not sufficient to just check the page's
// mark bit vector because the object that's dirty on the page could have started on an earlier page
auto markBits = chunk->GetMarkBitVectorForPages<TBlockAttributes::BitVectorCount>(heapBlockPageAddress);
if (!markBits->IsAllClear())
{
Assert(chunk->map[id2]->GetHeapBlockType() == blockType);
// Small finalizable heap blocks require the HeapBlock * (to look up object attributes).
// For others, this is null
TBlockType* block = GetHeapBlockForRescan<TBlockType>(chunk, id2);
uint bucketIndex = chunk->blockInfo[id2].bucketIndex;
if (!SmallNormalHeapBucketBase<TBlockType>::RescanObjectsOnPage(block,
(char *)dirtyPage, heapBlockPageAddress, markBits, HeapInfo::GetObjectSizeForBucketIndex<TBlockAttributes>(bucketIndex), bucketIndex, anyObjectsMarkedOnPage, recycler))
{
// Failed due to OOM
((TBlockType*) chunk->map[id2])->SetNeedOOMRescan(recycler);
return false;
}
return true;
}
// Didn't actually rescan the block.
return false;
}
template <typename TBlockType>
TBlockType*
HeapBlockMap32::GetHeapBlockForRescan(HeapBlockMap32::L2MapChunk* chunk, uint id2) const
{
return nullptr;
}
template <>
SmallFinalizableHeapBlock*
HeapBlockMap32::GetHeapBlockForRescan(HeapBlockMap32::L2MapChunk* chunk, uint id2) const
{
return (SmallFinalizableHeapBlock*) chunk->map[id2];
}
template <>
MediumFinalizableHeapBlock*
HeapBlockMap32::GetHeapBlockForRescan(HeapBlockMap32::L2MapChunk* chunk, uint id2) const
{
return (MediumFinalizableHeapBlock*)chunk->map[id2];
}
void
HeapBlockMap32::MakeAllPagesReadOnly(Recycler* recycler)
{
this->ChangeProtectionLevel(recycler, PAGE_READONLY, PAGE_READWRITE);
}
void
HeapBlockMap32::MakeAllPagesReadWrite(Recycler* recycler)
{
this->ChangeProtectionLevel(recycler, PAGE_READWRITE, PAGE_READONLY);
}
void
HeapBlockMap32::ChangeProtectionLevel(Recycler* recycler, DWORD protectFlags, DWORD expectedOldFlags)
{
this->ForEachSegment(recycler, [&](char* segmentStart, size_t segmentLength, Segment* currentSegment, PageAllocator* segmentPageAllocator)
{
// Ideally, we shouldn't to exclude LargeBlocks here but guest arenas are allocated
// from this allocator and we touch them during marking if they're pending delete
if ((segmentPageAllocator != recycler->GetRecyclerLeafPageAllocator())
&& (segmentPageAllocator != recycler->GetRecyclerLargeBlockPageAllocator()))
{
Assert(currentSegment->IsPageSegment());
((PageSegment*)currentSegment)->ChangeSegmentProtection(protectFlags, expectedOldFlags);
}
});
}
///
/// The GetWriteWatch API can fail under low-mem situations if called to retrieve write-watch for a large number of pages
/// (On Win10, > 255 pages). This helper is to handle the failure case. In the case of failure, we degrade to retrieving
/// the write-watch one page at a time since that's expected to succeed
///
UINT
HeapBlockMap32::GetWriteWatchHelper(Recycler * recycler, DWORD writeWatchFlags, void* baseAddress, size_t regionSize,
void** addresses, ULONG_PTR* count, LPDWORD granularity)
{
UINT ret = 0;
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
if (recycler->GetRecyclerFlagsTable().ForceGetWriteWatchOOM)
{
if (regionSize != AutoSystemInfo::PageSize)
{
ret = (UINT) -1;
}
}
else
#endif
{
ret = ::GetWriteWatch(writeWatchFlags, baseAddress, regionSize, addresses, count, granularity);
}
if (ret != 0 && regionSize != AutoSystemInfo::PageSize)
{
ret = GetWriteWatchHelperOnOOM(writeWatchFlags, baseAddress, regionSize, addresses, count, granularity);
}
Assert(ret == 0);
return ret;
}
// OOM codepath- Retrieve write-watch one page at a time
// It's slow, but we are okay with that during OOM
// Factored into its own function to help the compiler inline the parent
UINT
HeapBlockMap32::GetWriteWatchHelperOnOOM(DWORD writeWatchFlags, _In_ void* baseAddress, size_t regionSize,
_Out_writes_(*count) void** addresses, _Inout_ ULONG_PTR* count, LPDWORD granularity)
{
const size_t pageCount = (regionSize / AutoSystemInfo::PageSize);
// Ensure target buffer
AnalysisAssertMsg(*count >= pageCount, "Not enough space in the buffer to store the write watch state for the given region size");
void* result = nullptr;
size_t dirtyCount = 0;
for (size_t i = 0; i < pageCount; i++)
{
result = nullptr;
char* pageAddress = ((char*)baseAddress) + (i * AutoSystemInfo::PageSize);
ULONG_PTR resultBufferCount = 1;
DWORD r = ::GetWriteWatch(writeWatchFlags, pageAddress, AutoSystemInfo::PageSize, &result, &resultBufferCount, granularity);
Assert(r == 0);
Assert(resultBufferCount <= 1);
AnalysisAssert(dirtyCount <= pageCount);
// The requested page was dirty
if (resultBufferCount == 1)
{
Assert(result == pageAddress);
addresses[dirtyCount] = pageAddress;
dirtyCount++;
}
}
Assert(dirtyCount <= *count);
*count = dirtyCount;
return 0;
}
uint
HeapBlockMap32::Rescan(Recycler * recycler, bool resetWriteWatch)
{
// Loop through segments and find dirty pages.
const DWORD writeWatchFlags = (resetWriteWatch ? WRITE_WATCH_FLAG_RESET : 0);
uint scannedPageCount = 0;
bool anyObjectsScannedOnPage = false;
this->ForEachSegment(recycler, [&] (char * segmentStart, size_t segmentLength, Segment * currentSegment, PageAllocator * segmentPageAllocator) {
Assert(segmentLength % AutoSystemInfo::PageSize == 0);
// Call GetWriteWatch for Small non-leaf segments.
// Large blocks have their own separate write watch handling.
if (segmentPageAllocator == recycler->GetRecyclerPageAllocator())
{
// array for WW results
void * dirtyPageAddresses[MaxGetWriteWatchPages];
Assert(segmentLength <= MaxGetWriteWatchPages * PageSize);
ULONG_PTR pageCount = MaxGetWriteWatchPages;
DWORD pageSize = PageSize;
UINT ret = HeapBlockMap32::GetWriteWatchHelper(recycler, writeWatchFlags, segmentStart, segmentLength, dirtyPageAddresses, &pageCount, &pageSize);
Assert(ret == 0);
Assert(pageSize == PageSize);
Assert(pageCount <= MaxGetWriteWatchPages);
// Process results:
// Loop through reported dirty pages and set their write watch bit.
for (uint i = 0; i < pageCount; i++)
{
char * dirtyPage = (char *)dirtyPageAddresses[i];
Assert((((size_t)dirtyPage) % PageSize) == 0);
Assert(dirtyPage >= segmentStart);
Assert(dirtyPage < segmentStart + segmentLength);
#if defined(_M_X64_OR_ARM64)
Assert(HeapBlockMap64::GetNodeStartAddress(dirtyPage) == this->startAddress);
#endif
if (RescanPage(dirtyPage, &anyObjectsScannedOnPage, recycler) && anyObjectsScannedOnPage)
{
scannedPageCount++;
}
}
}
#ifdef RECYCLER_WRITE_BARRIER
else if (segmentPageAllocator == recycler->GetRecyclerWithBarrierPageAllocator())
{
// Loop through pages for this segment and check write barrier.
size_t pageCount = segmentLength / AutoSystemInfo::PageSize;
for (size_t i = 0; i < pageCount; i++)
{
char * pageAddress = segmentStart + (i * AutoSystemInfo::PageSize);
Assert((size_t)(pageAddress - segmentStart) < segmentLength);
#if defined(_M_X64_OR_ARM64)
Assert(HeapBlockMap64::GetNodeStartAddress(pageAddress) == this->startAddress);
#endif
// TODO: We are not resetting the write barrier here when RescanFlags_ResetWriteWatch is passed.
// We never have previously, but it still seems like we should.
BYTE writeBarrierByte = RecyclerWriteBarrierManager::GetWriteBarrier(pageAddress);
SwbVerboseTrace(recycler->GetRecyclerFlagsTable(), L"Address: 0x%p, Write Barrier value: %u\n", pageAddress, writeBarrierByte);
bool isDirty = (writeBarrierByte == 1);
if (isDirty)
{
if (RescanPage(pageAddress, &anyObjectsScannedOnPage, recycler) && anyObjectsScannedOnPage)
{
scannedPageCount++;
}
}
}
}
#endif
else
{
Assert(segmentPageAllocator == recycler->GetRecyclerLeafPageAllocator() ||
segmentPageAllocator == recycler->GetRecyclerLargeBlockPageAllocator());
}
});
return scannedPageCount;
}
bool
HeapBlockMap32::OOMRescan(Recycler * recycler)
{
this->anyHeapBlockRescannedDuringOOM = false;
bool noHeapBlockNeedsRescan = true;
// Loop through segments and find pages that need OOM Rescan.
this->ForEachSegment(recycler, [=, &noHeapBlockNeedsRescan] (char * segmentStart, size_t segmentLength, Segment * currentSegment, PageAllocator * segmentPageAllocator) {
Assert(segmentLength % AutoSystemInfo::PageSize == 0);
// Process Small non-leaf segments (including write barrier blocks).
// Large blocks have their own separate write watch handling.
if (segmentPageAllocator == recycler->GetRecyclerPageAllocator()
#ifdef RECYCLER_WRITE_BARRIER
|| segmentPageAllocator == recycler->GetRecyclerWithBarrierPageAllocator()
#endif
)
{
if (recycler->NeedOOMRescan())
{
// We hit OOM again. Don't try to process any more blocks, leave them for the next OOM pass.
return;
}
// Loop through pages for this segment and check OOM flag.
size_t pageCount = segmentLength / AutoSystemInfo::PageSize;
for (size_t i = 0; i < pageCount; i++)
{
char * pageAddress = segmentStart + (i * AutoSystemInfo::PageSize);
Assert((size_t)(pageAddress - segmentStart) < segmentLength);
#if defined(_M_X64_OR_ARM64)
Assert(HeapBlockMap64::GetNodeStartAddress(pageAddress) == this->startAddress);
#endif
uint id1 = GetLevel1Id(pageAddress);
L2MapChunk * chunk = map[id1];
if (chunk != nullptr)
{
uint id2 = GetLevel2Id(pageAddress);
HeapBlock * heapBlock = chunk->map[id2];
if (heapBlock != nullptr && heapBlock->GetAddress() == pageAddress)
{
if (heapBlock->GetAndClearNeedOOMRescan())
{
noHeapBlockNeedsRescan = false;
HeapBlock::HeapBlockType blockType = chunk->blockInfo[id2].blockType;
// Determine block type and process as appropriate
switch (blockType)
{
case HeapBlock::HeapBlockType::FreeBlockType:
// Can't have a free block that has OOMRescan flag set
Assert(false);
break;
case HeapBlock::HeapBlockType::SmallNormalBlockType:
#ifdef RECYCLER_WRITE_BARRIER
case HeapBlock::HeapBlockType::SmallNormalBlockWithBarrierType:
#endif
if (!RescanHeapBlockOnOOM<SmallNormalHeapBlock>((SmallNormalHeapBlock*)heapBlock, pageAddress, blockType, chunk->blockInfo[id2].bucketIndex, chunk, recycler))
{
return;
}
break;
case HeapBlock::HeapBlockType::SmallFinalizableBlockType:
#ifdef RECYCLER_WRITE_BARRIER
case HeapBlock::HeapBlockType::SmallFinalizableBlockWithBarrierType:
#endif
if (!RescanHeapBlockOnOOM<SmallFinalizableHeapBlock>((SmallFinalizableHeapBlock*) heapBlock, pageAddress, blockType, chunk->blockInfo[id2].bucketIndex, chunk, recycler))
{
return;
}
break;