Skip to content

JIT: (bug) optRemoveRedundantZeroInits removes a required zero store after a catch-handler write #134004

Closed

Description

@EgorBo

optRemoveRedundantZeroInits scans only along unique normal successors and can skip a catch handler that writes an address-exposed local. It then deletes an explicit zero store at the rejoin block, leaving the handler-written value observable.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public class Program
{
    static bool s_throw = true;

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    static int Zero() => 0;

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Thrower() { if (s_throw) throw new Exception("boom"); }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Consume(int v) { }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void SetRef(ref int x, int v) { x = v; }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int p)
    {
        int x = 0;
        try { Thrower(); }
        catch (Exception) { SetRef(ref x, p); Consume(x); }
        x = Zero();          // <-- this store is deleted by optRemoveRedundantZeroInits
        Consume(x);
        return x;
    }

    static int Main()
    {
        int r = Test(5);
        Console.WriteLine("Test(5) = " + r);
        return r == 0 ? 100 : 1;
    }
}

Expected

Test(5) = 0

Exit code 100.

Actual

Test(5) = 5

Exit code 1. JitDump shows the pass skipping the catch handler and deleting the rejoin store:

Analyzing BB01
Analyzing BB02
Analyzing BB04            <-- BB03, the catch handler, is never analyzed
removing useless STMT00003 ( 0x01A[E--] ... ??? ) ... STORE_LCL_VAR int (AX) V01 loc0 = 0

Disassembly confirms the rejoin block uses [rbp-0x08] without restoring it to zero after the handler writes p.

Notes

Compiler::optRemoveRedundantZeroInits() (optimizer.cpp) walks via GetUniqueSucc(), which ignores EH predecessors of the successor; BB04 has an unvisited catch predecessor that redefines the local.
The trigger is a neverTracked local such as address-exposed x with !fgVarNeedsExplicitZeroInit(...); an object-local variant returns non-null instead of null.
Also reproduces on released .NET 10.0.12 x64. Stop the scan at rejoins with unvisited/EH predecessors or apply the implicit-control-flow guard to explicit zero-store removal.

Activity

  1. added this to the 12.0.0 milestone on Sep 15, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 15, 2026
  3. dotnet-policy-service commented on Sep 15, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. changed the title [-]JIT (bug): optRemoveRedundantZeroInits removes a required zero store after a catch-handler write[/-] [+]JIT: (bug) optRemoveRedundantZeroInits removes a required zero store after a catch-handler write[/+] on Sep 16, 2026
  5. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 8.0.425
    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
  6. self-assigned this
    on Oct 13, 2026
  7. added a commit that references this issue on Oct 14, 2026
    1f50563
  8. locked and limited conversation to collaborators on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions