.NET 8 Release mode causes AccessViolationException instead of IndexOutOfRangeException
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
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
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
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
Create parent directories only after the containment check in InstallHelper.TryExtractToDirectory Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PowerShell/PSResourceGet#2056 ·