Exemplo n.º 1
0
        private MemberDependency GetTypeReferenceMemberDependency(TypeReference typeReference)
        {
            var provider = new MemberMetadataInfoTypeProvider(_reader);
            var typeInfo = provider.GetFullName(typeReference);

            return(CreateMemberDependency(typeInfo));
        }
Exemplo n.º 2
0
        private MemberDependency GetMemberReferenceMemberDependency(MemberReference memberReference)
        {
            var provider      = new MemberMetadataInfoTypeProvider(_reader);
            var memberRefInfo = provider.GetMemberRefInfo(memberReference);

            // Add the parent type to the types list (only needed when we want to report memberrefs defined in the current assembly)
            if (memberRefInfo.ParentType.IsTypeDef || (memberRefInfo.ParentType.IsPrimitiveType && _currentAssemblyName.Equals("mscorlib", StringComparison.OrdinalIgnoreCase)))
            {
                MemberDependency.Add(CreateMemberDependency(memberRefInfo.ParentType));
            }

            var dep = new MemberDependency
            {
                CallingAssembly = CallingAssembly,
                MemberDocId     = $"{GetPrefix(memberReference)}:{memberRefInfo}",
                TypeDocId       = $"T:{memberRefInfo.ParentType}",
                IsPrimitive     = memberRefInfo.ParentType.IsPrimitiveType
            };

            if (memberRefInfo.ParentType.DefinedInAssembly.HasValue)
            {
                dep.DefinedInAssemblyIdentity = _reader.FormatAssemblyInfo(memberRefInfo.ParentType.DefinedInAssembly.Value);
            }
            // If no assembly is set, then the type is either a primitive type or it's in the current assembly.
            // Mscorlib is special-cased for testing purposes.
            else if (!memberRefInfo.ParentType.IsPrimitiveType || string.Equals(_currentAssemblyName, "mscorlib", StringComparison.OrdinalIgnoreCase))
            {
                dep.DefinedInAssemblyIdentity = _currentAssemblyInfo;
            }

            return(dep);
        }
Exemplo n.º 3
0
        public void ComputeData()
        {
            // Primitives need to have their assembly set, so we search for a
            // reference to System.Object that is considered a possible
            // framework assembly and use that for any primitives that don't
            // have an assembly
            var systemObjectAssembly = _objectFinder.GetSystemRuntimeAssemblyInformation(_reader);

            var provider = new MemberMetadataInfoTypeProvider(_reader);

            // Get type references
            foreach (var handle in _reader.TypeReferences)
            {
                try
                {
                    var entry    = _reader.GetTypeReference(handle);
                    var typeInfo = provider.GetFullName(entry);
                    var assembly = GetAssembly(typeInfo);
                    var typeReferenceMemberDependency = CreateMemberDependency(typeInfo, assembly);

                    if (typeReferenceMemberDependency != null)
                    {
                        MemberDependency.Add(typeReferenceMemberDependency);
                    }
                }
                catch (BadImageFormatException)
                {
                    // Some obfuscators will inject dead types that break decompilers
                    // (for example, types that serve as each others' scopes).
                    //
                    // For portability/compatibility analysis purposes, though,
                    // we can skip such malformed references and just analyze those
                    // that we can successfully decode.
                }
            }

            // Get member references
            foreach (var handle in _reader.MemberReferences)
            {
                try
                {
                    var entry = _reader.GetMemberReference(handle);

                    var memberReferenceMemberDependency = GetMemberReferenceMemberDependency(entry, systemObjectAssembly);
                    if (memberReferenceMemberDependency != null)
                    {
                        MemberDependency.Add(memberReferenceMemberDependency);
                    }
                }
                catch (BadImageFormatException)
                {
                    // Some obfuscators will inject dead types that break decompilers
                    // (for example, types that serve as each others' scopes).
                    //
                    // For portability/compatibility analysis purposes, though,
                    // we can skip such malformed references and just analyze those
                    // that we can successfully decode.
                }
            }
        }
        private MemberDependency GetMemberReferenceMemberDependency(MemberReference memberReference, AssemblyReferenceInformation systemObjectAssembly)
        {
            var provider      = new MemberMetadataInfoTypeProvider(_reader);
            var memberRefInfo = provider.GetMemberRefInfo(memberReference);

            AssemblyReferenceInformation definedInAssemblyIdentity = null;

            if (memberRefInfo.ParentType.DefinedInAssembly.HasValue)
            {
                definedInAssemblyIdentity = _reader.FormatAssemblyInfo(memberRefInfo.ParentType.DefinedInAssembly.Value);
            }
            else if (memberRefInfo.ParentType.IsPrimitiveType)
            {
                definedInAssemblyIdentity = systemObjectAssembly;
            }
            else
            {
                definedInAssemblyIdentity = _currentAssemblyInfo;
            }

            // Apply heuristic to determine if API is most likely defined in a framework assembly
            if (!_assemblyFilter.IsFrameworkAssembly(definedInAssemblyIdentity))
            {
                return(null);
            }

            // Add the parent type to the types list (only needed when we want to report memberrefs defined in the current assembly)
            if (memberRefInfo.ParentType.IsTypeDef || (memberRefInfo.ParentType.IsPrimitiveType && _currentAssemblyName.Equals("mscorlib", StringComparison.OrdinalIgnoreCase)))
            {
                var memberDependency = CreateMemberDependency(memberRefInfo.ParentType);

                if (memberDependency != null)
                {
                    MemberDependency.Add(memberDependency);
                }
            }

            return(new MemberDependency
            {
                CallingAssembly = CallingAssembly,
                MemberDocId = $"{GetPrefix(memberReference)}:{memberRefInfo}",
                TypeDocId = $"T:{memberRefInfo.ParentType}",
                IsPrimitive = memberRefInfo.ParentType.IsPrimitiveType,
                DefinedInAssemblyIdentity = definedInAssemblyIdentity
            });
        }
        public void ComputeData()
        {
            AssemblyReferenceInformation systemObjectAssembly = null;

            var provider = new MemberMetadataInfoTypeProvider(_reader);

            // Get type references
            foreach (var handle in _reader.TypeReferences)
            {
                try
                {
                    var entry    = _reader.GetTypeReference(handle);
                    var typeInfo = provider.GetFullName(entry);
                    var assembly = GetAssembly(typeInfo);
                    var typeReferenceMemberDependency = CreateMemberDependency(typeInfo, assembly);

                    if (typeReferenceMemberDependency != null)
                    {
                        MemberDependency.Add(typeReferenceMemberDependency);
                    }

                    // Primitives need to have their assembly set, so we search for a reference to System.Object that is considered a possible framework
                    // assembly and use that for any primitives that don't have an assembly
                    if (systemObjectAssembly == null &&
                        string.Equals(typeInfo.Namespace, "System", StringComparison.Ordinal) &&
                        string.Equals(typeInfo.Name, "Object", StringComparison.Ordinal) &&
                        _assemblyFilter.IsFrameworkAssembly(assembly))
                    {
                        systemObjectAssembly = assembly;
                    }
                }
                catch (BadImageFormatException)
                {
                    // Some obfuscators will inject dead types that break decompilers
                    // (for example, types that serve as each others' scopes).
                    //
                    // For portability/compatibility analysis purposes, though,
                    // we can skip such malformed references and just analyze those
                    // that we can successfully decode.
                }
            }

            if (systemObjectAssembly == null)
            {
                throw new PortabilityAnalyzerException(LocalizedStrings.MissingAssemblyInfo);
            }

            // Get member references
            foreach (var handle in _reader.MemberReferences)
            {
                try
                {
                    var entry = _reader.GetMemberReference(handle);

                    var memberReferenceMemberDependency = GetMemberReferenceMemberDependency(entry, systemObjectAssembly);
                    if (memberReferenceMemberDependency != null)
                    {
                        MemberDependency.Add(memberReferenceMemberDependency);
                    }
                }
                catch (BadImageFormatException)
                {
                    // Some obfuscators will inject dead types that break decompilers
                    // (for example, types that serve as each others' scopes).
                    //
                    // For portability/compatibility analysis purposes, though,
                    // we can skip such malformed references and just analyze those
                    // that we can successfully decode.
                }
            }
        }