Skip to content

Evaluation doesn't work because of being stopped at an unsafe gc point, but the method is not optimized #115719

Closed

Description

@eterekhin

Description

Image

Reproduction Steps

Please use this code snippet for reproduction

using System.Buffers;

public class Program
{
    public static void Main(string[] args)
    {
        var tokenPath = ReadOnlySequence<byte>.Empty;
        var propertyFullPath2 = Method(tokenPath);
    }

    static ReadOnlySequence<byte> Method(ReadOnlySequence<byte> tokenPath)
    {
        return ReadOnlySequence<byte>.Empty;
    }
}

Expected behavior

Evaluation is allowed at that point

Actual behavior

Evaluation is not allowed

Regression?

No response

Known Workarounds

No response

Configuration

.NET 9 and a fresh master, not tested on earlier versions
Mac Apple M2 Max

Other information

I collected COMPlus logs, please take a look

COMPLUS.LOG

Activity

  1. added
    untriaged
    on May 19, 2025
  2. added
    needs-area-label
    on May 19, 2025
  3. added and removed
    needs-area-label
    on May 19, 2025
  4. tommcdon commented on May 19, 2025

    @tommcdon
    Member

    Hi @eterekhin! Thanks for reporting this issue! I am also able to reproduce this issue on Windows x64 and on .NET 8.

  5. added this to the 10.0.0 milestone on May 19, 2025
  6. removed
    untriaged
    on May 19, 2025
  7. changed the title [-]Evaluation on MacOS doesn't work because of being stopped at an unsafe gc point, but the method is not optimized[/-] [+]Evaluation doesn't work because of being stopped at an unsafe gc point, but the method is not optimized[/+] on May 20, 2025
  8. eterekhin commented on May 24, 2025

    @eterekhin
    ContributorAuthor

    Hello! I would like to share some results of my investigation

    This bug seems to be caused by unrolling structs with GC references(code) at the beginning of a sequence point. For such code an uninterruptible GC range is created and evaluation is prohibited according to this check

    if (!fInException && !g_pDebugger->IsThreadAtSafePlace(pThread))

    Correspoing piece of code of Main method (from my repro code snippet) , on Windows x64

    G_M24006_IG06:        ; offs=0x000069, size=0x0012, bbWeight=1, PerfScore 6.00, BB01 [0000], nogc, extend
    
    IN000d: 000069 vmovdqu  xmm0, xmmword ptr [V01 rbp+0x68] // SEQ POINT STARTS HERE
    IN000e: 00006E vmovdqu  xmmword ptr [V05 rbp+0x20], xmm0
    IN000f: 000073 mov      rax, qword ptr [V01+0x10 rbp+0x78]
    IN0010: 000077 mov      qword ptr [V05+0x10 rbp+0x30], rax
    
    G_M24006_IG07:        ; offs=0x00007B, size=0x000D, bbWeight=1, PerfScore 2.00, BB01 [0000], extend
    
    IN0011: 00007B lea      rdx, [V05 rbp+0x20]
    IN0012: 00007F lea      rcx, [V04 rbp+0x38]
    IN0013: 000083 call     Program:Method(System.Buffers.ReadOnlySequence`1[ubyte]):System.Buffers.ReadOnlySequence`1[ubyte]

    In the latest versions we encounter this bug less frequently (as I understood it thanks to #95565), but on .NET 7 it is reproduced with the following code

    public class Program
    {
        public static void Main(string[] args)
        {
            var s = new SomeStruct();
            var propertyFullPath2 = Method(s);
        }
    
        static SomeStruct Method(SomeStruct s) => s;
        public struct SomeStruct
        {
            public int Value;
            public object Reference;
        }
    }

    For a sake of experiment, I tried inserting NO_OP node before such struct stores, that emits nop instruction in the beginning of the seq point, and it fixed the issue, but I am not sure if that's the proper to do so

    Could you please share your thoughts on that? How can we approach this issue?
    Thanks you!

  9. modified the milestones: 10.0.0, 11.0.0 on Jul 8, 2025
  10. steveisok commented on Jul 14, 2026

    @steveisok
    Member

    Confirming @eterekhin's diagnosis, and re-scoping this as a JIT issue.

    Cause. The debugger's safe-place gate (EECodeManager::IsGcSafe) only accepts interruptible or safe-point offsets. An offset strictly inside a NoGC region is neither, so func-eval is rejected. These regions come from JIT-unrolled block copies of GC-ref structs (lowerxarch.cpp) — your ReadOnlySequence<byte> by-value copy. #103540 only exempted the region's first instruction; interior offsets are still the hole.

    Current state With your repro plus three extra layouts ({int,object}, {object,object}, {object×3}), func-eval at a breakpoint on each call site succeeds on .NET 10.0.8 and 11.0.0-preview.5 (also still passes on 8.0.28 / 9.0.17). Reason it's now hard to hit: source breakpoints bind to the statement's sequence point, which sits on the region's first instruction (the offset #103540 exempts), and Break All redirects threads to safe points — so neither path lands strictly inside a region.

    Fix direction (JIT). A debugger-side change would be wrong — GC genuinely can't happen inside the region. The fix would be to generalize #103540: keep a statement's sequence point from landing inside a NoGC region (or make it a real safe point) for all cases, not just the first instruction. Your NO_OP-before-store prototype does exactly this; the open question is avoiding the per-store codegen cost.

    Note

    This comment was generated with the assistance of AI (GitHub Copilot).

  11. added
    area-CodeGen-coreclr
    and removed on Jul 14, 2026
  12. removed this from the 11.0.0 milestone on Jul 14, 2026
  13. dotnet-policy-service commented on Jul 14, 2026

    @dotnet-policy-service
    Contributor

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

  14. added this to the 11.0.0 milestone on Jul 15, 2026
  15. JulieLeeMSFT commented on Jul 15, 2026

    @JulieLeeMSFT
    Member
  16. locked and limited conversation to collaborators on Aug 24, 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