public static unsafe bool TryGetNamedTypeForTypeReference(MetadataReader metadataReader, TypeReferenceHandle typeRefHandle, out RuntimeTypeHandle runtimeTypeHandle)
        {
            IntPtr moduleHandle = ModuleList.Instance.GetModuleForMetadataReader(metadataReader);
            MetadataTable mapTable = MetadataTable.CreateTypeMapTable(moduleHandle);
            foreach (var ptrEntry in mapTable)
            {
                TypeMapEntry* pCurrentEntry = (TypeMapEntry*)ptrEntry;
                if (pCurrentEntry->TypeDefinitionHandle.AsHandle().Equals(typeRefHandle))
                {
                    runtimeTypeHandle = RuntimeAugments.CreateRuntimeTypeHandle(pCurrentEntry->EEType);
                    return true;
                }
            }

            runtimeTypeHandle = default(RuntimeTypeHandle);
            return false;
        }
        } // Read

        public static uint Read(this NativeReader reader, uint offset, out TypeReferenceHandle[] values)
        {
            uint count;
            offset = reader.DecodeUnsigned(offset, out count);
            if (count == 0)
            {
                values = s_emptyTypeReferenceHandleArray;
            }
            else
            {
                values = new TypeReferenceHandle[count];
                for (uint i = 0; i < count; ++i)
                {
                    TypeReferenceHandle tmp;
                    offset = reader.Read(offset, out tmp);
                    values[i] = tmp;
                }
            }
            return offset;
        } // Read
        } // Read

        public static uint Read(this NativeReader reader, uint offset, out TypeReferenceHandle handle)
        {
            uint value;
            offset = reader.DecodeUnsigned(offset, out value);
            handle = new TypeReferenceHandle((int)value);
            handle._Validate();
            return offset;
        } // Read
 public abstract bool TryGetNamedTypeForTypeReference(MetadataReader metadataReader, TypeReferenceHandle typeRefHandle, out RuntimeTypeHandle runtimeTypeHandle);
 public abstract bool TryGetTypeReferenceForNamedType(RuntimeTypeHandle runtimeTypeHandle, out MetadataReader metadataReader, out TypeReferenceHandle typeRefHandle);
        } // IsNull

        internal bool IsNull(TypeReferenceHandle handle)
        {
            return (handle._value & 0x00FFFFFF) == 0;
        } // IsNull
        } // ToHandle

        internal Handle ToHandle(TypeReferenceHandle handle)
        {
            return new Handle(handle._value);
        } // ToHandle
        } // GetTypeInstantiationSignature

        public TypeReference GetTypeReference(TypeReferenceHandle handle)
        {
            var record = new TypeReference() { _reader = this, _handle = handle };
            var offset = (uint)handle.Offset;
            offset = _streamReader.Read(offset, out record._parentNamespaceOrType);
            offset = _streamReader.Read(offset, out record._typeName);
            offset = _streamReader.Read(offset, out record._customAttributes);
            return record;
        } // GetTypeReference
        } // Equals

        public bool Equals(TypeReferenceHandle handle)
        {
            return _value == handle._value;
        } // Equals
        public unsafe static bool TryGetNamedTypeForTypeReference(MetadataReader metadataReader, TypeReferenceHandle typeRefHandle, out RuntimeTypeHandle runtimeTypeHandle)
        {
            int hashCode = typeRefHandle.ComputeHashCode(metadataReader);

            IntPtr moduleHandle = ModuleList.Instance.GetModuleForMetadataReader(metadataReader);

            NativeReader typeMapReader;
            if (TryGetNativeReaderForBlob(moduleHandle, ReflectionMapBlob.TypeMap, out typeMapReader))
            {
                NativeParser typeMapParser = new NativeParser(typeMapReader, 0);
                NativeHashtable typeHashtable = new NativeHashtable(typeMapParser);

                ExternalReferencesTable externalReferences = default(ExternalReferencesTable);
                externalReferences.InitializeCommonFixupsTable(moduleHandle);

                var lookup = typeHashtable.Lookup(hashCode);
                NativeParser entryParser;
                while (!(entryParser = lookup.GetNext()).IsNull)
                {
                    var foundTypeIndex = entryParser.GetUnsigned();
                    if (entryParser.GetUnsigned().AsHandle().Equals(typeRefHandle))
                    {
                        runtimeTypeHandle = externalReferences.GetRuntimeTypeHandleFromIndex(foundTypeIndex);
                        return true;
                    }
                }
            }

            runtimeTypeHandle = default(RuntimeTypeHandle);
            return false;
        }
        public unsafe static bool TryGetTypeReferenceForNamedType(RuntimeTypeHandle runtimeTypeHandle, out MetadataReader metadataReader, out TypeReferenceHandle typeRefHandle)
        {
            // Iterate over all modules, starting with the module that defines the EEType
            foreach (IntPtr moduleHandle in ModuleList.Enumerate(RuntimeAugments.GetModuleFromTypeHandle(runtimeTypeHandle)))
            {
                NativeReader typeMapReader;
                if (TryGetNativeReaderForBlob(moduleHandle, ReflectionMapBlob.TypeMap, out typeMapReader))
                {
                    NativeParser typeMapParser = new NativeParser(typeMapReader, 0);
                    NativeHashtable typeHashtable = new NativeHashtable(typeMapParser);

                    ExternalReferencesTable externalReferences = default(ExternalReferencesTable);
                    externalReferences.InitializeCommonFixupsTable(moduleHandle);

                    var lookup = typeHashtable.Lookup(runtimeTypeHandle.GetHashCode());
                    NativeParser entryParser;
                    while (!(entryParser = lookup.GetNext()).IsNull)
                    {
                        RuntimeTypeHandle foundType = externalReferences.GetRuntimeTypeHandleFromIndex(entryParser.GetUnsigned());
                        if (foundType.Equals(runtimeTypeHandle))
                        {
                            Handle entryMetadataHandle = entryParser.GetUnsigned().AsHandle();
                            if (entryMetadataHandle.HandleType == HandleType.TypeReference)
                            {
                                metadataReader = ModuleList.Instance.GetMetadataReaderForModule(moduleHandle);
                                typeRefHandle = entryMetadataHandle.ToTypeReferenceHandle(metadataReader);
                                return true;
                            }
                        }
                    }
                }
            }

            metadataReader = null;
            typeRefHandle = default(TypeReferenceHandle);

            return false;
        }
Exemple #12
0
        public unsafe void TestReferenceHashCodes()
        {
            var wr = new Writer.MetadataWriter();

            var systemRuntimeScopeRecord = new Writer.ScopeReference
            {
                Name         = (Writer.ConstantStringValue) "System.Runtime",
                MajorVersion = 4,
            };

            var rootNamespaceRecord = new Writer.NamespaceReference
            {
                Name = null,
                ParentScopeOrNamespace = systemRuntimeScopeRecord,
            };

            var fooTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = rootNamespaceRecord,
                TypeName = (Writer.ConstantStringValue) "Foo",
            };

            var nestedTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = fooTypeRecord,
                TypeName = (Writer.ConstantStringValue) "Nested",
            };

            var reallyNestedTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = nestedTypeRecord,
                TypeName = (Writer.ConstantStringValue) "ReallyNested",
            };

            var systemNamespaceRecord = new Writer.NamespaceReference
            {
                Name = (Writer.ConstantStringValue) "System",
                ParentScopeOrNamespace = rootNamespaceRecord,
            };

            var objectTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = systemNamespaceRecord,
                TypeName = (Writer.ConstantStringValue) "Object",
            };

            wr.AdditionalRootRecords.Add(objectTypeRecord);
            wr.AdditionalRootRecords.Add(fooTypeRecord);
            wr.AdditionalRootRecords.Add(nestedTypeRecord);
            wr.AdditionalRootRecords.Add(reallyNestedTypeRecord);
            var ms = new MemoryStream();

            wr.Write(ms);

            fixed(byte *pBuffer = ms.ToArray())
            {
                var rd = new Reader.MetadataReader((IntPtr)pBuffer, (int)ms.Length);

                var fooTypeHandle   = new Reader.TypeReferenceHandle(wr.GetRecordHandle(fooTypeRecord));
                var fooTypeHashCode = TypeHashingAlgorithms.ComputeNameHashCode("Foo");

                Assert.Equal(fooTypeHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(fooTypeHandle, rd));

                var objectTypeHandle = new Reader.TypeReferenceHandle(wr.GetRecordHandle(objectTypeRecord));

                Assert.Equal(TypeHashingAlgorithms.ComputeNameHashCode("System.Object"), MetadataTypeHashingAlgorithms.ComputeHashCode(objectTypeHandle, rd));

                var nestedTypeHandle   = new Reader.TypeReferenceHandle(wr.GetRecordHandle(nestedTypeRecord));
                var nestedTypeHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(fooTypeHashCode, TypeHashingAlgorithms.ComputeNameHashCode("Nested"));

                Assert.Equal(nestedTypeHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(nestedTypeHandle, rd));

                var reallyNestedTypeHandle   = new Reader.TypeReferenceHandle(wr.GetRecordHandle(reallyNestedTypeRecord));
                var reallyNestedTypeHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(nestedTypeHashCode, TypeHashingAlgorithms.ComputeNameHashCode("ReallyNested"));

                Assert.Equal(reallyNestedTypeHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(reallyNestedTypeHandle, rd));
            }
        }
Exemple #13
0
        public unsafe void TestReferenceHashCodes()
        {
            var wr = new Writer.MetadataWriter();

            var systemRuntimeScopeRecord = new Writer.ScopeReference
            {
                Name = (Writer.ConstantStringValue)"System.Runtime",
                MajorVersion = 4,
            };

            var rootNamespaceRecord = new Writer.NamespaceReference
            {
                Name = null,
                ParentScopeOrNamespace = systemRuntimeScopeRecord,
            };

            var fooTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = rootNamespaceRecord,
                TypeName = (Writer.ConstantStringValue)"Foo",
            };

            var nestedTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = fooTypeRecord,
                TypeName = (Writer.ConstantStringValue)"Nested",
            };

            var reallyNestedTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = nestedTypeRecord,
                TypeName = (Writer.ConstantStringValue)"ReallyNested",
            };

            var systemNamespaceRecord = new Writer.NamespaceReference
            {
                Name = (Writer.ConstantStringValue)"System",
                ParentScopeOrNamespace = rootNamespaceRecord,
            };

            var objectTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = systemNamespaceRecord,
                TypeName = (Writer.ConstantStringValue)"Object",
            };

            wr.AdditionalRootRecords.Add(objectTypeRecord);
            wr.AdditionalRootRecords.Add(fooTypeRecord);
            wr.AdditionalRootRecords.Add(nestedTypeRecord);
            wr.AdditionalRootRecords.Add(reallyNestedTypeRecord);
            var ms = new MemoryStream();
            wr.Write(ms);

            fixed (byte* pBuffer = ms.ToArray())
            {
                var rd = new Reader.MetadataReader((IntPtr)pBuffer, (int)ms.Length);

                var fooTypeHandle = new Reader.TypeReferenceHandle(wr.GetRecordHandle(fooTypeRecord));
                var fooTypeHashCode = TypeHashingAlgorithms.ComputeNameHashCode("Foo");
                Assert.Equal(fooTypeHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(fooTypeHandle, rd));

                var objectTypeHandle = new Reader.TypeReferenceHandle(wr.GetRecordHandle(objectTypeRecord));
                Assert.Equal(TypeHashingAlgorithms.ComputeNameHashCode("System.Object"), MetadataTypeHashingAlgorithms.ComputeHashCode(objectTypeHandle, rd));

                var nestedTypeHandle = new Reader.TypeReferenceHandle(wr.GetRecordHandle(nestedTypeRecord));
                var nestedTypeHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(fooTypeHashCode, TypeHashingAlgorithms.ComputeNameHashCode("Nested"));
                Assert.Equal(nestedTypeHashCode , MetadataTypeHashingAlgorithms.ComputeHashCode(nestedTypeHandle, rd));

                var reallyNestedTypeHandle = new Reader.TypeReferenceHandle(wr.GetRecordHandle(reallyNestedTypeRecord));
                var reallyNestedTypeHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(nestedTypeHashCode, TypeHashingAlgorithms.ComputeNameHashCode("ReallyNested"));
                Assert.Equal(reallyNestedTypeHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(reallyNestedTypeHandle, rd));
            }
        }
        public static unsafe bool TryGetTypeReferenceForNamedType(RuntimeTypeHandle runtimeTypeHandle, out MetadataReader metadataReader, out TypeReferenceHandle typeRefHandle)
        {
            // Iterate over all modules, starting with the module that defines the EEType
            foreach (IntPtr moduleHandle in ModuleList.Enumerate(RuntimeAugments.GetModuleFromTypeHandle(runtimeTypeHandle)))
            {
                MetadataTable mapTable = MetadataTable.CreateTypeMapTable(moduleHandle);
                foreach (var ptrEntry in mapTable)
                {
                    var pCurrentEntry = (TypeMapEntry*)ptrEntry;
                    RuntimeTypeHandle entryTypeHandle = RuntimeAugments.CreateRuntimeTypeHandle(pCurrentEntry->EEType);
                    Handle entryMetadataHandle = pCurrentEntry->TypeDefinitionHandle.AsHandle();
                    if (entryTypeHandle.Equals(runtimeTypeHandle) &&
                        entryMetadataHandle.HandleType == HandleType.TypeReference)
                    {
                        metadataReader = ModuleList.Instance.GetMetadataReaderForModule(moduleHandle);
                        typeRefHandle = entryMetadataHandle.ToTypeReferenceHandle(metadataReader);
                        return true;
                    }
                }
            }

            metadataReader = null;
            typeRefHandle = default(TypeReferenceHandle);

            return false;
        }
Exemple #15
0
        } // Read

        public static uint Read(this NativeReader reader, uint offset, out TypeReferenceHandle[] values)
        {
            uint count;
            offset = reader.DecodeUnsigned(offset, out count);
            #if !NETFX_45
            if (count == 0)
            {
                values = Array.Empty<TypeReferenceHandle>();
            }
            else
            #endif
            {
                values = new TypeReferenceHandle[count];
                for (uint i = 0; i < count; ++i)
                {
                    TypeReferenceHandle tmp;
                    offset = reader.Read(offset, out tmp);
                    values[i] = tmp;
                }
            }
            return offset;
        } // Read