Repository navigation
Description
Description
We have a code running that runs correctly on Windows x64 and that was running correctly on Arm64 with .NET 7.
Starting from .NET 8, we observed that one if statement does not seem to behave as written in the code.
Extract of the code is like this:
public void Select(Entity entity)
{
if (ShouldSelectEntity(entity)
{
SelectEntity(entity);
}
}
private void ShouldSelectEntity(Entity entity)
{
if (entity.TestObject != null ||
entity.Score <= m_scoreThreshold) // THIS DOES NOT SEEM TO WORK!!!
return false;
... // Other complex conditions, returning true or false according to entity content.
}
private void SelectEntity(Entity entity)
{
if (entity.Score <= m_scoreThreshold)
{
// We should never get here because of the test in
// ShouldSelectEntity.
throw new Exception($"Should not have selected this object because Score {entity.Score} is lower than threshold {m_scoreThreshold}");
}
... // Other stuff.
}We observed that the exception in SelectEntity is thrown, even if it should never happens.
The code above is not sufficient to reproduce the bug. For the bug to be present, the above code, it needs to be part of a larger program. The initial program has been stripped greatly and is published here:
https://github.com/Louis-Antoine-Blais-Morin/JitBugArm64
It is still relatively large, but it seems that removing parts of the program seems to make the bug disappear. Maybe more effort can lead to a smaller code.
Reproduction Steps
- Checkout code from
https://github.com/Louis-Antoine-Blais-Morin/JitBugArm64 - Open JitBugArm64.sln
- Compile it in Release in VisualStudio 17.9.1.
- Copy the content of bin\Release\net8.0 on a Arm64 platform running Linux and where .NET Runtime 8.0.3 is installed
- dotnet run JitBugArm64.dll
- An exception is thrown after few seconds
Expected behavior
The if statement should behave as expected in the C# code.
Therefore, no exception should be thrown in the code linked to this bug.
Actual behavior
The if statement in the ShouldSelectEntity does not behave correctly.
The function seems to returns true even if entity.Score > m_scoreThreshold.
This seems to happen after a few seconds of correct operation.
I suspect that the behavior starts to be incorrect after Tier 1 JIT compilation.
Regression?
This is a regression since the program is working correctly with .NET 7.
The program is also working correctly under Windows x64.
Known Workarounds
The following workarounds seem to fix the behavior:
- Replace the
if ( ... || ...) return false;by two individualifstatements. - Add the following attribute on the
ShouldSelectEntitymethod:[MethodImpl(MethodImplOptions.NoInlining)] - Remove the
<DebugSymbols>true</DebugSymbols>in the .csproj
Configuration
$ dotnet --list-runtimes
Microsoft.AspNetCore.App 8.0.3 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.3 [/usr/share/dotnet/shared/Microsoft.NETCore.App]`
Processor Architecture is Arm64 Cortex A53 / Armv8-A.
Linux is "Yocto Kirkstone".
Other information
No response
Activity
jakobbotsch commentedon Mar 21, 2024
cc @dotnet/jit-contrib
JulieLeeMSFT commentedon Mar 21, 2024
@kunalspathak PTAL.
AndyAyersMS commentedon Mar 26, 2024
@kunalspathak can you give an update on this? Or if you're tied up can we find somebody else to dig in?
kunalspathak commentedon Mar 26, 2024
Sorry, I forgot about this. I was able to repro this and I can confirm that this doesn't repro on .NET 9
main. I will investigate.kunalspathak commentedon Mar 27, 2024
The problem is the reverse condition was wrongly calculated as seen in screenshot( left= net9/correct, right= net8/wrong)
We fixed it in #92316 and might need to backport.
kunalspathak commentedon Mar 27, 2024
btw, this is easily reproducible with
DOTNET_TieredCompilation=0and markingSelectas NoInlining andShouldSelectEntityas AggresivelyInline. I also changedScoreThreshold=10.0fandScore = (float)0.0f,//random.NextDouble(),kunalspathak commentedon Mar 28, 2024
I verified this fix locally and it doesn't throw Exception anymore.
kunalspathak commentedon May 2, 2024
This should be part of the upcoming .NET 8.0.5
AndyAyersMS commentedon May 31, 2024
@Louis-Antoine-Blais-Morin .NET 8.0.5 is now available -- when you get a chance can you verify it fixes this issue for you?
Louis-Antoine-Blais-Morin commentedon May 31, 2024
@AndyAyersMS
Thanks for all this work. We followed it closely.
With the fix now available, we are now trying once again to switch to .NET 8 (because of the problem, we stayed in .NET 7).
I will inform you about the results in the coming days.
kunalspathak commentedon Jul 25, 2024
@Louis-Antoine-Blais-Morin - did you get a chance to validate this?
Louis-Antoine-Blais-Morin commentedon Jul 29, 2024
Good morning,
Providing feedback on this got of my radar for a while.
I had time to test the correction today:
[MethodImpl(MethodImplOptions.NoInlining)]that was in our code as a workaroundSo, as far as I can see, the bug is fixed.
Thanks a lot!