Skip to content

JIT: don't skip covariant store check for shared sealed element types - #134940

Merged
EgorBo merged 3 commits into
mainfrom
copilot/fix-covariant-array-store-check
Oct 1, 2026
Merged

EgorBo merged 3 commits into
mainfrom
copilot/fix-covariant-array-store-check

Conversation

Copilot AI commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

In R2R/NativeAOT shared generic code, the JIT removed the covariant array store check when storing a G<U> into a G<T>[] (with G sealed). This let a G<object> be stored into a G<string>[]:

public sealed class G<T> { }

[MethodImpl(MethodImplOptions.NoInlining)]
static void Test<T, U>(G<T>[] arr, G<U> val)
{
    object[] o = arr;
    o[0] = val; // Test<string, object>: should throw ArrayTypeMismatchException
}

Root cause

gtCanSkipCovariantStoreCheck skips the check when isExactType(elem) && valueHandle == elem. In shared code both handles are G<__Canon>, but they can stand for different runtime types. crossgen2 and ILC report the precise G<__Canon>[] for the parameter, so the shortcut fires. Plain JIT isn't affected because the VM reports the parameter as __Canon[], which isn't sealed.

Change

  • gentree.cpp: the sealed/handle-equality shortcut now also requires !eeIsSharedInst(arrayElementHandle). This matches the existing shared-type filter in VN's exact-type IND(obj) folding.
  • Nested arrays: also covered (e.g. G<__Canon>[] stored into G<__Canon>[][]), because the shared-type flag also applies to arrays of shared types.
  • Other skip paths: unchanged. The exact object[] path and the compareTypesForCast == Must path already return conservative answers for shared types.

Impact

  • Unshared sealed element types (e.g. G<int>[]) still skip the check.
  • Shared code storing G<T> into G<T>[] with the same T now calls CORINFO_HELP_ARRADDR_ST. Type handles alone can't tell type parameters apart, so there's no cheaper way to keep this correct.

@azure-pipelines

azure-pipelines Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Sep 30, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Copilot AI changed the title [WIP] Fix covariant array store check for generic class JIT: don't skip covariant store check for shared sealed element types Sep 30, 2026
Copilot AI requested a review from EgorBo September 30, 2026 14:46
@EgorBo

EgorBo commented Sep 30, 2026

Copy link
Copy Markdown
Member

@copilot add a jit regression test (no csproj)

Copilot AI commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

@copilot add a jit regression test (no csproj)

Added in 343cebb: src/tests/JIT/Regression_ro_2/Runtime_134889.cs, which gets picked up by the existing Regression_ro_2.csproj wildcard, so there's no new csproj. It covers a parameter array, a nested array, and a local new G<T>[1], plus a same-T store that must still succeed. Run through crossgen2, the test fails with the fix reverted and passes with it. Plain JIT passes either way, so the test only catches the bug in the R2R/NativeAOT lanes.

@EgorBo
EgorBo marked this pull request as ready for review September 30, 2026 16:02
@EgorBo

EgorBo commented Oct 1, 2026

Copy link
Copy Markdown
Member

PTAL @jakobbotsch @dotnet/jit-contrib

@EgorBo
EgorBo requested a review from jakobbotsch October 1, 2026 13:14
@jakobbotsch

Copy link
Copy Markdown
Member

crossgen2 and ILC report the precise G<__Canon>[] for the parameter, so the shortcut fires. Plain JIT isn't affected because the VM reports the parameter as __Canon[], which isn't sealed.

I wonder if this difference is expected or not. Maybe CoreCLR should also report G<__Canon>[]?

@EgorBo

EgorBo commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

crossgen2 and ILC report the precise G<__Canon>[] for the parameter, so the shortcut fires. Plain JIT isn't affected because the VM reports the parameter as __Canon[], which isn't sealed.

I wonder if this difference is expected or not. Maybe CoreCLR should also report G<__Canon>[]?

It seems like it intentionally does it, but I am not sure why (or rather why ilc/cg2 don't do it)

    // To avoid loading useless shared instantiations, normalize shared instantiations to the canonical form
    // (e.g. List<_Canon>[] -> _Canon[])
    // The denormalized shared instantiations should be needed only during JITing, so it is fine to skip this
    // for DACCESS_COMPILE.
    if (elemType.IsCanonicalSubtype())
    {
        elemType = ClassLoader::CanonicalizeGenericArg(elemType);
    }

@EgorBo
EgorBo merged commit 55ed719 into main Oct 1, 2026
141 checks passed
@EgorBo
EgorBo deleted the copilot/fix-covariant-array-store-check branch October 1, 2026 18:33
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 2, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: (bug) R2R/NativeAOT skip covariant array store check for different instantiations of a sealed generic class

3 participants