When a generic class has a tuple property utilizing its generic type, a null-conditional access of the tuple's item causes System.AccessViolationException: Attempted to read or write protected memory.
Observed on .NET Core runtimes 3.1, 2.2, and 2.1 in Windows, and 2.2 in Ubuntu 16. The 2.x runtimes do not display the error to console, but it is logged under Application in the Windows Event Log.
.NET Framework runtime appears to be unaffected.
Program.cs -
using System;
class Program
{
static void Main()
{
GenericMethod<string>();
Console.WriteLine("Hello World!");
}
static void GenericMethod<T>()
{
new GenericClass<T>().Prop.Item2?.ToString();
}
}
public class GenericClass<T>
{
public ValueTuple<long, T> Prop { get; }
}
Crasher.csproj -
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
> dotnet run -c Release
Hello World!
> dotnet run -c Release
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Program.GenericMethod[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]()
at Program.Main()
Invalid IL in GenericMethod<T> generated by Roslyn.
@jaredpar Could you please transfer this issue to Roslyn repo? This is likely dup of https://github.com/dotnet/roslyn/issues/40690.
@RikkiGibson can u confirm this is a dupe of #40690
Yes, this looks like a dupe. The program works with the latest Microsoft.Net.Compilers.Toolset compiler.