runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Crash when deserializing certain types in Blazor Web Assembly projects when project is published

Open jdebache opened this issue 3 years ago • 12 comments

Description

I have a type which implements some numerical operation interfaces. I'm obtaining a serialized version of this type in my Blazor Web Assembly front end, which I'm deserializing. This deserialization fails because of some missing method:

Error: System.TypeLoadException: Parent class vtable failed to initialize, due to: Could not load list of method overrides due to Method not found: !2 System.Numerics.IAdditionOperators`3.op_Addition(!0,!1) assembly:Repro.Wasm.dll type:AbstractType member:(null)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[<<Main>$>d__0](:5432/<<Main>$>d__0& )
   at Program.<Main>$(:5432/String[] args)
    at qi (dotnet.7.0.2.2sr8yltp5e.js:5:80156)
    at Ji (dotnet.7.0.2.2sr8yltp5e.js:5:80040)
    at _Repro_Wasm__entrypoint (_Repro_Wasm__entrypoint:27:5)
    at dotnet.7.0.2.2sr8yltp5e.js:5:69011
    at Object.ji [as call_assembly_entry_point] (dotnet.7.0.2.2sr8yltp5e.js:5:69166)
    at Object.callEntryPoint (blazor.webassembly.js:1:46050)
    at Vt (blazor.webassembly.js:1:62248)

Reproduction Steps

  • Clone https://github.com/hypdeb/repros
  • Navigate to deserialize-with-abstract-static-methods (ignore the name, it was based on my original assumptions for the cause of the issue)
  • Run docker build . -t repro-wasm
  • Run docker run -p 5432:80 repro-wasm
  • Open your browser to localhost:5432
  • Open dev tools and notice the exception above in the console

Expected behavior

The exception obtained through the repro above does not occur.

Actual behavior

The exception does occur.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

I've looked into the _framework folder of the published version of the app, and the System.Numerics dll is missing. It is present in the output of a simple build, which I think explains why it works if you simply run the app using dotnet run.

jdebache avatar Feb 07 '23 06:02 jdebache

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost avatar Feb 07 '23 06:02 ghost

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

Issue Details

Description

I have a type which implements some numerical operation interfaces. I'm obtaining a serialized version of this type in my Blazor Web Assembly front end, which I'm deserializing. This deserialization fails because of some missing method:

Error: System.TypeLoadException: Parent class vtable failed to initialize, due to: Could not load list of method overrides due to Method not found: !2 System.Numerics.IAdditionOperators`3.op_Addition(!0,!1) assembly:Repro.Wasm.dll type:AbstractType member:(null)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[<<Main>$>d__0](:5432/<<Main>$>d__0& )
   at Program.<Main>$(:5432/String[] args)
    at qi (dotnet.7.0.2.2sr8yltp5e.js:5:80156)
    at Ji (dotnet.7.0.2.2sr8yltp5e.js:5:80040)
    at _Repro_Wasm__entrypoint (_Repro_Wasm__entrypoint:27:5)
    at dotnet.7.0.2.2sr8yltp5e.js:5:69011
    at Object.ji [as call_assembly_entry_point] (dotnet.7.0.2.2sr8yltp5e.js:5:69166)
    at Object.callEntryPoint (blazor.webassembly.js:1:46050)
    at Vt (blazor.webassembly.js:1:62248)

Reproduction Steps

  • Clone https://github.com/hypdeb/repros
  • Navigate to deserialize-with-abstract-static-methods (ignore the name, it was based on my original assumptions for the cause of the issue)
  • Run docker build . -t repro-wasm
  • Run docker run -p 5432:80 repro-wasm
  • Open your browser to localhost:5432
  • Open dev tools and notice the exception above in the console

Expected behavior

The exception obtained through the repro above does not occur.

Actual behavior

The exception does occur.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

I've looked into the _framework folder of the published version of the app, and the System.Numerics dll is missing. It is present in the output of a simple build, which I think explains why it works if you simply run the app using dotnet run.

Author: hypdeb
Assignees: -
Labels:

arch-wasm, untriaged

Milestone: -

ghost avatar Feb 07 '23 08:02 ghost

This sounds like trimming issue

/cc @vitek-karas

marek-safar avatar Feb 07 '23 08:02 marek-safar

Indeed: I tried with PublishTrimmed false just now and it seems to work in the small reproduction repository I produced. Sadly it doesn't seem to work in my real application.

Edit: oh it actually does work! I guess I had some caching issue last night. So definitely a trimming thing, thanks for the hint, that's a workaround I guess.

jdebache avatar Feb 07 '23 08:02 jdebache

Note: The fact that System.Numerics is missing is expected, it's a facade assembly (only has type forwarders), the actual types live in System.Private.CoreLib which is there. Trimming will redirect the type references as necessary.

The problem is that after trimming the application has a type like this:

class AbstractType : IAdditionOperator<AbstractType, AbstractType, AbstractType>
{
    // Using IL naming to avoid misunderstanding due to C# syntax sugar around custom operators
    // This has an impl record for IAdditionOperator<AbstractType,AbstractType,AbstractType>.op_Addition(AbstractType, AbstractType).
    public static AbstractType op_Addition(AbstractType a, AbstractType b) { ... }
}

But then the trimmed corelib has this:

public interface IAdditionOperators<T, U , V>
{
     // Empty - there are no members here
}

This is clearly wrong IL, since the method impl in the app points to non-existing method on the interface.

I prepared a repro from the console project, by modifying the linker command line (since Blazor runs with customized command line to some extent from the default console project settings). repro.zip

(Unzip, run linker from that directory with @linker.rsp)

If the --action copy is changed to --action link (and the main assembly is rooted via -a Repro library) everything works correctly - the interface is still empty, but the implementation method doesn't have an impl record anymore.

vitek-karas avatar Feb 07 '23 13:02 vitek-karas

@jtschuster could you please take a look - this is very likely in the same area as you worked before. Static interface method trimming.

vitek-karas avatar Feb 07 '23 13:02 vitek-karas

Tagging subscribers to this area: @agocke, @sbomer, @vitek-karas See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I have a type which implements some numerical operation interfaces. I'm obtaining a serialized version of this type in my Blazor Web Assembly front end, which I'm deserializing. This deserialization fails because of some missing method:

Error: System.TypeLoadException: Parent class vtable failed to initialize, due to: Could not load list of method overrides due to Method not found: !2 System.Numerics.IAdditionOperators`3.op_Addition(!0,!1) assembly:Repro.Wasm.dll type:AbstractType member:(null)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[<<Main>$>d__0](:5432/<<Main>$>d__0& )
   at Program.<Main>$(:5432/String[] args)
    at qi (dotnet.7.0.2.2sr8yltp5e.js:5:80156)
    at Ji (dotnet.7.0.2.2sr8yltp5e.js:5:80040)
    at _Repro_Wasm__entrypoint (_Repro_Wasm__entrypoint:27:5)
    at dotnet.7.0.2.2sr8yltp5e.js:5:69011
    at Object.ji [as call_assembly_entry_point] (dotnet.7.0.2.2sr8yltp5e.js:5:69166)
    at Object.callEntryPoint (blazor.webassembly.js:1:46050)
    at Vt (blazor.webassembly.js:1:62248)

Reproduction Steps

  • Clone https://github.com/hypdeb/repros
  • Navigate to deserialize-with-abstract-static-methods (ignore the name, it was based on my original assumptions for the cause of the issue)
  • Run docker build . -t repro-wasm
  • Run docker run -p 5432:80 repro-wasm
  • Open your browser to localhost:5432
  • Open dev tools and notice the exception above in the console

Expected behavior

The exception obtained through the repro above does not occur.

Actual behavior

The exception does occur.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

I've looked into the _framework folder of the published version of the app, and the System.Numerics dll is missing. It is present in the output of a simple build, which I think explains why it works if you simply run the app using dotnet run.

Author: hypdeb
Assignees: jtschuster
Labels:

arch-wasm, untriaged, area-Tools-ILLink

Milestone: -

ghost avatar Feb 07 '23 15:02 ghost

It seems like the IAdditionOperators.op_Addition is expected to be removed because it's unused, and usually all overrides of a removed static abstract method would have their .overrides metadata stripped in SweepStep.SweepOverrides(). But because the definition of the overriding method is in a 'copy' assembly, it doesn't get swept and we have an entry in .overrides that points to a removed method. I'm working on a PR to mark the methods pointed to in the .overrides of marked methods in Copy and CopyUsed assemblies, then the trimmer will keep the IAdditionOperators.op_Addition.

jtschuster avatar Feb 14 '23 22:02 jtschuster

@jtschuster I'm not too familiar with the versioning process. Will this be part of .NET 8 only or will it also make it to future versions of .NET 7 ?

jdebache avatar Feb 16 '23 20:02 jdebache

I think we should backport this to .NET 7, so you will see this in a patch to 7.0 (ship time TBD)

agocke avatar Feb 16 '23 23:02 agocke

Great news ! Thanks a lot !

jdebache avatar Feb 17 '23 07:02 jdebache

@jtschuster please backport this change to linker repo

marek-safar avatar Feb 17 '23 11:02 marek-safar