Skip to content

JIT: Tier1 with Dynamic PGO drops isinst in inlined guarded devirtualization (Roslyn MetadataWriter NRE, .NET 10.0.12 x64) #135350

Closed

Description

@rbuergi

Summary

On .NET 10.0.12 linux-x64, Tier1 code with PGO for Microsoft.Cci.MetadataWriter.PopulateNestedClassTableRows() (Roslyn 5.9.0, Microsoft.CodeAnalysis.dll) inlines NamedTypeSymbol.AsNestedTypeDefinitionImpl behind a guarded-devirtualization check. Inside that inline, the JIT drops the isinst NamedTypeSymbol in SourceMemberContainerTypeSymbol.ContainingType:

// SourceMemberContainerTypeSymbol (Roslyn 5.9.0), IL: ldarg.0; ldfld _containingSymbol; isinst NamedTypeSymbol; ret
public override NamedTypeSymbol? ContainingType => _containingSymbol as NamedTypeSymbol;   // _containingSymbol : NamespaceOrTypeSymbol

The guard ContainingType != null is compiled as _containingSymbol != null. A top-level type's _containingSymbol is a namespace, which is non-null. So every top-level source type is treated as nested, AsNestedTypeDefinition returns this, and the next ContainingTypeDefinition call throws NullReferenceException.

