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

Reflection.Emit cleanups #67170

Merged
merged 6 commits into from
Apr 19, 2022
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
28 changes: 0 additions & 28 deletions src/coreclr/System.Private.CoreLib/src/System/Exception.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,34 +87,6 @@ public MethodBase? TargetSite
}
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "The API will return null if the metadata for current method cannot be established.")]
private string? CreateSourceName()
{
StackTrace st = new StackTrace(this, fNeedFileInfo: false);
if (st.FrameCount > 0)
{
StackFrame sf = st.GetFrame(0)!;
MethodBase? method = sf.GetMethod();
if (method == null)
return null;

Module module = method.Module;

if (!(module is RuntimeModule rtModule))
{
if (module is System.Reflection.Emit.ModuleBuilder moduleBuilder)
rtModule = moduleBuilder.InternalModule;
else
throw new ArgumentException(SR.Argument_MustBeRuntimeReflectionObject);
}

return rtModule.GetRuntimeAssembly().GetSimpleName();
}

return null;
}

// This method will clear the _stackTrace of the exception object upon deserialization
// to ensure that references from another AD/Process dont get accidentally used.
[OnDeserialized]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ namespace System.Reflection.Emit
{
public sealed partial class AssemblyBuilder : Assembly
{
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern RuntimeModule GetInMemoryAssemblyModule(RuntimeAssembly assembly);

#region Internal Data Members

internal readonly AssemblyBuilderAccess _access;
Expand All @@ -43,7 +40,7 @@ internal AssemblyBuilder(AssemblyName name!!,
AssemblyBuilderAccess access,
Assembly? callingAssembly,
AssemblyLoadContext? assemblyLoadContext,
IEnumerable<CustomAttributeBuilder>? unsafeAssemblyAttributes)
IEnumerable<CustomAttributeBuilder>? assemblyAttributes)
{
if (access != AssemblyBuilderAccess.Run && access != AssemblyBuilderAccess.RunAndCollect)
{
Expand All @@ -63,17 +60,6 @@ internal AssemblyBuilder(AssemblyName name!!,
// Clone the name in case the caller modifies it underneath us.
name = (AssemblyName)name.Clone();

// Scan the assembly level attributes for any attributes which modify how we create the
jkotas marked this conversation as resolved.
Show resolved Hide resolved
// assembly. Currently, we look for any attribute which modifies the security transparency
// of the assembly.
List<CustomAttributeBuilder>? assemblyAttributes = null;
if (unsafeAssemblyAttributes != null)
{
// Create a copy to ensure that it cannot be modified from another thread
// as it is used further below.
assemblyAttributes = new List<CustomAttributeBuilder>(unsafeAssemblyAttributes);
}

RuntimeAssembly? retAssembly = null;
CreateDynamicAssembly(ObjectHandleOnStack.Create(ref name),
(int)access,
Expand All @@ -85,12 +71,10 @@ internal AssemblyBuilder(AssemblyName name!!,

// Make sure that ManifestModule is properly initialized
// We need to do this before setting any CustomAttribute
RuntimeModule internalModule = (RuntimeModule)GetInMemoryAssemblyModule(InternalAssembly);

// Note that this ModuleBuilder cannot be used for RefEmit yet
// because it hasn't been initialized.
// However, it can be used to set the custom attribute on the Assembly
_manifestModuleBuilder = new ModuleBuilder(this, internalModule);
_manifestModuleBuilder = new ModuleBuilder(this, (RuntimeModule)InternalAssembly.ManifestModule);

if (assemblyAttributes != null)
{
Expand Down Expand Up @@ -143,7 +127,7 @@ internal static AssemblyBuilder InternalDefineDynamicAssembly(
AssemblyBuilderAccess access,
Assembly? callingAssembly,
AssemblyLoadContext? assemblyLoadContext,
IEnumerable<CustomAttributeBuilder>? unsafeAssemblyAttributes)
IEnumerable<CustomAttributeBuilder>? assemblyAttributes)
{
lock (s_assemblyBuilderLock)
{
Expand All @@ -152,7 +136,7 @@ internal static AssemblyBuilder InternalDefineDynamicAssembly(
access,
callingAssembly,
assemblyLoadContext,
unsafeAssemblyAttributes);
assemblyAttributes);
}
}
#endregion
Expand Down Expand Up @@ -202,48 +186,6 @@ internal void CheckTypeNameConflict(string strTypeName, TypeBuilder? enclosingTy
_manifestModuleBuilder.CheckTypeNameConflict(strTypeName, enclosingType);
}

internal static void CheckContext(params Type[]?[]? typess)
{
if (typess == null)
{
return;
}

foreach (Type[]? types in typess)
{
if (types != null)
{
CheckContext(types);
}
}
}

internal static void CheckContext(params Type?[]? types)
{
if (types == null)
{
return;
}

foreach (Type? type in types)
{
if (type == null)
{
continue;
}

if (type.Module == null || type.Module.Assembly == null)
{
throw new ArgumentException(SR.Argument_TypeNotValid);
}

if (type.Module.Assembly == typeof(object).Module.Assembly)
{
continue;
}
}
}

public override bool Equals(object? obj) => base.Equals(obj);

public override int GetHashCode() => base.GetHashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,11 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder)

public void SetBaseTypeConstraint([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? baseTypeConstraint)
{
AssemblyBuilder.CheckContext(baseTypeConstraint);
m_type.SetParent(baseTypeConstraint);
}

public void SetInterfaceConstraints(params Type[]? interfaceConstraints)
{
AssemblyBuilder.CheckContext(interfaceConstraints);
m_type.SetInterfaces(interfaceConstraints);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,15 +612,11 @@ private int GetTokenNoLock()

public void SetParameters(params Type[] parameterTypes)
{
AssemblyBuilder.CheckContext(parameterTypes);

SetSignature(null, null, null, parameterTypes, null, null);
}

public void SetReturnType(Type? returnType)
{
AssemblyBuilder.CheckContext(returnType);

SetSignature(returnType, null, null, null, null, null);
}

Expand All @@ -633,11 +629,6 @@ public void SetSignature(
if (m_token != 0)
return;

AssemblyBuilder.CheckContext(returnType);
AssemblyBuilder.CheckContext(returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes);
AssemblyBuilder.CheckContext(parameterTypeRequiredCustomModifiers);
AssemblyBuilder.CheckContext(parameterTypeOptionalCustomModifiers);

ThrowIfGeneric();

if (returnType != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace System.Reflection.Emit
{
// deliberately not [serializable]
public partial class ModuleBuilder : Module
{
internal static string UnmangleTypeName(string typeName)
Expand Down Expand Up @@ -748,74 +747,22 @@ public override MethodInfo[] GetMethods(BindingFlags bindingFlags)

#region Define Type

public TypeBuilder DefineType(string name)
{
lock (SyncRoot)
{
return DefineTypeNoLock(name, TypeAttributes.NotPublic, null, null, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize);
}
}

public TypeBuilder DefineType(string name, TypeAttributes attr)
{
lock (SyncRoot)
{
return DefineTypeNoLock(name, attr, null, null, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize);
}
}

public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent)
{
lock (SyncRoot)
{
AssemblyBuilder.CheckContext(parent);

return DefineTypeNoLock(name, attr, parent, null, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize);
}
}

public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, int typesize)
{
lock (SyncRoot)
{
return DefineTypeNoLock(name, attr, parent, null, PackingSize.Unspecified, typesize);
}
}

public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, PackingSize packingSize, int typesize)
{
lock (SyncRoot)
{
return DefineTypeNoLock(name, attr, parent, null, packingSize, typesize);
return new TypeBuilder(name, attr, parent, null, this, packingSize, typesize, null);
}
}

public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces)
{
lock (SyncRoot)
{
return DefineTypeNoLock(name, attr, parent, interfaces, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize);
return new TypeBuilder(name, attr, parent, interfaces, this, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize, null);
}
}

private TypeBuilder DefineTypeNoLock(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces, PackingSize packingSize, int typesize)
{
return new TypeBuilder(name, attr, parent, interfaces, this, packingSize, typesize, null);
}

public TypeBuilder DefineType(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, PackingSize packsize)
{
lock (SyncRoot)
{
return DefineTypeNoLock(name, attr, parent, packsize);
}
}

private TypeBuilder DefineTypeNoLock(string name, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, PackingSize packsize)
{
return new TypeBuilder(name, attr, parent, null, this, packsize, TypeBuilder.UnspecifiedTypeSize, null);
}

#endregion

#region Define Enum
Expand All @@ -824,10 +771,9 @@ private TypeBuilder DefineTypeNoLock(string name, TypeAttributes attr, [Dynamica
// Nested enum types can be defined manually using ModuleBuilder.DefineType.
public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type underlyingType)
{
AssemblyBuilder.CheckContext(underlyingType);
lock (SyncRoot)
{
EnumBuilder enumBuilder = DefineEnumNoLock(name, visibility, underlyingType);
EnumBuilder enumBuilder = new EnumBuilder(name, underlyingType, visibility, this);

// This enum is not generic, nested, and cannot have any element type.
// Replace the TypeBuilder object in _typeBuilderDict with this EnumBuilder object.
Expand All @@ -837,23 +783,10 @@ public EnumBuilder DefineEnum(string name, TypeAttributes visibility, Type under
}
}

private EnumBuilder DefineEnumNoLock(string name, TypeAttributes visibility, Type underlyingType)
{
return new EnumBuilder(name, underlyingType, visibility, this);
}

#endregion

#region Define Global Method

[RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")]
public MethodBuilder DefinePInvokeMethod(string name, string dllName, MethodAttributes attributes,
CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes,
CallingConvention nativeCallConv, CharSet nativeCharSet)
{
return DefinePInvokeMethod(name, dllName, name, attributes, callingConvention, returnType, parameterTypes, nativeCallConv, nativeCharSet);
}

[RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")]
public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes,
CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, CallingConvention nativeCallConv,
Expand All @@ -867,24 +800,10 @@ public MethodBuilder DefinePInvokeMethod(string name, string dllName, string ent
throw new ArgumentException(SR.Argument_GlobalFunctionHasToBeStatic);
}

AssemblyBuilder.CheckContext(returnType);
AssemblyBuilder.CheckContext(parameterTypes);

return _globalTypeBuilder.DefinePInvokeMethod(name, dllName, entryName, attributes, callingConvention, returnType, parameterTypes, nativeCallConv, nativeCharSet);
}
}

public MethodBuilder DefineGlobalMethod(string name, MethodAttributes attributes, Type? returnType, Type[]? parameterTypes)
{
return DefineGlobalMethod(name, attributes, CallingConventions.Standard, returnType, parameterTypes);
}

public MethodBuilder DefineGlobalMethod(string name, MethodAttributes attributes, CallingConventions callingConvention,
Type? returnType, Type[]? parameterTypes)
{
return DefineGlobalMethod(name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
}

public MethodBuilder DefineGlobalMethod(string name, MethodAttributes attributes, CallingConventions callingConvention,
Type? returnType, Type[]? requiredReturnTypeCustomModifiers, Type[]? optionalReturnTypeCustomModifiers,
Type[]? parameterTypes, Type[][]? requiredParameterTypeCustomModifiers, Type[][]? optionalParameterTypeCustomModifiers)
Expand All @@ -911,11 +830,6 @@ private MethodBuilder DefineGlobalMethodNoLock(string name, MethodAttributes att
throw new ArgumentException(SR.Argument_GlobalFunctionHasToBeStatic);
}

AssemblyBuilder.CheckContext(returnType);
AssemblyBuilder.CheckContext(requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers, parameterTypes);
AssemblyBuilder.CheckContext(requiredParameterTypeCustomModifiers);
AssemblyBuilder.CheckContext(optionalParameterTypeCustomModifiers);

return _globalTypeBuilder.DefineMethod(name, attributes, callingConvention,
returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers,
parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
Expand Down Expand Up @@ -1019,8 +933,6 @@ internal int GetTypeToken(Type type)

private int GetTypeTokenWorkerNoLock(Type type!!, bool getGenericDefinition)
{
AssemblyBuilder.CheckContext(type);

// Return a token for the class relative to the Module. Tokens
// are used to indentify objects when the objects are used in IL
// instructions. Tokens are always relative to the Module. For example,
Expand Down Expand Up @@ -1268,9 +1180,6 @@ private int GetArrayMethodTokenNoLock(Type arrayClass, string methodName, Callin
throw new ArgumentException(SR.Argument_HasToBeArrayClass);
}

AssemblyBuilder.CheckContext(returnType, arrayClass);
AssemblyBuilder.CheckContext(parameterTypes);

// Return a token for the MethodInfo for a method on an Array. This is primarily
// used to get the LoadElementAddress method.
SignatureHelper sigHelp = SignatureHelper.GetMethodSigHelper(
Expand All @@ -1286,9 +1195,6 @@ private int GetArrayMethodTokenNoLock(Type arrayClass, string methodName, Callin
public MethodInfo GetArrayMethod(Type arrayClass, string methodName, CallingConventions callingConvention,
Type? returnType, Type[]? parameterTypes)
{
AssemblyBuilder.CheckContext(returnType, arrayClass);
AssemblyBuilder.CheckContext(parameterTypes);

// GetArrayMethod is useful when you have an array of a type whose definition has not been completed and
// you want to access methods defined on Array. For example, you might define a type and want to define a
// method that takes an array of the type as a parameter. In order to access the elements of the array,
Expand Down
Loading