예제 #1
0
        public bool IsFrameworkAssembly(AssemblyReferenceInformation assembly)
        {
            if (assembly == null)
            {
                // If we don't have the assembly, default to including the API
                return(true);
            }

            if (MicrosoftKeys.Contains(assembly.PublicKeyToken))
            {
                return(true);
            }

            if (FrameworkAssemblyNamePrefixes.Any(p => assembly.Name.StartsWith(p, StringComparison.OrdinalIgnoreCase)))
            {
                return(true);
            }

            if (string.Equals(assembly.Name, "mscorlib", StringComparison.OrdinalIgnoreCase))
            {
                return(true);
            }

            return(false);
        }
예제 #2
0
        /// <summary>
        /// Tries to locate the assembly containing <see cref="object"/>.
        /// </summary>
        public bool TryGetSystemRuntimeAssemblyInformation(MetadataReader reader, out AssemblyReferenceInformation assemblyReference)
        {
            if (reader.TryGetCurrentAssemblyName(out var name) && SystemObjectAssemblies.Contains(name))
            {
                assemblyReference = reader.FormatAssemblyInfo();
                return(true);
            }

            var microsoftAssemblies = reader.AssemblyReferences
                                      .Select(handle =>
            {
                var assembly = reader.GetAssemblyReference(handle);
                return(reader.FormatAssemblyInfo(assembly));
            })
                                      .Where(_assemblyFilter.IsFrameworkAssembly)
                                      .Where(assembly => SystemObjectAssemblies.Contains(assembly.Name))
                                      .OrderByDescending(assembly => assembly.Version);

            var matchingAssembly = microsoftAssemblies.FirstOrDefault();

            if (matchingAssembly != default(AssemblyReferenceInformation))
            {
                assemblyReference = matchingAssembly;
                return(true);
            }

            assemblyReference = null;
            return(false);
        }
            public bool IsFrameworkAssembly(AssemblyReferenceInformation assembly)
            {
                var comparison = StringComparison.Ordinal;
                var name       = assembly?.Name;

                var result = string.Equals(_assemblyName, name, comparison) ||
                             string.Equals("mscorlib", name, comparison) ||
                             string.Equals("System.Runtime", name, comparison);

                return(result);
            }
        private MemberDependency CreateMemberDependency(MemberMetadataInfo type, AssemblyReferenceInformation definedInAssembly)
        {
            // Apply heuristic to determine if API is most likely defined in a framework assembly
            if (!_assemblyFilter.IsFrameworkAssembly(definedInAssembly))
            {
                return(null);
            }

            return(new MemberDependency
            {
                CallingAssembly = CallingAssembly,
                MemberDocId = $"T:{type}",
                DefinedInAssemblyIdentity = definedInAssembly
            });
        }
        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 static void EqualityTests()
        {
            var assemblyInfo1 = new AssemblyReferenceInformation("name", Version.Parse("4.0"), "neutral", "1234");
            var assemblyInfo2 = new AssemblyReferenceInformation("Name", Version.Parse("4.0"), "neutral", "1234");
            var assemblyInfo3 = new AssemblyReferenceInformation("name2", Version.Parse("4.0"), "neutral", "1234");

            Assert.Equal($"name, Version=4.0, Culture=neutral, PublicKeyToken=1234", assemblyInfo1.ToString());
            Assert.Equal($"Name, Version=4.0, Culture=neutral, PublicKeyToken=1234", assemblyInfo2.ToString());
            Assert.Equal($"name2, Version=4.0, Culture=neutral, PublicKeyToken=1234", assemblyInfo3.ToString());

            Assert.True(assemblyInfo1.Equals(assemblyInfo2));
            Assert.False(assemblyInfo1.Equals(assemblyInfo3));
            Assert.False(assemblyInfo2.Equals(assemblyInfo3));

            Assert.Equal(assemblyInfo1.GetHashCode(), assemblyInfo2.GetHashCode());
            Assert.NotEqual(assemblyInfo1.GetHashCode(), assemblyInfo3.GetHashCode());
            Assert.NotEqual(assemblyInfo2.GetHashCode(), assemblyInfo3.GetHashCode());
        }
        public DependencyFinderEngineHelper(IDependencyFilter assemblyFilter, MetadataReader metadataReader, IAssemblyFile file)
        {
            _assemblyFilter   = assemblyFilter;
            _reader           = metadataReader;
            _assemblyLocation = file.Name;

            MemberDependency = new List <MemberDependency>();
            CallingAssembly  = new AssemblyInfo
            {
                AssemblyIdentity       = metadataReader.FormatAssemblyInfo().ToString(),
                FileVersion            = file.Version ?? string.Empty,
                TargetFrameworkMoniker = metadataReader.GetTargetFrameworkMoniker() ?? string.Empty
            };

            // Get assembly info
            var assemblyDefinition = _reader.GetAssemblyDefinition();

            _currentAssemblyInfo = _reader.FormatAssemblyInfo(assemblyDefinition);
            _currentAssemblyName = _reader.GetString(assemblyDefinition.Name);
        }
예제 #8
0
 public static bool IsFrameworkMember(this IDependencyFilter filter, AssemblyReferenceInformation assembly)
 {
     return(filter.IsFrameworkMember(assembly.Name, assembly.PublicKeyToken));
 }
        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.
                }
            }
        }
        public void AssemblyNameStartsWithSpecifiedString(string name, bool succeed)
        {
            var assembly = new AssemblyReferenceInformation(name, Version.Parse("4.0"), string.Empty, string.Empty);

            Assert.Equal(succeed, _assemblyFilter.IsFrameworkAssembly(assembly));
        }
        public void DotNetFrameworkFilterCheckPublicKeyToken(string publicKeyToken, bool succeed)
        {
            var assembly = new AssemblyReferenceInformation(string.Empty, Version.Parse("4.0"), string.Empty, publicKeyToken);

            Assert.Equal(succeed, _assemblyFilter.IsFrameworkAssembly(assembly));
        }
예제 #12
0
 public bool IsFrameworkAssembly(AssemblyReferenceInformation assembly)
 {
     return(true);
 }
예제 #13
0
 public bool IsFrameworkAssembly(AssemblyReferenceInformation assembly)
 {
     return(string.Equals(_assemblyName, assembly?.Name));
 }
예제 #14
0
 public bool IsFrameworkAssembly(AssemblyReferenceInformation assembly)
 {
     return(string.Equals(_assemblyName, assembly?.Name, StringComparison.Ordinal));
 }