Skip to content

JIT: (bug) UTF16 string comparison expansion reorders receiver null check ahead of earlier side effects #133966

Closed

Description

@EgorBo

The importer expansion for short UTF16 string comparisons can append the unrolled comparison before earlier side-effecting expressions still on the evaluation stack, causing the receiver null check to throw first.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static int Test(string s, int[] a, int i)
    {
        return a[i] + (s.Equals("ab") ? 1 : 0);
    }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static int Test2(string s, int x, int y)
    {
        return (x / y) + (s.StartsWith("ab", StringComparison.Ordinal) ? 1 : 0);
    }

    private static void Run(string name, Func<int> f)
    {
        try { Console.WriteLine(name + ": " + f()); }
        catch (Exception e) { Console.WriteLine(name + ": " + e.GetType().Name); }
    }

    public static void Main()
    {
        int[] a = new int[1];
        Run("Test", () => Test(null, a, 5));
        Run("Test2", () => Test2(null, 1, 0));
    }
}

Expected

Test: IndexOutOfRangeException
Test2: DivideByZeroException

Actual

Test: NullReferenceException
Test2: NullReferenceException

Notes

impUtf16StringComparison stores the string temp and unrolled comparison with CHECK_SPILL_NONE, so prior stack entries are not spilled before the receiver dereference is appended.
The same pattern exists in impUtf16SpanComparison; a long literal that skips the unrolled expansion preserves the expected exception order.

Activity

  1. added this to the 12.0.0 milestone on Sep 15, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 15, 2026
  3. dotnet-policy-service commented on Sep 15, 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 Sep 17, 2026
  5. added a commit that references this issue on Sep 17, 2026
    1b5f911
  6. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 8.0.425
    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
  7. added a commit that references this issue on Sep 18, 2026
    9e48059
  8. locked and limited conversation to collaborators on Oct 19, 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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions