Inheritance: BlobSignature
Example #1
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 #2
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 #3
0
 public int GetVariableIndex(VariableSignature variable)
 {
     var localVarSig = _owner.Signature != null ? _owner.Signature.Signature as LocalVariableSignature : null;
     if (localVarSig == null)
         throw new InvalidOperationException("Method body does not have a valid local variable signature.");
     return localVarSig.Variables.IndexOf(variable);
 }
Example #4
0
 public static MsilInstruction Create(MsilOpCode code, VariableSignature operand)
 {
     if (code.OperandType != MsilOperandType.InlineVar && code.OperandType != MsilOperandType.ShortInlineVar)
         throw new ArgumentException("Opcode does not accept a local variable operand.", "code");
     return new MsilInstruction(0, code, operand);
 }
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);
        }