You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Framework trimming inlines CoreCLR's CoreLib into other inbox libraries #79513
Error message
System.TypeInitializationException : A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
---- System.NotSupportedException : 'System.Data.DataRowExtensions+UnboxT1[System.Nullable1[System.Int32]].NullableFieldSystem.Int32' is missing native code. MethodInfo.MakeGenericMethod() is not compatible with AOT compilation. Inspect and fix AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
Stack trace
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x163
at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0xd
at System.Data.DataRowExtensions.Field[T](DataRow, String) + 0x16
at MonoTests.System.Data.DataRowExtensionsTest.Field_T_DBNullFieldValue() + 0x83
at System.Data.DataSetExtensions.Tests!+0x809144
at System.Reflection.DynamicInvokeInfo.Invoke(Object, IntPtr, Object[], BinderBundle, Boolean) + 0xc4
----- Inner Stack Trace -----
at System.Reflection.Runtime.MethodInfos.RuntimeNamedMethodInfo1.GetUncachedMethodInvoker(RuntimeTypeInfo[], MemberInfo) + 0x2f at System.Reflection.Runtime.MethodInfos.RuntimeNamedMethodInfo1.MakeGenericMethod(Type[]) + 0x18e
at System.Data.DataRowExtensions.UnboxT1.<Create>g__CreateWhenDynamicCodeSupported|1_0() + 0x6d at System.Data.DataRowExtensions.UnboxT1.Create() + 0x9
at System.Data.DataRowExtensions.UnboxT`1..cctor() + 0x1e
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xc4
Ok, this is a pretty bad product issue that affects more than just NativeAOT. I guess it's a happy accident we found it. The thing that surfaced it was 5435aa8 that changed how we run NativeAOT testing - we started building all of CoreCLR. I couldn't repro the issue locally because I don't like building junk I don't need - building all of CoreCLR is what triggers the problem though.
<UseNativeAotCoreLibCondition="$(_subset.Contains('+clr.nativeaotlibs+')) and !$(_subset.Contains('+clr.native+')) and !$(_subset.Contains('+clr.runtime+'))">true</UseNativeAotCoreLib>
If we build clr.runtime subset, we no longer build libraries against NativeAOT's corelib, but use CoreCLR's corelib instead. This is not desirable - we want to use NativeAOT's so that we run APICompat with it. Another thing this changes is what corelib we use when trimming the framework as part of framework build. Guess what RuntimeFeature.IsDynamicCodeSupported is on CoreCLR's corelib. The illink that we use during library build turns the above snippet into:
We could make the test pass again by switching back to using NativeAOT's corelib during libraries build.
But here's why it's a product issue: all shipping libraries come from a build that uses CoreCLR corelib. I think this also includes platforms targeted only by Mono - e.g. the System.Data.Common in this test is not RID-specific and we probably don't have an e.g. iOS specific build.
The worst part of this: we don't detect any of this in our testing because the testing always uses the "expected" corelib (Mono's on Mono platforms, NativeAOT's in NativeAOT testing).
Basically, framework build is inlining arbitrary parts of CoreLib, but we don't know whether the implementations couldn't actually differ.
This sounds like we run runtime-pack trimming during runtime-neutral part of the libraries build that is not correct. Instead, it needs to run during runtime-specific part of the build when we are building runtime packs (similar to where R2R compilation runs for CoreCLR).
But here's why it's a product issue: all shipping libraries come from a build that uses CoreCLR corelib. I think this also includes platforms targeted only by Mono - e.g. the System.Data.Common in this test is not RID-specific and we probably don't have an e.g. iOS specific build.
All shipping libraries (packages, etc.) build against CoreLib's reference assembly which is expected to be runtime neutral. cc @akoeplinger. How does the runtime variant of CoreLib come into play here?
Another thing this changes is what corelib we use when trimming the framework as part of framework build. Guess what RuntimeFeature.IsDynamicCodeSupported is on CoreCLR's corelib. The illink that we use during library build turns the above snippet into:
When you say "framework build", I assume you refer to the shared framework packages build (which lives in src/installer/pkg/sfx)?
This sounds like we run runtime-pack trimming during runtime-neutral part of the libraries build that is not correct.
AFAIK the libraries build doesn't do any kind of runtime-pack trimming, neither do we invoke crossgen2. The only trimming that happens is inside illink.targets that runs as part of a library's individual build.
All shipping libraries (packages, etc.) build against CoreLib's reference assembly which is expected to be runtime neutral. cc @akoeplinger. How does the runtime variant of CoreLib come into play here?
When we run trimming on the framework, we use the implementation assembly. The ref assembly wouldn't work for trimming.
That's understood. But as I said, AFAIK we don't do any runtime-pack trimming during the libraries build. I.e. our out-of-band libraries packages like System.Formats.Cbor that don't ship inside the shared framework don't get trimmed in any special way aside from what is happening in illink.targets. And correct me if I'm wrong, the library-mode trimming in illink.targets doesn't receive CoreLib as an input.
Ok, I can see that now - but that invalidates my theory. I was looking in a wrong place.
And correct me if I'm wrong, the library-mode trimming in illink.targets doesn't receive CoreLib as an input.
The library-mode trimming in illink.targets does receive CoreLib as an input and that's the problem. Here is the command line for library-mode trimming of System.Data.Common. Notice that it has -d "C:\runtime\artifacts\bin\coreclr\windows.x64.Release\IL as input.
I double checked the framework we ship in 7.0 runtime.win-x64.microsoft.dotnet.ilcompiler and we did inline CoreCLR's CoreLib in it. It is a product issue.
(This is the previous version of the method that doesn't have the CreateWhenDynamicCodeSupported local function extracted, but the problem is the same - we wanted to take the if (1 == 0) codepath.)
changed the title [-][NativeAOT] Couple tests from System.Data.DataSetExtensions.Tests fail on NativeAOT[/-][+]Framework trimming inlines CoreCLR's CoreLib into other inbox libraries[/+]on Dec 13, 2022
The library-mode trimming in illink.targets does receive CoreLib as an input and that's the problem. Here is the command line for library-mode trimming of System.Data.Common. Notice that it has -d "C:\runtime\artifacts\bin\coreclr\windows.x64.Release\IL as input.
During compilation, even though we have a ProjectReference pointing to the CoreLib/src project, we build against CoreLib/ref. But that ILLink invocation doesn't honor that as it uses the ReferencePath msbuild item instead of ReferencePathWithRefAssemblies. I see that ILLink already receives reference assemblies as an input which makes me wonder if we could use CoreLib/ref here instead.
Activity
VSadov commentedon Dec 11, 2022
Error message
System.TypeInitializationException : A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
---- System.NotSupportedException : 'System.Data.DataRowExtensions+UnboxT1[System.Nullable1[System.Int32]].NullableFieldSystem.Int32' is missing native code. MethodInfo.MakeGenericMethod() is not compatible with AOT compilation. Inspect and fix AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
Stack trace
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x163
at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0xd
at System.Data.DataRowExtensions.Field[T](DataRow, String) + 0x16
at MonoTests.System.Data.DataRowExtensionsTest.Field_T_DBNullFieldValue() + 0x83
at System.Data.DataSetExtensions.Tests!+0x809144
at System.Reflection.DynamicInvokeInfo.Invoke(Object, IntPtr, Object[], BinderBundle, Boolean) + 0xc4
----- Inner Stack Trace -----
at System.Reflection.Runtime.MethodInfos.RuntimeNamedMethodInfo
1.GetUncachedMethodInvoker(RuntimeTypeInfo[], MemberInfo) + 0x2f at System.Reflection.Runtime.MethodInfos.RuntimeNamedMethodInfo1.MakeGenericMethod(Type[]) + 0x18eat System.Data.DataRowExtensions.UnboxT
1.<Create>g__CreateWhenDynamicCodeSupported|1_0() + 0x6d at System.Data.DataRowExtensions.UnboxT1.Create() + 0x9at System.Data.DataRowExtensions.UnboxT`1..cctor() + 0x1e
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xc4
MichalStrehovsky commentedon Dec 12, 2022
DataRowExtensions was fixed up to be AOT safe so this is very suspicious:
runtime/src/libraries/System.Data.Common/src/System/Data/DataRowExtensions.cs
Lines 149 to 156 in cfdf0f6
We were not even supposed to compile
CreateWhenDynamicCodeSupported.MichalStrehovsky commentedon Dec 12, 2022
Ok, this is a pretty bad product issue that affects more than just NativeAOT. I guess it's a happy accident we found it. The thing that surfaced it was 5435aa8 that changed how we run NativeAOT testing - we started building all of CoreCLR. I couldn't repro the issue locally because I don't like building junk I don't need - building all of CoreCLR is what triggers the problem though.
runtime/eng/Subsets.props
Line 106 in 7e10f81
If we build clr.runtime subset, we no longer build libraries against NativeAOT's corelib, but use CoreCLR's corelib instead. This is not desirable - we want to use NativeAOT's so that we run APICompat with it. Another thing this changes is what corelib we use when trimming the framework as part of framework build. Guess what
RuntimeFeature.IsDynamicCodeSupportedis on CoreCLR's corelib. The illink that we use during library build turns the above snippet into:We could make the test pass again by switching back to using NativeAOT's corelib during libraries build.
But here's why it's a product issue: all shipping libraries come from a build that uses CoreCLR corelib. I think this also includes platforms targeted only by Mono - e.g. the System.Data.Common in this test is not RID-specific and we probably don't have an e.g. iOS specific build.
The worst part of this: we don't detect any of this in our testing because the testing always uses the "expected" corelib (Mono's on Mono platforms, NativeAOT's in NativeAOT testing).
Basically, framework build is inlining arbitrary parts of CoreLib, but we don't know whether the implementations couldn't actually differ.
Cc @dotnet/ilc-contrib @vitek-karas @sbomer
jkotas commentedon Dec 12, 2022
cc @ViktorHofer
jkotas commentedon Dec 12, 2022
This sounds like we run runtime-pack trimming during runtime-neutral part of the libraries build that is not correct. Instead, it needs to run during runtime-specific part of the build when we are building runtime packs (similar to where R2R compilation runs for CoreCLR).
ViktorHofer commentedon Dec 12, 2022
All shipping libraries (packages, etc.) build against CoreLib's reference assembly which is expected to be runtime neutral. cc @akoeplinger. How does the runtime variant of CoreLib come into play here?
When you say "framework build", I assume you refer to the shared framework packages build (which lives in src/installer/pkg/sfx)?
AFAIK the libraries build doesn't do any kind of runtime-pack trimming, neither do we invoke crossgen2. The only trimming that happens is inside illink.targets that runs as part of a library's individual build.
MichalStrehovsky commentedon Dec 12, 2022
When we run trimming on the framework, we use the implementation assembly. The ref assembly wouldn't work for trimming.
ViktorHofer commentedon Dec 12, 2022
That's understood. But as I said, AFAIK we don't do any runtime-pack trimming during the libraries build. I.e. our out-of-band libraries packages like System.Formats.Cbor that don't ship inside the shared framework don't get trimmed in any special way aside from what is happening in illink.targets. And correct me if I'm wrong, the library-mode trimming in illink.targets doesn't receive CoreLib as an input.
jkotas commentedon Dec 12, 2022
ViktorHofer commentedon Dec 12, 2022
jkotas commentedon Dec 12, 2022
Ok, I can see that now - but that invalidates my theory. I was looking in a wrong place.
The library-mode trimming in illink.targets does receive CoreLib as an input and that's the problem. Here is the command line for library-mode trimming of System.Data.Common. Notice that it has
-d "C:\runtime\artifacts\bin\coreclr\windows.x64.Release\ILas input.MichalStrehovsky commentedon Dec 13, 2022
I double checked the framework we ship in 7.0
runtime.win-x64.microsoft.dotnet.ilcompilerand we did inline CoreCLR's CoreLib in it. It is a product issue.(This is the previous version of the method that doesn't have the
CreateWhenDynamicCodeSupportedlocal function extracted, but the problem is the same - we wanted to take theif (1 == 0)codepath.)[-][NativeAOT] Couple tests from System.Data.DataSetExtensions.Tests fail on NativeAOT[/-][+]Framework trimming inlines CoreCLR's CoreLib into other inbox libraries[/+]ViktorHofer commentedon Dec 13, 2022
Great find @jkotas 👍
We do have a set of projects that directly reference CoreLib, i.e. System.Data.Common:
runtime/src/libraries/System.Data.Common/src/System.Data.Common.csproj
Lines 311 to 316 in 7edc1f6
runtime/eng/illink.targets
Line 277 in c635ae2
During compilation, even though we have a ProjectReference pointing to the CoreLib/src project, we build against CoreLib/ref. But that ILLink invocation doesn't honor that as it uses the
ReferencePathmsbuild item instead ofReferencePathWithRefAssemblies. I see that ILLink already receives reference assemblies as an input which makes me wonder if we could use CoreLib/ref here instead.Use ReferencePathWithRefAssemblies for libs ILLink
Use ReferencePathWithRefAssemblies for libs ILLink (#79595)