.NET 8 Release mode causes AccessViolationException instead of IndexOutOfRangeException

Open
#116,516 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
compilers

Research direction

Start by running the supplied Main and Bar reproduction in Release with net8.0, then compare it with net9.0 and Debug. Focus on the difference between the expected IndexOutOfRangeException and the observed AccessViolationException; the work is done when the net8.0 Release case behaves consistently with the expected result.

Written by the indexing model from the issue text.

Description

area-CodeGen-coreclr
Description

When running the following code with different configurations in .NET 8, the program behavior is different.
When using Debug configuration, the program exits normally.
When using Release configuration, the program throws System.AccessViolationException. It happens sometimes with a high probability.

However, it doesn't happen in .NET 9.

Reproduction Steps
  1. Using the following code:
using System.Runtime.CompilerServices;
using Xunit;

public class Test
{
    public static void Main(string[] args)
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 200; j++)
            {
                Assert.Throws<IndexOutOfRangeException>(() => Bar(new int[10], new C()));
            }
            Thread.Sleep(100);
        }
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static void Bar(int[] arr, I iface)
    {
        if (arr == null)
        {
            return;
        }

        iface.Foo();

        int i = 10000000;
        do
        {
            Console.WriteLine(arr[i]);
            i++;
        } while (i < arr.Length);
    }
}

internal interface I
{
    void Foo();
}

internal class C : I
{
    public void Foo()
    {
    }
}
Expected behavior

Both exit normally.

Actual behavior

When using Debug configuration, the program exits normally.
When using Release configuration, the program throws System.AccessViolationException.

Regression?

No response

Known Workarounds

No response

Configuration
.NET SDK:
 Version:           9.0.301
 Commit:            a596cd22e2
 Workload version:  9.0.300-manifests.9bcbba73
 MSBuild version:   17.14.5+edd3bbf37

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.26100
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\9.0.301\

Host:
  Version:      9.0.6
  Architecture: x64
  Commit:       3875b54e7b

.NET SDKs installed:
  8.0.411 [C:\Program Files\dotnet\sdk]
  9.0.301 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 8.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 8.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 9.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Here is the csproj file.
When TargetFramework is net8.0, the program throws System.AccessViolationException.
When TargetFramework is net9.0, the program exits normally.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="xunit" Version="2.9.3" />
  </ItemGroup>

</Project>

Other information

No response

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.