dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.03k stars 4.68k forks source link

NativeAOT when building shared library #71063

Closed rodrigovaras closed 2 years ago

rodrigovaras commented 2 years ago

Description

Attempting to create a shared library using the NativeAOT tools on my managed assembly entry point. I tried the NativeLibrary and succeeded but not my project.

Reproduction Steps

Create a .NET project that has several dependencies. Attempt to run: dotnet publish /p:NativeLib=Shared /p:SelfContained=true -r win-x64 -c Release

Expected behavior

Native library being created for the Windows platform

Actual behavior

EXEC : error : Failed to load type 'Void ()' from assembly '?' [D:\mr\mixedreality.eu.proto\client\sdk.net\src\Hololen s\Hololens.Runtime.Native\Hololens.Runtime.Native.csproj] Internal.TypeSystem.TypeSystemException+TypeLoadException: Failed to load type 'Void ()' from assembly '?' at Internal.TypeSystem.ThrowHelper.ThrowTypeLoadException(ExceptionStringID id, String typeName, String assemblyNa me) in //src/coreclr/tools/Common/TypeSystem/Common/ThrowHelper.cs:line 17 at ILCompiler.DependencyAnalysis.EETypeNode..ctor(NodeFactory factory, TypeDesc type) in //src/coreclr/tools/aot/ ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs:line 81 at ILCompiler.DependencyAnalysis.NodeFactory.CreateNecessaryTypeNode(TypeDesc type) in //src/coreclr/tools/aot/IL Compiler.Compiler/Compiler/DependencyAnalysis/NodeFactory.cs:line 508 at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) at ILCompiler.DependencyAnalysis.ReflectableFieldNode.GetStaticDependencies(NodeFactory factory) in //src/coreclr /tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ReflectableFieldNode.cs:line 89 at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer2.GetStaticDependenciesImpl(DependencyNodeCore1 node ) in //src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/DependencyAnalyzer.cs:line 181 at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer2.GetStaticDependencies(DependencyNodeCore1 node) in //src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/DependencyAnalyzer.cs:line 221 at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer2.ProcessMarkStack() in /_/src/coreclr/tools/aot/ILCo mpiler.DependencyAnalysisFramework/DependencyAnalyzer.cs:line 256 at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer2.ComputeMarkedNodes() in //src/coreclr/tools/aot/IL Compiler.DependencyAnalysisFramework/DependencyAnalyzer.cs:line 307 at ILCompiler.ILScanner.ILCompiler.IILScanner.Scan() in //src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ILSc anner.cs:line 140 at ILCompiler.Program.Run(String[] args) in //src/coreclr/tools/aot/ILCompiler/Program.cs:line 833 at ILCompiler.Program.Main(String[] args) in //src/coreclr/tools/aot/ILCompiler/Program.cs:line 1090 C:\Users\rodrigov.nuget\packages\microsoft.dotnet.ilcompiler\7.0.0-preview.6.22321.1\build\Microsoft.NETCore.Native.ta rgets(270,5): error MSB3073: The command ""C:\Users\rodrigov.nuget\packages\runtime.win-x64.microsoft.dotnet.ilcompile r\7.0.0-preview.6.22321.1\tools\ilc" @"D:\mr\mixedreality.eu.proto\client\sdk.net\bin\obj\Hololens.Runtime.Native\Rele ase\net6.0-windows10.0.19041.0\win-x64\native\MixedReality.EU.HoloLens.Runtime.Native.ilc.rsp"" exited with code 1. [D: \mr\mixedreality.eu.proto\client\sdk.net\src\Hololens\Hololens.Runtime.Native\Hololens.Runtime.Native.csproj]

Regression?

No response

Known Workarounds

No response

Configuration

.NET -> 6.0.300 Architecture : win-x64

Other information

No response

MichalStrehovsky commented 2 years ago

Could you do a search in your codebase for fields that have delegate*<void> somewhere in their type? From the stack the issue you're running into is caused by a field of type delegate*<void> but we have explicit code that should make this situation impossible to reach:

https://github.com/dotnet/runtime/blob/46f53e387e0396542f7dcefa2bb7627ef3a2bf1d/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ReflectableFieldNode.cs#L86-L89

ReflectableFieldNode.GetStaticDependencies should never be calling CreateNecessaryTypeNode if the type of the field is a function pointer. It's a bit of a puzzle how we end up with the stack/error you're hitting.

rodrigovaras commented 2 years ago

Could you do a search in your codebase for fields that have delegate*<void> somewhere in their type? From the stack the issue you're running into is caused by a field of type delegate*<void> but we have explicit code that should make this situation impossible to reach:

https://github.com/dotnet/runtime/blob/46f53e387e0396542f7dcefa2bb7627ef3a2bf1d/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ReflectableFieldNode.cs#L86-L89

ReflectableFieldNode.GetStaticDependencies should never be calling CreateNecessaryTypeNode if the type of the field is a function pointer. It's a bit of a puzzle how we end up with the stack/error you're hitting.

Hi Michael, i did more research and our project define the framework target as 'net6.0-windows' , this seems to be reason of the problem. The reason we will forced to do that is because of some winrt C# interop required for some of the components. I experiment removing the dependency and use 'net6.0' and it seems to work now (of course without the full functionality we needed) but it was a good exercise to see the trimming warnings and the size of the generated dll. If you are curious the size is around 22 MB compared with a 'from the scratch C++ native effort' that is around 8.5 MB. Closing this since the problem goes away. Please comment if in the future a specific 'windows only' target would eventually work.