Inheritance: CallingConventionSignature
        public static CallingConventionSignature FromReader(MetadataImage image, IBinaryStreamReader reader)
        {
            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));

            case CallingConventionAttributes.Property:
                return(PropertySignature.FromReader(image, reader));

            case CallingConventionAttributes.Local:
                return(LocalVariableSignature.FromReader(image, reader));

            case CallingConventionAttributes.GenericInstance:
                return(GenericInstanceMethodSignature.FromReader(image, reader));

            case CallingConventionAttributes.Field:
                return(FieldSignature.FromReader(image, reader));
            }
            throw new NotSupportedException();
        }
        public static new LocalVariableSignature FromReader(MetadataHeader header, IBinaryStreamReader reader)
        {
            var signature = new LocalVariableSignature()
            {
                StartOffset = reader.Position,
                Attributes = (CallingConventionAttributes)reader.ReadByte()
            };

            var count = reader.ReadCompressedUInt32();

            for (int i = 0; i < count; i++)
                signature.Variables.Add(VariableSignature.FromReader(header, reader));
            return signature;
        }
Example #3
0
        public new static LocalVariableSignature FromReader(MetadataImage image, IBinaryStreamReader reader)
        {
            var signature = new LocalVariableSignature
            {
                Attributes = (CallingConventionAttributes)reader.ReadByte()
            };

            var count = reader.ReadCompressedUInt32();

            for (int i = 0; i < count; i++)
            {
                signature.Variables.Add(VariableSignature.FromReader(image, reader));
            }
            return(signature);
        }
Example #4
0
        /// <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);
        }
Example #5
0
        public void VariablesTest()
        {
            // set up temp assembly.
            var assembly = Utilities.CreateTempNetAssembly();
            var typeSystem = assembly.NetDirectory.MetadataHeader.TypeSystem;
            var tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>();
            var methodTable = tableStream.GetTable<MethodDefinition>();
            var signatureTable = tableStream.GetTable<StandAloneSignature>();

            var variable = new VariableSignature(typeSystem.String);
            var variable2 = new VariableSignature(typeSystem.Int32);

            // create localvarsig.
            var localVarSig = new LocalVariableSignature();
            localVarSig.Variables.Add(variable);
            localVarSig.Variables.Add(variable2);
            var signature = new StandAloneSignature(localVarSig);
            signatureTable.Add(signature);

            // write code.
            var body = methodTable[0].MethodBody;
            body.Signature = signature;

            body.Instructions.Clear();
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ldloc, variable));
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Pop));
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ldloc, variable2));
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Pop));
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ret));

            // build and validate.
            assembly = Utilities.RebuildNetAssembly(assembly, true);
            methodTable = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>().GetTable<MethodDefinition>();
            var newBody = methodTable[0].MethodBody;

            Assert.IsNotNull(newBody.Signature);
            Assert.IsInstanceOfType(newBody.Signature.Signature, typeof(LocalVariableSignature));

            var newLocalVarSig = (LocalVariableSignature)newBody.Signature.Signature;
            Assert.AreEqual(localVarSig.Variables.Count, newLocalVarSig.Variables.Count);

            for (int i = 0; i < localVarSig.Variables.Count; i++)
                Utilities.ValidateType(localVarSig.Variables[i].VariableType, newLocalVarSig.Variables[i].VariableType);

            Assert.IsInstanceOfType(newBody.Instructions[0].Operand, typeof(VariableSignature));
            Utilities.ValidateType(variable.VariableType,
                ((VariableSignature)newBody.Instructions[0].Operand).VariableType);

            Assert.IsInstanceOfType(newBody.Instructions[2].Operand, typeof(VariableSignature));
            Utilities.ValidateType(variable2.VariableType,
                ((VariableSignature)newBody.Instructions[2].Operand).VariableType);
        }