Skip to content

When cloning method descs be careful with fields that have different offsets between the source and destination. #115798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/coreclr/vm/methodtablebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7148,16 +7148,14 @@ VOID MethodTableBuilder::AllocAndInitMethodDescChunk(COUNT_T startIndex, COUNT_T
// and should not be used. We should go to the effort of having proper constructors
// in the MethodDesc class. </NICE>

memcpy(pUnboxedMD, pMD, pMD->SizeOf());

// Reset the chunk index
pUnboxedMD->SetChunkIndex(pChunk);

if (bmtGenerics->GetNumGenericArgs() == 0) {
pUnboxedMD->SetHasNonVtableSlot();
if (bmtGenerics->GetNumGenericArgs() == 0)
{
memcpy(pUnboxedMD, pMD, pMD->GetBaseSize());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically the fix. memcpy(pUnboxedMD, pMD, pMD->SizeOf()); vs. memcpy(pUnboxedMD, pMD, pMD->GetBaseSize());.

memcpy(pUnboxedMD, pMD, pMD->SizeOf()); may copy a few fields to wrong locations. Then it is not enough to write correct values to the new locations, we would also need to clean up wrong locations.

It is better for the initial memcopy to do only the base portion, which is the same between different layouts. The rest stays zero-inited.

Copy link
Member Author

@VSadov VSadov May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem that we see now is that we are corrupting a slot that is later set via interlocked exchange, which expects 0 comparand.
Since we have junk in the slot, the exchange does not happen and the junk stays in the slot and causes crashes.


// By settings HasNonVTableSlot, the following chunks of data have been shifted around.
// This is an example of the fragility noted in the memcpy comment above
pUnboxedMD->SetHasNonVtableSlot();

if (pUnboxedMD->HasNativeCodeSlot())
{
*pUnboxedMD->GetAddrOfNativeCodeSlot() = *pMD->GetAddrOfNativeCodeSlot();
Expand All @@ -7171,6 +7169,13 @@ VOID MethodTableBuilder::AllocAndInitMethodDescChunk(COUNT_T startIndex, COUNT_T
*pUnboxedMD->GetAddrOfAsyncMethodData() = *pMD->GetAddrOfAsyncMethodData();
}
}
else
{
memcpy(pUnboxedMD, pMD, pMD->SizeOf());
}

// Reset the chunk index
pUnboxedMD->SetChunkIndex(pChunk);

//////////////////////////////////////////////////////////
// Modify the original MethodDesc to be an unboxing stub
Expand Down
55 changes: 55 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_115667/test115667.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using Xunit;
using System.Threading.Tasks;

public interface I0
{
Task<bool> M8();
}

public struct S1 : I0
{
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public async Task<bool> M8()
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
return false;
}
}

public class Runtime_115667
{
[Fact]
public static void TestEntryPoint()
{
System.Runtime.Loader.AssemblyLoadContext alc = new CollectibleALC();
System.Reflection.Assembly asm = alc.LoadFromAssemblyPath(System.Reflection.Assembly.GetExecutingAssembly().Location);
System.Reflection.MethodInfo mi = asm.GetType(typeof(Runtime_115667).FullName).GetMethod(nameof(MainInner));
System.Type runtimeTy = asm.GetType(typeof(Runtime).FullName);
mi.Invoke(null, new object[] { System.Activator.CreateInstance(runtimeTy) });
}

public static void MainInner(IRuntime rt)
{
bool vr1 = new S1().M8().GetAwaiter().GetResult();
}
}

public interface IRuntime
{
void WriteLine<T>(string site, T value);
}

public class Runtime : IRuntime
{
public void WriteLine<T>(string site, T value) => System.Console.WriteLine(value);
}

public class CollectibleALC : System.Runtime.Loader.AssemblyLoadContext
{
public CollectibleALC() : base(true)
{
}
}
12 changes: 12 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_115667/test115667.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Optimize>True</Optimize>
<CLRTestPriority>1</CLRTestPriority>
</PropertyGroup>
<ItemGroup>
<Compile Include="test115667.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" />
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions src/tests/issues.targets
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,9 @@
<ExcludeList Include="$(XunitTestBinBase)/JIT/Regression/JitBlue/Runtime_90219/Runtime_90219/**">
<Issue>https://github.com/dotnet/runtimelab/issues/155: Assembly.Load</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/Regressions/coreclr/GitHub_115667/**">
<Issue>https://github.com/dotnet/runtimelab/issues/155: Assembly.Load</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/reflection\DefaultInterfaceMethods\Emit\*">
<Issue>https://github.com/dotnet/runtimelab/issues/155: Reflection.Emit</Issue>
</ExcludeList>
Expand Down Expand Up @@ -3064,6 +3067,9 @@
<ExcludeList Include="$(XunitTestBinBase)/JIT/Regression/JitBlue/Runtime_90219/Runtime_90219/*">
<Issue>Loads an assembly from file</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/Regressions/coreclr/GitHub_115667/**">
<Issue>Loads an assembly from file</Issue>
</ExcludeList>
</ItemGroup>

<!-- Apple mobile NativeAOT -->
Expand Down
Loading