Repository navigation
Description
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 ifblock->CanRemoveJumpToNext()at that point. Here thejmptoL2is emitted (the layout-next block produces no code) and removed later byemitRemoveJumpToNextInst(emit.cpp).emitRemoveJumpToNextInstkeeps a 1-byte NOP only whenidjIsAfterCallBeforeEpilogis set and the target IG is an epilog; the EH-boundary case isn't considered. The GC-state NOP thatemitAddLabelinserts 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
dotnet-policy-service commentedon Oct 8, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
JIT: preserve post-call nops during late jump removal (dotnet#137261)