Skip to content

ILVerify does not detect disallowed boxing of byref-like types #132954

Closed

Description

@masonwheeler

Description

I'm working on adding ref struct support to the Boo compiler, and as expected it's producing some invalid IL until I get the details worked out. But one of the early tests is failing, throwing System.InvalidProgramException: Common Language Runtime detected an invalid program at runtime, while passing ILVerify with no errors.

Reproduction Steps

Open the attached zip file. It contains the testcase, and its ILDASM disassembly for convenience. The disassembly has one non-trivial method, Main.

This is the original code. It should do exactly what it looks like it does:

import System

var span = "Hello, World!".AsSpan()
for letter in span:
	Console.Write(letter)
Console.WriteLine()

The generated assembly throws InvalidProgramException at runtime, but ILVerify gives it a clean bill of health, so it's not clear what I need to fix in the codegen.

Expected behavior

ILVerify should correctly diagnose invalid assemblies.

Actual behavior

ILVerify shows no errors.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 10, Windows 10, x64.

Other information

No response

Activity

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

    @jkotas
    Member

    box valuetype [System.Private.CoreLib]System.ReadOnlySpan1/Enumerator` is the problem. Boxing of byref-like types is not allowed.

  5. added
    area-Tools-ILVerification
    and removed
    area-CodeGen-coreclr
    on Aug 30, 2026
  6. dotnet-policy-service commented on Aug 30, 2026

    @dotnet-policy-service
    Contributor

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

  7. changed the title [-]Unexplained error loading experimental assembly.[/-] [+]ILVerify does not detect disallowed boxing of byref-like types[/+] on Aug 30, 2026
  8. masonwheeler commented on Aug 30, 2026

    @masonwheeler
    ContributorAuthor

    Thanks! That'll get me unblocked.

  9. masonwheeler commented on Aug 30, 2026

    @masonwheeler
    ContributorAuthor

    @jkotas ...or not. After iterating on this a little bit, I get a new version that now throws System.ExecutionEngineException. ILVerify still gives it a clean bill of health, and strangely, I don't see any substantial difference between this and the IL output of Roslyn here. Any idea what's going wrong?

    New version:

    .method private hidebysig static void  Main(string[] argv) cil managed
    {
      .entrypoint
      // Code size       75 (0x4b)
      .maxstack  1
      .locals init (valuetype [System.Private.CoreLib]System.ReadOnlySpan`1<char> V_0,
               char& V_1,
               valuetype [System.Private.CoreLib]System.ReadOnlySpan`1/Enumerator<char> V_2)
      IL_0000:  ldstr      "Hello, World!"
      IL_0005:  call       valuetype [System.Private.CoreLib]System.ReadOnlySpan`1<char> [System.Private.CoreLib]System.MemoryExtensions::AsSpan(string)
      IL_000a:  stloc.0
      IL_000b:  ldloca.s   V_0
      IL_000d:  call       instance valuetype [System.Private.CoreLib]System.ReadOnlySpan`1/Enumerator<!0> valuetype [System.Private.CoreLib]System.ReadOnlySpan`1<char>::GetEnumerator()
      IL_0012:  stloc.2
      .try
      {
        IL_0013:  br         IL_0027
        IL_0018:  ldloca.s   V_2
        IL_001a:  call       instance !0& modreq([System.Private.CoreLib]System.Runtime.InteropServices.InAttribute) valuetype [System.Private.CoreLib]System.ReadOnlySpan`1/Enumerator<char>::get_Current()
        IL_001f:  stloc.1
        IL_0020:  ldloc.1
        IL_0021:  ldind.u2
        IL_0022:  call       void [System.Console]System.Console::Write(char)
        IL_0027:  ldloca.s   V_2
        IL_0029:  call       instance bool valuetype [System.Private.CoreLib]System.ReadOnlySpan`1/Enumerator<char>::MoveNext()
        IL_002e:  brtrue     IL_0018
        IL_0033:  leave      IL_0045
      }  // end .try
      finally
      {
        IL_0038:  ldloc.2
        IL_0039:  constrained. valuetype [System.Private.CoreLib]System.ReadOnlySpan`1/Enumerator<char>
        IL_003f:  callvirt   instance void [System.Private.CoreLib]System.IDisposable::Dispose()
        IL_0044:  endfinally
      }  // end handler
      IL_0045:  call       void [System.Console]System.Console::WriteLine()
      IL_004a:  ret
    } // end of method Span_hello_world2Module::Main
    
    
  10. EgorBo commented on Aug 30, 2026

    @EgorBo
    Member

    your IL should be:

    -      IL_0038:  ldloc.2
    +      IL_0038:  ldloca.s   V_2
  11. masonwheeler commented on Aug 30, 2026

    @masonwheeler
    ContributorAuthor

    @EgorBo And now it works. Thanks!

    So now we have two ref-related problems that ILVerify misses, one of which the JIT also doesn't appear to recognize.

  12. added a commit that references this issue on Aug 30, 2026
  13. jkotas commented on Aug 30, 2026

    @jkotas
    Member

    JIT also doesn't appear to recognize.

    JIT is not expected to validate the IL.

  14. masonwheeler commented on Aug 30, 2026

    @masonwheeler
    ContributorAuthor

    @jkotas What I meant by that was, whatever mechanism detected that the other error was "an invalid program" before it begins to execute and causes an execution engine exception, which I assume is part of the JIT, did not catch this one.

  15. added a commit that references this issue on Aug 31, 2026
  16. removed
    untriaged
    on Oct 9, 2026
  17. added this to the Future milestone on Oct 9, 2026
  18. added
    help wanted
    on Oct 9, 2026
  19. added a commit that references this issue on Oct 16, 2026
    1730f81
  20. locked and limited conversation to collaborators on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tools-ILVerificationhelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions