/// <summary> /// Reads a single pinned type signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the signature resides in.</param> /// <param name="reader">The reader to use.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read signature.</returns> public static PinnedTypeSignature FromReader( MetadataImage image, IBinaryStreamReader reader, RecursionProtection protection) { return(new PinnedTypeSignature(TypeSignature.FromReader(image, reader, false, protection))); }
private static TypeSignature ReadTypeSignature( MetadataImage image, IBinaryStreamReader reader, RecursionProtection protection) { var elementType = (ElementType)reader.ReadByte(); switch (elementType) { case ElementType.Array: return(ArrayTypeSignature.FromReader(image, reader, protection)); case ElementType.Boxed: return(BoxedTypeSignature.FromReader(image, reader, protection)); case ElementType.ByRef: return(ByReferenceTypeSignature.FromReader(image, reader, protection)); case ElementType.CModOpt: return(OptionalModifierSignature.FromReader(image, reader, protection)); case ElementType.CModReqD: return(RequiredModifierSignature.FromReader(image, reader, protection)); case ElementType.Class: return(TypeDefOrRefSignature.FromReader(image, reader, protection)); case ElementType.FnPtr: return(FunctionPointerTypeSignature.FromReader(image, reader, protection)); case ElementType.GenericInst: return(GenericInstanceTypeSignature.FromReader(image, reader, protection)); case ElementType.MVar: return(GenericParameterSignature.FromReader(image, reader, GenericParameterType.Method)); case ElementType.Pinned: return(PinnedTypeSignature.FromReader(image, reader, protection)); case ElementType.Ptr: return(PointerTypeSignature.FromReader(image, reader, protection)); case ElementType.Sentinel: return(SentinelTypeSignature.FromReader(image, reader, protection)); case ElementType.SzArray: return(SzArrayTypeSignature.FromReader(image, reader, protection)); case ElementType.ValueType: var type = TypeDefOrRefSignature.FromReader(image, reader, protection); type.IsValueType = true; return(type); case ElementType.Var: return(GenericParameterSignature.FromReader(image, reader, GenericParameterType.Type)); default: return(MsCorLibTypeSignature.FromElementType(image, elementType)); } }
/// <summary> /// Reads a single generic instance type signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the signature was defined in.</param> /// <param name="reader">The reader to use.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read signature.</returns> public static GenericInstanceTypeSignature FromReader( MetadataImage image, IBinaryStreamReader reader, RecursionProtection protection) { if (!reader.CanRead(sizeof(byte))) { return(null); } var elementType = (ElementType)reader.ReadByte(); var type = ReadTypeDefOrRef(image, reader, protection); var signature = new GenericInstanceTypeSignature(type) { IsValueType = elementType == ElementType.ValueType }; if (!reader.TryReadCompressedUInt32(out uint count)) { return(signature); } for (int i = 0; i < count; i++) { signature.GenericArguments.Add(TypeSignature.FromReader(image, reader, false, protection)); } return(signature); }
/// <summary> /// Reads a single property signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the signature resides in.</param> /// <param name="reader">The reader to use.</param> /// <param name="readToEnd">Determines whether any extra data after the signature should be read and /// put into the <see cref="ExtendableBlobSignature.ExtraData"/> property.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read signature.</returns> public new static PropertySignature FromReader( MetadataImage image, IBinaryStreamReader reader, bool readToEnd, RecursionProtection protection) { var signature = new PropertySignature { Attributes = (CallingConventionAttributes)reader.ReadByte(), }; if (!reader.TryReadCompressedUInt32(out uint paramCount)) { return(null); } signature.PropertyType = TypeSignature.FromReader(image, reader, false, protection); for (int i = 0; i < paramCount; i++) { signature.Parameters.Add(ParameterSignature.FromReader(image, reader, protection)); } if (readToEnd) { signature.ExtraData = reader.ReadToEnd(); } return(signature); }
/// <summary> /// Reads a single function pointer type at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the function pointer was defined in.</param> /// <param name="reader">The reader to use.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read function pointer</returns> public static FunctionPointerTypeSignature FromReader( MetadataImage image, IBinaryStreamReader reader, RecursionProtection protection) { return(new FunctionPointerTypeSignature(MethodSignature.FromReader(image, reader, false, protection))); }
private static CallingConventionSignature ReadSignature( MetadataImage image, IBinaryStreamReader reader, RecursionProtection protection) { var flag = reader.ReadByte(); reader.Position--; switch ((CallingConventionAttributes)flag & SignatureTypeMask) { case CallingConventionAttributes.Default: case CallingConventionAttributes.C: case CallingConventionAttributes.ExplicitThis: case CallingConventionAttributes.FastCall: case CallingConventionAttributes.StdCall: case CallingConventionAttributes.ThisCall: case CallingConventionAttributes.VarArg: return(MethodSignature.FromReader(image, reader, false, protection)); case CallingConventionAttributes.Property: return(PropertySignature.FromReader(image, reader, false, protection)); case CallingConventionAttributes.Local: return(LocalVariableSignature.FromReader(image, reader, false, protection)); case CallingConventionAttributes.GenericInstance: return(GenericInstanceMethodSignature.FromReader(image, reader, protection)); case CallingConventionAttributes.Field: return(FieldSignature.FromReader(image, reader, false, protection)); } throw new NotSupportedException(); }
/// <summary> /// Reads a single annotated type signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the signature resides in.</param> /// <param name="reader">The reader to use.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read signature.</returns> public static RequiredModifierSignature FromReader( MetadataImage image, IBinaryStreamReader reader, RecursionProtection protection) { return(new RequiredModifierSignature(ReadTypeDefOrRef(image, reader, protection), TypeSignature.FromReader(image, reader, false, protection))); }
/// <summary> /// Reads a single type signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the signature resides in.</param> /// <param name="reader">The reader to use.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read signature.</returns> public static TypeDefOrRefSignature FromReader( MetadataImage image, IBinaryStreamReader reader, RecursionProtection protection) { var type = ReadTypeDefOrRef(image, reader, protection); return(type == null ? null : new TypeDefOrRefSignature(type)); }
/// <inheritdoc /> protected override TypeSignature GetSignature() { var reader = _parentModule.DotNetDirectory.Metadata .GetStream <BlobStream>() .GetBlobReaderByIndex(_row.Signature); var protection = RecursionProtection.CreateNew(); protection.TraversedTokens.Add(MetadataToken); return(TypeSignature.FromReader(_parentModule, reader, protection)); }
/// <summary> /// Reads a single field signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the field is defined in.</param> /// <param name="reader">The reader to use.</param> /// <param name="readToEnd">Determines whether any extra data after the signature should be read and /// put into the <see cref="ExtendableBlobSignature.ExtraData"/> property.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read signature.</returns> public new static FieldSignature FromReader( MetadataImage image, IBinaryStreamReader reader, bool readToEnd, RecursionProtection protection) { return(new FieldSignature { Attributes = (CallingConventionAttributes)reader.ReadByte(), FieldType = TypeSignature.FromReader(image, reader, false, protection), ExtraData = readToEnd ? reader.ReadToEnd() : null }); }
/// <summary> /// Reads a single calling convention signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the signature is defined in.</param> /// <param name="reader">The reader to use.</param> /// <param name="readToEnd">Determines whether any extra data after the signature should be read and /// put into the <see cref="ExtendableBlobSignature.ExtraData"/> property.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read signature.</returns> public static CallingConventionSignature FromReader( MetadataImage image, IBinaryStreamReader reader, bool readToEnd, RecursionProtection protection) { var signature = ReadSignature(image, reader, protection); if (readToEnd) { signature.ExtraData = reader.ReadToEnd(); } return(signature); }
internal new static GenericInstanceTypeSignature FromReader(ModuleDefinition module, IBinaryStreamReader reader, RecursionProtection protection) { var genericType = TypeSignature.FromReader(module, reader, protection); var signature = new GenericInstanceTypeSignature(genericType.ToTypeDefOrRef(), genericType.ElementType == ElementType.ValueType); if (!reader.TryReadCompressedUInt32(out uint count)) { return(signature); } for (int i = 0; i < count; i++) { signature.TypeArguments.Add(TypeSignature.FromReader(module, reader, protection)); } return(signature); }
/// <summary> /// Reads a single method signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the field is defined in.</param> /// <param name="reader">The reader to use.</param> /// <param name="readToEnd">Determines whether any extra data after the signature should be read and /// put into the <see cref="ExtendableBlobSignature.ExtraData"/> property.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read signature.</returns> public new static MethodSignature FromReader( MetadataImage image, IBinaryStreamReader reader, bool readToEnd, RecursionProtection protection) { if (!reader.CanRead(sizeof(byte))) { return(null); } var signature = new MethodSignature { Attributes = (CallingConventionAttributes)reader.ReadByte() }; if (signature.IsGeneric) { if (!reader.TryReadCompressedUInt32(out uint genericParameterCount)) { return(signature); } signature.GenericParameterCount = (int)genericParameterCount; } if (!reader.TryReadCompressedUInt32(out uint parameterCount)) { return(signature); } signature.ReturnType = TypeSignature.FromReader(image, reader); for (int i = 0; i < parameterCount; i++) { signature.Parameters.Add(ParameterSignature.FromReader(image, reader, protection)); } if (readToEnd) { signature.ExtraData = reader.ReadToEnd(); } return(signature); }
/// <summary> /// Reads a single generic instance method signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the signature was defined in.</param> /// <param name="reader">The reader to use.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read signature.</returns> public static GenericInstanceMethodSignature FromReader( MetadataImage image, IBinaryStreamReader reader, RecursionProtection protection) { var signature = new GenericInstanceMethodSignature { Attributes = (CallingConventionAttributes)reader.ReadByte() }; if (!reader.TryReadCompressedUInt32(out uint count)) { return(signature); } for (int i = 0; i < count; i++) { signature.GenericArguments.Add(TypeSignature.FromReader(image, reader, false, protection)); } return(signature); }
/// <summary> /// Reads a single local variable signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the signature was defined in.</param> /// <param name="reader">The reader to use.</param> /// <param name="readToEnd">Determines whether any extra data after the signature should be read and /// put into the <see cref="ExtendableBlobSignature.ExtraData"/> property.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read signature.</returns> public new static LocalVariableSignature FromReader( MetadataImage image, IBinaryStreamReader reader, bool readToEnd, RecursionProtection protection) { var signature = new LocalVariableSignature { Attributes = (CallingConventionAttributes)reader.ReadByte() }; uint count = reader.ReadCompressedUInt32(); for (int i = 0; i < count; i++) { signature.Variables.Add(VariableSignature.FromReader(image, reader, protection)); } if (readToEnd) { signature.ExtraData = reader.ReadToEnd(); } return(signature); }
/// <summary> /// Reads a TypeDefOrRef coded index from the provided blob reader. /// </summary> /// <param name="module">The module containing the blob signature.</param> /// <param name="reader">The blob reader.</param> /// <param name="protection">The object responsible for detecting infinite recursion.</param> /// <param name="allowTypeSpec">Indicates the coded index to the type is allowed to be decoded to a member in /// the type specification table.</param> /// <returns>The decoded and resolved type definition or reference.</returns> protected static ITypeDefOrRef ReadTypeDefOrRef(ModuleDefinition module, IBinaryStreamReader reader, RecursionProtection protection, bool allowTypeSpec) { if (!reader.TryReadCompressedUInt32(out uint codedIndex)) { return(InvalidTypeDefOrRef.Get(InvalidTypeSignatureError.BlobTooShort)); } var decoder = module.GetIndexEncoder(CodedIndex.TypeDefOrRef); var token = decoder.DecodeIndex(codedIndex); // Check if type specs can be encoded. if (token.Table == TableIndex.TypeSpec && !allowTypeSpec) { return(InvalidTypeDefOrRef.Get(InvalidTypeSignatureError.IllegalTypeSpec)); } switch (token.Table) { // Check for infinite recursion. case TableIndex.TypeSpec when !protection.TraversedTokens.Add(token): return(InvalidTypeDefOrRef.Get(InvalidTypeSignatureError.MetadataLoop)); // Any other type is legal. case TableIndex.TypeSpec: case TableIndex.TypeDef: case TableIndex.TypeRef: if (module.TryLookupMember(token, out var member) && member is ITypeDefOrRef typeDefOrRef) { return(typeDefOrRef); } break; } return(InvalidTypeDefOrRef.Get(InvalidTypeSignatureError.InvalidCodedIndex)); }
/// <summary> /// Reads a type signature from a blob reader. /// </summary> /// <param name="module">The module containing the blob signature.</param> /// <param name="reader">The blob signature reader.</param> /// <param name="protection">The object responsible for detecting infinite recursion.</param> /// <returns>The type signature.</returns> /// <exception cref="ArgumentOutOfRangeException">Occurs when the blob reader points to an element type that is /// invalid or unsupported.</exception> public static TypeSignature FromReader(ModuleDefinition module, IBinaryStreamReader reader, RecursionProtection protection) { var elementType = (ElementType)reader.ReadByte(); switch (elementType) { case ElementType.Void: case ElementType.Boolean: case ElementType.Char: case ElementType.I1: case ElementType.U1: case ElementType.I2: case ElementType.U2: case ElementType.I4: case ElementType.U4: case ElementType.I8: case ElementType.U8: case ElementType.R4: case ElementType.R8: case ElementType.String: case ElementType.I: case ElementType.U: case ElementType.TypedByRef: case ElementType.Object: return(module.CorLibTypeFactory.FromElementType(elementType)); case ElementType.ValueType: return(new TypeDefOrRefSignature(ReadTypeDefOrRef(module, reader, protection, false), true)); case ElementType.Class: return(new TypeDefOrRefSignature(ReadTypeDefOrRef(module, reader, protection, false), false)); case ElementType.Ptr: return(new PointerTypeSignature(FromReader(module, reader, protection))); case ElementType.ByRef: return(new ByReferenceTypeSignature(FromReader(module, reader, protection))); case ElementType.Var: return(new GenericParameterSignature(module, GenericParameterType.Type, (int)reader.ReadCompressedUInt32())); case ElementType.MVar: return(new GenericParameterSignature(module, GenericParameterType.Method, (int)reader.ReadCompressedUInt32())); case ElementType.Array: return(ArrayTypeSignature.FromReader(module, reader, protection)); case ElementType.GenericInst: return(GenericInstanceTypeSignature.FromReader(module, reader, protection)); case ElementType.FnPtr: throw new NotImplementedException(); case ElementType.SzArray: return(new SzArrayTypeSignature(FromReader(module, reader, protection))); case ElementType.CModReqD: return(new CustomModifierTypeSignature( ReadTypeDefOrRef(module, reader, protection, true), true, FromReader(module, reader, protection))); case ElementType.CModOpt: return(new CustomModifierTypeSignature( ReadTypeDefOrRef(module, reader, protection, true), false, FromReader(module, reader, protection))); case ElementType.Sentinel: return(new SentinelTypeSignature()); case ElementType.Pinned: return(new PinnedTypeSignature(FromReader(module, reader, protection))); case ElementType.Boxed: return(new BoxedTypeSignature(FromReader(module, reader, protection))); case ElementType.Internal: IntPtr address = IntPtr.Size switch { 4 => new IntPtr(reader.ReadInt32()), _ => new IntPtr(reader.ReadInt64()) }; //Get Internal Method Through Reflection var GetTypeFromHandleUnsafeReflection = typeof(Type) .GetMethod("GetTypeFromHandleUnsafe", ((BindingFlags)(-1)), null, new[] { typeof(IntPtr) }, null); //Invoke It To Get The Value var Type = (Type)GetTypeFromHandleUnsafeReflection?.Invoke(null, new object[] { address }); //Import it return(new TypeDefOrRefSignature(new ReferenceImporter(module).ImportType((Type)))); default: throw new ArgumentOutOfRangeException($"Invalid or unsupported element type {elementType}."); } }
/// <summary> /// Reads a type signature from a blob reader. /// </summary> /// <param name="module">The module containing the blob signature.</param> /// <param name="reader">The blob signature reader.</param> /// <returns>The type signature.</returns> /// <exception cref="ArgumentOutOfRangeException">Occurs when the blob reader points to an element type that is /// invalid or unsupported.</exception> public static TypeSignature FromReader(ModuleDefinition module, IBinaryStreamReader reader) => FromReader(module, reader, RecursionProtection.CreateNew());
internal new static ArrayTypeSignature FromReader(ModuleDefinition parentModule, IBinaryStreamReader reader, RecursionProtection protection) { var signature = new ArrayTypeSignature(TypeSignature.FromReader(parentModule, reader, protection)); // Rank if (!reader.TryReadCompressedUInt32(out uint rank)) { return(signature); } // Sizes. if (!reader.TryReadCompressedUInt32(out uint numSizes)) { return(signature); } var sizes = new List <uint>(); for (int i = 0; i < numSizes; i++) { if (!reader.TryReadCompressedUInt32(out uint size)) { return(signature); } sizes.Add(size); } // Lower bounds. if (!reader.TryReadCompressedUInt32(out uint numLoBounds)) { return(signature); } var loBounds = new List <uint>(); for (int i = 0; i < numLoBounds; i++) { if (!reader.TryReadCompressedUInt32(out uint bound)) { return(signature); } loBounds.Add(bound); } // Create dimensions. for (int i = 0; i < rank; i++) { int?size = null, lowerBound = null; if (i < numSizes) { size = (int)sizes[i]; } if (i < numLoBounds) { lowerBound = (int)loBounds[i]; } signature.Dimensions.Add(new ArrayDimension(size, lowerBound)); } return(signature); }
/// <summary> /// Reads a single coded index to a type definition or reference and resolves it. /// </summary> /// <param name="image">The image the type resides in.</param> /// <param name="reader">The reader to use.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The type, or <c>null</c> if recursion was detected.</returns> protected static ITypeDefOrRef ReadTypeDefOrRef(MetadataImage image, IBinaryStreamReader reader, RecursionProtection protection) { var tableStream = image.Header.GetStream <TableStream>(); if (!reader.TryReadCompressedUInt32(out uint codedIndex)) { return(null); } // If the resolved type is a TypeSpec, it can be a (malicious) loop to the same blob signature that we // were coming from. var token = tableStream.GetIndexEncoder(CodedIndex.TypeDefOrRef).DecodeIndex(codedIndex); if (token.TokenType == MetadataTokenType.TypeSpec && !protection.TraversedTokens.Add(token)) { return(InvalidTypeDefOrRef.Get(InvalidTypeSignatureError.MetadataLoop)); } image.TryResolveMember(token, out var type); return(type as ITypeDefOrRef); }
/// <summary> /// Reads a single type signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the signature resides in.</param> /// <param name="reader">The reader to use.</param> /// <param name="readToEnd">Determines whether any extra data after the signature should be read and /// put into the <see cref="ExtendableBlobSignature.ExtraData"/> property.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read signature.</returns> public static TypeSignature FromReader(MetadataImage image, IBinaryStreamReader reader, bool readToEnd, RecursionProtection protection) { var signature = ReadTypeSignature(image, reader, protection); if (signature != null && readToEnd) { signature.ExtraData = reader.ReadToEnd(); } return(signature); }
/// <summary> /// Reads a single array type signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the array was defined in.</param> /// <param name="reader">The reader to use.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read array.</returns> public static ArrayTypeSignature FromReader(MetadataImage image, IBinaryStreamReader reader, RecursionProtection protection) { var signature = new ArrayTypeSignature(TypeSignature.FromReader(image, reader, false, protection)); // Rank if (!reader.TryReadCompressedUInt32(out uint rank)) { return(signature); } // Sizes. if (!reader.TryReadCompressedUInt32(out uint numSizes)) { return(signature); } var sizes = new List <uint>(); for (int i = 0; i < numSizes; i++) { if (!reader.TryReadCompressedUInt32(out uint size)) { return(signature); } sizes.Add(size); } // Lower bounds. if (!reader.TryReadCompressedUInt32(out uint numLoBounds)) { return(signature); } var loBounds = new List <uint>(); for (int i = 0; i < numLoBounds; i++) { if (!reader.TryReadCompressedUInt32(out uint bound)) { return(signature); } loBounds.Add(bound); } // Create dimensions. for (int i = 0; i < rank; i++) { var dimension = new ArrayDimension(); if (i < numSizes) { dimension.Size = (int)sizes[i]; } if (i < numLoBounds) { dimension.LowerBound = (int)loBounds[i]; } signature.Dimensions.Add(dimension); } return(signature); }
/// <summary> /// Reads a single dimension array type signature with 0 as a lower bound from an input stream. /// </summary> /// <param name="parentModule">The module containing the signature.</param> /// <param name="reader">The input stream.</param> /// <param name="protection">The object instance responsible for detecting infinite recursion.</param> /// <returns>The signature.</returns> public new static SzArrayTypeSignature FromReader(ModuleDefinition parentModule, IBinaryStreamReader reader, RecursionProtection protection) { return(new SzArrayTypeSignature(TypeSignature.FromReader(parentModule, reader, protection))); }
/// <summary> /// Reads a single dimension array type signature with 0 as a lower bound from an input stream. /// </summary> /// <param name="parentModule">The module containing the signature.</param> /// <param name="reader">The input stream.</param> /// <returns>The signature.</returns> public new static SzArrayTypeSignature FromReader(ModuleDefinition parentModule, IBinaryStreamReader reader) { return(FromReader(parentModule, reader, RecursionProtection.CreateNew())); }