Repository navigation
Description
This suppression is invalid:
Lines 487 to 488 in 3628544
| [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().FullNameThis 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
Line 506 in 3628544
| t = _type.Assembly.GetType(typeName); |
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
steveharter commentedon Nov 29, 2023
@vitek-karas were you going to further triage\research this as part of linkability?
vitek-karas commentedon Nov 29, 2023
Currently I don't have any plans to tackle this. I ran into it during some investigation, so I created the bug.
sbomer commentedon Feb 27, 2024
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.
buyaa-n commentedon Jul 26, 2024
@sbomer is this still planned for V9.0?
sbomer commentedon Jul 26, 2024
I'm fixing up the suppression mentioned in this issue in #105577.
However, this method has another suppression that looks invalid:
runtime/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectTypeDescriptionProvider.ReflectedTypeData.cs
Lines 513 to 514 in 9d7551e
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
ericstj commentedon Aug 6, 2024
@sbomer does this need to be fixed for 9.0? It looks like the mentioned suppression has been there since 6.0. cc @eerhardt
sbomer commentedon Aug 6, 2024
Here's an example of a visible behavior difference in a trimmed app due to the bad suppression:
dotnet runoutput:The
Type.GetTypecallsite 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
PublishTrimmedapp (there are no trim warnings):Trimming didn't keep
ExpandableObjectConverterbecause it can't see through theType.GetTypeof 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.
sbomer commentedon Aug 7, 2024
I don't think I'm the right person to fix this, but happy to help with the trimming aspect.
steveharter commentedon Aug 9, 2024
So when using an assembly name ("assemblyDoesntExist.dll") for the assembly type name like this:
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:
sbomer commentedon Aug 9, 2024
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.
steveharter commentedon Aug 9, 2024
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:
Perhaps that code could throw if an assembly name is specified?