Skip to content

Framework trimming inlines CoreCLR's CoreLib into other inbox libraries #79513

Closed

Description

@VSadov

I will disable System.Data.DataSetExtensions.Tests for now to unblock CI, but perhaps the tests can be fixed

---- System.NotSupportedException : 'System.Data.DataRowExtensions+UnboxT1[System.Nullable1[System.Int32]].NullableFieldSystem.Int32' is missing native code.

Activity

  1. ghost added
    untriaged
    on Dec 11, 2022
  2. VSadov commented on Dec 11, 2022

    @VSadov
    MemberAuthor

    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

  3. MichalStrehovsky commented on Dec 12, 2022

    @MichalStrehovsky
    Member

    DataRowExtensions was fixed up to be AOT safe so this is very suspicious:

    if (typeof(T).IsValueType && default(T) == null)
    {
    if (!RuntimeFeature.IsDynamicCodeSupported)
    return NullableFieldUsingReflection;
    return CreateWhenDynamicCodeSupported();
    }
    return NonNullableField;

    We were not even supposed to compile CreateWhenDynamicCodeSupported.

  4. MichalStrehovsky commented on Dec 12, 2022

    @MichalStrehovsky
    Member

    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.

    <UseNativeAotCoreLib Condition="$(_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:

            if (typeof(T).get_IsValueType() && default(T) == null)
    	{
    		if (1 == 0)
    		{
    		}
    		return CreateWhenDynamicCodeSupported();
    	}

    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

  5. jkotas commented on Dec 12, 2022

    @jkotas
    Member
  6. jkotas commented on Dec 12, 2022

    @jkotas
    Member

    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).

  7. ViktorHofer commented on Dec 12, 2022

    @ViktorHofer
    Member

    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.

  8. MichalStrehovsky commented on Dec 12, 2022

    @MichalStrehovsky
    Member

    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.

  9. ViktorHofer commented on Dec 12, 2022

    @ViktorHofer
    Member

    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.

  10. jkotas commented on Dec 12, 2022

    @jkotas
  11. ViktorHofer commented on Dec 12, 2022

    @ViktorHofer
  12. jkotas commented on Dec 12, 2022

    @jkotas
    Member

    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.

    C:\Program Files\dotnet\dotnet.exe "C:\Users\jkotas\.nuget\packages\microsoft.net.illink.tasks\7.0.100-1.22606.1\tools\net7.0\illink.dll" -a "C:\runtime\artifacts\obj\System.Data.Common\Release\net8.0\PreTrim/System.Data.Common.dll" library
           -out "C:\runtime\artifacts\obj\System.Data.Common\Release\net8.0"
           --warnaserror-  --ignore-link-attributes true --skip-unresolved true --trim-mode skip --action skip --action link System.Data.Common -b true --nowarn IL2008;IL2009;IL2012;IL2025;IL2026;IL2035;IL2050;IL2032;IL2055;IL2057;IL2058;IL2059;IL2060;IL2061;IL2062;IL2063;IL2064;IL2065;IL2066;IL2067;IL2068;IL2069;IL2070;IL2071;IL2072;IL2073;IL2074;IL2075;IL2076;IL2077;IL2078;IL2079;IL2080;IL2081;IL2082;IL2083;IL2084;IL2085;IL2086;IL2087;IL2088;IL2089;IL2090;IL2091;IL2121
            -d "C:\runtime\artifacts\bin\System.Collections\Release\net8.0"
            -d "C:\runtime\artifacts\bin\System.Collections.NonGeneric\Release\net8.0"
            -d "C:\runtime\artifacts\bin\System.ComponentModel.TypeConverter\Release\net8.0"
            -d "C:\runtime\artifacts\bin\coreclr\windows.x64.Release\IL"
            -d "C:\runtime\artifacts\bin\System.Private.Uri\Release\net8.0"
            -d "C:\runtime\artifacts\bin\System.Runtime\Release\net8.0"
            -d "C:\runtime\artifacts\bin\microsoft.netcore.app.ref\ref\net8.0"
    
  13. MichalStrehovsky commented on Dec 13, 2022

    @MichalStrehovsky
    Member

    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.

    	if (typeof(T)!.IsValueType && default(T) == null)
    	{
    		if (1 == 0)
    		{
    		}
    		return typeof(UnboxT<T>)!.GetMethod("NullableField", BindingFlags.Static | BindingFlags.NonPublic)!.MakeGenericMethod(Nullable.GetUnderlyingType(typeof(T))).CreateDelegate<Func<object, T>>();
    	}

    (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.)

  14. 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
  15. removed
    untriaged
    on Dec 13, 2022
  16. added this to the 8.0.0 milestone on Dec 13, 2022
  17. ViktorHofer commented on Dec 13, 2022

    @ViktorHofer
    Member

    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.

    Great find @jkotas 👍

    We do have a set of projects that directly reference CoreLib, i.e. System.Data.Common:

    <ProjectReference Include="$(CoreLibProject)" />
    <ProjectReference Include="..\..\System.Collections\src\System.Collections.csproj" />
    <ProjectReference Include="..\..\System.Collections.NonGeneric\src\System.Collections.NonGeneric.csproj" />
    <ProjectReference Include="..\..\System.ComponentModel.TypeConverter\src\System.ComponentModel.TypeConverter.csproj" />
    <ProjectReference Include="..\..\System.Runtime\src\System.Runtime.csproj" />
    <ProjectReference Include="..\..\System.Private.Uri\src\System.Private.Uri.csproj" />
    ILLink then receives these references here:
    <_DependencyDirectoriesTemp Include="@(ReferencePath->'%(RootDir)%(Directory)')" />

    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.

  18. ghost added
    in-pr
    on Dec 13, 2022
  19. ghost removed
    in-pr
    on Dec 13, 2022
  20. ghost locked as resolved and limited conversation to collaborators on Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions