Skip to content

Commit 383a4e8

Browse files
Do not look for StackTraceHiddenAttribute if there's none in the app
1 parent f7fb923 commit 383a4e8

File tree

5 files changed

+76
-11
lines changed

5 files changed

+76
-11
lines changed

src/coreclr/nativeaot/System.Private.StackTraceMetadata/src/Internal/StackTraceMetadata/StackTraceMetadata.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.Reflection.Runtime.General;
8+
using System.Runtime.CompilerServices;
89

910
using Internal.Metadata.NativeFormat;
1011
using Internal.NativeFormat;
@@ -41,6 +42,10 @@ internal static void Initialize()
4142
RuntimeAugments.InitializeStackTraceMetadataSupport(new StackTraceMetadataCallbacksImpl());
4243
}
4344

45+
[Intrinsic]
46+
[AnalysisCharacteristic]
47+
internal static extern bool StackTraceHiddenMetadataPresent();
48+
4449
/// <summary>
4550
/// Locate the containing module for a method and try to resolve its name based on start address.
4651
/// </summary>
@@ -75,21 +80,24 @@ public static unsafe string GetMethodNameFromStartAddressIfAvailable(IntPtr meth
7580
out TypeDefinitionHandle typeHandle,
7681
out MethodHandle methodHandle))
7782
{
78-
foreach (CustomAttributeHandle cah in reader.GetTypeDefinition(typeHandle).CustomAttributes)
83+
if (StackTraceHiddenMetadataPresent())
7984
{
80-
if (cah.IsCustomAttributeOfType(reader, ["System", "Diagnostics"], "StackTraceHiddenAttribute"))
85+
foreach (CustomAttributeHandle cah in reader.GetTypeDefinition(typeHandle).CustomAttributes)
8186
{
82-
isStackTraceHidden = true;
83-
break;
87+
if (cah.IsCustomAttributeOfType(reader, ["System", "Diagnostics"], "StackTraceHiddenAttribute"))
88+
{
89+
isStackTraceHidden = true;
90+
break;
91+
}
8492
}
85-
}
8693

87-
foreach (CustomAttributeHandle cah in reader.GetMethod(methodHandle).CustomAttributes)
88-
{
89-
if (cah.IsCustomAttributeOfType(reader, ["System", "Diagnostics"], "StackTraceHiddenAttribute"))
94+
foreach (CustomAttributeHandle cah in reader.GetMethod(methodHandle).CustomAttributes)
9095
{
91-
isStackTraceHidden = true;
92-
break;
96+
if (cah.IsCustomAttributeOfType(reader, ["System", "Diagnostics"], "StackTraceHiddenAttribute"))
97+
{
98+
isStackTraceHidden = true;
99+
break;
100+
}
93101
}
94102
}
95103

src/coreclr/nativeaot/System.Private.StackTraceMetadata/src/System.Private.StackTraceMetadata.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@
2323
<Compile Include="$(CompilerCommonPath)\Internal\Runtime\StackTraceData.cs">
2424
<Link>Internal\Runtime\StackTraceData.cs</Link>
2525
</Compile>
26+
<Compile Include="..\..\System.Private.CoreLib\src\System\Runtime\CompilerServices\AnalysisCharacteristicAttribute.cs" />
27+
<Compile Include="..\..\Runtime.Base\src\System\Runtime\CompilerServices\IntrinsicAttribute.cs" />
2628
</ItemGroup>
2729
</Project>

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/MethodMetadataNode.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public MethodMetadataNode(MethodDesc method, bool isMinimal)
4040
public override IEnumerable<DependencyListEntry> GetStaticDependencies(NodeFactory factory)
4141
{
4242
DependencyList dependencies = new DependencyList();
43-
dependencies.Add(factory.TypeMetadata((MetadataType)_method.OwningType), "Owning type metadata");
43+
44+
var owningType = (MetadataType)_method.OwningType;
45+
dependencies.Add(factory.TypeMetadata(owningType), "Owning type metadata");
4446

4547
if (!_isMinimal)
4648
{
@@ -77,6 +79,12 @@ public override IEnumerable<DependencyListEntry> GetStaticDependencies(NodeFacto
7779
{
7880
GenericArgumentDataFlow.ProcessGenericArgumentDataFlow(ref dependencies, factory, new MessageOrigin(_method), parameterType, _method);
7981
}
82+
83+
if (_method.HasCustomAttribute("System.Diagnostics", "StackTraceHiddenAttribute")
84+
|| owningType.HasCustomAttribute("System.Diagnostics", "StackTraceHiddenAttribute"))
85+
{
86+
dependencies.Add(factory.AnalysisCharacteristic("StackTraceHiddenMetadataPresent"), "Method is StackTraceHidden");
87+
}
8088
}
8189

8290
return dependencies;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Reflection;
7+
8+
[Type]
9+
class Program
10+
{
11+
[Method]
12+
static int Main()
13+
{
14+
// Sanity check: we don't currently expect attributes on types to be optimized away
15+
if (GetTypeSecretly(nameof(TypeAttribute)) == null)
16+
throw new Exception("Type");
17+
18+
// Main should be reflection visible
19+
if (MethodBase.GetCurrentMethod().Name != nameof(Main))
20+
throw new Exception("Name");
21+
22+
// But we should have optimized out the attributes on it
23+
if (GetTypeSecretly(nameof(MethodAttribute)) != null)
24+
throw new Exception("Method");
25+
26+
return 100;
27+
}
28+
29+
[UnconditionalSuppressMessage("Trimming", "IL2057", Justification = "That's the point")]
30+
static Type GetTypeSecretly(string name) => Type.GetType(name);
31+
}
32+
33+
class MethodAttribute : Attribute;
34+
class TypeAttribute : Attribute;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<CLRTestPriority>0</CLRTestPriority>
5+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
6+
<RequiresProcessIsolation>true</RequiresProcessIsolation>
7+
<ReferenceXUnitWrapperGenerator>false</ReferenceXUnitWrapperGenerator>
8+
<Optimize>true</Optimize>
9+
</PropertyGroup>
10+
<ItemGroup>
11+
<Compile Include="AttributeTrimming.cs" />
12+
</ItemGroup>
13+
</Project>

0 commit comments

Comments
 (0)