Example #1
0
        public void MethodTableHeapEnumeration()
        {
            using (DataTarget dt = TestTargets.Types.LoadFullDump())
            {
                ClrRuntime runtime = dt.ClrVersions.Single().CreateRuntime();
                ClrHeap    heap    = runtime.Heap;

                foreach (ClrType type in heap.EnumerateObjectAddresses().Select(obj => heap.GetObjectType(obj)).Unique())
                {
                    Assert.NotEqual(0ul, type.MethodTable);

                    ClrType typeFromHeap;

                    if (type.IsArray)
                    {
                        ClrType componentType = type.ComponentType;
                        Assert.NotNull(componentType);

                        typeFromHeap = heap.GetTypeByMethodTable(type.MethodTable, componentType.MethodTable);
                    }
                    else
                    {
                        typeFromHeap = heap.GetTypeByMethodTable(type.MethodTable);
                    }

                    Assert.Equal(type.MethodTable, typeFromHeap.MethodTable);
                    Assert.Same(type, typeFromHeap);
                }
            }
        }
Example #2
0
        public static ClrType GetTypeFromEE(ClrRuntime Runtime, ulong EEClass)
        {
            ClrHeap heap = Runtime.Heap;
            //ClrType type = null;
            ulong mt = heap.GetMethodTableByEEClass(EEClass);

            if (mt == 0)
            {
                return(null);
            }
            return(heap.GetTypeByMethodTable(mt));

            /*
             * int max = heap.TypeIndexLimit;
             * if (max < 1)
             * {
             *  var objList = heap.EnumerateObjects().GetEnumerator();
             *  if (objList.MoveNext())
             *  {
             *      type = heap.GetObjectType(objList.Current);
             *  }
             * } else
             *  type = Runtime.GetHeap().GetTypeByIndex(0);
             * if (type == null)
             *  throw new NullReferenceException("There is no object in the heap");
             * ClrType EEType = (ClrType)RunMethod(type, "ConstructObjectType", EEClass);
             * return EEType;
             */
        }
Example #3
0
        public string DumpASM()
        {
            TextWriter asmWriter = new StringWriter();

            using (DataTarget target = DataTarget.AttachToProcess(Process.GetCurrentProcess().Id, 5000, AttachFlag.Passive))
            {
                foreach (ClrInfo clrInfo in target.ClrVersions)
                {
                    this.engine.UpdateLog("Found CLR Version:" + clrInfo.Version.ToString());

                    // This is the data needed to request the dac from the symbol server:
                    ModuleInfo dacInfo = clrInfo.DacInfo;
                    this.engine.UpdateLog($"Filesize:  {dacInfo.FileSize:X}");
                    this.engine.UpdateLog($"Timestamp: {dacInfo.TimeStamp:X}");
                    this.engine.UpdateLog($"Dac File:  {dacInfo.FileName}");

                    ClrRuntime runtime   = target.ClrVersions.Single().CreateRuntime();
                    var        appDomain = runtime.AppDomains[0];
                    var        module    = appDomain.Modules.LastOrDefault(m => m.AssemblyName != null && m.AssemblyName.StartsWith(assemblyName));

                    asmWriter.WriteLine(
                        $"; {clrInfo.ModuleInfo.ToString()} ({clrInfo.Flavor} {clrInfo.Version})");
                    asmWriter.WriteLine(
                        $"; {clrInfo.DacInfo.FileName} ({clrInfo.DacInfo.TargetArchitecture} {clrInfo.DacInfo.Version})");
                    asmWriter.WriteLine();
                    foreach (var typeClr in module.EnumerateTypes())
                    {
                        asmWriter.WriteLine($"; Type {typeClr.Name}");

                        ClrHeap heap    = runtime.Heap;
                        ClrType @object = heap.GetTypeByMethodTable(typeClr.MethodTable);

                        foreach (ClrMethod method in @object.Methods)
                        {
                            MethodCompilationType compileType = method.CompilationType;
                            ArchitectureMode      mode        = clrInfo.DacInfo.TargetArchitecture == Architecture.X86
                                ? ArchitectureMode.x86_32
                                : ArchitectureMode.x86_64;

                            this.currentMethodAddress = 0;
                            var translator = new IntelTranslator
                            {
                                SymbolResolver = (Instruction instruction, long addr, ref long offset) =>
                                                 ResolveSymbol(runtime, instruction, addr, ref currentMethodAddress)
                            };

                            // This not work even ClrMd says opposite...
                            //ulong startAddress = method.NativeCode;
                            //ulong endAddress = method.ILOffsetMap.Select(entry => entry.EndAddress).Max();

                            DisassembleAndWrite(method, mode, translator, ref currentMethodAddress, asmWriter);
                            this.engine.UpdateLog($"Method {method.Name} disassembled to ASM.");
                            asmWriter.WriteLine();
                        }
                    }
                    break;
                }
            }
            return(asmWriter.ToString());
        }
Example #4
0
        public void GetObjectMethodTableTest()
        {
            using (DataTarget dt = TestTargets.AppDomains.LoadFullDump())
            {
                ClrRuntime runtime = dt.ClrVersions.Single().CreateRuntime();
                ClrHeap    heap    = runtime.Heap;

                int i = 0;
                foreach (ulong obj in heap.EnumerateObjectAddresses())
                {
                    i++;
                    ClrType type = heap.GetObjectType(obj);

                    if (type.IsArray)
                    {
                        ulong mt, cmt;
                        bool  result = heap.TryGetMethodTable(obj, out mt, out cmt);

                        Assert.True(result);
                        Assert.NotEqual(0ul, mt);
                        Assert.Equal(type.MethodTable, mt);

                        Assert.Same(type, heap.GetTypeByMethodTable(mt, cmt));
                    }
                    else
                    {
                        ulong mt = heap.GetMethodTable(obj);

                        Assert.NotEqual(0ul, mt);
                        Assert.Contains(mt, type.EnumerateMethodTables());

                        Assert.Same(type, heap.GetTypeByMethodTable(mt));
                        Assert.Same(type, heap.GetTypeByMethodTable(mt, 0));

                        ulong mt2, cmt;
                        bool  res = heap.TryGetMethodTable(obj, out mt2, out cmt);

                        Assert.True(res);
                        Assert.Equal(mt, mt2);
                        Assert.Equal(0ul, cmt);
                    }
                }
            }
        }
Example #5
0
        public DesktopMethod(DesktopRuntimeBase runtime, ulong md, IMethodDescData mdData, MethodAttributes attrs)
        {
            _runtime        = runtime;
            _sig            = runtime.GetNameForMD(md);
            _ip             = mdData.NativeCodeAddr;
            CompilationType = mdData.JITType;
            _attrs          = attrs;
            _token          = mdData.MDToken;
            GCInfo          = mdData.GCInfo;
            ClrHeap heap = runtime.Heap;

            _type       = (DesktopHeapType)heap.GetTypeByMethodTable(mdData.MethodTable, 0);
            HotColdInfo = new HotColdRegions {
                HotStart = _ip, HotSize = mdData.HotSize, ColdStart = mdData.ColdStart, ColdSize = mdData.ColdSize
            };
        }
Example #6
0
        public void EETypeTest()
        {
            using DataTarget dt = TestTargets.AppDomains.LoadFullDump();
            ClrRuntime runtime = dt.ClrVersions.Single().CreateRuntime();
            ClrHeap    heap    = runtime.Heap;

            HashSet <ulong> methodTables = (from obj in heap.EnumerateObjectAddresses()
                                            let type = heap.GetObjectType(obj)
                                                       where !type.IsFree
                                                       select heap.GetMethodTable(obj)).Unique();

            Assert.DoesNotContain(0ul, methodTables);

            foreach (ulong mt in methodTables)
            {
                ClrType type    = heap.GetTypeByMethodTable(mt);
                ulong   eeclass = heap.GetEEClassByMethodTable(mt);
                Assert.NotEqual(0ul, eeclass);

                Assert.NotEqual(0ul, heap.GetMethodTableByEEClass(eeclass));
            }
        }
Example #7
0
        public override ClrMethod GetMethodByHandle(ulong methodHandle)
        {
            if (methodHandle == 0)
            {
                return(null);
            }

            IMethodDescData methodDesc = GetMethodDescData(methodHandle);

            if (methodDesc == null)
            {
                return(null);
            }

            ClrType type = Heap.GetTypeByMethodTable(methodDesc.MethodTable);

            if (type == null)
            {
                return(null);
            }

            return(type.GetMethod(methodDesc.MDToken));
        }
Example #8
0
 /// <summary>
 ///     Retrieves the given type by its MethodTable/ComponentMethodTable pair.
 /// </summary>
 /// <param name="methodTable">The ClrType.MethodTable for the requested type.</param>
 /// <param name="componentMethodTable">The ClrType's component MethodTable for the requested type.</param>
 /// <returns>A ClrType object, or null if no such type exists.</returns>
 /// <inheritdoc />
 public IClrType GetTypeByMethodTable(ulong methodTable, ulong componentMethodTable) =>
 Converter.Convert(Heap.GetTypeByMethodTable(methodTable, componentMethodTable));