Inheritance: AsmResolver.Builder.FileSegmentBuilder, IOperandResolver
Exemple #1
0
        public static void ValidateCode(string code, MethodBody body)
        {
            var builder = new StringBuilder();

            foreach (var instruction in body.Instructions)
            {
                builder.AppendLine(String.Format("{0}{1}", instruction.OpCode.Name,
                                                 instruction.Operand != null ? " " + instruction.OperandToString() : String.Empty));
            }
            Assert.IsTrue(code.TrimEnd() == builder.ToString().TrimEnd());
        }
        public static ExceptionHandler FromReader(MethodBody methodBody, IBinaryStreamReader reader, bool fatFormat)
        {
            var offset = reader.Position;
            var handerType = fatFormat ? reader.ReadUInt32() : reader.ReadUInt16();
            var tryOffset = fatFormat ? reader.ReadInt32() : reader.ReadUInt16();
            var tryLength = fatFormat ? reader.ReadInt32() : reader.ReadByte();
            var handlerOffset = fatFormat ? reader.ReadInt32() : reader.ReadUInt16();
            var handlerLength = fatFormat ? reader.ReadInt32() : reader.ReadByte();
            var classTokenOrFilterOffset = reader.ReadUInt32();

            var handler = new ExceptionHandler((ExceptionHandlerType)handerType)
            {
                StartOffset = offset,
                IsFat = fatFormat,
                TryStart = methodBody.GetInstructionByOffset(tryOffset),
                TryEnd = methodBody.GetInstructionByOffset(tryOffset + tryLength),
                HandlerStart = methodBody.GetInstructionByOffset(handlerOffset),
                HandlerEnd = methodBody.GetInstructionByOffset(handlerOffset + handlerLength),
            };

            switch (handler.HandlerType)
            {
                case ExceptionHandlerType.Exception:
                    handler.CatchType = (ITypeDefOrRef)((IOperandResolver)methodBody).ResolveMember(new MetadataToken(classTokenOrFilterOffset));
                    break;
                case ExceptionHandlerType.Filter:
                    handler.FilterStart = methodBody.GetInstructionByOffset((int)classTokenOrFilterOffset);
                    break;
            }

            return handler;
        }
Exemple #3
0
 public MethodBodyOperandBuilder(NetBuildingContext buildingContext, MethodBody owner)
 {
     if (buildingContext == null)
         throw new ArgumentNullException("buildingContext");
     if (owner == null)
         throw new ArgumentNullException("owner");
     _buildingContext = buildingContext;
     _owner = owner;
 }
Exemple #4
0
        public static MethodBody FromReadingContext(MethodDefinition method, ReadingContext context)
        {
            var reader = context.Reader;
            var body = new MethodBody(method)
            {
                StartOffset = reader.Position,
            };

            var bodyHeader = reader.ReadByte();
            uint codeSize;

            if ((bodyHeader & 0x3) == 0x3)
            {
                reader.Position--;
                var fatBodyHeader = reader.ReadUInt16();
                var headerSize = (fatBodyHeader >> 12) * 4;

                var hasSections = (fatBodyHeader & 0x8) == 0x8;
                body.InitLocals = (fatBodyHeader & 0x10) == 0x10;
                body.MaxStack = reader.ReadUInt16();
                codeSize = reader.ReadUInt32();

                var localVarSig = reader.ReadUInt32();
                if (localVarSig != 0)
                {
                    var header = method.Header;
                    var tableStream = header.GetStream<TableStream>();

                    MetadataMember signature;
                    tableStream.TryResolveMember(new MetadataToken(localVarSig), out signature);
                    body.Signature = signature as StandAloneSignature;
                }

                if (hasSections)
                {
                    body._sectionReadingContext = context.CreateSubContext(reader.Position + codeSize);
                    body._sectionReadingContext.Reader.Align(4);
                }
            }
            else if ((bodyHeader & 0x2) == 0x2)
            {
                codeSize = (uint)(bodyHeader >> 2);
                body.MaxStack = 8;
            }
            else
                throw new ArgumentException("Invalid method body header signature.");

            body._msilReadingContext = context.CreateSubContext(reader.Position, (int)codeSize);
            return body;
        }
Exemple #5
0
 public static void ValidateCode(string code, MethodBody body)
 {
     var builder = new StringBuilder();
     foreach (var instruction in body.Instructions)
     {
         builder.AppendLine(String.Format("{0}{1}", instruction.OpCode.Name,
             instruction.Operand != null ? " " + instruction.OperandToString() : String.Empty));
     }
     Assert.IsTrue(code.TrimEnd() == builder.ToString().TrimEnd());
 }