Skip to content

Commit

Permalink
Suppresses the trimmer warning on TypeAnalysis ctor (#48823)
Browse files Browse the repository at this point in the history
* eh fix

* test change that inadvertently got checked in earlier

* Suppresses the trimmer warning on TypeAnalysis ctor

* Incorporating FB

* Update src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs

Co-authored-by: Eric Erhardt <[email protected]>

* Update src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs

Co-authored-by: Eric Erhardt <[email protected]>

* Update src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs

Co-authored-by: Eric Erhardt <[email protected]>

* Fix DynamicDependency as per PR feedback

* an earlier change got reverted

* fixed proj file netcore app condition check

* fixed NETCORE_ENGINEERING_TELEMETRY build failures

* fixeing another NETCORE_ENGINEERING_TELEMETRY build failures

* Adding RequiresUnreferencedCode to TypeAnalysis ctor instead of suppressing the warning to get FB, not fully fixed

* PR FB and suppressing warnings for safe calls

* propagated the warning all the way up

* CI build break fix for one file

* excluding NativeRTEventSrc from being build in a project

* Missed couple of supppressions on NativeRTEventSrc

* build break fixes

Co-authored-by: Eric Erhardt <[email protected]>
  • Loading branch information
LakshanF and eerhardt authored Mar 8, 2021
1 parent ecc354a commit 07ea61f
Show file tree
Hide file tree
Showing 34 changed files with 471 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#if NET46
Expand Down Expand Up @@ -43,6 +44,10 @@ namespace System.Net
#endif
internal sealed partial class NetEventSource : EventSource
{
#if !ES_BUILD_STANDALONE
private const string EventSourceSuppressMessage = "Parameters to this method are primitive and are trimmer safe";
#endif

/// <summary>The single event source instance to use for all logging.</summary>
public static readonly NetEventSource Log = new NetEventSource();

Expand Down Expand Up @@ -373,6 +378,10 @@ private static string Format(FormattableString s)

#region Custom WriteEvent overloads

#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
Justification = EventSourceSuppressMessage)]
#endif
[NonEvent]
private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string? arg3, string? arg4)
{
Expand Down Expand Up @@ -417,6 +426,10 @@ private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string?
}
}

#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
Justification = EventSourceSuppressMessage)]
#endif
[NonEvent]
private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, byte[]? arg3)
{
Expand Down Expand Up @@ -460,6 +473,10 @@ private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, byte[]?
}
}

#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
Justification = EventSourceSuppressMessage)]
#endif
[NonEvent]
private unsafe void WriteEvent(int eventId, string? arg1, int arg2, int arg3, int arg4)
{
Expand Down Expand Up @@ -498,6 +515,10 @@ private unsafe void WriteEvent(int eventId, string? arg1, int arg2, int arg3, in
}
}

#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
Justification = EventSourceSuppressMessage)]
#endif
[NonEvent]
private unsafe void WriteEvent(int eventId, string? arg1, int arg2, string? arg3)
{
Expand Down Expand Up @@ -533,6 +554,10 @@ private unsafe void WriteEvent(int eventId, string? arg1, int arg2, string? arg3
}
}

#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
Justification = EventSourceSuppressMessage)]
#endif
[NonEvent]
private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, int arg3)
{
Expand Down Expand Up @@ -568,6 +593,10 @@ private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, int arg3
}
}

#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
Justification = EventSourceSuppressMessage)]
#endif
[NonEvent]
private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string? arg3, int arg4)
{
Expand Down Expand Up @@ -610,6 +639,10 @@ private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string?
}
}

#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
Justification = EventSourceSuppressMessage)]
#endif
[NonEvent]
private unsafe void WriteEvent(int eventId, string arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8)
{
Expand Down Expand Up @@ -668,6 +701,10 @@ private unsafe void WriteEvent(int eventId, string arg1, int arg2, int arg3, int
}
}

#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
Justification = EventSourceSuppressMessage)]
#endif
[NonEvent]
private unsafe void WriteEvent(int eventId, string arg1, string arg2, int arg3, int arg4, int arg5)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
using System.Net.Security;

namespace System.Net
{
internal sealed partial class NetEventSource
{
#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "parameter intent is an enum and is trimmer safe")]
#endif
[Event(AcquireDefaultCredentialId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
public void AcquireDefaultCredential(string packageName, Interop.SspiCli.CredentialUse intent)
{
Expand Down Expand Up @@ -59,6 +64,10 @@ public void AcceptSecurityContext(SafeFreeCredentials? credential, SafeDeleteCon
private void AcceptSecurityContext(string credential, string context, Interop.SspiCli.ContextFlags inFlags) =>
WriteEvent(AcceptSecuritContextId, credential, context, (int)inFlags);

#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "parameter errorCode is an enum and is trimmer safe")]
#endif
[Event(OperationReturnedSomethingId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
public void OperationReturnedSomething(string operation, Interop.SECURITY_STATUS errorCode)
{
Expand All @@ -68,6 +77,10 @@ public void OperationReturnedSomething(string operation, Interop.SECURITY_STATUS
}
}

#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "parameter errorCode is an enum and is trimmer safe")]
#endif
[Event(SecurityContextInputBufferId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
public void SecurityContextInputBuffer(string context, int inputBufferSize, int outputBufferSize, Interop.SECURITY_STATUS errorCode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<ItemGroup Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) != '.NETCoreApp'">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicDependencyAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard1.1'">
<Compile Include="$(CommonPath)System\HexConverter.cs" Link="Common\System\HexConverter.cs" />
Expand Down Expand Up @@ -105,4 +106,4 @@
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard1.1' and '$(TargetFramework)' != '$(NetCoreAppCurrent)'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.CompilerServices.Unsafe\src\System.Runtime.CompilerServices.Unsafe.ilproj" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ public void Message(string? Message)
/// <summary>
/// Events from DiagnosticSource can be forwarded to EventSource using this event.
/// </summary>
#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Arguments parameter is trimmer safe")]
#endif
[Event(2, Keywords = Keywords.Events)]
private void Event(string SourceName, string EventName, IEnumerable<KeyValuePair<string, string?>>? Arguments)
{
Expand All @@ -243,6 +247,10 @@ private void EventJson(string SourceName, string EventName, string ArgmentsJson)
/// <summary>
/// Used to mark the beginning of an activity
/// </summary>
#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Arguments parameter is trimmer safe")]
#endif
[Event(4, Keywords = Keywords.Events)]
private void Activity1Start(string SourceName, string EventName, IEnumerable<KeyValuePair<string, string?>> Arguments)
{
Expand All @@ -252,6 +260,10 @@ private void Activity1Start(string SourceName, string EventName, IEnumerable<Key
/// <summary>
/// Used to mark the end of an activity
/// </summary>
#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Arguments parameter is trimmer safe")]
#endif
[Event(5, Keywords = Keywords.Events)]
private void Activity1Stop(string SourceName, string EventName, IEnumerable<KeyValuePair<string, string?>> Arguments)
{
Expand All @@ -261,6 +273,10 @@ private void Activity1Stop(string SourceName, string EventName, IEnumerable<KeyV
/// <summary>
/// Used to mark the beginning of an activity
/// </summary>
#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Arguments parameter is trimmer safe")]
#endif
[Event(6, Keywords = Keywords.Events)]
private void Activity2Start(string SourceName, string EventName, IEnumerable<KeyValuePair<string, string?>> Arguments)
{
Expand All @@ -270,6 +286,10 @@ private void Activity2Start(string SourceName, string EventName, IEnumerable<Key
/// <summary>
/// Used to mark the end of an activity that can be recursive.
/// </summary>
#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Arguments parameter is trimmer safe")]
#endif
[Event(7, Keywords = Keywords.Events)]
private void Activity2Stop(string SourceName, string EventName, IEnumerable<KeyValuePair<string, string?>> Arguments)
{
Expand All @@ -279,6 +299,10 @@ private void Activity2Stop(string SourceName, string EventName, IEnumerable<KeyV
/// <summary>
/// Used to mark the beginning of an activity
/// </summary>
#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Arguments parameter is trimmer safe")]
#endif
[Event(8, Keywords = Keywords.Events, ActivityOptions = EventActivityOptions.Recursive)]
private void RecursiveActivity1Start(string SourceName, string EventName, IEnumerable<KeyValuePair<string, string?>> Arguments)
{
Expand All @@ -288,6 +312,10 @@ private void RecursiveActivity1Start(string SourceName, string EventName, IEnume
/// <summary>
/// Used to mark the end of an activity that can be recursive.
/// </summary>
#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Arguments parameter is trimmer safe")]
#endif
[Event(9, Keywords = Keywords.Events, ActivityOptions = EventActivityOptions.Recursive)]
private void RecursiveActivity1Stop(string SourceName, string EventName, IEnumerable<KeyValuePair<string, string?>> Arguments)
{
Expand All @@ -311,6 +339,10 @@ private void NewDiagnosticListener(string SourceName)
/// <param name="SourceName">The ActivitySource name</param>
/// <param name="ActivityName">The Activity name</param>
/// <param name="Arguments">Name and value pairs of the Activity properties</param>
#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Arguments parameter is trimmer safe")]
#endif
#if NO_EVENTSOURCE_COMPLEX_TYPE_SUPPORT
[Event(11, Keywords = Keywords.Events)]
#else
Expand All @@ -325,6 +357,10 @@ private void ActivityStart(string SourceName, string ActivityName, IEnumerable<K
/// <param name="SourceName">The ActivitySource name</param>
/// <param name="ActivityName">The Activity name</param>
/// <param name="Arguments">Name and value pairs of the Activity properties</param>
#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Arguments parameter is trimmer safe")]
#endif
#if NO_EVENTSOURCE_COMPLEX_TYPE_SUPPORT
[Event(12, Keywords = Keywords.Events)]
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,29 @@ protected void WriteEvent(int eventId, long arg1, byte[]? arg2) { }
protected void WriteEvent(int eventId, long arg1, long arg2) { }
protected void WriteEvent(int eventId, long arg1, long arg2, long arg3) { }
protected void WriteEvent(int eventId, long arg1, string? arg2) { }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. This can be suppressed if the object is a primitive type")]
protected void WriteEvent(int eventId, params object?[] args) { }
protected void WriteEvent(int eventId, string? arg1) { }
protected void WriteEvent(int eventId, string? arg1, int arg2) { }
protected void WriteEvent(int eventId, string? arg1, int arg2, int arg3) { }
protected void WriteEvent(int eventId, string? arg1, long arg2) { }
protected void WriteEvent(int eventId, string? arg1, string? arg2) { }
protected void WriteEvent(int eventId, string? arg1, string? arg2, string? arg3) { }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. This can be suppressed if the object is a primitive type")]
[System.CLSCompliantAttribute(false)]
protected unsafe void WriteEventCore(int eventId, int eventDataCount, System.Diagnostics.Tracing.EventSource.EventData* data) { }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. This can be suppressed if the object is a primitive type")]
protected void WriteEventWithRelatedActivityId(int eventId, System.Guid relatedActivityId, params object?[] args) { }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. This can be suppressed if the object is a primitive type")]
[System.CLSCompliantAttribute(false)]
protected unsafe void WriteEventWithRelatedActivityIdCore(int eventId, System.Guid* relatedActivityId, int eventDataCount, System.Diagnostics.Tracing.EventSource.EventData* data) { }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. This can be suppressed if the object is a primitive type")]
public void Write<T>(string? eventName, System.Diagnostics.Tracing.EventSourceOptions options, T data) { }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. This can be suppressed if the object is a primitive type")]
public void Write<T>(string? eventName, ref System.Diagnostics.Tracing.EventSourceOptions options, ref System.Guid activityId, ref System.Guid relatedActivityId, ref T data) { }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. This can be suppressed if the object is a primitive type")]
public void Write<T>(string? eventName, ref System.Diagnostics.Tracing.EventSourceOptions options, ref T data) { }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. This can be suppressed if the object is a primitive type")]
public void Write<T>(string? eventName, T data) { }
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
protected internal partial struct EventData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<!-- Ensure Assemblies are first resolved via targeting pack when targeting net461 -->
<AssemblySearchPaths Condition="'$(TargetFramework)' == 'net461'">$(NuGetPackageRoot)\microsoft.targetingpack.netframework.v4.6.1\1.0.1\lib\net461\;$(AssemblySearchPaths)</AssemblySearchPaths>
</PropertyGroup>
<ItemGroup Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) != '.NETCoreApp'">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true'" >
<Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs"
Link="Common\Interop\Windows\Interop.Libraries.cs" />
Expand Down
12 changes: 12 additions & 0 deletions src/libraries/System.Net.Http/src/System/Net/Http/HttpTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public void Http20ConnectionClosed()
ConnectionClosed(versionMajor: 2, versionMinor: 0);
}

#if !ES_BUILD_STANDALONE
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Parameters to this method are primitive and are trimmer safe")]
#endif
[NonEvent]
private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, int arg3, string? arg4, byte arg5, byte arg6, HttpVersionPolicy arg7)
{
Expand Down Expand Up @@ -215,6 +219,10 @@ private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, int arg3
}
}

#if !ES_BUILD_STANDALONE
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Parameters to this method are primitive and are trimmer safe")]
#endif
[NonEvent]
private unsafe void WriteEvent(int eventId, double arg1, byte arg2, byte arg3)
{
Expand Down Expand Up @@ -243,6 +251,10 @@ private unsafe void WriteEvent(int eventId, double arg1, byte arg2, byte arg3)
}
}

#if !ES_BUILD_STANDALONE
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Parameters to this method are primitive and are trimmer safe")]
#endif
[NonEvent]
private unsafe void WriteEvent(int eventId, byte arg1, byte arg2)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;

namespace System.Net
Expand Down Expand Up @@ -73,6 +74,10 @@ public static void AuthenticationError(Uri? uri, string message)
public void AuthenticationError(string? uri, string message) =>
WriteEvent(AuthenticationErrorId, uri, message);

#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
Justification = "Parameters to this method are primitive and are trimmer safe")]
#endif
[NonEvent]
private unsafe void WriteEvent(int eventId, int arg1, int arg2, int arg3, string? arg4, string? arg5)
{
Expand Down
Loading

0 comments on commit 07ea61f

Please sign in to comment.