In the failing processes, once that Tier1 body has been compiled, every later Compilation.Emit in the process fails the same way. We see this intermittently in about 30-50% of CI runs of one test shard (in a private repository's CI), and could not reproduce it locally on osx-arm64 or on linux-x64 under qemu.

Environment

  • .NET 10.0.12, linux-x64, GitHub-hosted runner. The CPU was INTEL(R) XEON(R) PLATINUM 8573C (avx2, avx512f) in the captured occurrence; earlier occurrences ran on an AMD EPYC 7763 (avx2, no avx512).
  • Default tiering settings (TieredCompilation, TieredPGO, OSR all at defaults).
  • Microsoft.CodeAnalysis / .CSharp 5.9.0. These are the stock NuGet lib/net10.0 IL images, not ReadyToRun, and we checked them byte for byte.

Stack

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.CSharp.Symbols.NamedTypeSymbol.Microsoft.Cci.ITypeDefinitionMember.get_ContainingTypeDefinition()
   at Microsoft.Cci.MetadataWriter.PopulateNestedClassTableRows()
   at Microsoft.Cci.MetadataWriter.BuildMetadataAndIL(...)
   at Microsoft.Cci.PeWriter.WritePeToStream(...)

Other processes fail through the same guard at a second call site, MetadataWriter.<GetConsolidatedTypeParameters>g__getConsolidatedTypeParameters|104_0 → get_ContainingTypeDefinition.

Evidence (DOTNET_JitStdOutFile + DOTNET_JitDisasmSummary=1 + DOTNET_JitDisasm=PopulateNestedClassTableRows *getConsolidatedTypeParameters* ...)

Tier history of the shared Roslyn copy in the failing process, in compile order:

method compiles
NamedTypeSymbol:AsNestedTypeDefinitionImpl Tier0 → Instrumented Tier0 → Tier1 (PGO) — long before the failure, correct (see below)
MetadataWriter:PopulateNestedClassTableRows Instrumented Tier0 (×2) → Tier1 (PGO), compiled after the last successful emit; the next emit fails
getConsolidatedTypeParameters Instrumented Tier0 (×2) → Tier1 (PGO) compiled after the first failure — same defect (below)

The listing header for the faulting method:

; Assembly listing for method Microsoft.Cci.MetadataWriter:PopulateNestedClassTableRows():this (Tier1)
; Emitting BLENDED_CODE for generic X64 + VEX + EVEX on Unix
; optimized using Synthesized PGO
; with Synthesized PGO: fgCalledCount is 205
; 17 inlinees with PGO data; 37 single block inlinees; 0 inlinees without PGO data

(The header says "Synthesized PGO" for the root method although Instrumented Tier0 versions of it ran first; we quote it as printed. The inlinees carry PGO data.)

Below is the loop body after GDV. Method-table 0x7FB5D6EBAA60 is the guessed class; by field layout it is SourceNamedTypeSymbol (sealed), and its +0x38 is SourceMemberContainerTypeSymbol._containingSymbol : NamespaceOrTypeSymbol. r12 is the type definition, r13/rsi is the PEModuleBuilder, and [module+0xB0] is PEModuleBuilder.SourceModule. The annotations are ours.

G_M000_IG15:
       mov      rax, 0x7FB5D6EBAA60          ; GDV: typeDef is SourceNamedTypeSymbol?
       cmp      qword ptr [r12], rax
       jne      G_M000_IG34                  ;   no  -> interface call AsNestedTypeDefinition (correct path)
G_M000_IG16:
       mov      r13, rsi                     ; (PEModuleBuilder)context.Module
       test     r13, r13
       je       G_M000_IG09
       jmp      G_M000_IG08                  ;   exact-type cast check on the module
G_M000_IG09:
       mov      rax, gword ptr [r12+0x38]    ; _containingSymbol
       test     rax, rax                     ; <-- "ContainingType != null" with NO isinst NamedTypeSymbol
       jne      G_M000_IG22
G_M000_IG10:
       xor      r13, r13                     ; return null (not nested)
...
G_M000_IG22:                                 ; ContainingModule == moduleBeingBuilt.SourceModule
       mov      rcx, gword ptr [r12+0x38]    ; ContainingSymbol (a NAMESPACE for a top-level type)
       test     rcx, rcx
       je       G_M000_IG53
       mov      rax, 0x7FB5D6EBAA60
       cmp      qword ptr [rcx], rax
       jne      SHORT G_M000_IG28            ; namespace -> virtual ContainingModule
...
G_M000_IG25:
       mov      rcx, gword ptr [r13+0xB0]    ; PEModuleBuilder.SourceModule
       ...
       cmp      rsi, rcx
G_M000_IG27:
       mov      r13, r12                     ; return this  ==> treated as NESTED
       jmp      G_M000_IG11
G_M000_IG17: ...
G_M000_IG19:
       mov      rdi, r13
       call     [NamedTypeSymbol:Microsoft.Cci.ITypeDefinitionMember.get_ContainingTypeDefinition()]   ; ContainingType == null -> NRE

The loop clone (IG41–IG49) has the same shape. There is no CORINFO_HELP_ISINSTANCEOFCLASS anywhere in this method's Tier1 body.

Internal control, same process, same field, same guessed class. Tier1 getConsolidatedTypeParameters contains the same inlined guard. It also has a second read of [x+0x38] as NamedTypeSymbol, and that one is expanded correctly:

; inlined AsNestedTypeDefinitionImpl — WRONG (isinst dropped)
G_M000_IG04:  cmp qword ptr [rbx], 0x7FB5D6EBAA60 ; je IG37
G_M000_IG39:  mov rdi, gword ptr [rbx+0x38]
              test rdi, rdi
              jne  SHORT G_M000_IG42                  ; non-null namespace => "has containing type"
...
; another `_containingSymbol as NamedTypeSymbol` in the same method — CORRECT
G_M000_IG48:  mov rsi, gword ptr [r12+0x38]
              mov rax, rsi
              test rax, rax
              je   SHORT G_M000_IG51
G_M000_IG49:  cmp qword ptr [rax], 0x7FB5D6EBAA60     ; likely-class fast path
              je   SHORT G_M000_IG51
G_M000_IG50:  mov rdi, 0x7FB5D686F1D0                 ; NamedTypeSymbol
              call CORINFO_HELP_ISINSTANCEOFCLASS     ; fallback present

The standalone Tier1 AsNestedTypeDefinitionImpl (GDV class SynthesizedReadOnlyListEnumeratorTypeSymbol) is correct. For that class ContainingSymbol is a field typed with a sealed NamedTypeSymbol subclass, so leaving out the cast there is legitimate.

Why we believe it is the JIT

  • The image and its mapping are unchanged at the failure. We compare the metadata hash, the IL hash of ~11k methods and the file hash before and after.
  • A private copy of Roslyn, loaded into a collectible ALC, emits the same source in the same process. It is compiled FullOpts, because collectible code is not tiered, so it gets no PGO and no GDV.
  • The symbol reads are correct when called directly: a top-level ContainingType reads null.
  • A member-less top-level class, with no nesting anywhere, reproduces the NRE in one method. That matches the guard misreading for every top-level type.
  • With DOTNET_TieredPGO=0 the failure has not been seen so far, but that is 1 clean run against 1 failing run with default settings, against a base failure rate of roughly 30-50%. That is statistically weak and we do not lean on it.

Possibly related

#134193: "Failed isinst to an interface is treated as non-null". Its stated mechanism is that the isinst result inherits the input's isNonNull. If that mechanism applies to class casts too, isinst(x) != null folds to x != null, and the helper call becomes dead. That is exactly the shape above. We have not verified that this is the same defect.

What is NOT established

  • No standalone repro. Locally, the methods tier up with profiles that keep the cast.
  • That this Tier1 body was the code executing at the fault. It was compiled after the last successful emit and before the first failing one, with no Roslyn JIT activity in between, but code activation/patching itself is not logged.
  • The method-table → class mapping is inferred, not observed. The release JIT's listing does not print class names; we derived them from Roslyn 5.9.0 field offsets (_containingSymbol at +0x38 in SourceNamedTypeSymbol; SourceModule at +0xB0 in PEModuleBuilder, which matches the listing). Both are consistent with the listing, but neither was read inside the failing process.
  • Why this process produced this profile / inline shape and others do not.
  • No JitDump: this was a release runtime in CI.
  • Whether this is the same defect as JIT: (bug) Failed isinst to an interface is treated as non-null, folding a GetType() compare and swallowing a NullReferenceException #134193.

We can provide the full jit.txt (Tier0/Instrumented/Tier1 listings of the four methods, and the summary for the whole process) on request.

Activity

  1. added
    untriaged
    on Oct 7, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 7, 2026
  3. dotnet-policy-service commented on Oct 7, 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. EgorBo commented on Oct 7, 2026

    @EgorBo
    Member

    I suspect #135363 fixes it, but cannot confirm since there is no repro I could reproduce locally

  5. self-assigned this
    on Oct 7, 2026
  6. added a commit that references this issue on Oct 8, 2026
    176db4e
  7. removed
    untriaged
    on Oct 8, 2026
  8. locked and limited conversation to collaborators on Nov 8, 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

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions