Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NativeAOT: bug on long comparison #1214

Closed
dellamonica opened this issue Jun 9, 2021 · 4 comments
Closed

NativeAOT: bug on long comparison #1214

dellamonica opened this issue Jun 9, 2021 · 4 comments
Labels
area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation

Comments

@dellamonica
Copy link

Here's a small project that reproduces the issue:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
  
  <PropertyGroup>
    <Optimize>true</Optimize>
    <RootAllApplicationAssemblies>false</RootAllApplicationAssemblies>
    <IlcInvariantGlobalization>true</IlcInvariantGlobalization>
    <IlcGenerateCompleteTypeMetadata>false</IlcGenerateCompleteTypeMetadata>
    <IlcDisableReflection>true</IlcDisableReflection>
    <IlcOptimizationPreference>Speed</IlcOptimizationPreference>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="6.0.0-*" />
  </ItemGroup>

</Project>
using System;

namespace BugNativeAOT
{
    internal class Program
    {
        private readonly struct Size
        {
            public int Width { get; }

            public int Height { get; }

            public long Area => Width * (long)Height;

            public Size(int width, int height)
            {
                Width = width;
                Height = height;
            }
        }

        private sealed class Bug
        {
            public Bug(Size s)
            {
                S = s;
            }

            public Size S { get; }
        }

        private static void Main(string[] args)
        {
            var t = new Bug(new Size(0, 0));
            if (t.S.Area == 0L)
            {
                Console.WriteLine("Expected: == operator");
            }
            else
            {
                Console.WriteLine("UNEXPECTED: == operator");
                Console.WriteLine($"Area value = {t.S.Area}");
            }
            if (0L.Equals(t.S.Area))
            {
                Console.WriteLine("Expected: long.Equals");
            }
            else
            {
                Console.WriteLine("UNEXPECTED: == long.Equals");
                Console.WriteLine($"Area value = {t.S.Area}");
            }
            if (((object)t.S.Area).Equals(0L))
            {
                Console.WriteLine("Expected: object.Equals boxed");
            }
            else
            {
                Console.WriteLine("UNEXPECTED: == object.Equals boxed");
                Console.WriteLine($"Area value = {t.S.Area}");
            }
        }
    }
}

The output is

UNEXPECTED: == operator
Area value = 0
UNEXPECTED: == long.Equals
Area value = 0
Expected: object.Equals boxed

I haven't tested other types and I'm not sure the example is exactly minimal, but it reproduces the bug I found when using something similar.

NOTE: if I disable the optimizations in the .csproj, the program outputs the expected results.

@jkotas
Copy link
Member

jkotas commented Jun 9, 2021

Thank you for reporting this!

This bug is fixed in dotnet/runtime repo already. I am going to submit a merge today to pick up the fix.

@jkotas jkotas added the area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation label Jun 9, 2021
@jkotas
Copy link
Member

jkotas commented Jun 9, 2021

Fixed by #1215. The nuget package with the fix should be available on the feed in a few hours.

@jkotas jkotas closed this as completed Jun 9, 2021
@dellamonica
Copy link
Author

Thank you very much @jkotas. Tomorrow I'll give it a try.
Just out of curiosity, and if it is not too much trouble, which commit fixes this?

@jkotas
Copy link
Member

jkotas commented Jun 9, 2021

It was fixed by dotnet/runtime#53806

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation
Projects
None yet
Development

No branches or pull requests

2 participants