Beispiel #1
0
        internal static ClrMethod Create(DesktopRuntimeBase runtime, IMethodDescData mdData)
        {
            if (mdData == null)
                return null;

            DesktopModule module = runtime.GetModule(mdData.Module);
            return Create(runtime, module != null ? module.GetMetadataImport() : null, mdData);
        }
Beispiel #2
0
 internal override ulong GetModuleAddress(ClrAppDomain appDomain)
 {
     if (DesktopModule == null)
     {
         return(0);
     }
     return(DesktopModule.GetDomainModule(appDomain));
 }
Beispiel #3
0
        public override SourceLocation GetSourceLocationForOffset(Address nativeOffset)
        {
            ClrType type = Type;

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

            DesktopModule module = (DesktopModule)type.Module;

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

            if (!module.IsPdbLoaded)
            {
                string val = module.TryDownloadPdb();
                if (val == null)
                {
                    return(null);
                }

                module.LoadPdb(val);
                if (!module.IsPdbLoaded)
                {
                    return(null);
                }
            }

            ILToNativeMap[] map = ILOffsetMap;
            if (map == null)
            {
                return(null);
            }

            int ilOffset = 0;

            if (map.Length > 1)
            {
                ilOffset = map[1].ILOffset;
            }

            for (int i = 0; i < map.Length; ++i)
            {
                //bug bug: we dont use nativeOffset
                if (map[i].StartAddress <= _ip && _ip <= map[i].EndAddress)
                {
                    ilOffset = map[i].ILOffset;
                    break;
                }
            }

            return(module.GetSourceInformation(MetadataToken, ilOffset));
        }
Beispiel #4
0
        internal DesktopModule GetModule(Address module)
        {
            if (module == 0)
            {
                return(null);
            }

            DesktopModule res;

            if (_modules.TryGetValue(module, out res))
            {
                return(res);
            }

            IModuleData moduleData = GetModuleData(module);

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

            string peFile       = GetPEFileName(moduleData.PEFile);
            string assemblyName = GetAssemblyName(moduleData.Assembly);

            if (_moduleSizes == null)
            {
                _moduleSizes = new Dictionary <Address, uint>();
                foreach (var native in _dataReader.EnumerateModules())
                {
                    _moduleSizes[native.ImageBase] = native.FileSize;
                }
            }

            if (_moduleFiles == null)
            {
                _moduleFiles = new Dictionary <string, DesktopModule>();
            }

            uint size = 0;

            _moduleSizes.TryGetValue(moduleData.ImageBase, out size);
            if (peFile == null)
            {
                res = new DesktopModule(this, moduleData, peFile, assemblyName, size);
            }
            else if (!_moduleFiles.TryGetValue(peFile, out res))
            {
                res = new DesktopModule(this, moduleData, peFile, assemblyName, size);
                _moduleFiles[peFile] = res;
            }

            _modules[module] = res;
            return(res);
        }
Beispiel #5
0
        internal static ClrMethod Create(DesktopRuntimeBase runtime, IMethodDescData mdData)
        {
            if (mdData == null)
            {
                return(null);
            }

            DesktopModule module = runtime.GetModule(mdData.Module);

            return(Create(runtime, module?.GetMetadataImport(), mdData));
        }
Beispiel #6
0
        private void InitEnumData()
        {
            if (!IsEnum)
            {
                throw new InvalidOperationException("Type is not an Enum.");
            }

            _enumData = new EnumData();
            MetaDataImport import = DesktopModule?.GetMetadataImport();

            if (import == null)
            {
                return;
            }

            IntPtr        hnd   = IntPtr.Zero;
            List <string> names = new List <string>();

            foreach (int token in import.EnumerateFields((int)_token))
            {
                if (import.GetFieldProps(token, out string name, out FieldAttributes attr, out IntPtr ppvSigBlob, out int pcbSigBlob, out int pdwCPlusTypeFlag, out IntPtr ppValue))
                {
                    if ((int)attr == 0x606 && name == "value__")
                    {
                        SigParser parser = new SigParser(ppvSigBlob, pcbSigBlob);

                        if (parser.GetCallingConvInfo(out int sigType) && parser.GetElemType(out int elemType))
                        {
                            _enumData.ElementType = (ClrElementType)elemType;
                        }
                    }

                    // public, static, literal, has default
                    int intAttr = (int)attr;
                    if ((int)attr == 0x8056)
                    {
                        names.Add(name);

                        SigParser parser = new SigParser(ppvSigBlob, pcbSigBlob);
                        parser.GetCallingConvInfo(out int ccinfo);
                        parser.GetElemType(out int elemType);

                        Type type = ClrRuntime.GetTypeForElementType((ClrElementType)pdwCPlusTypeFlag);
                        if (type != null)
                        {
                            object o = System.Runtime.InteropServices.Marshal.PtrToStructure(ppValue, type);
                            _enumData.NameToValue[name] = o;
                            _enumData.ValueToName[o]    = name;
                        }
                    }
                }
            }
        }
Beispiel #7
0
        internal DesktopHeapType(Func <string> typeNameFactory, DesktopModule module, uint token, ulong mt, IMethodTableData mtData, DesktopGCHeap heap)
            : base(mt, heap, module, token)
        {
            _name = new Lazy <string>(typeNameFactory);

            Shared            = mtData.Shared;
            _parent           = mtData.Parent;
            _baseSize         = mtData.BaseSize;
            _componentSize    = mtData.ComponentSize;
            _containsPointers = mtData.ContainsPointers;
            _hasMethods       = mtData.NumMethods > 0;
        }
Beispiel #8
0
        public List <ClrInterface> InitInterfaces()
        {
            if (DesktopModule == null)
            {
                _interfaces = DesktopHeap.EmptyInterfaceList;
                return(null);
            }

            BaseDesktopHeapType baseType   = BaseType as BaseDesktopHeapType;
            List <ClrInterface> interfaces = baseType != null ? new List <ClrInterface>(baseType.Interfaces) : null;
            MetaDataImport      import     = DesktopModule.GetMetadataImport();

            if (import == null)
            {
                _interfaces = DesktopHeap.EmptyInterfaceList;
                return(null);
            }

            foreach (int token in import.EnumerateInterfaceImpls((int)_token))
            {
                if (import.GetInterfaceImplProps(token, out int mdClass, out int mdIFace))
                {
                    if (interfaces == null)
                    {
                        interfaces = new List <ClrInterface>();
                    }

                    var result = GetInterface(import, mdIFace);
                    if (result != null && !interfaces.Contains(result))
                    {
                        interfaces.Add(result);
                    }
                }
            }

            if (interfaces == null)
            {
                _interfaces = DesktopHeap.EmptyInterfaceList;
            }
            else
            {
                _interfaces = interfaces.ToArray();
            }

            return(interfaces);
        }
Beispiel #9
0
        private void InitFlags()
        {
            if (_attributes != 0 || DesktopModule == null)
            {
                return;
            }

            MetaDataImport import = DesktopModule.GetMetadataImport();

            if (import == null)
            {
                _attributes = (TypeAttributes)0x70000000;
                return;
            }

            if (!import.GetTypeDefAttributes((int)_token, out _attributes) || _attributes == 0)
            {
                _attributes = (TypeAttributes)0x70000000;
            }
        }
Beispiel #10
0
        private DesktopAppDomain InitDomain(ulong domain, string name = null)
        {
            ulong[]        bases      = new ulong[1];
            IAppDomainData domainData = GetAppDomainData(domain);

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

            DesktopAppDomain appDomain = new DesktopAppDomain(this, domainData, name ?? GetAppDomaminName(domain));

            if (domainData.AssemblyCount > 0)
            {
                foreach (ulong assembly in GetAssemblyList(domain, domainData.AssemblyCount))
                {
                    IAssemblyData assemblyData = GetAssemblyData(domain, assembly);
                    if (assemblyData == null)
                    {
                        continue;
                    }

                    if (assemblyData.ModuleCount > 0)
                    {
                        foreach (ulong module in GetModuleList(assembly, assemblyData.ModuleCount))
                        {
                            DesktopModule clrModule = GetModule(module);
                            if (clrModule != null)
                            {
                                clrModule.AddMapping(appDomain, module);
                                appDomain.AddModule(clrModule);
                            }
                        }
                    }
                }
            }

            return(appDomain);
        }
Beispiel #11
0
        internal DesktopModule GetModule(ulong module)
        {
            if (module == 0)
            {
                return(null);
            }

            if (_modules.TryGetValue(module, out DesktopModule res))
            {
                return(res);
            }

            IModuleData moduleData = GetModuleData(module);

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

            string peFile       = GetPEFileName(moduleData.PEFile);
            string assemblyName = GetAssemblyName(moduleData.Assembly);

            if (peFile == null)
            {
                res = new DesktopModule(this, module, moduleData, peFile, assemblyName);
            }
            else if (!_moduleFiles.TryGetValue(peFile, out res))
            {
                res = new DesktopModule(this, module, moduleData, peFile, assemblyName);
                _moduleFiles[peFile] = res;
            }

            // We've modified the 'real' module list, so clear the cached version.
            _moduleList      = null;
            _modules[module] = res;
            return(res);
        }
Beispiel #12
0
        internal override ClrType GetTypeByMethodTable(ulong mt, ulong _, ulong obj)
        {
            if (mt == 0)
            {
                return(null);
            }

            ClrType ret = null;

            // See if we already have the type.
            if (_indices.TryGetValue(mt, out int index))
            {
                ret = _types[index];
            }
            else
            {
                // No, so we'll have to construct it.
                ulong         moduleAddr = DesktopRuntime.GetModuleForMT(mt);
                DesktopModule module     = DesktopRuntime.GetModule(moduleAddr);
                uint          token      = DesktopRuntime.GetMetadataToken(mt);

                bool isFree = mt == DesktopRuntime.FreeMethodTable;
                if (token == 0xffffffff && !isFree)
                {
                    return(null);
                }

                // Dynamic functions/modules
                uint tokenEnt = token;
                if (!isFree && (module == null || module.IsDynamic))
                {
                    tokenEnt = unchecked ((uint)mt);
                }

                ModuleEntry modEnt = new ModuleEntry(module, tokenEnt);

                if (ret == null)
                {
                    IMethodTableData mtData = DesktopRuntime.GetMethodTableData(mt);
                    if (mtData == null)
                    {
                        return(null);
                    }

                    IMethodTableCollectibleData mtCollectibleData = DesktopRuntime.GetMethodTableCollectibleData(mt);
                    ret = new DesktopHeapType(() => GetTypeName(mt, module, token), module, token, mt, mtData, this, mtCollectibleData);

                    index        = _types.Count;
                    _indices[mt] = index;

                    // Arrays share a common token, so it's not helpful to look them up here.
                    if (!ret.IsArray)
                    {
                        _typeEntry[modEnt] = index;
                    }

                    _types.Add(ret);
                    Debug.Assert(_types[index] == ret);
                }
            }

            if (obj != 0 && ret.ComponentType == null && ret.IsArray)
            {
                ret.ComponentType = TryGetComponentType(obj, 0);
            }

            return(ret);
        }
Beispiel #13
0
        private void InitFields()
        {
            if (_fields != null)
            {
                return;
            }

            if (IsFree)
            {
                _fields = new List <ClrInstanceField>();
                return;
            }

            DesktopRuntimeBase runtime   = DesktopHeap.DesktopRuntime;
            IFieldInfo         fieldInfo = runtime.GetFieldInfo(_constructedMT);

            if (fieldInfo == null)
            {
                // Fill fields so we don't repeatedly try to init these fields on error.
                _fields = new List <ClrInstanceField>();
                return;
            }

            _fields = new List <ClrInstanceField>((int)fieldInfo.InstanceFields);

            // Add base type's fields.
            if (BaseType != null)
            {
                foreach (var field in BaseType.Fields)
                {
                    _fields.Add(field);
                }
            }

            int   count     = (int)(fieldInfo.InstanceFields + fieldInfo.StaticFields) - _fields.Count;
            ulong nextField = fieldInfo.FirstField;
            int   i         = 0;

            MetaDataImport import = null;

            if (nextField != 0 && DesktopModule != null)
            {
                import = DesktopModule.GetMetadataImport();
            }

            while (i < count && nextField != 0)
            {
                IFieldData field = runtime.GetFieldData(nextField);
                if (field == null)
                {
                    break;
                }

                // We don't handle context statics.
                if (field.IsContextLocal)
                {
                    nextField = field.NextField;
                    continue;
                }

                // Get the name of the field.
                string          name     = null;
                FieldAttributes attr     = FieldAttributes.PrivateScope;
                int             sigLen   = 0;
                IntPtr          ppValue  = IntPtr.Zero;
                IntPtr          fieldSig = IntPtr.Zero;

                if (import != null)
                {
                    import.GetFieldProps((int)field.FieldToken, out name, out attr, out fieldSig, out sigLen, out int pdwCPlusTypeFlab, out ppValue);
                }

                // If we couldn't figure out the name, at least give the token.
                if (import == null || name == null)
                {
                    name = string.Format("<ERROR:{0:X}>", field.FieldToken);
                }

                // construct the appropriate type of field.
                if (field.IsThreadLocal)
                {
                    if (_threadStatics == null)
                    {
                        _threadStatics = new List <ClrThreadStaticField>((int)fieldInfo.ThreadStaticFields);
                    }

                    // TODO:  Renable when thread statics are fixed.
                    //m_threadStatics.Add(new RealTimeMemThreadStaticField(m_heap, field, name));
                }
                else if (field.IsStatic)
                {
                    if (_statics == null)
                    {
                        _statics = new List <ClrStaticField>();
                    }

                    // TODO:  Enable default values.

                    /*
                     * object defaultValue = null;
                     *
                     *
                     * FieldAttributes sdl = FieldAttributes.Static | FieldAttributes.HasDefault | FieldAttributes.Literal;
                     * if ((attr & sdl) == sdl)
                     *  Debugger.Break();
                     */
                    _statics.Add(new DesktopStaticField(DesktopHeap, field, this, name, attr, null, fieldSig, sigLen));
                }
                else // instance variable
                {
                    _fields.Add(new DesktopInstanceField(DesktopHeap, field, name, attr, fieldSig, sigLen));
                }

                i++;
                nextField = field.NextField;
            }

            _fields.Sort((a, b) => a.Offset.CompareTo(b.Offset));
        }
Beispiel #14
0
        internal DesktopHeapType(string typeName, DesktopModule module, uint token, ulong mt, IMethodTableData mtData, DesktopGCHeap heap)
            : base(heap, module, token)
        {
            _name = typeName;

            _constructedMT = mt;
            Shared = mtData.Shared;
            _parent = mtData.Parent;
            _baseSize = mtData.BaseSize;
            _componentSize = mtData.ComponentSize;
            _containsPointers = mtData.ContainsPointers;
            _hasMethods = mtData.NumMethods > 0;
        }
Beispiel #15
0
        internal DesktopModule GetModule(Address module)
        {
            if (module == 0)
                return null;

            DesktopModule res;
            if (_modules.TryGetValue(module, out res))
                return res;

            IModuleData moduleData = GetModuleData(module);
            if (moduleData == null)
                return null;

            string peFile = GetPEFileName(moduleData.PEFile);
            string assemblyName = GetAssemblyName(moduleData.Assembly);

            if (_moduleSizes == null)
            {
                _moduleSizes = new Dictionary<Address, uint>();
                foreach (var native in _dataReader.EnumerateModules())
                    _moduleSizes[native.ImageBase] = native.FileSize;
            }

            if (_moduleFiles == null)
                _moduleFiles = new Dictionary<string, DesktopModule>();

            uint size = 0;
            _moduleSizes.TryGetValue(moduleData.ImageBase, out size);
            if (peFile == null)
            {
                res = new DesktopModule(this, module, moduleData, peFile, assemblyName, size);
            }
            else if (!_moduleFiles.TryGetValue(peFile, out res))
            {
                res = new DesktopModule(this, module, moduleData, peFile, assemblyName, size);
                _moduleFiles[peFile] = res;
            }

            _modules[module] = res;
            return res;
        }
Beispiel #16
0
        internal override ClrType GetTypeByMethodTable(ulong mt, ulong cmt, ulong obj)
        {
            if (mt == 0)
            {
                return(null);
            }

            ClrType componentType = null;

            if (mt == DesktopRuntime.ArrayMethodTable)
            {
                if (cmt != 0)
                {
                    componentType = GetTypeByMethodTable(cmt, 0);
                    if (componentType != null)
                    {
                        cmt = componentType.MethodTable;
                    }
                    else if (obj != 0)
                    {
                        componentType = TryGetComponentType(obj, cmt);
                        if (componentType != null)
                        {
                            cmt = componentType.MethodTable;
                        }
                    }
                }
                else
                {
                    componentType = ObjectType;
                    cmt           = ObjectType.MethodTable;
                }
            }
            else
            {
                cmt = 0;
            }

            TypeHandle hnd = new TypeHandle(mt, cmt);
            ClrType    ret = null;

            // See if we already have the type.
            if (_indices.TryGetValue(hnd, out int index))
            {
                ret = _types[index];
            }
            else if (mt == DesktopRuntime.ArrayMethodTable && cmt == 0)
            {
                // Handle the case where the methodtable is an array, but the component method table
                // was not specified.  (This happens with fields.)  In this case, return System.Object[],
                // with an ArrayComponentType set to System.Object.
                uint token = DesktopRuntime.GetMetadataToken(mt);
                if (token == 0xffffffff)
                {
                    return(null);
                }

                ModuleEntry modEnt = new ModuleEntry(ArrayType.Module, token);

                ret   = ArrayType;
                index = _types.Count;

                _indices[hnd]      = index;
                _typeEntry[modEnt] = index;
                _types.Add(ret);

                Debug.Assert(_types[index] == ret);
            }
            else
            {
                // No, so we'll have to construct it.
                ulong         moduleAddr = DesktopRuntime.GetModuleForMT(hnd.MethodTable);
                DesktopModule module     = DesktopRuntime.GetModule(moduleAddr);
                uint          token      = DesktopRuntime.GetMetadataToken(mt);

                bool isFree = mt == DesktopRuntime.FreeMethodTable;
                if (token == 0xffffffff && !isFree)
                {
                    return(null);
                }

                // Dynamic functions/modules
                uint tokenEnt = token;
                if (!isFree && (module == null || module.IsDynamic))
                {
                    tokenEnt = (uint)mt;
                }

                ModuleEntry modEnt = new ModuleEntry(module, tokenEnt);

                if (ret == null)
                {
                    IMethodTableData mtData = DesktopRuntime.GetMethodTableData(mt);
                    if (mtData == null)
                    {
                        return(null);
                    }

                    ret = new DesktopHeapType(() => GetTypeName(hnd, module, token), module, token, mt, mtData, this)
                    {
                        ComponentType = componentType
                    };
                    index = _types.Count;
                    ((DesktopHeapType)ret).SetIndex(index);
                    _indices[hnd]      = index;
                    _typeEntry[modEnt] = index;
                    _types.Add(ret);

                    Debug.Assert(_types[index] == ret);
                }
            }

            if (obj != 0 && ret.ComponentType == null && ret.IsArray)
            {
                ret.ComponentType = TryGetComponentType(obj, cmt);
            }

            return(ret);
        }
Beispiel #17
0
        internal DesktopHeapType(string typeName, DesktopModule module, uint token, ulong mt, IMethodTableData mtData, DesktopGCHeap heap, int index)
            : base(heap, module, token)
        {
            m_name = typeName;
            m_index = index;

            m_handle = mt;
            Shared = mtData.Shared;
            m_parent = mtData.Parent;
            m_baseSize = mtData.BaseSize;
            m_componentSize = mtData.ComponentSize;
            m_containsPointers = mtData.ContainsPointers;
            m_hasMethods = mtData.NumMethods > 0;
        }