Skip to content

Possible miscompile in loop inside try/finally #48394

Closed

Description

@james-hunt-ra

Description

The function F, below, returns 1 when run in Release mode on .NET 5 on x64 Win 10.

It is expected to return 0 - which it does indeed return in both Debug configuration, and also under .NET Core 3.1.

using System;

namespace Miscompile
{
    class Program
    {       
        static void Main()
        {
            int result = F();
            if (result == 0)
            {
                Console.WriteLine("OK");
            }
            else
            {
                Console.WriteLine($"FAILED! result = {result}");
            }
        }

        // When compiled in Release mode, this function returns 1 under .NET 5
        // Expect it to return 0, which is the case under .NET Core 3.1
        private static int F()
        {
            bool x = false;            
            int c = 0;

            try
            {
                while (true)
                {
                    // It appears this assignment is erroneously removed or hoisted out of loop?
                    c = 0;

                    if (!x)
                    {
                        c++;
                        x = true;
                    }
                    else
                    {
                        return c;
                    }

                    // Uncommenting the below fixes issue, even if the assignment
                    // to c at loop start is commented out
                    // c = 0;
                }
            }
            finally
            {
                // finally block appears necessary to 
                // reproduce the error
                // Commenting out the finally block, the if expression,
                // or even just the WriteLine statement fixes the problem
                if (c != 0)
                {
                    Console.WriteLine("c not zero");
                }
            }
        }        
    }
}

Configuration

.NET Version: .NET 5
OS: Windows 10
Arch: x64

Does not occur on .NET Core 3.1 or below.
Does not occur in Debug builds.
Not tested on other architectures or operating systems.

Regression?

Yes - worked in .NET 3.1 and before.

Other information

Workarounds - see the comments in code above. There are two minor alterations that stop it miscompiling.

Activity

  1. ghost added
    untriaged
    on Feb 17, 2021
  2. added
    area-CodeGen-coreclr
    on Feb 17, 2021
  3. EgorBo commented on Feb 17, 2021

    @EgorBo
    Member

    A bit simplified repro:

        private static void Foo2()
        {
            bool x = false;
            int c = 0;
    
            try
            {
                while (true)
                {
                    c = 0;
    
                    if (!x)
                    {
                        c = c + 100;
                        x = true;
                    }
                    else
                    {
                        return;
                    }
                }
            }
            finally
            {
                Console.WriteLine(c);
            }
        }

    Here is the asm diff https://www.diffchecker.com/yYXyTBgm
    Left: SSA-phase is disabled, Right: SSA is enabled (by default)

    Left one produces the expected output.
    (I also disabled finally-cloning for both to make asm less verbose)

    As @james-hunt-ra pointed out, c=0 is removed from loop (but it's not PHASE_OPTIMIZE_BRANCHES or PHASE_HOIST_LOOP_CODE related)

    /cc @dotnet/jit-contrib

  4. AndyAyersMS commented on Feb 17, 2021

    @AndyAyersMS
    Member

    Seems like it could be the new in 5.0 optimization that removes redundant zero inits (#36918, and subsequent PRs like #37786, #38314).

  5. AndyAyersMS commented on Feb 17, 2021

    @AndyAyersMS
    Member

    The redundant zero init opt is checking lvLiveInOutOfHndlr but only after possibly optimizing away a store.

    Wonder if the last bool in this clause was meant to be &&? cc @erozenfeld

    if (BitVecOps::IsMember(&bitVecTraits, zeroInitLocals, lclNum) ||
    (lclDsc->lvIsStructField &&
    BitVecOps::IsMember(&bitVecTraits, zeroInitLocals, lclDsc->lvParentLcl)) ||
    (!lclDsc->lvTracked && !fgVarNeedsExplicitZeroInit(lclNum, bbInALoop, bbIsReturn)))

  6. erozenfeld commented on Feb 17, 2021

    @erozenfeld
    Contributor

    We should never remove zero inits that are in a loop so these lines should be:

    if (!bbInALoop)
    {
        if (BitVecOps::IsMember(&bitVecTraits, zeroInitLocals, lclNum) || 
            (lclDsc->lvIsStructField && 
             BitVecOps::IsMember(&bitVecTraits, zeroInitLocals, lclDsc->lvParentLcl)) || 
            (!lclDsc->lvTracked && !fgVarNeedsExplicitZeroInit(lclNum, bbInALoop, bbIsReturn))) 
    
  7. AndyAyersMS commented on Feb 18, 2021

    @AndyAyersMS
    Member

    Thanks Eugene... let me see what this does.

  8. self-assigned this
    on Feb 18, 2021
  9. removed
    untriaged
    on Feb 18, 2021
  10. added this to the 6.0.0 milestone on Feb 18, 2021
  11. AndyAyersMS commented on Feb 18, 2021

    @AndyAyersMS
    Member

    Seeing a fair number of diffs.

    Will take a while to sort which of these are fixes and which of these are us now being too cautious, and to assess how much of the original opt is now undone.

  12. AndyAyersMS commented on Feb 18, 2021

    @AndyAyersMS
    Member

    @EgorBo is going to follow up here.

  13. erozenfeld commented on Feb 18, 2021

    @erozenfeld
    Contributor

    Slightly less conservative check is:

    if (!bbInALoop || bbIsReturn)
    {
        if (BitVecOps::IsMember(&bitVecTraits, zeroInitLocals, lclNum) || 
            (lclDsc->lvIsStructField && 
             BitVecOps::IsMember(&bitVecTraits, zeroInitLocals, lclDsc->lvParentLcl)) || 
            (!lclDsc->lvTracked && !fgVarNeedsExplicitZeroInit(lclNum, bbInALoop, bbIsReturn))) 
    

    similar to what we do in fgVarNeedsExplicitZeroInit

  14. erozenfeld commented on Feb 18, 2021

    @erozenfeld
    Contributor

    @EgorBo ping me if you have questions about the algorithm

  15. ghost added
    in-pr
    on Feb 23, 2021
  16. ghost removed
    in-pr
    on Feb 26, 2021
  17. reopened this on Feb 26, 2021
  18. EgorBo commented on Mar 15, 2021

    @EgorBo
    Member

    Closed by #48810 (5.0) and #48571 (main)

  19. ghost locked as resolved and limited conversation to collaborators on Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrbug

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions