Add PreserveDependency to StackFrameHelper (#32023)
authorVitek Karas <vitek.karas@microsoft.com>
Mon, 17 Feb 2020 15:36:58 +0000 (07:36 -0800)
committerGitHub <noreply@github.com>
Mon, 17 Feb 2020 15:36:58 +0000 (07:36 -0800)
While linker can recognize the hardcoded type name in the InitializeSoruceInfo it sometimes fails to resolve the type as the System.Diagnostics.StackTrace is not directly referenced by most assemblies. This is partially an internal linker limitation and the attribute is a workaround.

That said linker would not be able to recognize the GetMethod or CreateInstance calls which follow and in member-level trimming mode would not correctly keep the GetSourceLineInfo nor the .ctor of the type.

src/coreclr/src/System.Private.CoreLib/src/System/Diagnostics/StackFrameHelper.cs

index 3c83ff6..20a0c57 100644 (file)
@@ -4,6 +4,7 @@
 
 using System.Threading;
 using System.Reflection;
+using System.Runtime.CompilerServices;
 
 namespace System.Diagnostics
 {
@@ -82,6 +83,13 @@ namespace System.Diagnostics
         // rgiLineNumber and rgiColumnNumber fields using the portable PDB reader if not already
         // done by GetStackFramesInternal (on Windows for old PDB format).
         //
+
+        // This is necessary because linker can't add new assemblies to the closure when recognizing Type.GetType
+        // so the code below is actually recognized by linker, but fails to resolve the type since the System.Diagnostics.StackTrace
+        // is not always part of the closure linker works on.
+        // PreserveDependencyAttribute on the other hand can pull in additional assemblies.
+        [PreserveDependency("GetSourceLineInfo", "System.Diagnostics.StackTraceSymbols", "System.Diagnostics.StackTrace")]
+        [PreserveDependency(".ctor()", "System.Diagnostics.StackTraceSymbols", "System.Diagnostics.StackTrace")]
         internal void InitializeSourceInfo(int iSkip, bool fNeedFileInfo, Exception? exception)
         {
             StackTrace.GetStackFramesInternal(this, iSkip, fNeedFileInfo, exception);