Skip to content

JIT: (bug) late jump removal drops the NOP required after a call at the end of a try region (x64) #135384

Closed

Description

@EgorBo

On x64 a call must not directly precede the end of a try region (clr-abi.md, "AMD64 padding info"). When the jmp after such a call is removed late by emitRemoveJumpToNextInst, the NOP is lost and the call's return address falls into the next try region.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public class Program
{
    static int s_dummy;

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Foo() => throw new Exception("from Foo");

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Test()
    {
        try
        {
            Foo();
            goto L2;
        }
        catch (Exception)
        {
            Console.WriteLine("caught1");
        }
        _ = s_dummy;
    L2:
        try
        {
            Console.WriteLine("done");
        }
        catch (Exception)
        {
            Console.WriteLine("caught2");
        }
    }

    public static void Main() => Test();
}

Test is jitted at Tier0 (default config).

Expected

A nop (or some other instruction) after call [Program:Foo()], so the return address stays inside try EH#0.

Actual

G_M24707_IG02:  ;; offset=0x0014
       call     [Program:Foo()]
G_M24707_IG03:  ;; offset=0x001A    ; return address == end of try EH#0 == start of try EH#1
       mov      rcx, 0x1E38A62BE60      ; 'done'
       call     [System.Console:WriteLine(System.String)]
EH#0: try [0014..001A) handled by [0031..0055) (class: 100000E)
EH#1: try [001A..002B) handled by [0055..0079) (class: 100000E)

The output on main is still correct (caught1, done) because the current managed EH adjusts caller return addresses. On .NET 9 with DOTNET_LegacyExceptionHandling=1 the wrong handler runs: it prints caught2 and skips done.

Regression?

Yes, regressed in .NET 9: .NET 8.0 keeps the jmp after the call. .NET 9.0, 10.0, 11.0-rc2 and main all end the try region with the call.

Notes

  • genEmitEndBlock (codegenlinear.cpp) emits the NOP only if block->CanRemoveJumpToNext() at that point. Here the jmp to L2 is emitted (the layout-next block produces no code) and removed later by emitRemoveJumpToNextInst (emit.cpp).
  • emitRemoveJumpToNextInst keeps a 1-byte NOP only when idjIsAfterCallBeforeEpilog is set and the target IG is an epilog; the EH-boundary case isn't considered. The GC-state NOP that emitAddLabel inserts after a call can be bypassed the same way.
  • Fix: keep a NOP when the removed jump follows a call and the next IG is in a different EH region.

Activity

  1. added this to the 12.0.0 milestone on Oct 8, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 8, 2026
  3. dotnet-policy-service commented on Oct 8, 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. self-assigned this
    on Nov 6, 2026
  5. added a commit that references this issue on Nov 11, 2026
    f598b38
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

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions