Skip to content

EventSource contains code which ILLink can't analyze #32863

Closed

Description

@vitek-karas

The implementation of EventSource has a code paths that uses reflection APIs which are not analyzable by the linker, effectively making any usage of EventSource potentially linker-unsafe. This in turn means that applications using ILLink during publish may end up broken at runtime due to missing assemblies/types/members.

Manifest creation

EventSource.CreateManifestAndDescriptors uses reflection enumeration methods Type.GetMethods and Type.GetFields as well as Type.GetNestedType to build the manifest description of an event source. If any of these methods/fields/nested types is not referenced directly by the application code linker may decide to remove them, so the manifest produced for a trimmed app might be different from the untrimmed app.

Accessing attributes

Most of the event source code uses EventSource.GetCustomAttributeHelper to access attributes on types. The way this is implemented is not analyzable by the linker.

attributeType is not analyzable right now

It comes from a fixed set of typeof expressions in the call sites, so this should be relatively easy to fix. Those attribute types must be kept as attributes, instantiated and must have all properties. The method will eventually call CreateInstance on the type and also GetProperty and then PropertyInfo.SetValue.

Performs name based scan of attributes

The method takes the attributeType and does a name based matched to all attributes from the member. So there's no good way to tell the linker it needs to keep attributes of a certain name (in any assembly). That said it seems this is for reflection-only scenarios. It's unclear if this is necessary in .NET Core - runtime itself doesn't support reflection-only. Maybe MetadataLoadContext is considered reflection-only. What are the scenarios for this? Does it have to be an enabled code path for runtime event-tracing?

Caching reflection information about types

The entry point to this system seems to be Statics.CreateDefaultTypeInfo.
This caches lot of reflection information about a specified Type. Uses GetProperties and GetCustomAttributes. Effectively requires the type to have all properties and attributes kept.

Using reflection to get a value of a property

PropertyValue uses PropertyInfo.GetValue or the indirect PropertyInfo.GetGetMethod to access property value. In some cases linker may remove the getter from a property if it is not used directly by any code in the application.

Similarly InvokeTypeInfo uses PropertyInfo.GetValue on a "random" property.

NullableTypeInfo uses reflection

The implementation of NullableTypeInfo uses reflection calls to create instance of the property value. The code is not analyzable by the linker as the type in question comes as an input. It's possible that with some advanced annotations the code is safe enough, hard to tell. Is it possible to create arbitrary types with this code, or is it really only a limited set of types which is allowed?

Asking for size of an object

EventSource.DecodeObject calls Marshal.SizeOf on a System.Type which is not statically analyzable. Technically this is unsafe as linker can't guarantee that the SizeOf will return the same values before and after linking (linker may remove unused fields).

Maps creation for enum types

ManifestBuilder.CreateManifestString uses Type.GetFields to enumerate all values on an Enum type. This is not analyzable by the linker currently as there's no way for linker to tell that the types in question are always enums. The code is safe (as linker keeps all fields on enums by default), we just need a way to describe this to linker in some maintainable way.

Similarly ManifestBuilder.GetTypeName also special cases enums and calls Type.GetFields on them. Same situation as above; just need to find a way to annotate this correctly.

Activity

  1. added
    untriaged
    on Feb 26, 2020
  2. MichalStrehovsky commented on Feb 26, 2020

    @MichalStrehovsky
    Member

    .NET Native would inject overrides of the GetMetadata method at the time of build. The body of the injected GetMetadata method would have all the information EventSource otherwise collects with reflection. I think the transform had some limitations though, so it was only enabled for event providers we ship with .NET Native.

  3. MichalStrehovsky commented on Feb 26, 2020

    @MichalStrehovsky
    Member

    As an aside, why is Marshal.SizeOf linker unsafe? This method only works for types that specify sequential/explicit layout. Linker should not touch instance fields on those (and I believe it doesn't).

  4. stephentoub commented on Feb 26, 2020

    @stephentoub
    Member

    What is the negative ramification of this? Let's say I have an event source with 5 events, but my app calls only one of them. Presumably then the linker removes the other 4 event methods, and the problem is that the generated manifest won't contain them. Is that the issue? And if so, since the events aren't getting raised anyway, what does that break?

    Simply trying to understand the scope of the issue.

  5. added
    linkable-framework
    on Feb 26, 2020
  6. vitek-karas commented on Feb 26, 2020

    @vitek-karas
    MemberAuthor

    @stephentoub I think that answering those questions is kind of the point of this issue. I don't fully understand what EventSource does and all the corner case features it supports, so it's hard to tell if the above mentioned places are potentially behavior breaking or not.

    I do agree that in general it doesn't sound too bad. That said I know that for example it supports a kind of a serializer (WriteEvent(object)) which compined with the fact that linker may remove property getters can lead to breaks. Also the attribute handling might be problematic as linker is typically very aggressive removing attributes, and the code accesses them in a way which the linker doesn't understand.

  7. stephentoub commented on Feb 26, 2020

    @stephentoub
    Member

    I think that answering those questions is kind of the point of this issue

    Ok, that wasn't clear to me. Thanks. Then this issue is a really a statement that some things might get removed, and we need to determine whether that's ok or not, and if not, then come up with a solution (but "do nothing because nothing bad happens" may also be the outcome).

  8. josalem commented on Feb 26, 2020

    @josalem
    Contributor
  9. vitek-karas commented on Feb 27, 2020

    @vitek-karas
    MemberAuthor

    Regarding Marshal.SizeOf - thanks for the feedback - I'll investigate if it is really safe. For now just ignore that part of the issue above.

  10. removed
    untriaged
    on Apr 6, 2020
  11. added this to the 5.0 milestone on Apr 6, 2020
  12. added
    tracking
    on Jul 8, 2020
  13. modified the milestones: 5.0.0, 6.0.0 on Jul 8, 2020
  14. KalleOlaviNiemitalo commented on Jan 7, 2021

    @KalleOlaviNiemitalo

    (This comment does not cover all the uses of reflection that were previously listed.)

    If the IL Linker trims some event methods and causes EventSource to omit some events from the manifest, that could prevent Event Viewer from properly displaying events that were logged by previous versions of the application:

    • If the event methods do not have EventAttribute, then EventSource generates IDs for them. Some events in the new manifest could get different IDs, even if those events are logged by both versions of the application. The developer could fix this by adding the attributes with explicit event IDs, which I think is a best practice anyway. An analyzer might help here.
    • If the event methods have EventAttribute, but the old version of the application logs some events that the new version doesn't, then Event Viewer would be unable to render those specific events, even if the application developer carefully preserved obsolete event methods in the source code. However, Event Viewer would still be able to display the event in XML format, including the names and values of parameters.

    If the IL Linker trims literals defined in the nested classes Channels, Keywords, Opcodes, or Tasks, then EventSource would be unable to find names for the enum values referenced by properties of EventAttribute. For example, ManifestBuilder.GetOpcodeName would fail. This would prevent Event Viewer from displaying those names (or the corresponding string resources), or would prevent EventSource from generating a manifest at all if EventManifestOptions.Strict is specified. Those concerns apply to the GetFields call here:

    foreach (FieldInfo staticField in nestedType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
    {
    AddProviderEnumKind(manifest, staticField, providerEnumKind);
    }

  15. KalleOlaviNiemitalo commented on Mar 11, 2021

    @KalleOlaviNiemitalo

    #49465 would apparently let the IL linker preserve the event methods and the nested types (such as Keywords).

    [DynamicallyAccessedMembers(
        DynamicallyAccessedMemberTypes.PublicMethods |
        DynamicallyAccessedMemberTypes.PublicNestedTypes)]
    public class EventSource

    Would the IL linker then also preserve the public literal fields of those public nested types, or would the developer still have to take care of that by adding more attributes or an XML linker configuration file?

    The EventSource documentation includes an example of how developers should define nested types in classes derived from EventSource, if they want to have custom keywords or channels in the manifest. Preferably, code like that should keep working.

  16. MichalStrehovsky commented on Mar 11, 2021

    @MichalStrehovsky
    Member

    NestedTypes currently only keeps the type in its hollowed out shape (doesn't keep any members).

    There's a discussion over at dotnet/linker#1174 (comment) to keep the nested types in their entirety. I think there's an agreement on doing it - someone just has to do it.

  17. eerhardt commented on May 18, 2021

    @eerhardt
    Member

    Can this issue be closed? I believe EventSource trimmability has been addressed.

  18. vitek-karas commented on May 19, 2021

    @vitek-karas
    MemberAuthor

    @LakshanF

    Can this issue be closed? I believe EventSource trimmability has been addressed.

  19. modified the milestones: 6.0.0, 7.0.0 on Jun 21, 2021
  20. LakshanF commented on Jul 28, 2021

    @LakshanF
    Contributor

    Closing this now since the above #56154 captures the work we did related to EventSource in .NET6 and the work needed in future (source generators for EventSource).

    Also, added # 56463 that is manifest based to the existing e2e smoke test we have currently.

  21. ghost locked as resolved and limited conversation to collaborators on Aug 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Tracing-coreclrfeature-requestlinkable-frameworktracking

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions