Example #1
0
        public static bool IsTypeInInheritanceChain(MetadataTypeStruct* typeDefinition, MetadataTypeStruct* chain)
        {
            while (chain != null)
            {
                if (chain == typeDefinition)
                    return true;

                chain = chain->ParentType;
            }

            return false;
        }
 public static MetadataMethodStruct* GetMethodDefinitionAddress(MetadataTypeStruct* data, uint slot)
 {
     return (MetadataMethodStruct*)*((uint*)data + MetadataTypeStruct.MethodsOffset + slot);
 }
Example #3
0
        public static MetadataPRDefinitionStruct* GetProtectedRegionEntryByAddress(uint address, MetadataTypeStruct* exceptionType, MetadataMethodStruct* methodDef)
        {
            var protectedRegionTable = methodDef->ProtectedRegionTable;

            if (protectedRegionTable == null)
                return null;

            uint method = (uint)methodDef->Method;

            if (method == 0)
                return null;

            uint offset = address - method;
            int entries = protectedRegionTable->NumberOfRegions;

            int entry = 0;
            MetadataPRDefinitionStruct* protectedRegionDef = null;
            uint currentStart = uint.MinValue;
            uint currentEnd = uint.MaxValue;

            while (entry < entries)
            {
                var prDef = MetadataPRTableStruct.GetProtectedRegionDefinitionAddress(protectedRegionTable, (uint)entry);

                uint start = prDef->StartOffset;
                uint end = prDef->EndOffset;

                if ((offset >= start) && (offset < end) && (start >= currentStart) && (end < currentEnd))
                {
                    var handlerType = prDef->HandlerType;
                    var exType = prDef->ExceptionType;

                    // If the handler is a finally clause, accept without testing
                    // If the handler is a exception clause, accept if the exception type is in the is within the inheritance chain of the exception object
                    if ((handlerType == ExceptionHandlerType.Finally) ||
                        (handlerType == ExceptionHandlerType.Exception && IsTypeInInheritanceChain(exType, exceptionType)))
                    {
                        protectedRegionDef = prDef;
                        currentStart = start;
                        currentEnd = end;
                    }
                }

                entry++;
            }

            return protectedRegionDef;
        }