Skip to content

Crossgen: (bug) allocation of an in-bubble type with an out-of-bubble base is treated as side-effect free (hoisted above throwing arg, finalized after base gains a finalizer) #137844

Closed

Description

@EgorBo

crossgen2's getNewHelper reports pHasSideEffects = false for an in-bubble type whose base class is outside the version bubble, even though that base can later gain a finalizer. The JIT then hoists the allocation above an earlier throwing argument, so once Base gets a finalizer, an object whose constructor never ran is finalized. Reproduces with the default PublishReadyToRun configuration.

Minimal Repro

App.dll is compiled with crossgen2 against Lib v1, then run with Lib v2.

// Lib.dll; v2 is built with V2 defined
public class Base
{
#if V2
    ~Base() => System.Console.WriteLine("Base finalized");
#endif
}
// App.dll
using System;
using System.Runtime.CompilerServices;

public class Derived : Base { }

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Consume(int x, object o) { }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Test(int a, int b) => Consume(a / b, new Derived());

    public static void Main()
    {
        try { Test(1, 0); } catch (DivideByZeroException) { Console.WriteLine("DivideByZeroException"); }
        GC.Collect();
        GC.WaitForPendingFinalizers();
        Console.WriteLine("Done");
    }
}

Expected

The JIT result (e.g. with DOTNET_ReadyToRun=0):

DivideByZeroException
Done

Actual

DivideByZeroException
Base finalized
Done

In the R2R code for Test, CORINFO_HELP_READYTORUN_NEW is called before the idiv.

Regression?

The bug is reported to also reproduce with .NET 10 PublishReadyToRun; this was not re-verified. It reproduces on main.

Notes

getNewHelper (CorInfoImpl.ReadyToRun.cs) treats an allocation as having side effects only when the allocated type itself is outside the version bubble. A finalizer can also be inherited, so the check should walk the base type chain up to System.Object. #56177 fixed only the case where the allocated type is outside the bubble.

Activity

  1. added this to the 12.0.0 milestone on Nov 18, 2026
  2. added
    area-crossgen2-coreclr
    on Nov 18, 2026
  3. added and removed
    area-crossgen2-coreclr
    on Nov 19, 2026
  4. dotnet-policy-service commented on Nov 19, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/crossgen-contrib
    See info in area-owners.md if you want to be subscribed.

  5. added a commit that references this issue on Nov 20, 2026
    223be71
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions