Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Method argument magically changes value in .Net 10 in release mode #122640

@Wojmik

Description

@Wojmik

Description

After upgrade from .Net 9 to 10, my application started to produce wrong result.
After chasing down I was able to find out, that argument passed from method to method changes its value. It happens only on .Net 10 release mode. And after thousands of iterations. Probably some optimization kick in, method is recompiled to broken version (stack corruption?).

I've extracted the code to new app presenting the issue.

I'm passing an int argument through few methods and observe the value of the argument (by Trace.WriteLine). From first method to second, and from second to third (nested calls).
At the beginning output looks like the following:

Alert-1, regionId 2419
Alert-2
Alert-3, regionId 2419

Alerts 1, 2 and 3 come from one of each methods. Value 2419 is correct. But after about a milion iterations the output starts looking like the following:

Alert-1, regionId 2419
Alert-2
Alert-3, regionId 2305

Value suddenly has changed to 2305. No idea from where the incorrect value come from.
Surprisingly, if value in second method is observed (by change Trace.WriteLine($"Alert-2"); to Trace.WriteLine($"Alert-2, regionId {regionToId}");), the issue disappears. Like in double slit experiment 😄.

What I've noticed - the method has to be generic, and has to work with big struct.

ChasingTheBug.zip

Reproduction Steps

Run attached application in release mode. It shows if the issue occurred or not, and shows the argument's value.

Expected behavior

Not change argument's value 😄.

Actual behavior

Value of the argument changes.

Regression?

Lack of.

Known Workarounds

I didn't investigate it in details yet. Probably bypass inlining is a workaround.

Configuration

.Net 10.0.101
Windows 11 25H2
x64
I checked the issue on few different computers with .Net 10 installed. On Windows 10 and Windows Server 2019 (all x64). The app behaves the same on all computers I've checked.

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions