Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add linker annotations to some of CoreLib #36532

Merged
merged 7 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -601,7 +602,9 @@ internal sealed class LicenseInteropProxy
// LicenseContext
private readonly MethodInfo _setSavedLicenseKey;

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
private readonly Type _licInfoHelper;

private readonly MethodInfo _licInfoHelperContains;

// RCW Activation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Buffers.Binary;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.SymbolStore;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -1114,7 +1115,7 @@ public virtual void MarkLabel(Label loc)
#endregion

#region IL Macros
public virtual void ThrowException(Type excType)
public virtual void ThrowException([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type excType)
{
// Emits the il to throw an exception

Expand All @@ -1136,6 +1137,7 @@ public virtual void ThrowException(Type excType)
Emit(OpCodes.Throw);
}

[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
private static Type GetConsoleType()
{
return Type.GetType("System.Console, System.Console", throwOnError: true)!;
Expand Down Expand Up @@ -1174,7 +1176,7 @@ public virtual void EmitWriteLine(LocalBuilder localBuilder)
throw new ArgumentException(SR.NotSupported_OutputStreamUsingTypeBuilder);
}
parameterTypes[0] = cls;
MethodInfo? mi = prop.ReturnType.GetMethod("WriteLine", parameterTypes);
MethodInfo? mi = typeof(System.IO.TextWriter).GetMethod("WriteLine", parameterTypes);
if (mi == null)
{
throw new ArgumentException(SR.Argument_EmitWriteLineType, nameof(localBuilder));
Expand Down Expand Up @@ -1214,7 +1216,7 @@ public virtual void EmitWriteLine(FieldInfo fld)
throw new NotSupportedException(SR.NotSupported_OutputStreamUsingTypeBuilder);
}
parameterTypes[0] = cls;
MethodInfo? mi = prop.ReturnType.GetMethod("WriteLine", parameterTypes);
MethodInfo? mi = typeof(System.IO.TextWriter).GetMethod("WriteLine", parameterTypes);
if (mi == null)
{
throw new ArgumentException(SR.Argument_EmitWriteLineType, nameof(fld));
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.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Globalization;
using System.Runtime.Loader;
Expand Down Expand Up @@ -83,7 +84,7 @@ public static partial class Activator
ref stackMark);
}

public static object? CreateInstance(Type type, bool nonPublic) =>
public static object? CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type, bool nonPublic) =>
MichalStrehovsky marked this conversation as resolved.
Show resolved Hide resolved
CreateInstance(type, nonPublic, wrapExceptions: true);

internal static object? CreateInstance(Type type, bool nonPublic, bool wrapExceptions)
Expand Down
9 changes: 5 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/Activator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Runtime.Remoting;
Expand All @@ -22,22 +23,22 @@ public static partial class Activator

[DebuggerHidden]
[DebuggerStepThrough]
public static object? CreateInstance(Type type, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture) =>
public static object? CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.PublicConstructors)] Type type, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture) =>
CreateInstance(type, bindingAttr, binder, args, culture, null);

[DebuggerHidden]
[DebuggerStepThrough]
public static object? CreateInstance(Type type, params object?[]? args) =>
public static object? CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type, params object?[]? args) =>
CreateInstance(type, ConstructorDefault, null, args, null, null);

[DebuggerHidden]
[DebuggerStepThrough]
public static object? CreateInstance(Type type, object?[]? args, object?[]? activationAttributes) =>
public static object? CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type, object?[]? args, object?[]? activationAttributes) =>
CreateInstance(type, ConstructorDefault, null, args, null, activationAttributes);

[DebuggerHidden]
[DebuggerStepThrough]
public static object? CreateInstance(Type type) =>
public static object? CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type type) =>
CreateInstance(type, nonPublic: false);

public static ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ public static string GetName(Type eventSourceType)
return GetName(eventSourceType, EventManifestOptions.None);
}

#if !ES_BUILD_STANDALONE
private const DynamicallyAccessedMemberTypes ManifestMemberTypes = DynamicallyAccessedMemberTypes.PublicNestedTypes
| DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods;
#endif

/// <summary>
/// Returns a string of the XML manifest associated with the eventSourceType. The scheme for this XML is
/// documented at in EventManifest Schema https://docs.microsoft.com/en-us/windows/desktop/WES/eventmanifestschema-schema.
Expand All @@ -347,7 +352,12 @@ public static string GetName(Type eventSourceType)
/// <param name="assemblyPathToIncludeInManifest">The manifest XML fragment contains the string name of the DLL name in
/// which it is embedded. This parameter specifies what name will be used</param>
/// <returns>The XML data string</returns>
public static string? GenerateManifest(Type eventSourceType, string? assemblyPathToIncludeInManifest)
public static string? GenerateManifest(
#if !ES_BUILD_STANDALONE
MichalStrehovsky marked this conversation as resolved.
Show resolved Hide resolved
[DynamicallyAccessedMembers(ManifestMemberTypes)]
#endif
Type eventSourceType,
string? assemblyPathToIncludeInManifest)
{
return GenerateManifest(eventSourceType, assemblyPathToIncludeInManifest, EventManifestOptions.None);
}
Expand All @@ -363,7 +373,13 @@ public static string GetName(Type eventSourceType)
/// <param name="flags">The flags to customize manifest generation. If flags has bit OnlyIfNeededForRegistration specified
/// this returns null when the eventSourceType does not require explicit registration</param>
/// <returns>The XML data string or null</returns>
public static string? GenerateManifest(Type eventSourceType, string? assemblyPathToIncludeInManifest, EventManifestOptions flags)
public static string? GenerateManifest(
#if !ES_BUILD_STANDALONE
[DynamicallyAccessedMembers(ManifestMemberTypes)]
#endif
Type eventSourceType,
string? assemblyPathToIncludeInManifest,
EventManifestOptions flags)
{
if (eventSourceType == null)
throw new ArgumentNullException(nameof(eventSourceType));
Expand Down Expand Up @@ -3056,7 +3072,13 @@ internal static Attribute GetCustomAttributeHelper(Type type, Type attributeType

// Helper to deal with the fact that the type we are reflecting over might be loaded in the ReflectionOnly context.
// When that is the case, we have the build the custom assemblies on a member by hand.
internal static Attribute? GetCustomAttributeHelper(MemberInfo member, Type attributeType, EventManifestOptions flags = EventManifestOptions.None)
internal static Attribute? GetCustomAttributeHelper(
MemberInfo member,
#if !ES_BUILD_STANDALONE
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties)]
eerhardt marked this conversation as resolved.
Show resolved Hide resolved
#endif
Type attributeType,
EventManifestOptions flags = EventManifestOptions.None)
{
#if !ES_BUILD_PN
// On ProjectN, ReflectionOnly() always equals false. AllowEventSourceOverride is an option that allows either Microsoft.Diagnostics.Tracing or
Expand Down Expand Up @@ -3094,11 +3116,9 @@ internal static Attribute GetCustomAttributeHelper(Type type, Type attributeType

if (attr != null)
{
Type t = attr.GetType();

foreach (CustomAttributeNamedArgument namedArgument in data.NamedArguments)
{
PropertyInfo p = t.GetProperty(namedArgument.MemberInfo.Name, BindingFlags.Public | BindingFlags.Instance)!;
PropertyInfo p = attributeType.GetProperty(namedArgument.MemberInfo.Name, BindingFlags.Public | BindingFlags.Instance)!;
vitek-karas marked this conversation as resolved.
Show resolved Hide resolved
object value = namedArgument.TypedValue.Value!;

if (p.PropertyType.IsEnum)
Expand Down Expand Up @@ -3185,7 +3205,13 @@ private static bool AttributeTypeNamesMatch(Type attributeType, Type reflectedAt
// return the UTF8 bytes. It also sets up the code:EventData structures needed to dispatch events
// at run time. 'source' is the event source to place the descriptors. If it is null,
// then the descriptors are not creaed, and just the manifest is generated.
private static byte[]? CreateManifestAndDescriptors(Type eventSourceType, string? eventSourceDllName, EventSource? source,
private static byte[]? CreateManifestAndDescriptors(
#if !ES_BUILD_STANDALONE
[DynamicallyAccessedMembers(ManifestMemberTypes)]
#endif
Type eventSourceType,
string? eventSourceDllName,
EventSource? source,
EventManifestOptions flags = EventManifestOptions.None)
{
ManifestBuilder? manifest = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Globalization;
using System.Collections.Generic;
using System.Configuration.Assemblies;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
using System.Security;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -106,8 +107,13 @@ public virtual Type[] GetTypes()

public virtual string EscapedCodeBase => AssemblyName.EscapeCodeBase(CodeBase);

[RequiresUnreferencedCode("Assembly.CreateInstance is not supported with trimming. Use Type.GetType instead.")]
public object? CreateInstance(string typeName) => CreateInstance(typeName, false, BindingFlags.Public | BindingFlags.Instance, binder: null, args: null, culture: null, activationAttributes: null);

[RequiresUnreferencedCode("Assembly.CreateInstance is not supported with trimming. Use Type.GetType instead.")]
public object? CreateInstance(string typeName, bool ignoreCase) => CreateInstance(typeName, ignoreCase, BindingFlags.Public | BindingFlags.Instance, binder: null, args: null, culture: null, activationAttributes: null);

[RequiresUnreferencedCode("Assembly.CreateInstance is not supported with trimming. Use Type.GetType instead.")]
public virtual object? CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object[]? args, CultureInfo? culture, object[]? activationAttributes)
{
Type? t = GetType(typeName, throwOnError: false, ignoreCase: ignoreCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace System.Reflection
{
public static partial class RuntimeReflectionExtensions
{
private const BindingFlags Everything = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;

public static IEnumerable<FieldInfo> GetRuntimeFields(this Type type)
public static IEnumerable<FieldInfo> GetRuntimeFields(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
this Type type)
{
if (type == null)
{
Expand All @@ -19,7 +22,9 @@ public static IEnumerable<FieldInfo> GetRuntimeFields(this Type type)
return type.GetFields(Everything);
}

public static IEnumerable<MethodInfo> GetRuntimeMethods(this Type type)
public static IEnumerable<MethodInfo> GetRuntimeMethods(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
this Type type)
{
if (type == null)
{
Expand All @@ -28,7 +33,9 @@ public static IEnumerable<MethodInfo> GetRuntimeMethods(this Type type)
return type.GetMethods(Everything);
}

public static IEnumerable<PropertyInfo> GetRuntimeProperties(this Type type)
public static IEnumerable<PropertyInfo> GetRuntimeProperties(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
this Type type)
{
if (type == null)
{
Expand All @@ -37,7 +44,9 @@ public static IEnumerable<PropertyInfo> GetRuntimeProperties(this Type type)
return type.GetProperties(Everything);
}

public static IEnumerable<EventInfo> GetRuntimeEvents(this Type type)
public static IEnumerable<EventInfo> GetRuntimeEvents(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
this Type type)
{
if (type == null)
{
Expand All @@ -46,7 +55,9 @@ public static IEnumerable<EventInfo> GetRuntimeEvents(this Type type)
return type.GetEvents(Everything);
}

public static FieldInfo? GetRuntimeField(this Type type, string name)
public static FieldInfo? GetRuntimeField(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
this Type type, string name)
{
if (type == null)
{
Expand All @@ -55,7 +66,9 @@ public static IEnumerable<EventInfo> GetRuntimeEvents(this Type type)
return type.GetField(name);
}

public static MethodInfo? GetRuntimeMethod(this Type type, string name, Type[] parameters)
public static MethodInfo? GetRuntimeMethod(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
this Type type, string name, Type[] parameters)
{
if (type == null)
{
Expand All @@ -64,7 +77,9 @@ public static IEnumerable<EventInfo> GetRuntimeEvents(this Type type)
return type.GetMethod(name, parameters);
}

public static PropertyInfo? GetRuntimeProperty(this Type type, string name)
public static PropertyInfo? GetRuntimeProperty(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
this Type type, string name)
{
if (type == null)
{
Expand All @@ -73,7 +88,9 @@ public static IEnumerable<EventInfo> GetRuntimeEvents(this Type type)
return type.GetProperty(name);
}

public static EventInfo? GetRuntimeEvent(this Type type, string name)
public static EventInfo? GetRuntimeEvent(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)]
this Type type, string name)
{
if (type == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo)
return IsAssignableFrom(typeInfo.AsType());
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
protected Type typeImpl = null!;

protected TypeDelegator() { }

public TypeDelegator(Type delegatingType)
// NOTE: delegatingType is marked as DynamicallyAccessedMemberTypes.All, but analysis tools special case
// calls to this constructor and propagate the existing dataflow metadata from delegatingType to this
// TypeDelegator. The only purpose of the annotation here is to avoid dataflow warnings _within_ this type.
public TypeDelegator([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type delegatingType)
MichalStrehovsky marked this conversation as resolved.
Show resolved Hide resolved
{
if (delegatingType is null)
throw new ArgumentNullException(nameof(delegatingType));
Expand Down