Skip to content

Invalid suppression in TypeConverter when loading a custom converter type #95265

Closed

Description

@vitek-karas

This suppression is invalid:

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Calling _type.Assembly.GetType on a non-assembly qualified type will still work. See https://github.com/mono/linker/issues/1895")]

It relies on the behavior like

namespace ns {
    public class MyConverter : TypeConverter {}
}

[TypeConverter("ns.MyConverter")]
public class MyType : ILibInterface {}

The above will work because the simple type name in the attribute refers to the type in the same assembly as the type which has the attribute applied to it.

The problem is that the same is not true for case where the attribute comes from a base class or interface. For example if the interface is defined as below in a separate assembly Lib:

namespace ns {
    public class MyConverter : TypeConverter {}
}

[TypeConverter("ns.MyConverter")]
public interface ILibInterface {}

Now remove the TypeConverter attribute from the MyType in the app like this, but keep the custom converter type:

namespace ns {
    public class MyConverter : TypeConverter {}
}

public class MyType : ILibInterface {}

And now use it:

TypeDescriptor.GetConverter(typeof(MyType)).GetType().FullName

This will print out ns.MyConverter, app - so it will load the custom converter from the app! Not the one from the Lib where the attribute is used. This is because of how the type lookup is implemented in

where it uses the assembly of the type on which we're getting the converter.

Now in trimming: TypeConverterAttribute.ctor([DynamicallyAccessedMembers(PublicConstructors)] string).
The behavior of this is defined as resolving the string passed in in the context of the scope where the call is made. So in the case of the attribute used on the interface above, the string is resolved to a type in the context of the ILibInterface, so it resolves the custom converter from the Lib assembly. The trimmer will preserve that, but it will remove the custom converter type from the App since there are no references to it.
At runtime the algorithm to lookup the type name will fail to resolve the type name in the attribute and fallback to the default converter instead.

I can't think of a solution to this, because the trimmer has no idea what type of resolution logic is used, it warns, but the warning is suppressed.

Important note - currently TypeDescriptor.GetConverter is annotated with RequiresUnreferecedCode so the above will produce a trimmer warning. So, the bug here is effectively covered by that. But the explanation of why we need the RUC in this case is at least partially incorrect. And efforts to optimize the annotations in the BCL library will be affected by the invalid suppression regardless.

/cc @sbomer @eerhardt @LakshanF

Activity

  1. added
    linkable-framework
    on Nov 27, 2023
  2. ghost added
    untriaged
    on Nov 27, 2023
  3. steveharter commented on Nov 29, 2023

    @steveharter
    Contributor

    @vitek-karas were you going to further triage\research this as part of linkability?

  4. vitek-karas commented on Nov 29, 2023

    @vitek-karas
    MemberAuthor

    Currently I don't have any plans to tackle this. I ran into it during some investigation, so I created the bug.

  5. added this to the Future milestone on Dec 6, 2023
  6. ghost removed
    untriaged
    on Dec 6, 2023
  7. sbomer commented on Feb 27, 2024

    @sbomer
    Member

    I re-opened #98955 which tracks ILLink warning when it encounters a non-qualified type name flowing into a location with DynamicallyAccessedMemberTypes. Once that is working as intended, I think the suppression is OK (just for a different reason than what's stated in the current justification). We could also add a feature switch that throws here when it encounters a non-qualified type name.

  8. modified the milestones: Future, 9.0.0 on Feb 27, 2024
  9. buyaa-n commented on Jul 26, 2024

    @buyaa-n
    Contributor

    @sbomer is this still planned for V9.0?

  10. sbomer commented on Jul 26, 2024

    @sbomer
    Member

    I'm fixing up the suppression mentioned in this issue in #105577.

    However, this method has another suppression that looks invalid:

    [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2057:TypeGetType",
    Justification = "Using the non-assembly qualified type name will still work.")]

    That's for this call:
    https://github.com/dotnet/runtime/blame/9d7551ecd3e1b1b6f224eabecc1c7f91a41f259c/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectTypeDescriptionProvider.ReflectedTypeData.cs#L535-L546

    I don't understand the design-time scenario, but from the trimming perspective this is bad because the trimmer has no idea what type is being looked up here, and may not preserve it.

    I don't understand the comment because the code looks like it will look up the type in System.ComponentModel.TypeConverter or System.Private.CoreLib, and I don't know how the design-time scenario would see different versions of either of these assemblies. I think area owners need to take a look at this one. @dotnet/area-system-componentmodel

  11. self-assigned this
    on Jul 30, 2024
  12. ericstj commented on Aug 6, 2024

    @ericstj
    Member

    @sbomer does this need to be fixed for 9.0? It looks like the mentioned suppression has been there since 6.0. cc @eerhardt

  13. sbomer commented on Aug 6, 2024

    @sbomer
    Member

    Here's an example of a visible behavior difference in a trimmed app due to the bad suppression:

    using System.ComponentModel;
    
    TypeDescriptor.RegisterType<MyType>();
    var converter = TypeDescriptor.GetConverterFromRegisteredType(typeof(MyType));
    Console.WriteLine(converter.GetType().Name);
    
    [TypeConverter ("System.ComponentModel.ExpandableObjectConverter, assemblyDoesntExist.dll")]
    class MyType {}

    dotnet run output:

    ExpandableObjectConverter
    

    The Type.GetType callsite I linked in my last comment finds the type even though we specified a fully-qualified name with an assembly that doesn't exist.

    Output of the PublishTrimmed app (there are no trim warnings):

    TypeConverter
    

    Trimming didn't keep ExpandableObjectConverter because it can't see through the Type.GetType of a substring of the input.

    The example is a bit contrived. I don't think this is likely to cause real world problems so probably not important to get fixed in 9.0, but I'd let area owners decide.

  14. sbomer commented on Aug 7, 2024

    @sbomer
    Member

    I don't think I'm the right person to fix this, but happy to help with the trimming aspect.

  15. removed their assignment
    on Aug 7, 2024
  16. self-assigned this
    on Aug 9, 2024
  17. steveharter commented on Aug 9, 2024

    @steveharter
    Contributor

    Trimming didn't keep ExpandableObjectConverter because it can't see through the Type.GetType of a substring of the input.

    So when using an assembly name ("assemblyDoesntExist.dll") for the assembly type name like this:

    [TypeConverter ("System.ComponentModel.ExpandableObjectConverter, assemblyDoesntExist.dll")]
    

    where the assembly name is later removed at run-time before the Type.GetType lookup, and the specified assembly didn't actually exist (before trimming), then the trimmer won't know what to do since the assembly never existed?

    If that's the case then I agree with this:

    The example is a bit contrived. I don't think this is likely to cause real world problems so probably not important to get fixed in 9.0, but I'd let area owners decide.

  18. sbomer commented on Aug 9, 2024

    @sbomer
    Member

    then the trimmer won't know what to do since the assembly never existed?

    Yes, pretty much. To be more specific, the trimmer will see the fully-qualified name passed to TypeConverter, and try to keep it (due to the annotations on TypeConverter), but because it doesn't exist there's nothing to keep. And because the trimmer doesn't see what value is passed for the non-assembly-qualified lookup, it warns at that Type.GetType callsite.

  19. steveharter commented on Aug 9, 2024

    @steveharter
    Contributor

    I'm going to close this; if it causes a real-world issue then we can re-evaluate.

    The given type name should not have an invalid assembly name, perhaps and older one to support design-time cases, but not invalid. The name is stripped out due to the comment in the source due to design-time:

    // At design time, it's possible for us to reuse
    // an assembly but add new types. The app domain
    // will cache the assembly based on identity, however,
    // so it could be looking in the previous version
    // of the assembly and not finding the type. We work
    // around this by looking for the non-assembly qualified
    // name, which causes the domain to raise a type
    // resolve event.

    Perhaps that code could throw if an assembly name is specified?

  20. locked and limited conversation to collaborators on Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.ComponentModellinkable-framework

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions