Beispiel #1
0
        /// <summary>
        ///     Dispose method
        /// </summary>
        /// <param name="disposing"><c>true</c> if called by <see cref="Dispose()" /></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
            {
                return;
            }
            Dispose(peImage);
            Dispose(stringsStream);
            Dispose(usStream);
            Dispose(blobStream);
            Dispose(guidStream);
            Dispose(tablesStream);
            var as2 = allStreams;

            if (as2 != null)
            {
                foreach (var stream in as2.GetSafeEnumerable())
                {
                    Dispose(stream);
                }
            }
            peImage                   = null;
            cor20Header               = null;
            mdHeader                  = null;
            stringsStream             = null;
            usStream                  = null;
            blobStream                = null;
            guidStream                = null;
            tablesStream              = null;
            allStreams                = null;
            fieldRidToTypeDefRid      = null;
            methodRidToTypeDefRid     = null;
            typeDefRidToNestedClasses = null;
        }
Beispiel #2
0
        private void InitializeInverseGenericParamOwnerRidList()
        {
            if (gpRidToOwnerRid != null)
            {
                return;
            }
            var gpTable            = tablesStream.GenericParamTable;
            var newGpRidToOwnerRid = new uint[gpTable.Rows];

            // Find all owners by reading the GenericParam.Owner column
            var ownerCol   = gpTable.TableInfo.Columns[2];
            var ownersDict = new ThreadSafe.Dictionary <uint, bool>();

#if THREAD_SAFE
            tablesStream.theLock.EnterWriteLock(); try {
#endif
            for (uint rid = 1; rid <= gpTable.Rows; rid++)
            {
                uint owner;
                if (!tablesStream.ReadColumn_NoLock(gpTable, rid, ownerCol, out owner))
                {
                    continue;
                }
                ownersDict[owner] = true;
            }
#if THREAD_SAFE
        }

        finally { tablesStream.theLock.ExitWriteLock(); }
#endif

            // Now that we have the owners, find all the generic params they own. An obfuscated
            // module could have 2+ owners pointing to the same generic param row.
            var owners = new ThreadSafe.List <uint>(ownersDict.Keys);
            owners.Sort();
            for (var i = 0; i < owners.Count; i++)
            {
                uint ownerToken;
                if (!CodedToken.TypeOrMethodDef.Decode(owners[i], out ownerToken))
                {
                    continue;
                }
                var ridList = GetGenericParamRidList(MDToken.ToTable(ownerToken), MDToken.ToRID(ownerToken));
                for (uint j = 0; j < ridList.Length; j++)
                {
                    var ridIndex = ridList[j] - 1;
                    if (newGpRidToOwnerRid[ridIndex] != 0)
                    {
                        continue;
                    }
                    newGpRidToOwnerRid[ridIndex] = owners[i];
                }
            }
            Interlocked.CompareExchange(ref gpRidToOwnerRid, newGpRidToOwnerRid, null);
        }
Beispiel #3
0
        private void InitializeInverseGenericParamConstraintOwnerRidList()
        {
            if (gpcRidToOwnerRid != null)
            {
                return;
            }
            var gpcTable            = tablesStream.GenericParamConstraintTable;
            var newGpcRidToOwnerRid = new uint[gpcTable.Rows];

            var ownerCol   = gpcTable.TableInfo.Columns[0];
            var ownersDict = new ThreadSafe.Dictionary <uint, bool>();

#if THREAD_SAFE
            tablesStream.theLock.EnterWriteLock(); try {
#endif
            for (uint rid = 1; rid <= gpcTable.Rows; rid++)
            {
                uint owner;
                if (!tablesStream.ReadColumn_NoLock(gpcTable, rid, ownerCol, out owner))
                {
                    continue;
                }
                ownersDict[owner] = true;
            }
#if THREAD_SAFE
        }

        finally { tablesStream.theLock.ExitWriteLock(); }
#endif

            var owners = new ThreadSafe.List <uint>(ownersDict.Keys);
            owners.Sort();
            for (var i = 0; i < owners.Count; i++)
            {
                var ownerToken = owners[i];
                var ridList    = GetGenericParamConstraintRidList(ownerToken);
                for (uint j = 0; j < ridList.Length; j++)
                {
                    var ridIndex = ridList[j] - 1;
                    if (newGpcRidToOwnerRid[ridIndex] != 0)
                    {
                        continue;
                    }
                    newGpcRidToOwnerRid[ridIndex] = ownerToken;
                }
            }
            Interlocked.CompareExchange(ref gpcRidToOwnerRid, newGpcRidToOwnerRid, null);
        }
        static AssemblyResolver()
        {
            gacInfos = new ThreadSafe.List <GacInfo>();

            if (Type.GetType("Mono.Runtime") != null)
            {
                var dirs = new ThreadSafe.Dictionary <string, bool>(StringComparer.OrdinalIgnoreCase);
                extraMonoPaths = new ThreadSafe.List <string>();
                foreach (var prefix in FindMonoPrefixes())
                {
                    var dir = Path.Combine(Path.Combine(Path.Combine(prefix, "lib"), "mono"), "gac");
                    if (dirs.ContainsKey(dir))
                    {
                        continue;
                    }
                    dirs[dir] = true;

                    if (Directory.Exists(dir))
                    {
                        gacInfos.Add(new GacInfo(-1, "", Path.GetDirectoryName(dir), new[]
                        {
                            Path.GetFileName(dir)
                        }));
                    }

                    dir = Path.GetDirectoryName(dir);
                    foreach (var verDir in monoVerDirs)
                    {
                        var dir2 = Path.Combine(dir, verDir);
                        if (Directory.Exists(dir2))
                        {
                            extraMonoPaths.Add(dir2);
                        }
                    }
                }

                var paths = Environment.GetEnvironmentVariable("MONO_PATH");
                if (paths != null)
                {
                    foreach (var path in paths.Split(Path.PathSeparator))
                    {
                        if (path != string.Empty && Directory.Exists(path))
                        {
                            extraMonoPaths.Add(path);
                        }
                    }
                }
            }
            else
            {
                var windir = Environment.GetEnvironmentVariable("WINDIR");
                if (!string.IsNullOrEmpty(windir))
                {
                    string path;

                    // .NET 1.x and 2.x
                    path = Path.Combine(windir, "assembly");
                    if (Directory.Exists(path))
                    {
                        gacInfos.Add(new GacInfo(2, "", path, new[]
                        {
                            "GAC_32", "GAC_64", "GAC_MSIL", "GAC"
                        }));
                    }

                    // .NET 4.x
                    path = Path.Combine(Path.Combine(windir, "Microsoft.NET"), "assembly");
                    if (Directory.Exists(path))
                    {
                        gacInfos.Add(new GacInfo(4, "v4.0_", path, new[]
                        {
                            "GAC_32", "GAC_64", "GAC_MSIL"
                        }));
                    }
                }
            }
        }
Beispiel #5
0
        private void InitializeNestedClassesDictionary()
        {
            var table     = tablesStream.NestedClassTable;
            var destTable = tablesStream.TypeDefTable;

            ThreadSafe.Dictionary <uint, bool> validTypeDefRids = null;
            var typeDefRidList = GetTypeDefRidList();

            if (typeDefRidList.Length != destTable.Rows)
            {
                validTypeDefRids = new ThreadSafe.Dictionary <uint, bool>((int)typeDefRidList.Length);
                for (uint i = 0; i < typeDefRidList.Length; i++)
                {
                    validTypeDefRids[typeDefRidList[i]] = true;
                }
            }

            var nestedRidsDict = new ThreadSafe.Dictionary <uint, bool>((int)table.Rows);
            var nestedRids     = new ThreadSafe.List <uint>((int)table.Rows); // Need it so we add the rids in correct order

            for (uint rid = 1; rid <= table.Rows; rid++)
            {
                if (validTypeDefRids != null && !validTypeDefRids.ContainsKey(rid))
                {
                    continue;
                }
                var row = tablesStream.ReadNestedClassRow(rid);
                if (row == null)
                {
                    continue; // Should never happen since rid is valid
                }
                if (!destTable.IsValidRID(row.NestedClass) || !destTable.IsValidRID(row.EnclosingClass))
                {
                    continue;
                }
                if (nestedRidsDict.ContainsKey(row.NestedClass))
                {
                    continue;
                }
                nestedRidsDict[row.NestedClass] = true;
                nestedRids.Add(row.NestedClass);
            }

            var newTypeDefRidToNestedClasses = new ThreadSafe.Dictionary <uint, RandomRidList>();

            foreach (var nestedRid in nestedRids)
            {
                var row = tablesStream.ReadNestedClassRow(GetNestedClassRid(nestedRid));
                if (row == null)
                {
                    continue;
                }
                RandomRidList ridList;
                if (!newTypeDefRidToNestedClasses.TryGetValue(row.EnclosingClass, out ridList))
                {
                    newTypeDefRidToNestedClasses[row.EnclosingClass] = ridList = new RandomRidList();
                }
                ridList.Add(nestedRid);
            }

            var newNonNestedTypes = new RandomRidList((int)(destTable.Rows - nestedRidsDict.Count));

            for (uint rid = 1; rid <= destTable.Rows; rid++)
            {
                if (validTypeDefRids != null && !validTypeDefRids.ContainsKey(rid))
                {
                    continue;
                }
                if (nestedRidsDict.ContainsKey(rid))
                {
                    continue;
                }
                newNonNestedTypes.Add(rid);
            }

            Interlocked.CompareExchange(ref nonNestedTypes, newNonNestedTypes, null);

            // Initialize this one last since it's tested by the callers of this method
            Interlocked.CompareExchange(ref typeDefRidToNestedClasses, newTypeDefRidToNestedClasses, null);
        }