Repository navigation
Description
Description
With the CoreCLR interpreter enabled (DOTNET_InterpMode=1), a TypeInitializationException raised by a static field access does not run the handlers of a try region when that access is the region's first instruction. The JIT runs them.
- If only the inner
trystarts with the access, itsfinallyis skipped and the enclosingcatchstill catches. - If the enclosing
tryalso starts there, the exception skips both handlers and reaches the caller. - With a non-inlined call to an empty method placed before the static access (
Pad()in the repro), both handlers run.
Reproduction Steps
using System;
using System.Runtime.CompilerServices;
// Each type has an explicit static constructor (precise initialization) and a field initializer that throws.
static class A { static A() { } public static readonly int X = Init(); static int Init() => throw new InvalidOperationException(); }
static class B { static B() { } public static readonly int X = Init(); static int Init() => throw new InvalidOperationException(); }
static class C { static C() { } public static readonly int X = Init(); static int Init() => throw new InvalidOperationException(); }
static class Program
{
static void Main()
{
Console.WriteLine($"1. static access first in the inner try: {InnerTryOnly()}");
Console.WriteLine($"2. static access first in the inner and outer try: {InnerAndOuterTry()}");
Console.WriteLine($"3. control, one call before the static access: {Control()}");
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void Pad() { }
[MethodImpl(MethodImplOptions.NoInlining)]
static string InnerTryOnly()
{
string result = "";
try
{
Pad();
try { GC.KeepAlive(A.X); }
finally { result += "finally ran; "; }
}
catch (TypeInitializationException) { result += "caught"; }
return result;
}
static string InnerAndOuterTry()
{
try { return InnerAndOuterTryCore(); }
catch (TypeInitializationException) { return "escaped both handlers, caught by the caller"; }
}
[MethodImpl(MethodImplOptions.NoInlining)]
static string InnerAndOuterTryCore()
{
string result = "";
try
{
try { GC.KeepAlive(B.X); }
finally { result += "finally ran; "; }
}
catch (TypeInitializationException) { result += "caught"; }
return result;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static string Control()
{
string result = "";
try
{
Pad();
try { Pad(); GC.KeepAlive(C.X); }
finally { result += "finally ran; "; }
}
catch (TypeInitializationException) { result += "caught"; }
return result;
}
}Compile with -optimize+ and run it twice, once without and once with DOTNET_InterpMode=1.
Expected behavior
The JIT's output, which is what the interpreter should print as well:
1. static access first in the inner try: finally ran; caught
2. static access first in the inner and outer try: finally ran; caught
3. control, one call before the static access: finally ran; caught
Actual behavior
With DOTNET_InterpMode=1:
1. static access first in the inner try: caught
2. static access first in the inner and outer try: escaped both handlers, caught by the caller
3. control, one call before the static access: finally ran; caught
Regression?
Unknown. It reproduces identically at 5c9146f5b75 (main, 2026-10-07) and on a branch based on it. The .NET 11 RC1 shared framework does not include libclrinterpreter.so, so I could not test a released build.
Known Workarounds
Make sure the static field access is not the first instruction of the try block. In the tested builds, a non-inlined call to an empty method in front of it (Pad() above) was enough.
Configuration
mainat5c9146f5b75d2cad47ce380b43cc75951fe4240e,./build.sh clr -rc checked, linux-x64, run withcorerun.- The runtime layer (
libcoreclr.so,libclrjit.so,libclrinterpreter.so,System.Private.CoreLib.dll) is from that build. The other libraries are from a Releaselibsbuild of a branch based on that commit. - The app is built with Roslyn from the repo's SDK,
-optimize+. - Also reproduces, with the same output, on windows-x64 (
build.cmd clr+libs -rc checked -lc releaseof a branch based on the same commit), run with the testhost'sdotnet.exe. - Not tried on arm64.
Other information
I narrowed the trigger with the same harness. Each case is try { <expression> } finally { ... } inside an outer try/catch that does not start at the same instruction:
expression in the try |
throwing IL instruction is the first in the region? | finally runs under the interpreter? |
|---|---|---|
GC.KeepAlive(A.X), static field whose type initializer throws (precise init, as above) |
yes (ldsfld) |
no |
the same, beforefieldinit type |
yes (ldsfld) |
no |
Pad(); GC.KeepAlive(A.X) |
no | yes |
GC.KeepAlive(new T()), T's static constructor throws |
yes (newobj) |
yes |
Thrower(), a call to a method that throws |
yes (call) |
yes |
GC.KeepAlive(s_null.Field), field load through a null reference |
no (an ldsfld of the receiver comes first) |
yes |
GC.KeepAlive((string)s_boxedInt), failing cast |
no (an ldsfld of the operand comes first) |
yes |
The last two rows do not test the region boundary, so they say nothing either way. Among the boundary cases tried, the failure appears only for a static field load at try entry. (Hypothesis, not traced:) the IP used to find handlers for an exception raised while initializing the class for a static field load seems to fall just before that instruction, outside a region that starts at it.
Note
This issue was drafted with AI assistance (Anthropic Claude, reviewed by OpenAI Codex). The repro program above was compiled and run exactly as shown, on the build described, and the outputs are copied from that run.
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.
pcshrosbree commentedon Oct 9, 2026
The native-helper paths of
INTOP_CALL_HELPER_*(andINTOP_UNBOX_ANY[_GENERIC]) ininterpexec.cppleavepFrame->ipat the start of the instruction while the helper runs. The helpers that can throw here are managed code, so an exception from one is dispatched as if from a call:SfiInitWorker/SfiNextWorkertreat the frame's ip as a return address and subtractSTACKWALK_CONTROLPC_ADJUST_OFFSET. When the helper call is the first instruction of a try region, as the static-base helper is for the field accesses in the repro, the adjusted ip falls before the region.INTOP_CALL, and the same opcodes when they interpret the helper (goto CALL_INTERP_METHOD), already record the next instruction's ip.Recording the next instruction's ip just before the native helper call fixes the repro above on linux-x64 Checked at 5c9146f.
interpexec.cppis unchanged on main. I'll open a PR with a regression test.Note
This comment was drafted with AI assistance (Anthropic Claude). The build and runs it cites were done at commit 5c9146f.
dotnet-policy-service commentedon Oct 9, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @BrzVlad, @janvorli
See info in area-owners.md if you want to be subscribed.
[Interpreter] Record the next instruction's ip before calling a manag…