Roslyn: Access violation on null-conditional access of tuple item when tuple is a generic class property

Created on 21 Jan 2020  路  3Comments  路  Source: dotnet/roslyn

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.

Minimal example

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>

Expected output

> dotnet run -c Release
Hello World!

Actual output

> 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()
Area-Compilers Bug Resolution-Duplicate

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings