Skip to content

Commit

Permalink
Fix max chunk size limiting (#81192)
Browse files Browse the repository at this point in the history
When the new stub heaps were implemented, the chunk size needed to be
limited so that all precodes for a chunk fit into a single memory page.
That was done in `MethodDescChunk::CreateChunk`. But it was discovered
now that there is another place where it needs to be limited, the
`MethodTableBuilder::AllocAndInitMethodDescChunk`. The
JIT\opt\ObjectStackAllocation\ObjectStackAllocationTests started to fail
in the outerloop due to too long chunk. The failure happens in crossgen2
as it is using a separate build of runtime in release and only that
uncovers the problem. And only when DOTNET_TieredCompilation=0 and
DOTNET_ProfApi_RejitOnAttach=0.

This change fixes the problem. With this change applied to the dotnet
runtime version used by the crossgen2 and patching it in place in the
.dotnet/shared/.... directory, the issue doesn't occur.

Close #81103
  • Loading branch information
janvorli authored Jan 26, 2023
1 parent 1429bdf commit 95723eb
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/coreclr/vm/methodtablebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6860,9 +6860,15 @@ VOID MethodTableBuilder::AllocAndInitMethodDescs()
SIZE_T sizeOfMethodDescs = 0; // current running size of methodDesc chunk
int startIndex = 0; // start of the current chunk (index into bmtMethod array)

// Limit the maximum MethodDescs per chunk by the number of precodes that can fit to a single memory page,
// since we allocate consecutive temporary entry points for all MethodDescs in the whole chunk.
DWORD maxPrecodesPerPage = Precode::GetMaxTemporaryEntryPointsCount();
DWORD methodDescCount = 0;

DeclaredMethodIterator it(*this);
while (it.Next())
{
DWORD currentSlotMethodDescCount = 1;
int tokenRange = GetTokenRange(it.Token());

// This code assumes that iterator returns tokens in ascending order. If this assumption does not hold,
Expand All @@ -6885,6 +6891,7 @@ VOID MethodTableBuilder::AllocAndInitMethodDescs()
// See comment in AllocAndInitMethodDescChunk
if (NeedsTightlyBoundUnboxingStub(*it))
{
currentSlotMethodDescCount = 2;
size *= 2;

if (bmtGenerics->GetNumGenericArgs() == 0) {
Expand All @@ -6896,7 +6903,8 @@ VOID MethodTableBuilder::AllocAndInitMethodDescs()
}

if (tokenRange != currentTokenRange ||
sizeOfMethodDescs + size > MethodDescChunk::MaxSizeOfMethodDescs)
sizeOfMethodDescs + size > MethodDescChunk::MaxSizeOfMethodDescs ||
methodDescCount + currentSlotMethodDescCount > maxPrecodesPerPage)
{
if (sizeOfMethodDescs != 0)
{
Expand All @@ -6906,9 +6914,11 @@ VOID MethodTableBuilder::AllocAndInitMethodDescs()

currentTokenRange = tokenRange;
sizeOfMethodDescs = 0;
methodDescCount = 0;
}

sizeOfMethodDescs += size;
methodDescCount += currentSlotMethodDescCount;
}

if (sizeOfMethodDescs != 0)
Expand Down

0 comments on commit 95723eb

Please sign in to comment.