Skip to content

System.AccessViolationException occurring in managed code on dotnet 7 #83242

@richard11ants

Description

@richard11ants

Description

My managed C# code that loops over some data is triggering a System.AccessViolationException

  • Does not happen in previous dotnet versions - same identical data/code has been running without problem on .NET 5 and .NET 6
  • Does not happen in .NET 7 debug mode
  • Only happens in .NET 7 release (optimized code)

I added trace messages to pinpoint the lines of code that fail - it seems that it fails to enter the loop at some point after many iterations have been performed. Interestingly it does not fail if I output the loop counter in a trace message.
Could it be some sort of loop optimization that has gone wrong? Or some other memory corruption caused by other parts of the code?
Either way, the code is fully managed and works fine in all other runtimes I have tested.

I'm new to going deep on this kind of problem, please let me know what other information I could capture to help diagnose further.

Reproduction Steps

Difficult to give full context as this is a small part of a large complex project, involving large streams of data.
I have narrowed down the problem to the following bit of code, where I have added trace messages while trying to narrow down the point at which it fails:

//...outer loop over data establishes customerStats and categoryIndex variables...
Trace.WriteLine("*** Pre Loop");
for (int measureIndex = 0; measureIndex < 4; measureIndex++)
{
Trace.WriteLine("*** Start Loop");
//Trace.WriteLine("*** measureIndex = " + measureIndex); // does not crash if this line is uncommented!
Measure measure = (Measure)measureIndex;
if (categoryIndex >= 0)
{
measureChangesPerCategory[categoryIndex][measureIndex] += customerStats.MeasureChange(measure, category);
}
if (category == GainLossCategory.SwitchedFromFocusProduct)
{
RecordProductsSwitchedAmount(productCodeSwitchedToGainMapPerMeasure[measureIndex], measure, customerActivity, header, p2Start, p2End, referenceCategorySelector, includeTax);
}
else if (category == GainLossCategory.SwitchedToFocusProduct)
{
RecordProductsSwitchedAmount(productCodeSwitchedFromLossMapPerMeasure[measureIndex], measure, customerActivity, header, p1Start, p1End, referenceCategorySelector, includeTax);
}
}
//..outer loop completes

Expected behavior

The loop will execute without failure.

Actual behavior

End of trace output in release mode looks like this:

... many repeated loops are performed until...
*** Pre Loop
*** Start Loop
*** Start Loop
*** Start Loop
*** Start Loop
*** Pre Loop
*** Start Loop
*** Start Loop
*** Start Loop
*** Start Loop
*** Pre Loop
An unhandled exception of type 'System.AccessViolationException' occurred in pluginlogic.dll
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Regression?

No response

Known Workarounds

No response

Configuration

Running dotnet SDK 7.0.201 / runtime 7.0.3
Has been reproduced in multiple environments (Ubuntu VM on Azure and on my Windows 11 x64 local development machine)

Other information

No response

Metadata

Metadata

Assignees

Labels

Priority:2Work that is important, but not critical for the releasearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions