Ejemplo n.º 1
0
            public TryHandler(ExceptionHandlingClause clause)
            {
                if (clause.Flags == ExceptionHandlingClauseOptions.Finally)
                {
                    Type = NodeType.TryFinally;
                }
                else
                {
                    Type = NodeType.TryCatch;
                }

                TryStartIp     = clause.TryOffset;
                TryEndIp       = clause.TryOffset + clause.TryLength;
                HandlerStartIp = clause.HandlerOffset;
                HandlerEndIp   = clause.HandlerOffset + clause.HandlerLength;

                if (HandlerStartIp == 0)
                {
                    HandlerStartIp = -1;
                }

                if (HandlerEndIp == 0)
                {
                    HandlerEndIp = -1;
                }
            }
Ejemplo n.º 2
0
        public ProjectingExceptionHandlingClause(ExceptionHandlingClause clause, Projector projector)
            : base(clause)
        {
            Debug.Assert(null != projector);

            _projector = projector;
        }
Ejemplo n.º 3
0
        public _ExceptionRegionInfo(ExceptionHandlingClause aExceptionClause)
        {
            try
            {
                ExceptionClause = aExceptionClause;
                HandlerOffset   = aExceptionClause.HandlerOffset;
                HandlerLength   = aExceptionClause.HandlerLength;
                TryOffset       = aExceptionClause.TryOffset;
                TryLength       = aExceptionClause.TryLength;

                if (aExceptionClause.Flags == ExceptionHandlingClauseOptions.Clause)
                {
                    Kind      = ExceptionRegionKind.Catch;
                    CatchType = aExceptionClause.CatchType;
                }
                else if (aExceptionClause.Flags.HasFlag(ExceptionHandlingClauseOptions.Fault))
                {
                    Kind = ExceptionRegionKind.Fault;
                }
                else if (aExceptionClause.Flags.HasFlag(ExceptionHandlingClauseOptions.Filter))
                {
                    Kind         = ExceptionRegionKind.Filter;
                    FilterOffset = aExceptionClause.FilterOffset;
                }
                else if (aExceptionClause.Flags.HasFlag(ExceptionHandlingClauseOptions.Finally))
                {
                    Kind = ExceptionRegionKind.Finally;
                }
            }
            catch
            {
                // ignored
            }
        }
Ejemplo n.º 4
0
        public _ExceptionRegionInfo(ExceptionHandlingClause aExceptionClause)
        {
            try
            {
                ExceptionClause = aExceptionClause;
                HandlerOffset   = aExceptionClause.HandlerOffset;
                HandlerLength   = aExceptionClause.HandlerLength;
                TryOffset       = aExceptionClause.TryOffset;
                TryLength       = aExceptionClause.TryLength;

                if (aExceptionClause.Flags == ExceptionHandlingClauseOptions.Clause)
                {
                    Kind      = ExceptionRegionKind.Catch;
                    CatchType = aExceptionClause.CatchType;
                }
                else if (aExceptionClause.Flags.HasFlag(ExceptionHandlingClauseOptions.Fault))
                {
                    Kind = ExceptionRegionKind.Fault;
                }
                else if (aExceptionClause.Flags.HasFlag(ExceptionHandlingClauseOptions.Filter))
                {
                    Kind         = ExceptionRegionKind.Filter;
                    FilterOffset = aExceptionClause.FilterOffset;
                    CatchType    = typeof(System.Exception); //TODO: Confirm that this is correct.
                }
                else if (aExceptionClause.Flags.HasFlag(ExceptionHandlingClauseOptions.Finally))
                {
                    Kind = ExceptionRegionKind.Finally;
                }
            }
            catch
            {
                // ignored
            }
        }
Ejemplo n.º 5
0
        public ProjectingExceptionHandlingClause(ExceptionHandlingClause clause, Projector projector)
            : base(clause)
        {
            Contract.Requires(null != projector);

            _projector = projector;
        }
Ejemplo n.º 6
0
 public ILOpCode(ILCode aCode, int aPosition, int aNextPosition, ExceptionHandlingClause aEhc)
 {
     Code         = aCode;
     Position     = aPosition;
     NextPosition = aNextPosition;
     Ehc          = aEhc;
 }
Ejemplo n.º 7
0
 private static Type GetCatchType(ExceptionHandlingClause clause)
 {
     try
     {
         return(clause.CatchType);
     }
     catch
     {
         return(null);
     }
 }
Ejemplo n.º 8
0
            public override ExceptionHandlingClause ProjectExceptionHandlingClause(ExceptionHandlingClause value)
            {
                if (value == null)
                {
                    return(null);
                }

                Debug.Assert(NeedsProjection(value));

                return(new ProjectingExceptionHandlingClause(value, this));
            }
Ejemplo n.º 9
0
 private static int GetFilterOffset(ExceptionHandlingClause clause)
 {
     try
     {
         return(clause.FilterOffset);
     }
     catch
     {
         return(-1);
     }
 }
        public bool IsCatchBlockBegin(int ilOffset, out ExceptionHandlingClause exceptionHandlingClause)
        {
            exceptionHandlingClause = null;
            int handlingClauseIndex;

            if (NonIlOffsets.IsCatchBlockBegin(ilOffset, out handlingClauseIndex))
            {
                exceptionHandlingClause = _exceptionHandlingClauses[handlingClauseIndex];
                return(true);
            }
            return(false);
        }
        private bool IsTryBlockEnd(int ilOffset, out ExceptionHandlingClause exceptionHandlingClause)
        {
            exceptionHandlingClause = null;
            int handlingClauseIndex;

            if (NonIlOffsets.IsTryBlockEnd(ilOffset, out handlingClauseIndex))
            {
                exceptionHandlingClause = _exceptionHandlingClauses[handlingClauseIndex];
                return(true);
            }
            return(false);
        }
 public ExceptionHandlingClauseData Build(ExceptionHandlingClause c)
 {
     return(new ExceptionHandlingClauseData
     {
         Flags = c.Flags,
         CatchType =
             c.Flags == ExceptionHandlingClauseOptions.Clause
                        ? typeInfoDataConverter.Build(c.CatchType)
                        : null,
         TryOffset = c.TryOffset,
         TryLength = c.TryLength,
         HandlerOffset = c.HandlerOffset,
         HandlerLength = c.HandlerLength
     });
 }
            public TryHandler(ExceptionHandlingClause clause)
            {
                TryStartIp     = clause.TryOffset;
                TryEndIp       = clause.TryOffset + clause.TryLength;
                HandlerStartIp = clause.HandlerOffset;
                HandlerEndIp   = clause.HandlerOffset + clause.HandlerLength;

                if (HandlerStartIp == 0)
                {
                    HandlerStartIp = -1;
                }

                if (HandlerEndIp == 0)
                {
                    HandlerEndIp = -1;
                }
            }
Ejemplo n.º 14
0
        internal OpCodeType(ILCode aCode, int aPosition, int aNextPosition, ExceptionHandlingClause aEhc)
        {
            ILCode       = aCode;
            Position     = aPosition;
            NextPosition = aNextPosition;
            Handler      = aEhc;

            NeedHandler = (aEhc != null) &&
                          ((aEhc.HandlerOffset == Position) || (((aEhc.Flags & ExceptionHandlingClauseOptions.Filter) != 0) && aEhc.FilterOffset == Position)) &&
                          (aEhc.Flags == ExceptionHandlingClauseOptions.Clause);

            HandlerRef      = ".Error";
            HandlerPosition = -1;
            if (aEhc != null && aEhc.HandlerOffset > Position)
            {
                HandlerPosition = aEhc.HandlerOffset;
                HandlerRef      = Helper.GetLabel(aEhc.HandlerOffset);
            }
        }
Ejemplo n.º 15
0
        public static ExceptionHandler ToDNLib(this ExceptionHandlingClause clause, ModuleDef module, IList <Instruction> instructions)
        {
            ExceptionHandlerType type   = clause.Flags.ToDNLib();
            ExceptionHandler     result = new ExceptionHandler(type);

            if (clause.CatchType != null)
            {
                result.CatchType = module.ToSig(clause.CatchType.ToSignature()).ToTypeDefOrRef();
            }
            if (type.HasFlag(ExceptionHandlerType.Filter))
            {
                result.FilterStart = instructions.AtOffset(clause.FilterOffset);
            }
            result.HandlerStart = instructions.AtOffset(clause.HandlerOffset);
            result.HandlerEnd   = instructions.AtOffset(clause.HandlerOffset + clause.HandlerLength);
            result.TryStart     = instructions.AtOffset(clause.TryOffset);
            result.TryEnd       = instructions.AtOffset(clause.TryOffset + clause.TryLength);
            return(result);
        }
Ejemplo n.º 16
0
 public OpField(Code aOpCode, int aPos, int aNextPos, FieldInfo aValue, ExceptionHandlingClause aCurrentExceptionHandler)
     : base(aOpCode, aPos, aNextPos, aCurrentExceptionHandler)
 {
     Value = aValue;
 }
Ejemplo n.º 17
0
        internal OpMethod(ILCode aCode, int aPosition, int aNextPosition, MethodBase aValue, ExceptionHandlingClause aEhc)
            : base(aCode, aPosition, aNextPosition, aEhc)
        {
            Value             = aValue;
            MethodUID         = aValue.GetHashCode();
            CallingConvention = CallingConvention.StdCall;

            var attrib = Value.GetCustomAttribute <DllImportAttribute>();

            if (attrib != null)
            {
                CallingConvention = attrib.CallingConvention;
            }
        }
Ejemplo n.º 18
0
Archivo: OpNone.cs Proyecto: vdt/AtomOS
 public OpNone(ILCode aCode, int aPosition, int aNextPosition, ExceptionHandlingClause aEhc)
     : base(aCode, aPosition, aNextPosition, aEhc)
 {
 }
Ejemplo n.º 19
0
 public OpSingle(ILCode aCode, int aPosition, int aNextPosition, float aValue, ExceptionHandlingClause aEhc)
     : base(aCode, aPosition, aNextPosition, aEhc)
 {
     Value = aValue;
 }
Ejemplo n.º 20
0
		internal unsafe static uint CallFilter (InternalSystem.Exception exception, ExceptionHandlingClause handler, void* callerBP)
		{
			Diagnostics.Error ("Unimplemented - ExceptionHandling.CallFilter");

			return 0;
		}
Ejemplo n.º 21
0
 public OpString(ILCode aCode, int aPosition, int aNextPosition, string aValue, ExceptionHandlingClause aEhc)
     : base(aCode, aPosition, aNextPosition, aEhc)
 {
     Value = aValue;
 }
Ejemplo n.º 22
0
 public OpNone(Code aOpCode, int aPos, int aNextPos, ExceptionHandlingClause aCurrentExceptionHandler)
     : base(aOpCode, aPos, aNextPos, aCurrentExceptionHandler)
 {
 }
Ejemplo n.º 23
0
 public abstract ExceptionHandlingClause ProjectExceptionHandlingClause(ExceptionHandlingClause value);
Ejemplo n.º 24
0
 internal OpToken(ILCode aCode, int aPosition, int aNextPosition, int aValue, Module aModule, Type[] aTypeGenericArgs, Type[] aMethodGenericArgs, ExceptionHandlingClause aEhc)
     : base(aCode, aPosition, aNextPosition, aEhc)
 {
     Value = aValue;
     if (IsField)
     {
         ValueField = aModule.ResolveField(Value, aTypeGenericArgs, aMethodGenericArgs);
     }
     if (IsType)
     {
         ValueType = aModule.ResolveType(Value, aTypeGenericArgs, aMethodGenericArgs);
     }
 }
Ejemplo n.º 25
0
        public static void TestMethodBody1()
        {
            using (MetadataLoadContext lc = new MetadataLoadContext(new CoreMetadataAssemblyResolver(), "mscorlib"))
            {
                Assembly coreAssembly = lc.LoadFromStream(TestUtils.CreateStreamForCoreAssembly());
                Assembly a            = lc.LoadFromByteArray(TestData.s_AssemblyWithMethodBodyImage);

                Type       nonsense = a.GetType("Nonsense`1", throwOnError: true);
                Type       theT     = nonsense.GetTypeInfo().GenericTypeParameters[0];
                MethodInfo m        = nonsense.GetMethod("Foo");
                Type       theM     = m.GetGenericArguments()[0];
                MethodBody mb       = m.GetMethodBody();
                byte[]     il       = mb.GetILAsByteArray();
                Assert.Equal <byte>(TestData.s_AssemblyWithMethodBodyILBytes, il);
                Assert.Equal(4, mb.MaxStackSize);
                Assert.True(mb.InitLocals);
                Assert.Equal(0x11000001, mb.LocalSignatureMetadataToken);

                IList <LocalVariableInfo> lvis = mb.LocalVariables;
                Assert.Equal(10, lvis.Count);

                Assert.Equal(0, lvis[0].LocalIndex);
                Assert.False(lvis[0].IsPinned);
                Assert.Equal(coreAssembly.GetType("System.Single", throwOnError: true), lvis[0].LocalType);

                Assert.Equal(1, lvis[1].LocalIndex);
                Assert.False(lvis[1].IsPinned);
                Assert.Equal(coreAssembly.GetType("System.Double", throwOnError: true), lvis[1].LocalType);

                Assert.Equal(2, lvis[2].LocalIndex);
                Assert.False(lvis[2].IsPinned);
                Assert.Equal(theT, lvis[2].LocalType);

                Assert.Equal(3, lvis[3].LocalIndex);
                Assert.False(lvis[3].IsPinned);
                Assert.Equal(theT.MakeArrayType(), lvis[3].LocalType);

                Assert.Equal(4, lvis[4].LocalIndex);
                Assert.False(lvis[4].IsPinned);
                Assert.Equal(coreAssembly.GetType("System.Collections.Generic.IList`1", throwOnError: true).MakeGenericType(theM), lvis[4].LocalType);

                Assert.Equal(5, lvis[5].LocalIndex);
                Assert.False(lvis[5].IsPinned);
                Assert.Equal(coreAssembly.GetType("System.String", throwOnError: true), lvis[5].LocalType);

                Assert.Equal(6, lvis[6].LocalIndex);
                Assert.False(lvis[6].IsPinned);
                Assert.Equal(coreAssembly.GetType("System.Int32", throwOnError: true).MakeArrayType(), lvis[6].LocalType);

                Assert.Equal(7, lvis[7].LocalIndex);
                Assert.True(lvis[7].IsPinned);
                Assert.Equal(coreAssembly.GetType("System.Int32", throwOnError: true).MakeByRefType(), lvis[7].LocalType);

                Assert.Equal(8, lvis[8].LocalIndex);
                Assert.False(lvis[8].IsPinned);
                Assert.Equal(coreAssembly.GetType("System.Int32", throwOnError: true).MakeArrayType(), lvis[8].LocalType);

                Assert.Equal(9, lvis[9].LocalIndex);
                Assert.False(lvis[9].IsPinned);
                Assert.Equal(coreAssembly.GetType("System.Boolean", throwOnError: true), lvis[9].LocalType);

                IList <ExceptionHandlingClause> ehcs = mb.ExceptionHandlingClauses;
                Assert.Equal(2, ehcs.Count);

                ExceptionHandlingClause ehc = ehcs[0];
                Assert.Equal(ExceptionHandlingClauseOptions.Finally, ehc.Flags);
                Assert.Equal(97, ehc.TryOffset);
                Assert.Equal(41, ehc.TryLength);
                Assert.Equal(138, ehc.HandlerOffset);
                Assert.Equal(5, ehc.HandlerLength);

                ehc = ehcs[1];
                Assert.Equal(ExceptionHandlingClauseOptions.Filter, ehc.Flags);
                Assert.Equal(88, ehc.TryOffset);
                Assert.Equal(58, ehc.TryLength);
                Assert.Equal(172, ehc.HandlerOffset);
                Assert.Equal(16, ehc.HandlerLength);
                Assert.Equal(146, ehc.FilterOffset);
            }
        }
Ejemplo n.º 26
0
		private static void PrintExceptionHandlingClauses (ExceptionHandlingClause exceptionHandlingClause)
		{
			Debug.COM1.Write ("\t\tType: ");
			Debug.COM1.WriteNumber ((int) exceptionHandlingClause.ExceptionType, true);
			Debug.COM1.WriteLine ();

			if (exceptionHandlingClause.ExceptionType == ExceptionHandlerType.Catch) {
				Testcase.Test (exceptionHandlingClause.TypeInfo != null, "Runtime", "exceptionHandlingClause.TypeInfo != null");

				if (exceptionHandlingClause.TypeInfo != null) {
					Debug.COM1.Write ("\t\tCatch Type: ");
					Debug.COM1.WriteLine (exceptionHandlingClause.TypeInfo.Name);
				}
			}
		}
Ejemplo n.º 27
0
		/// <summary>
		/// Calls the handler.
		/// </summary>
		/// <param name="exception">The exception.</param>
		/// <param name="getNewHandler">if set to <c>true</c> [get new handler].</param>
		/// <param name="handler">The handler.</param>
		/// <param name="i">The i.</param>
		/// <param name="clauseIndex">Index of the clause.</param>
		/// <param name="callerBP">The caller BP.</param>
		/// <returns></returns>
		private unsafe static bool CallHandler (InternalSystem.Exception exception, bool getNewHandler, ExceptionHandlingClause handler, int i, int clauseIndex, void* callerBP)
		{
			if (handler != null) {
#if DEBUG_EXCEPTION_HANDLING
				Debug.COM1.Write ("Calling the found handler from Frame: #");
				Debug.COM1.WriteNumber ((int) (i - exception.IgnoreStackFramesCount), false);
				Debug.COM1.Write ("/Clause: #");
				Debug.COM1.WriteNumber ((int) clauseIndex, false);
				Debug.COM1.WriteLine ();
#endif

				exception.CallingStack [i].IgnoreMethodBoundaryClause [clauseIndex] = true;

				if (handler.ExceptionType == ExceptionHandlerType.Finally
						|| handler.ExceptionType == ExceptionHandlerType.Fault) {
					ExceptionHandling.CallFinallyFault (exception, handler, callerBP);

				} else if (handler.ExceptionType == ExceptionHandlerType.Filter) {
					// If it is a filter and it fails look for the next Exception Handler
					if (ExceptionHandling.CallFilter (exception, handler, callerBP) == 0)
						getNewHandler = true;

					else
						ExceptionHandling.CallHandler (exception, handler, callerBP);

				} else if (handler.ExceptionType == ExceptionHandlerType.Catch)
					ExceptionHandling.CallHandler (exception, handler, callerBP);
			}

			return getNewHandler;
		}
Ejemplo n.º 28
0
		internal unsafe static void CallFinallyFault (InternalSystem.Exception exception, ExceptionHandlingClause handler, void* callerBP)
		{
			Diagnostics.Error ("Unimplemented - ExceptionHandling.CallFinallyFault");
		}
Ejemplo n.º 29
0
 internal OpField(ILCode aCode, int aPosition, int aNextPosition, FieldInfo aValue, ExceptionHandlingClause aEhc)
     : base(aCode, aPosition, aNextPosition, aEhc)
 {
     Value = aValue;
 }
Ejemplo n.º 30
0
 internal OpBranch(ILCode aCode, int aPosition, int aNextPosition, int aValue, ExceptionHandlingClause aEhc)
     : base(aCode, aPosition, aNextPosition, aEhc)
 {
     Value = aValue;
 }
Ejemplo n.º 31
0
        //public static Dictionary<MethodBase, uint> MethodUIDs = new Dictionary<MethodBase, uint>();

        public OpMethod(ILCode aCode, int aPosition, int aNextPosition, MethodBase aValue, ExceptionHandlingClause aEhc)
            : base(aCode, aPosition, aNextPosition, aEhc)
        {
            Value     = aValue;
            MethodUID = 0;

            if (aValue.IsAbstract)
            {
                MethodUID = (uint)aValue.GetHashCode();

                /*
                 * if (MethodUIDs.ContainsKey(aValue))
                 * {
                 *  MethodUID = MethodUIDs[aValue];
                 * }
                 * else
                 * {
                 *  MethodUID = Counter++;
                 *  MethodUIDs.Add(aValue, MethodUID);
                 * }*/
            }
        }
Ejemplo n.º 32
0
 public OpType(Code aOpCode, int aPos, int aNextPos, Type aValue, ExceptionHandlingClause aCurrentExceptionHandler)
     : base(aOpCode, aPos, aNextPos, aCurrentExceptionHandler)
 {
     Value = aValue;
 }
        public DelegatingExceptionHandlingClause(ExceptionHandlingClause clause)
        {
            Contract.Requires(clause != null);

            _clause = clause;
        }
Ejemplo n.º 34
0
        public static void TestEHClauses()
        {
            using (MetadataLoadContext lc = new MetadataLoadContext(new CoreMetadataAssemblyResolver(), "mscorlib"))
            {
                Assembly coreAssembly = lc.LoadFromStream(TestUtils.CreateStreamForCoreAssembly());
                Assembly a            = lc.LoadFromByteArray(TestData.s_AssemblyWithEhClausesImage);

                Type gt   = a.GetType("G`1", throwOnError: true);
                Type et   = a.GetType("MyException`2", throwOnError: true);
                Type gtP0 = gt.GetGenericTypeParameters()[0];
                Type etP0 = et.GetGenericTypeParameters()[0];
                Type etP1 = et.GetGenericTypeParameters()[1];
                {
                    MethodInfo m    = gt.GetMethod("Catch");
                    Type       theM = m.GetGenericArguments()[0];

                    MethodBody body = m.GetMethodBody();
                    IList <ExceptionHandlingClause> ehs = body.ExceptionHandlingClauses;
                    Assert.Equal(1, ehs.Count);
                    ExceptionHandlingClause eh = ehs[0];
                    Assert.Equal(ExceptionHandlingClauseOptions.Clause, eh.Flags);
                    Assert.Equal(1, eh.TryOffset);
                    Assert.Equal(15, eh.TryLength);
                    Assert.Equal(16, eh.HandlerOffset);
                    Assert.Equal(16, eh.HandlerLength);
                    Assert.Throws <InvalidOperationException>(() => eh.FilterOffset);
                    Assert.Equal(et.MakeGenericType(gtP0, theM), eh.CatchType);
                }

                {
                    Type sysInt32  = coreAssembly.GetType("System.Int32", throwOnError: true);
                    Type sysSingle = coreAssembly.GetType("System.Single", throwOnError: true);

                    MethodInfo m = gt.MakeGenericType(sysInt32).GetMethod("Catch").MakeGenericMethod(sysSingle);

                    MethodBody body = m.GetMethodBody();
                    IList <ExceptionHandlingClause> ehs = body.ExceptionHandlingClauses;
                    Assert.Equal(1, ehs.Count);
                    ExceptionHandlingClause eh = ehs[0];
                    Assert.Equal(ExceptionHandlingClauseOptions.Clause, eh.Flags);
                    Assert.Equal(1, eh.TryOffset);
                    Assert.Equal(15, eh.TryLength);
                    Assert.Equal(16, eh.HandlerOffset);
                    Assert.Equal(16, eh.HandlerLength);
                    Assert.Throws <InvalidOperationException>(() => eh.FilterOffset);
                    Assert.Equal(et.MakeGenericType(sysInt32, sysSingle), eh.CatchType);
                }

                {
                    MethodInfo m    = gt.GetMethod("Finally");
                    MethodBody body = m.GetMethodBody();
                    IList <ExceptionHandlingClause> ehs = body.ExceptionHandlingClauses;
                    Assert.Equal(1, ehs.Count);
                    ExceptionHandlingClause eh = ehs[0];
                    Assert.Equal(ExceptionHandlingClauseOptions.Finally, eh.Flags);
                    Assert.Equal(1, eh.TryOffset);
                    Assert.Equal(15, eh.TryLength);
                    Assert.Equal(16, eh.HandlerOffset);
                    Assert.Equal(14, eh.HandlerLength);
                    Assert.Throws <InvalidOperationException>(() => eh.FilterOffset);
                    Assert.Throws <InvalidOperationException>(() => eh.CatchType);
                }

                {
                    MethodInfo m    = gt.GetMethod("Fault");
                    MethodBody body = m.GetMethodBody();
                    IList <ExceptionHandlingClause> ehs = body.ExceptionHandlingClauses;
                    Assert.Equal(1, ehs.Count);
                    ExceptionHandlingClause eh = ehs[0];
                    Assert.Equal(ExceptionHandlingClauseOptions.Fault, eh.Flags);
                    Assert.Equal(1, eh.TryOffset);
                    Assert.Equal(15, eh.TryLength);
                    Assert.Equal(16, eh.HandlerOffset);
                    Assert.Equal(14, eh.HandlerLength);
                    Assert.Throws <InvalidOperationException>(() => eh.FilterOffset);
                    Assert.Throws <InvalidOperationException>(() => eh.CatchType);
                }

                {
                    MethodInfo m    = gt.GetMethod("Filter");
                    MethodBody body = m.GetMethodBody();
                    IList <ExceptionHandlingClause> ehs = body.ExceptionHandlingClauses;
                    Assert.Equal(1, ehs.Count);
                    ExceptionHandlingClause eh = ehs[0];
                    Assert.Equal(ExceptionHandlingClauseOptions.Filter, eh.Flags);
                    Assert.Equal(1, eh.TryOffset);
                    Assert.Equal(15, eh.TryLength);
                    Assert.Equal(40, eh.HandlerOffset);
                    Assert.Equal(16, eh.HandlerLength);
                    Assert.Equal(16, eh.FilterOffset);
                    Assert.Throws <InvalidOperationException>(() => eh.CatchType);
                }
            }
        }
Ejemplo n.º 35
0
        /// <summary>
        /// Reads the method header from the instruction stream.
        /// </summary>
        /// <param name="reader">The reader used to decode the instruction stream.</param>
        /// <returns></returns>
        private MethodHeader ReadMethodHeader(EndianAwareBinaryReader reader)
        {
            MethodHeader header = new MethodHeader();

            // Read first byte
            header.Flags = (MethodFlags)reader.ReadByte();

            // Check least significant 2 bits
            switch (header.Flags & MethodFlags.HeaderMask)
            {
                case MethodFlags.TinyFormat:
                    header.CodeSize = ((uint)(header.Flags & MethodFlags.TinyCodeSizeMask) >> 2);
                    header.Flags &= MethodFlags.HeaderMask;
                    break;

                case MethodFlags.FatFormat:
                    // Read second byte of flags
                    header.Flags = (MethodFlags)(reader.ReadByte() << 8 | (byte)header.Flags);
                    if (MethodFlags.ValidHeader != (header.Flags & MethodFlags.HeaderSizeMask))
                        throw new InvalidDataException(@"Invalid method header.");
                    header.MaxStack = reader.ReadUInt16();
                    header.CodeSize = reader.ReadUInt32();
                    header.LocalsSignature = new Token(reader.ReadUInt32()); // ReadStandAloneSigRow
                    break;

                default:
                    throw new InvalidDataException(@"Invalid method header while trying to decode " + this.methodCompiler.Method.FullName + ". (Flags = " + header.Flags.ToString("X") + ", Rva = " + this.methodCompiler.Method.Rva + ")");
            }

            // Are there sections following the code?
            if (MethodFlags.MoreSections != (header.Flags & MethodFlags.MoreSections))
                return header;

            // Yes, seek to them and process those sections
            long codepos = reader.BaseStream.Position;

            // Seek to the end of the code...
            long dataSectPos = codepos + header.CodeSize;
            if (0 != (dataSectPos & 3))
                dataSectPos += (4 - (dataSectPos % 4));
            reader.BaseStream.Position = dataSectPos;

            // Read all headers, so the IL decoder knows how to handle these...
            byte flags;

            do
            {
                flags = reader.ReadByte();
                bool isFat = (0x40 == (flags & 0x40));
                int length;
                int blocks;
                if (isFat)
                {
                    byte a = reader.ReadByte();
                    byte b = reader.ReadByte();
                    byte c = reader.ReadByte();

                    length = (c << 24) | (b << 16) | a;
                    blocks = (length - 4) / 24;
                }
                else
                {
                    length = reader.ReadByte();
                    blocks = (length - 4) / 12;

                    /* Read & skip the padding. */
                    reader.ReadInt16();
                }

                Debug.Assert(0x01 == (flags & 0x3F), @"Unsupported method data section.");

                // Read the clause
                for (int i = 0; i < blocks; i++)
                {
                    ExceptionHandlingClause clause = new ExceptionHandlingClause();
                    clause.Read(reader, isFat);
                    methodCompiler.ExceptionClauseHeader.AddClause(clause);
                }
            }
            while (0x80 == (flags & 0x80));

            reader.BaseStream.Position = codepos;

            return header;
        }
Ejemplo n.º 36
0
        /// <summary>
        /// Traces the specified label.
        /// </summary>
        /// <param name="label">The label.</param>
        private void Trace(int label, ExceptionHandlingClause clause)
        {
            outgoingStack = new Stack<Operand>[basicBlocks.Count];
            scheduledMoves = new Stack<Operand>[basicBlocks.Count];
            processed = new BitArray(basicBlocks.Count);
            processed.SetAll(false);
            enqueued = new BitArray(basicBlocks.Count);
            enqueued.SetAll(false);

            var firstBlock = FindBlock(label);
            processed.Set(firstBlock.Sequence, true);
            workList.Enqueue(new WorkItem(firstBlock, new Stack<Operand>()));

            while (workList.Count > 0)
            {
                AssignOperands(workList.Dequeue());
            }
        }