internal CustomAttributeData(Module scope, CustomAttributeRecord caRecord) { this.m_scope = scope; this.m_ctor = (ConstructorInfo) RuntimeType.GetMethodBase(scope, (int) caRecord.tkCtor); ParameterInfo[] parametersNoCopy = this.m_ctor.GetParametersNoCopy(); this.m_ctorParams = new CustomAttributeCtorParameter[parametersNoCopy.Length]; for (int i = 0; i < parametersNoCopy.Length; i++) { this.m_ctorParams[i] = new CustomAttributeCtorParameter(InitCustomAttributeType(parametersNoCopy[i].ParameterType, scope)); } FieldInfo[] fields = this.m_ctor.DeclaringType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); PropertyInfo[] properties = this.m_ctor.DeclaringType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); this.m_namedParams = new CustomAttributeNamedParameter[properties.Length + fields.Length]; for (int j = 0; j < fields.Length; j++) { this.m_namedParams[j] = new CustomAttributeNamedParameter(fields[j].Name, CustomAttributeEncoding.Field, InitCustomAttributeType(fields[j].FieldType, scope)); } for (int k = 0; k < properties.Length; k++) { this.m_namedParams[k + fields.Length] = new CustomAttributeNamedParameter(properties[k].Name, CustomAttributeEncoding.Property, InitCustomAttributeType(properties[k].PropertyType, scope)); } this.m_members = new MemberInfo[fields.Length + properties.Length]; fields.CopyTo(this.m_members, 0); properties.CopyTo(this.m_members, fields.Length); CustomAttributeEncodedArgument.ParseAttributeArguments(caRecord.blob, ref this.m_ctorParams, ref this.m_namedParams, this.m_scope); }
private static bool IsCustomAttributeDefined(RuntimeModule decoratedModule, int decoratedMetadataToken, RuntimeType attributeFilterType, bool mustBeInheritable) { if (decoratedModule.Assembly.ReflectionOnly) { throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyCA")); } MetadataImport metadataImport = decoratedModule.MetadataImport; CustomAttributeRecord[] customAttributeRecords = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken); Assembly lastAptcaOkAssembly = null; for (int i = 0; i < customAttributeRecords.Length; i++) { RuntimeType type; IRuntimeMethodInfo info; bool flag; bool flag2; CustomAttributeRecord caRecord = customAttributeRecords[i]; if (FilterCustomAttributeRecord(caRecord, metadataImport, ref lastAptcaOkAssembly, decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, null, null, out type, out info, out flag, out flag2)) { return(true); } } return(false); }
private CustomAttributeData(RuntimeModule scope, CustomAttributeRecord caRecord) { this.m_scope = scope; this.m_ctor = (RuntimeConstructorInfo)RuntimeType.GetMethodBase(scope, caRecord.tkCtor); ParameterInfo[] parametersNoCopy = this.m_ctor.GetParametersNoCopy(); this.m_ctorParams = new CustomAttributeCtorParameter[parametersNoCopy.Length]; for (int i = 0; i < parametersNoCopy.Length; i++) { this.m_ctorParams[i] = new CustomAttributeCtorParameter(CustomAttributeData.InitCustomAttributeType((RuntimeType)parametersNoCopy[i].ParameterType)); } FieldInfo[] fields = this.m_ctor.DeclaringType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); PropertyInfo[] properties = this.m_ctor.DeclaringType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); this.m_namedParams = new CustomAttributeNamedParameter[properties.Length + fields.Length]; for (int j = 0; j < fields.Length; j++) { this.m_namedParams[j] = new CustomAttributeNamedParameter(fields[j].Name, CustomAttributeEncoding.Field, CustomAttributeData.InitCustomAttributeType((RuntimeType)fields[j].FieldType)); } for (int k = 0; k < properties.Length; k++) { this.m_namedParams[k + fields.Length] = new CustomAttributeNamedParameter(properties[k].Name, CustomAttributeEncoding.Property, CustomAttributeData.InitCustomAttributeType((RuntimeType)properties[k].PropertyType)); } this.m_members = new MemberInfo[fields.Length + properties.Length]; fields.CopyTo(this.m_members, 0); properties.CopyTo(this.m_members, fields.Length); CustomAttributeEncodedArgument.ParseAttributeArguments(caRecord.blob, ref this.m_ctorParams, ref this.m_namedParams, this.m_scope); }
internal static AttributeUsageAttribute GetAttributeUsage(RuntimeType decoratedAttribute) { RuntimeModule runtimeModule = decoratedAttribute.GetRuntimeModule(); MetadataImport metadataImport = runtimeModule.MetadataImport; CustomAttributeRecord[] customAttributeRecords = CustomAttributeData.GetCustomAttributeRecords(runtimeModule, decoratedAttribute.MetadataToken); AttributeUsageAttribute attribute = null; for (int i = 0; i < customAttributeRecords.Length; i++) { CustomAttributeRecord record = customAttributeRecords[i]; RuntimeType type = runtimeModule.ResolveType(metadataImport.GetParentToken((int)record.tkCtor), null, null) as RuntimeType; if (type == ((RuntimeType)typeof(AttributeUsageAttribute))) { AttributeTargets targets; bool flag; bool flag2; if (attribute != null) { throw new FormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Format_AttributeUsage"), new object[] { type })); } ParseAttributeUsageAttribute(record.blob, out targets, out flag, out flag2); attribute = new AttributeUsageAttribute(targets, flag2, flag); } } if (attribute == null) { return(AttributeUsageAttribute.Default); } return(attribute); }
internal static CustomAttributeRecord[] GetCustomAttributeRecords(RuntimeModule module, int targetToken) { MetadataImport metadataImport = module.MetadataImport; MetadataEnumResult metadataEnumResult; metadataImport.EnumCustomAttributes(targetToken, out metadataEnumResult); CustomAttributeRecord[] array = new CustomAttributeRecord[metadataEnumResult.Length]; for (int i = 0; i < array.Length; i++) { metadataImport.GetCustomAttributeProps(metadataEnumResult[i], out array[i].tkCtor.Value, out array[i].blob); } return(array); }
internal static CustomAttributeRecord[] GetCustomAttributeRecords(RuntimeModule module, int targetToken) { MetadataImport metadataImport = module.MetadataImport; MetadataEnumResult result; metadataImport.EnumCustomAttributes(targetToken, out result); CustomAttributeRecord[] customAttributeRecordArray = new CustomAttributeRecord[result.Length]; for (int index = 0; index < customAttributeRecordArray.Length; ++index) { metadataImport.GetCustomAttributeProps(result[index], out customAttributeRecordArray[index].tkCtor.Value, out customAttributeRecordArray[index].blob); } return(customAttributeRecordArray); }
internal static unsafe CustomAttributeRecord[] GetCustomAttributeRecords(RuntimeModule module, int targetToken) { MetadataImport metadataImport = module.MetadataImport; int count = metadataImport.EnumCustomAttributesCount(targetToken); int * result = (int *)stackalloc byte[(((IntPtr)count) * 4)]; metadataImport.EnumCustomAttributes(targetToken, result, count); CustomAttributeRecord[] recordArray = new CustomAttributeRecord[count]; for (int i = 0; i < count; i++) { metadataImport.GetCustomAttributeProps(result[i], out recordArray[i].tkCtor.Value, out recordArray[i].blob); } return(recordArray); }
private static unsafe bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg) { ctor = (IRuntimeMethodInfo)null; attributeType = (RuntimeType)null; ctorHasParameters = false; isVarArg = false; IntPtr num = (IntPtr)((void *)((IntPtr)(void *)caRecord.blob.Signature + caRecord.blob.Length)); attributeType = decoratedModule.ResolveType(scope.GetParentToken((int)caRecord.tkCtor), (Type[])null, (Type[])null) as RuntimeType; if (!attributeFilterType.IsAssignableFrom((TypeInfo)attributeType) || !CustomAttribute.AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes) || (attributeType.Attributes & TypeAttributes.WindowsRuntime) == TypeAttributes.WindowsRuntime) { return(false); } RuntimeAssembly targetAssembly = (RuntimeAssembly)attributeType.Assembly; RuntimeAssembly sourceAssembly = (RuntimeAssembly)decoratedModule.Assembly; if ((Assembly)targetAssembly != lastAptcaOkAssembly && !RuntimeAssembly.AptcaCheck(targetAssembly, sourceAssembly)) { return(false); } lastAptcaOkAssembly = (Assembly)sourceAssembly; ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor); isVarArg = ((uint)methodSignature[0] & 5U) > 0U; ctorHasParameters = (uint)methodSignature[1] > 0U; RuntimeTypeHandle runtimeTypeHandle1; if (ctorHasParameters) { ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), (int)caRecord.tkCtor); } else { // ISSUE: explicit reference operation // ISSUE: variable of a reference type IRuntimeMethodInfo& local = @ctor; runtimeTypeHandle1 = attributeType.GetTypeHandleInternal(); IRuntimeMethodInfo defaultConstructor = runtimeTypeHandle1.GetDefaultConstructor();
internal static unsafe CustomAttributeRecord[] GetCustomAttributeRecords(Module module, int targetToken) { MetadataImport metadataImport = module.MetadataImport; int count = metadataImport.EnumCustomAttributesCount(targetToken); int* result = stackalloc int[count]; metadataImport.EnumCustomAttributes(targetToken, result, count); CustomAttributeRecord[] recordArray = new CustomAttributeRecord[count]; for (int i = 0; i < count; i++) { metadataImport.GetCustomAttributeProps(result[i], out recordArray[i].tkCtor.Value, out recordArray[i].blob); } return recordArray; }
[System.Security.SecurityCritical] // auto-generated internal unsafe static CustomAttributeRecord[] GetCustomAttributeRecords(RuntimeModule module, int targetToken) { MetadataImport scope = module.MetadataImport; MetadataEnumResult tkCustomAttributeTokens; scope.EnumCustomAttributes(targetToken, out tkCustomAttributeTokens); CustomAttributeRecord[] records = new CustomAttributeRecord[tkCustomAttributeTokens.Length]; for (int i = 0; i < records.Length; i++) { scope.GetCustomAttributeProps( tkCustomAttributeTokens[i], out records[i].tkCtor.Value, out records[i].blob); } return records; }
[System.Security.SecuritySafeCritical] // auto-generated private CustomAttributeData(RuntimeModule scope, CustomAttributeRecord caRecord) { m_scope = scope; m_ctor = (RuntimeConstructorInfo)RuntimeType.GetMethodBase(scope, caRecord.tkCtor); ParameterInfo[] parameters = m_ctor.GetParametersNoCopy(); m_ctorParams = new CustomAttributeCtorParameter[parameters.Length]; for (int i = 0; i < parameters.Length; i++) m_ctorParams[i] = new CustomAttributeCtorParameter(InitCustomAttributeType((RuntimeType)parameters[i].ParameterType)); FieldInfo[] fields = m_ctor.DeclaringType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); PropertyInfo[] properties = m_ctor.DeclaringType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); m_namedParams = new CustomAttributeNamedParameter[properties.Length + fields.Length]; for (int i = 0; i < fields.Length; i++) m_namedParams[i] = new CustomAttributeNamedParameter( fields[i].Name, CustomAttributeEncoding.Field, InitCustomAttributeType((RuntimeType)fields[i].FieldType)); for (int i = 0; i < properties.Length; i++) m_namedParams[i + fields.Length] = new CustomAttributeNamedParameter( properties[i].Name, CustomAttributeEncoding.Property, InitCustomAttributeType((RuntimeType)properties[i].PropertyType)); m_members = new MemberInfo[fields.Length + properties.Length]; fields.CopyTo(m_members, 0); properties.CopyTo(m_members, fields.Length); CustomAttributeEncodedArgument.ParseAttributeArguments(caRecord.blob, ref m_ctorParams, ref m_namedParams, m_scope); }
private static unsafe object[] GetCustomAttributes(RuntimeModule decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType attributeFilterType, bool mustBeInheritable, IList derivedAttributes, bool isDecoratedTargetSecurityTransparent) { if (decoratedModule.Assembly.ReflectionOnly) { throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyCA")); } MetadataImport metadataImport = decoratedModule.MetadataImport; CustomAttributeRecord[] customAttributeRecords = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken); Type elementType = (((attributeFilterType == null) || attributeFilterType.IsValueType) || attributeFilterType.ContainsGenericParameters) ? typeof(object) : attributeFilterType; if ((attributeFilterType == null) && (customAttributeRecords.Length == 0)) { return(CreateAttributeArrayHelper(elementType, 0)); } object[] attributes = CreateAttributeArrayHelper(elementType, customAttributeRecords.Length); int length = 0; SecurityContextFrame frame = new SecurityContextFrame(); frame.Push(decoratedModule.GetRuntimeAssembly()); Assembly lastAptcaOkAssembly = null; for (int i = 0; i < customAttributeRecords.Length; i++) { bool flag2; bool flag3; object obj2 = null; CustomAttributeRecord caRecord = customAttributeRecords[i]; IRuntimeMethodInfo ctor = null; RuntimeType attributeType = null; int namedArgs = 0; IntPtr signature = caRecord.blob.Signature; IntPtr blobEnd = (IntPtr)(((void *)signature) + caRecord.blob.Length); int num4 = (int)((long)((((void *)blobEnd) - ((void *)signature)) / 1)); if (FilterCustomAttributeRecord(caRecord, metadataImport, ref lastAptcaOkAssembly, decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, attributes, derivedAttributes, out attributeType, out ctor, out flag2, out flag3)) { if (ctor != null) { RuntimeMethodHandle.CheckLinktimeDemands(ctor, decoratedModule, isDecoratedTargetSecurityTransparent); } RuntimeConstructorInfo.CheckCanCreateInstance(attributeType, flag3); if (flag2) { obj2 = CreateCaObject(decoratedModule, ctor, ref signature, blobEnd, out namedArgs); } else { obj2 = RuntimeTypeHandle.CreateCaInstance(attributeType, ctor); if (num4 == 0) { namedArgs = 0; } else { if (Marshal.ReadInt16(signature) != 1) { throw new CustomAttributeFormatException(); } signature = (IntPtr)(((void *)signature) + 2); namedArgs = Marshal.ReadInt16(signature); signature = (IntPtr)(((void *)signature) + 2); } } for (int j = 0; j < namedArgs; j++) { string str; bool flag4; RuntimeType type; object obj3; IntPtr ptr1 = caRecord.blob.Signature; GetPropertyOrFieldData(decoratedModule, ref signature, blobEnd, out str, out flag4, out type, out obj3); try { if (flag4) { if ((type == null) && (obj3 != null)) { type = (RuntimeType)obj3.GetType(); if (type == Type_RuntimeType) { type = Type_Type; } } RuntimePropertyInfo property = null; if (type == null) { property = attributeType.GetProperty(str) as RuntimePropertyInfo; } else { property = attributeType.GetProperty(str, type, Type.EmptyTypes) as RuntimePropertyInfo; } if (property == null) { throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag4 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), new object[] { str })); } RuntimeMethodInfo setMethod = property.GetSetMethod(true) as RuntimeMethodInfo; if (setMethod.IsPublic) { RuntimeMethodHandle.CheckLinktimeDemands(setMethod, decoratedModule, isDecoratedTargetSecurityTransparent); setMethod.Invoke(obj2, BindingFlags.Default, null, new object[] { obj3 }, null, true); } } else { RtFieldInfo field = attributeType.GetField(str) as RtFieldInfo; if (isDecoratedTargetSecurityTransparent) { RuntimeFieldHandle.CheckAttributeAccess(field.FieldHandle, decoratedModule.GetNativeHandle()); } field.InternalSetValue(obj2, obj3, BindingFlags.Default, Type.DefaultBinder, null, false); } } catch (Exception exception) { throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag4 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), new object[] { str }), exception); } } if (!signature.Equals(blobEnd)) { throw new CustomAttributeFormatException(); } attributes[length++] = obj2; } } frame.Pop(); if ((length == customAttributeRecords.Length) && (pcaCount == 0)) { return(attributes); } object[] destinationArray = CreateAttributeArrayHelper(elementType, length + pcaCount); Array.Copy(attributes, 0, destinationArray, 0, length); return(destinationArray); }
[System.Security.SecurityCritical] // auto-generated private unsafe static bool FilterCustomAttributeRecord( CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg) { ctor = null; attributeType = null; ctorHasParameters = false; isVarArg = false; IntPtr blobStart = caRecord.blob.Signature; IntPtr blobEnd = (IntPtr)((byte*)blobStart + caRecord.blob.Length); #if FEATURE_LEGACYNETCF if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) { try { // Resolve attribute type from ctor parent token found in decorated decoratedModule scope attributeType = decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType; } catch(Exception) { return false; } } else #endif // Resolve attribute type from ctor parent token found in decorated decoratedModule scope attributeType = decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType; // Test attribute type against user provided attribute type filter if (!(attributeFilterType.IsAssignableFrom(attributeType))) return false; // Ensure if attribute type must be inheritable that it is inhertiable // Ensure that to consider a duplicate attribute type AllowMultiple is true if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes)) return false; // Windows Runtime attributes aren't real types - they exist to be read as metadata only, and as such // should be filtered out of the GetCustomAttributes path. if ((attributeType.Attributes & TypeAttributes.WindowsRuntime) == TypeAttributes.WindowsRuntime) { return false; } #if FEATURE_APTCA // APTCA checks RuntimeAssembly attributeAssembly = (RuntimeAssembly)attributeType.Assembly; RuntimeAssembly decoratedModuleAssembly = (RuntimeAssembly)decoratedModule.Assembly; if (attributeAssembly != lastAptcaOkAssembly && !RuntimeAssembly.AptcaCheck(attributeAssembly, decoratedModuleAssembly)) return false; // Cache last successful APTCA check (optimization) lastAptcaOkAssembly = decoratedModuleAssembly; #endif // FEATURE_APTCA // Resolve the attribute ctor ConstArray ctorSig = scope.GetMethodSignature(caRecord.tkCtor); isVarArg = (ctorSig[0] & 0x05) != 0; ctorHasParameters = ctorSig[1] != 0; if (ctorHasParameters) { // Resolve method ctor token found in decorated decoratedModule scope ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), caRecord.tkCtor); } else { // Resolve method ctor token from decorated decoratedModule scope ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor(); if (ctor == null && !attributeType.IsValueType) throw new MissingMethodException(".ctor"); } // Visibility checks MetadataToken tkParent = new MetadataToken(); if (decoratedToken.IsParamDef) { tkParent = new MetadataToken(scope.GetParentToken(decoratedToken)); tkParent = new MetadataToken(scope.GetParentToken(tkParent)); } else if (decoratedToken.IsMethodDef || decoratedToken.IsProperty || decoratedToken.IsEvent || decoratedToken.IsFieldDef) { tkParent = new MetadataToken(scope.GetParentToken(decoratedToken)); } else if (decoratedToken.IsTypeDef) { tkParent = decoratedToken; } else if (decoratedToken.IsGenericPar) { tkParent = new MetadataToken(scope.GetParentToken(decoratedToken)); // decoratedToken is a generic parameter on a method. Get the declaring Type of the method. if (tkParent.IsMethodDef) tkParent = new MetadataToken(scope.GetParentToken(tkParent)); } else { // We need to relax this when we add support for other types of decorated tokens. Contract.Assert(decoratedToken.IsModule || decoratedToken.IsAssembly, "The decoratedToken must be either an assembly, a module, a type, or a member."); } // If the attribute is on a type, member, or parameter we check access against the (declaring) type, // otherwise we check access against the module. RuntimeTypeHandle parentTypeHandle = tkParent.IsTypeDef ? decoratedModule.ModuleHandle.ResolveTypeHandle(tkParent) : new RuntimeTypeHandle(); return RuntimeMethodHandle.IsCAVisibleFromDecoratedType(attributeType.TypeHandle, ctor, parentTypeHandle, decoratedModule); }
internal unsafe static CustomAttributeRecord[] GetCustomAttributeRecords(Module module, int targetToken) { MetadataImport scope = module.MetadataImport; int cCustomAttributeTokens = scope.EnumCustomAttributesCount(targetToken); int* tkCustomAttributeTokens = stackalloc int[cCustomAttributeTokens]; scope.EnumCustomAttributes(targetToken, tkCustomAttributeTokens, cCustomAttributeTokens); CustomAttributeRecord[] records = new CustomAttributeRecord[cCustomAttributeTokens]; for (int i = 0; i < cCustomAttributeTokens; i++) { scope.GetCustomAttributeProps( tkCustomAttributeTokens[i], out records[i].tkCtor.Value, out records[i].blob); } return records; }
internal unsafe static bool FilterCustomAttributeRecord( CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, Module decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out RuntimeMethodHandle ctor, out bool ctorHasParameters, out bool isVarArg) { ctor = new RuntimeMethodHandle(); attributeType = null; ctorHasParameters = false; isVarArg = false; IntPtr blobStart = caRecord.blob.Signature; IntPtr blobEnd = (IntPtr)((byte*)blobStart + caRecord.blob.Length); // Resolve attribute type from ctor parent token found in decorated decoratedModule scope attributeType = decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType; // Test attribute type against user provided attribute type filter if (!(attributeFilterType.IsAssignableFrom(attributeType))) return false; if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes)) return false; // APTCA checks if (attributeType.Assembly != lastAptcaOkAssembly && !attributeType.Assembly.AptcaCheck(decoratedModule.Assembly)) return false; // Cache last successful APTCA check (optimization) lastAptcaOkAssembly = decoratedModule.Assembly; // Resolve the attribute ctor ConstArray ctorSig = scope.GetMethodSignature(caRecord.tkCtor); isVarArg = (ctorSig[0] & 0x05) != 0; ctorHasParameters = ctorSig[1] != 0; if (ctorHasParameters) { // Resolve method ctor token found in decorated decoratedModule scope ctor = decoratedModule.ModuleHandle.ResolveMethodHandle(caRecord.tkCtor); } else { // Resolve method ctor token from decorated decoratedModule scope ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor(); if (ctor.IsNullHandle() && !attributeType.IsValueType) throw new MissingMethodException(".ctor"); } // Visibility checks if (ctor.IsNullHandle()) { if (!attributeType.IsVisible && !attributeType.TypeHandle.IsVisibleFromModule(decoratedModule.ModuleHandle)) return false; return true; } if (ctor.IsVisibleFromModule(decoratedModule)) return true; MetadataToken tkParent = new MetadataToken(); if (decoratedToken.IsParamDef) { tkParent = new MetadataToken(scope.GetParentToken(decoratedToken)); tkParent = new MetadataToken(scope.GetParentToken(tkParent)); } else if (decoratedToken.IsMethodDef || decoratedToken.IsProperty || decoratedToken.IsEvent || decoratedToken.IsFieldDef) { tkParent = new MetadataToken(scope.GetParentToken(decoratedToken)); } else if (decoratedToken.IsTypeDef) { tkParent = decoratedToken; } if (tkParent.IsTypeDef) return ctor.IsVisibleFromType(decoratedModule.ModuleHandle.ResolveTypeHandle(tkParent)); return false; }
internal static unsafe bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, Module decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out RuntimeMethodHandle ctor, out bool ctorHasParameters, out bool isVarArg) { ctor = new RuntimeMethodHandle(); attributeType = null; ctorHasParameters = false; isVarArg = false; IntPtr ptr1 = (IntPtr) (((void*) caRecord.blob.Signature) + caRecord.blob.Length); attributeType = decoratedModule.ResolveType(scope.GetParentToken((int) caRecord.tkCtor), null, null) as RuntimeType; if (!attributeFilterType.IsAssignableFrom(attributeType)) { return false; } if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes)) { return false; } if ((attributeType.Assembly != lastAptcaOkAssembly) && !attributeType.Assembly.AptcaCheck(decoratedModule.Assembly)) { return false; } lastAptcaOkAssembly = decoratedModule.Assembly; ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor); isVarArg = (methodSignature[0] & 5) != 0; ctorHasParameters = methodSignature[1] != 0; if (ctorHasParameters) { ctor = decoratedModule.ModuleHandle.ResolveMethodHandle((int) caRecord.tkCtor); } else { ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor(); if (ctor.IsNullHandle() && !attributeType.IsValueType) { throw new MissingMethodException(".ctor"); } } if (ctor.IsNullHandle()) { if (!attributeType.IsVisible && !attributeType.TypeHandle.IsVisibleFromModule(decoratedModule.ModuleHandle)) { return false; } return true; } if (ctor.IsVisibleFromModule(decoratedModule)) { return true; } MetadataToken token = new MetadataToken(); if (decoratedToken.IsParamDef) { token = new MetadataToken(scope.GetParentToken((int) decoratedToken)); token = new MetadataToken(scope.GetParentToken((int) token)); } else if ((decoratedToken.IsMethodDef || decoratedToken.IsProperty) || (decoratedToken.IsEvent || decoratedToken.IsFieldDef)) { token = new MetadataToken(scope.GetParentToken((int) decoratedToken)); } else if (decoratedToken.IsTypeDef) { token = decoratedToken; } return (token.IsTypeDef && ctor.IsVisibleFromType(decoratedModule.ModuleHandle.ResolveTypeHandle((int) token))); }
private static unsafe bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg) { ctor = null; attributeType = null; ctorHasParameters = false; isVarArg = false; IntPtr ptr1 = (IntPtr)(((void *)caRecord.blob.Signature) + caRecord.blob.Length); attributeType = decoratedModule.ResolveType(scope.GetParentToken((int)caRecord.tkCtor), null, null) as RuntimeType; if (!attributeFilterType.IsAssignableFrom(attributeType)) { return(false); } if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes)) { return(false); } RuntimeAssembly targetAssembly = (RuntimeAssembly)attributeType.Assembly; RuntimeAssembly assembly = (RuntimeAssembly)decoratedModule.Assembly; if ((targetAssembly != lastAptcaOkAssembly) && !RuntimeAssembly.AptcaCheck(targetAssembly, assembly)) { return(false); } lastAptcaOkAssembly = assembly; ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor); isVarArg = (methodSignature[0] & 5) != 0; ctorHasParameters = methodSignature[1] != 0; if (ctorHasParameters) { ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), (int)caRecord.tkCtor); } else { ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor(); if ((ctor == null) && !attributeType.IsValueType) { throw new MissingMethodException(".ctor"); } } if (ctor == null) { if (!attributeType.IsVisible && !attributeType.TypeHandle.IsVisibleFromModule(decoratedModule)) { return(false); } return(true); } if (RuntimeMethodHandle.IsVisibleFromModule(ctor, decoratedModule)) { return(true); } MetadataToken token = new MetadataToken(); if (decoratedToken.IsParamDef) { token = new MetadataToken(scope.GetParentToken((int)decoratedToken)); token = new MetadataToken(scope.GetParentToken((int)token)); } else if ((decoratedToken.IsMethodDef || decoratedToken.IsProperty) || (decoratedToken.IsEvent || decoratedToken.IsFieldDef)) { token = new MetadataToken(scope.GetParentToken((int)decoratedToken)); } else if (decoratedToken.IsTypeDef) { token = decoratedToken; } return(token.IsTypeDef && RuntimeMethodHandle.IsVisibleFromType(ctor, decoratedModule.ModuleHandle.ResolveTypeHandle((int)token))); }
private static unsafe object[] GetCustomAttributes(RuntimeModule decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType attributeFilterType, bool mustBeInheritable, IList derivedAttributes, bool isDecoratedTargetSecurityTransparent) { if (decoratedModule.Assembly.ReflectionOnly) { throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyCA")); } MetadataImport metadataImport = decoratedModule.MetadataImport; CustomAttributeRecord[] attributeRecords = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken); Type elementType = (attributeFilterType == (RuntimeType)null || attributeFilterType.IsValueType ? 1 : (attributeFilterType.ContainsGenericParameters ? 1 : 0)) != 0 ? typeof(object) : (Type)attributeFilterType; if (attributeFilterType == (RuntimeType)null && attributeRecords.Length == 0) { return(CustomAttribute.CreateAttributeArrayHelper(elementType, 0)); } object[] attributeArrayHelper1 = CustomAttribute.CreateAttributeArrayHelper(elementType, attributeRecords.Length); int length = 0; SecurityContextFrame securityContextFrame = new SecurityContextFrame(); securityContextFrame.Push(decoratedModule.GetRuntimeAssembly()); Assembly lastAptcaOkAssembly = (Assembly)null; for (int index1 = 0; index1 < attributeRecords.Length; ++index1) { CustomAttributeRecord caRecord = attributeRecords[index1]; IRuntimeMethodInfo ctor = (IRuntimeMethodInfo)null; RuntimeType attributeType = (RuntimeType)null; int namedArgs = 0; IntPtr ptr1 = caRecord.blob.Signature; IntPtr blobEnd = (IntPtr)((void *)((IntPtr)(void *)ptr1 + caRecord.blob.Length)); int num = (int)((sbyte *)(void *)blobEnd - (sbyte *)(void *)ptr1); bool ctorHasParameters; bool isVarArg; if (CustomAttribute.FilterCustomAttributeRecord(caRecord, metadataImport, ref lastAptcaOkAssembly, decoratedModule, (MetadataToken)decoratedMetadataToken, attributeFilterType, mustBeInheritable, attributeArrayHelper1, derivedAttributes, out attributeType, out ctor, out ctorHasParameters, out isVarArg)) { if (ctor != null) { RuntimeMethodHandle.CheckLinktimeDemands(ctor, decoratedModule, isDecoratedTargetSecurityTransparent); } RuntimeConstructorInfo.CheckCanCreateInstance((Type)attributeType, isVarArg); object target; if (ctorHasParameters) { target = CustomAttribute.CreateCaObject(decoratedModule, ctor, ref ptr1, blobEnd, out namedArgs); } else { target = RuntimeTypeHandle.CreateCaInstance(attributeType, ctor); if (num == 0) { namedArgs = 0; } else { if ((int)Marshal.ReadInt16(ptr1) != 1) { throw new CustomAttributeFormatException(); } IntPtr ptr2 = (IntPtr)((void *)((IntPtr)(void *)ptr1 + 2)); namedArgs = (int)Marshal.ReadInt16(ptr2); ptr1 = (IntPtr)((void *)((IntPtr)(void *)ptr2 + 2)); } } for (int index2 = 0; index2 < namedArgs; ++index2) { IntPtr signature = caRecord.blob.Signature; string name; bool isProperty; RuntimeType type; object obj; CustomAttribute.GetPropertyOrFieldData(decoratedModule, ref ptr1, blobEnd, out name, out isProperty, out type, out obj); try { if (isProperty) { if (type == (RuntimeType)null && obj != null) { type = (RuntimeType)obj.GetType(); if (type == CustomAttribute.Type_RuntimeType) { type = CustomAttribute.Type_Type; } } RuntimePropertyInfo runtimePropertyInfo = !(type == (RuntimeType)null) ? attributeType.GetProperty(name, (Type)type, Type.EmptyTypes) as RuntimePropertyInfo : attributeType.GetProperty(name) as RuntimePropertyInfo; if ((PropertyInfo)runtimePropertyInfo == (PropertyInfo)null) { throw new CustomAttributeFormatException(string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Environment.GetResourceString(isProperty ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), (object)name)); } RuntimeMethodInfo runtimeMethodInfo = runtimePropertyInfo.GetSetMethod(true) as RuntimeMethodInfo; if (runtimeMethodInfo.IsPublic) { RuntimeMethodHandle.CheckLinktimeDemands((IRuntimeMethodInfo)runtimeMethodInfo, decoratedModule, isDecoratedTargetSecurityTransparent); runtimeMethodInfo.UnsafeInvoke(target, BindingFlags.Default, (Binder)null, new object[1] { obj }, (CultureInfo)null); } } else { RtFieldInfo rtFieldInfo = attributeType.GetField(name) as RtFieldInfo; if (isDecoratedTargetSecurityTransparent) { RuntimeFieldHandle.CheckAttributeAccess(rtFieldInfo.FieldHandle, decoratedModule.GetNativeHandle()); } rtFieldInfo.CheckConsistency(target); rtFieldInfo.UnsafeSetValue(target, obj, BindingFlags.Default, Type.DefaultBinder, (CultureInfo)null); } } catch (Exception ex) { throw new CustomAttributeFormatException(string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Environment.GetResourceString(isProperty ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), (object)name), ex); } } if (!ptr1.Equals((object)blobEnd)) { throw new CustomAttributeFormatException(); } attributeArrayHelper1[length++] = target; } } securityContextFrame.Pop(); if (length == attributeRecords.Length && pcaCount == 0) { return(attributeArrayHelper1); } object[] attributeArrayHelper2 = CustomAttribute.CreateAttributeArrayHelper(elementType, length + pcaCount); Array.Copy((Array)attributeArrayHelper1, 0, (Array)attributeArrayHelper2, 0, length); return(attributeArrayHelper2); }
private unsafe static bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg) { ctor = null; attributeType = null; ctorHasParameters = false; isVarArg = false; IntPtr signature = caRecord.blob.Signature; IntPtr intPtr = (IntPtr)((void *)((byte *)((void *)signature) + caRecord.blob.Length)); attributeType = (decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType); if (!attributeFilterType.IsAssignableFrom(attributeType)) { return(false); } if (!CustomAttribute.AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes)) { return(false); } if ((attributeType.Attributes & TypeAttributes.WindowsRuntime) == TypeAttributes.WindowsRuntime) { return(false); } RuntimeAssembly runtimeAssembly = (RuntimeAssembly)attributeType.Assembly; RuntimeAssembly runtimeAssembly2 = (RuntimeAssembly)decoratedModule.Assembly; if (runtimeAssembly != lastAptcaOkAssembly && !RuntimeAssembly.AptcaCheck(runtimeAssembly, runtimeAssembly2)) { return(false); } lastAptcaOkAssembly = runtimeAssembly2; ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor); isVarArg = ((methodSignature[0] & 5) > 0); ctorHasParameters = (methodSignature[1] > 0); if (ctorHasParameters) { ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), caRecord.tkCtor); } else { ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor(); if (ctor == null && !attributeType.IsValueType) { throw new MissingMethodException(".ctor"); } } MetadataToken token = default(MetadataToken); if (decoratedToken.IsParamDef) { token = new MetadataToken(scope.GetParentToken(decoratedToken)); token = new MetadataToken(scope.GetParentToken(token)); } else if (decoratedToken.IsMethodDef || decoratedToken.IsProperty || decoratedToken.IsEvent || decoratedToken.IsFieldDef) { token = new MetadataToken(scope.GetParentToken(decoratedToken)); } else if (decoratedToken.IsTypeDef) { token = decoratedToken; } else if (decoratedToken.IsGenericPar) { token = new MetadataToken(scope.GetParentToken(decoratedToken)); if (token.IsMethodDef) { token = new MetadataToken(scope.GetParentToken(token)); } } RuntimeTypeHandle sourceTypeHandle = token.IsTypeDef ? decoratedModule.ModuleHandle.ResolveTypeHandle(token) : default(RuntimeTypeHandle); return(RuntimeMethodHandle.IsCAVisibleFromDecoratedType(attributeType.TypeHandle, ctor, sourceTypeHandle, decoratedModule)); }
private unsafe static object[] GetCustomAttributes(RuntimeModule decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType attributeFilterType, bool mustBeInheritable, IList derivedAttributes, bool isDecoratedTargetSecurityTransparent) { if (decoratedModule.Assembly.ReflectionOnly) { throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyCA")); } MetadataImport metadataImport = decoratedModule.MetadataImport; CustomAttributeRecord[] customAttributeRecords = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken); Type elementType = (attributeFilterType == null || attributeFilterType.IsValueType || attributeFilterType.ContainsGenericParameters) ? typeof(object) : attributeFilterType; if (attributeFilterType == null && customAttributeRecords.Length == 0) { return(CustomAttribute.CreateAttributeArrayHelper(elementType, 0)); } object[] array = CustomAttribute.CreateAttributeArrayHelper(elementType, customAttributeRecords.Length); int num = 0; SecurityContextFrame securityContextFrame = default(SecurityContextFrame); securityContextFrame.Push(decoratedModule.GetRuntimeAssembly()); Assembly assembly = null; for (int i = 0; i < customAttributeRecords.Length; i++) { object obj = null; CustomAttributeRecord caRecord = customAttributeRecords[i]; IRuntimeMethodInfo runtimeMethodInfo = null; RuntimeType runtimeType = null; int num2 = 0; IntPtr intPtr = caRecord.blob.Signature; IntPtr intPtr2 = (IntPtr)((void *)((byte *)((void *)intPtr) + caRecord.blob.Length)); int num3 = (int)((long)((byte *)((void *)intPtr2) - (byte *)((void *)intPtr))); bool flag; bool isVarArg; if (CustomAttribute.FilterCustomAttributeRecord(caRecord, metadataImport, ref assembly, decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, array, derivedAttributes, out runtimeType, out runtimeMethodInfo, out flag, out isVarArg)) { if (runtimeMethodInfo != null) { RuntimeMethodHandle.CheckLinktimeDemands(runtimeMethodInfo, decoratedModule, isDecoratedTargetSecurityTransparent); } RuntimeConstructorInfo.CheckCanCreateInstance(runtimeType, isVarArg); if (flag) { obj = CustomAttribute.CreateCaObject(decoratedModule, runtimeMethodInfo, ref intPtr, intPtr2, out num2); } else { obj = RuntimeTypeHandle.CreateCaInstance(runtimeType, runtimeMethodInfo); if (num3 == 0) { num2 = 0; } else { if (Marshal.ReadInt16(intPtr) != 1) { throw new CustomAttributeFormatException(); } intPtr = (IntPtr)((void *)((byte *)((void *)intPtr) + 2)); num2 = (int)Marshal.ReadInt16(intPtr); intPtr = (IntPtr)((void *)((byte *)((void *)intPtr) + 2)); } } for (int j = 0; j < num2; j++) { IntPtr signature = caRecord.blob.Signature; string text; bool flag2; RuntimeType runtimeType2; object obj2; CustomAttribute.GetPropertyOrFieldData(decoratedModule, ref intPtr, intPtr2, out text, out flag2, out runtimeType2, out obj2); try { if (flag2) { if (runtimeType2 == null && obj2 != null) { runtimeType2 = (RuntimeType)obj2.GetType(); if (runtimeType2 == CustomAttribute.Type_RuntimeType) { runtimeType2 = CustomAttribute.Type_Type; } } RuntimePropertyInfo runtimePropertyInfo; if (runtimeType2 == null) { runtimePropertyInfo = (runtimeType.GetProperty(text) as RuntimePropertyInfo); } else { runtimePropertyInfo = (runtimeType.GetProperty(text, runtimeType2, Type.EmptyTypes) as RuntimePropertyInfo); } if (runtimePropertyInfo == null) { throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag2 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), text)); } RuntimeMethodInfo runtimeMethodInfo2 = runtimePropertyInfo.GetSetMethod(true) as RuntimeMethodInfo; if (runtimeMethodInfo2.IsPublic) { RuntimeMethodHandle.CheckLinktimeDemands(runtimeMethodInfo2, decoratedModule, isDecoratedTargetSecurityTransparent); runtimeMethodInfo2.UnsafeInvoke(obj, BindingFlags.Default, null, new object[] { obj2 }, null); } } else { RtFieldInfo rtFieldInfo = runtimeType.GetField(text) as RtFieldInfo; if (isDecoratedTargetSecurityTransparent) { RuntimeFieldHandle.CheckAttributeAccess(rtFieldInfo.FieldHandle, decoratedModule.GetNativeHandle()); } rtFieldInfo.CheckConsistency(obj); rtFieldInfo.UnsafeSetValue(obj, obj2, BindingFlags.Default, Type.DefaultBinder, null); } } catch (Exception inner) { throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag2 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), text), inner); } } if (!intPtr.Equals(intPtr2)) { throw new CustomAttributeFormatException(); } array[num++] = obj; } } securityContextFrame.Pop(); if (num == customAttributeRecords.Length && pcaCount == 0) { return(array); } object[] array2 = CustomAttribute.CreateAttributeArrayHelper(elementType, num + pcaCount); Array.Copy(array, 0, array2, 0, num); return(array2); }