Skip to content

[wasm] R2R: NullReferenceException casting a null interface-call result to a reference-type generic parameter #131635

Closed

Description

@pavelsavara

Description

On browser-wasm (CoreCLR) with PublishReadyToRun, a shared-generic method that casts the null result of an interface call to its reference-type generic parameter throws NullReferenceException instead of returning null.

This breaks Blazor WebAssembly startup on CoreCLR, because ServiceProviderServiceExtensions.GetService<T>() is (T?)provider.GetService(typeof(T)) and throws when the requested service is not registered.

Repro

using System;

interface IProv { object? Get(Type t); }
sealed class EmptyProv : IProv { public object? Get(Type t) => null; }

class Program
{
    static T? GetFrom<T>(IProv p) => (T?)p.Get(typeof(T));

    static void Main()
    {
        var p = new EmptyProv();
        Console.WriteLine(p.Get(typeof(IComparable)) is null); // True (OK)
        Console.WriteLine(GetFrom<IComparable>(p));            // NullReferenceException under wasm R2R
    }
}

GetFrom<T> must be ReadyToRun-compiled and instantiated over a reference type (shared __Canon). Interpreted execution returns null as expected.

Expected

GetFrom<IComparable>(p) returns null (casting a null reference to a reference type is null).

Actual

System.NullReferenceException: Arg_NullReferenceException.

Notes

  • Specific to the null result of an interface/virtual call. (T) of a null returned by a static call, or of a null parameter, does not reproduce.
  • The non-generic provider.GetService(Type) path is fine.

Configuration

  • -os browser -a wasm, CoreCLR, PublishReadyToRun (crossgen2, --obj-format:wasm)
  • Reproduces in Node and in the browser.

Note

This issue was written with the assistance of GitHub Copilot.

Activity

  1. added
    arch-wasm
    on Jul 31, 2026
  2. dotnet-policy-service commented on Jul 31, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
    See info in area-owners.md if you want to be subscribed.

  3. added
    untriaged
    on Jul 31, 2026
  4. added
    area-CodeGen-coreclr
    and removed
    untriaged
    on Jul 31, 2026
  5. added this to the 11.0.0 milestone on Jul 31, 2026
  6. dotnet-policy-service commented on Jul 31, 2026

    @dotnet-policy-service
    Contributor

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

  7. pavelsavara commented on Jul 31, 2026

    @pavelsavara
    MemberAuthor

    This is the underlying story, it prevents Di in Blazor from starting the app.

    NullReferenceException: Arg_NullReferenceException
       at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider )
       at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetComponentActivatorOrDefault(IServiceProvider)
       at Microsoft.AspNetCore.Components.RenderTree.Renderer..ctor(IServiceProvider, ILoggerFactory)
       at Microsoft.AspNetCore.Components.RenderTree.WebRenderer..ctor(IServiceProvider, ILoggerFactory, JsonSerializerOptions, JSComponentInterop)
       at Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer..ctor(IServiceProvider, ResourceAssetCollection, ILoggerFactory, JSComponentInterop, Boolean )
       at Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHost.RunAsyncCore(CancellationToken, WebAssemblyCultureProvider )
       at _7_blazor_coreclr.Program.Main(String[] args)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions)
    
  8. pavelsavara commented on Jul 31, 2026

    @pavelsavara
    MemberAuthor
  9. pavelsavara commented on Jul 31, 2026

    @pavelsavara
    MemberAuthor

    probably yet another exhibition of the same issue

    blazor.webassembly.js:1 Uncaught (in promise) Error: System.NullReferenceException: Arg_NullReferenceException
       at System.Text.Json.Utf8JsonReader.ReadSingleSegment()
       at System.Text.Json.Utf8JsonReader.Read()
       at System.Text.Json.JsonDocument.Parse(ReadOnlySpan`1, JsonReaderOptions, MetadataDb&, StackRowStack&)
       at System.Text.Json.JsonDocument.ParseUnrented(ReadOnlyMemory`1, JsonReaderOptions, JsonTokenType , Boolean )
       at System.Text.Json.JsonDocument.TryParseValue(Utf8JsonReader&, JsonDocument& , Boolean, Boolean, Boolean )
       at System.Text.Json.Serialization.Converters.JsonElementConverter.Read(Utf8JsonReader&, Type, JsonSerializerOptions)
       at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader&, Type, JsonSerializerOptions, ReadStack& , T& , Boolean& )
       at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader&, T& , JsonSerializerOptions, ReadStack&)
       at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Utf8JsonReader&, ReadStack&)
       at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.DeserializeAsObject(Utf8JsonReader&, ReadStack&)
       at System.Text.Json.JsonSerializer.ReadAsObject(Utf8JsonReader&, JsonTypeInfo)
       at System.Text.Json.JsonSerializer.Deserialize(Utf8JsonReader&, Type , JsonSerializerOptions )
       at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.ParseArguments(JSRuntime, String, String, Type[])
       at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.InvokeSynchronously(JSRuntime, DotNetInvocationInfo& , IDotNetObjectReference, String)
       at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.BeginInvokeDotNet(JSRuntime, DotNetInvocationInfo, String )
        at E.endInvokeDotNetFromJS (http://localhost:52646/_framework/blazor.webassembly.js:1:4521)
        at Object.On [as endInvokeDotNetFromJS] (http://localhost:52646/_framework/blazor.webassembly.js:1:80976)
        at http://localhost:52646/_framework/dotnet.runtime.0z4f63phum.js:4:28947
        at Object.Gt [as invokeJSImportST] (http://localhost:52646/_framework/dotnet.runtime.0z4f63phum.js:4:28700)
        at _SystemInteropJS_InvokeJSImportST (http://localhost:52646/_framework/dotnet.native.2t5mn09wlw.js:1:81384)
        at http://localhost:59905/_framework/System.Runtime.InteropServices.JavaScript.wnjkmu9es4.wasm:wasm-function[31]:0x955a
        at http://localhost:59905/_framework/Microsoft.JSInterop.WebAssembly.ybbllj6kmb.wasm:wasm-function[13]:0x369d
        at http://localhost:59905/_framework/dotnet.native.rdxgseajyb.wasm:wasm-function[7486]:0x2b6185
        at http://localhost:59905/_framework/dotnet.native.rdxgseajyb.wasm:wasm-function[1447]:0x7c09c
        at http://localhost:59905/_framework/dotnet.native.rdxgseajyb.wasm:wasm-function[489]:0x22410
    
  10. AndyAyersMS commented on Jul 31, 2026

    @AndyAyersMS
    Member

    I have a fix, should have a PR up soon.

    The extra "SP" arg prepending to managed calls is tripping up some logic in the JIT.

  11. added a commit that references this issue on Jul 31, 2026
    f2b2cee
  12. locked and limited conversation to collaborators on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

arch-wasmarea-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions