Example #1
0
 public DmdParsedTypeRef(DmdModule ownerModule, DmdParsedTypeRef declaringTypeRef, DmdTypeScope typeScope, string @namespace, string name, IList <DmdCustomModifier> customModifiers) : base(ownerModule, 0, customModifiers)
 {
     this.typeScope        = typeScope;
     this.declaringTypeRef = declaringTypeRef;
     MetadataNamespace     = @namespace;
     MetadataName          = name ?? throw new ArgumentNullException(nameof(name));
 }
Example #2
0
        internal void GetFrameMethodInfo(out DmdModule module, out int methodMetadataToken, out IList <DmdType> genericTypeArguments, out IList <DmdType> genericMethodArguments)
        {
            engine.VerifyCorDebugThread();
            var corFrame = CorFrame;

            methodMetadataToken = (int)corFrame.Token;
            var corModule = corFrame.Function?.Module;

            if (corModule != null)
            {
                module = engine.TryGetModule(corModule)?.GetReflectionModule() ?? throw new InvalidOperationException();
                if (!corFrame.GetTypeAndMethodGenericParameters(out var typeGenArgs, out var methGenArgs))
                {
                    throw new InvalidOperationException();
                }
                var reflectionAppDomain = module.AppDomain;
                genericTypeArguments   = Convert(reflectionAppDomain, typeGenArgs);
                genericMethodArguments = Convert(reflectionAppDomain, methGenArgs);
                return;
            }

            module = null;
            methodMetadataToken    = 0;
            genericTypeArguments   = Array.Empty <DmdType>();
            genericMethodArguments = Array.Empty <DmdType>();
        }
Example #3
0
 DmdTypeNameParser(DmdModule ownerModule, string typeFullName, IList <DmdType> genericTypeArguments)
 {
     this.ownerModule          = ownerModule;
     typeDefResolver           = null;
     reader                    = new StringReader(typeFullName ?? string.Empty);
     this.genericTypeArguments = genericTypeArguments ?? Array.Empty <DmdType>();
     recursionCounter          = 0;
 }
Example #4
0
		DmdCustomAttributeReader(DmdModule module, DmdDataStream reader, DmdType ctorReflectedType, IList<DmdType> genericTypeArguments, bool ownsReader) {
			this.module = module;
			this.reader = reader;
			this.ctorReflectedType = ctorReflectedType;
			this.genericTypeArguments = genericTypeArguments;
			this.ownsReader = ownsReader;
			recursionCounter = 0;
		}
Example #5
0
 public static DmdType?Parse(DmdModule ownerModule, string typeFullName, IList <DmdType>?genericTypeArguments)
 {
     try {
         return(ParseThrow(ownerModule, typeFullName, genericTypeArguments));
     }
     catch (TypeNameParserException) {
         return(null);
     }
 }
Example #6
0
        /// <summary>
        /// Gets the builder instance. It's assumed to be stored in a field in the current 'this' instance.
        ///
        /// The decompiler should already know the field. If that info isn't available, we'll try to find
        /// the field by name, and if that fails, by field type.
        ///
        /// null is returned if we couldn't find the field or if we failed to read the field.
        /// </summary>
        /// <param name="evalInfo">Evaluation info</param>
        /// <param name="builderFieldModule">Module of builder field or null if unknown</param>
        /// <param name="builderFieldToken">Token of builder field or 0 if unknown</param>
        /// <returns></returns>
        public static DbgDotNetValue TryGetBuilder(DbgEvaluationInfo evalInfo, DmdModule builderFieldModule, uint builderFieldToken)
        {
            DbgDotNetValueResult thisArg   = default;
            DbgDotNetValueResult tmpResult = default;

            try {
                var runtime = evalInfo.Runtime.GetDotNetRuntime();
                thisArg = runtime.GetParameterValue(evalInfo, 0);
                if (!thisArg.IsNormalResult || thisArg.Value.IsNull)
                {
                    return(null);
                }
                if (thisArg.Value.Type.IsByRef)
                {
                    tmpResult = thisArg.Value.LoadIndirect();
                    if (!tmpResult.IsNormalResult || tmpResult.Value.IsNull)
                    {
                        return(null);
                    }
                    thisArg.Value?.Dispose();
                    thisArg   = tmpResult;
                    tmpResult = default;
                }

                DmdFieldInfo builderField = null;
                if (builderFieldModule != null && builderFieldToken != 0)
                {
                    builderField = thisArg.Value.Type.GetField(builderFieldModule, (int)builderFieldToken);
                }
                if ((object)builderField == null)
                {
                    builderField = TryGetBuilderField(thisArg.Value.Type);
                }
                if ((object)builderField == null)
                {
                    return(null);
                }
                Debug.Assert((object)builderField == TryGetBuilderFieldByType(thisArg.Value.Type));
                Debug.Assert((object)TryGetBuilderFieldByname(thisArg.Value.Type) == null ||
                             (object)TryGetBuilderFieldByname(thisArg.Value.Type) == TryGetBuilderFieldByType(thisArg.Value.Type));
                tmpResult = runtime.LoadField(evalInfo, thisArg.Value, builderField);
                if (!tmpResult.IsNormalResult || tmpResult.Value.IsNull)
                {
                    return(null);
                }
                var fieldValue = tmpResult.Value;
                tmpResult = default;
                return(fieldValue);
            }
            finally {
                thisArg.Value?.Dispose();
                tmpResult.Value?.Dispose();
            }
        }
Example #7
0
		internal static DmdCustomAttributeNamedArgument[] ReadNamedArguments(DmdModule module, DmdDataStream stream, DmdType ctorReflectedType, int numNamedArgs, IList<DmdType> genericTypeArguments) {
			using (var reader = new DmdCustomAttributeReader(module, stream, ctorReflectedType, genericTypeArguments, ownsReader: false)) {
				try {
					return reader.ReadNamedArguments(numNamedArgs);
				}
				catch (CABlobParserException) {
				}
				catch (ResolveException) {
				}
				catch (IOException) {
				}
				return null;
			}
		}
Example #8
0
		public static DmdCustomAttributeData Read(DmdModule module, DmdDataStream stream, DmdConstructorInfo ctor) {
			using (var reader = new DmdCustomAttributeReader(module, stream, ctor.ReflectedType, GetGenericArguments(ctor.ReflectedType), ownsReader: true)) {
				try {
					return reader.Read(ctor);
				}
				catch (CABlobParserException) {
				}
				catch (ResolveException) {
				}
				catch (IOException) {
				}
				return null;
			}
		}
Example #9
0
 public static (DmdType type, bool containedGenericParams) ReadTypeSignature(DmdModule module, DmdDataStream reader, IList <DmdType> genericTypeArguments, IList <DmdType> genericMethodArguments, bool resolve)
 {
     try {
         using (var sigReader = new DmdSignatureReader(module, reader, genericTypeArguments, genericMethodArguments, resolve)) {
             var type = sigReader.ReadType().type;
             return(type, sigReader.containedGenericParams);
         }
     }
     catch (IOException) {
     }
     catch (OutOfMemoryException) {
     }
     return(module.AppDomain.System_Void, false);
 }
Example #10
0
        public override void Remove(DmdModule module)
        {
            if (module == null)
            {
                throw new ArgumentNullException(nameof(module));
            }
            var moduleImpl = module as DmdModuleImpl;

            if (moduleImpl == null)
            {
                throw new InvalidOperationException();
            }
            lock (LockObject) {
                bool b = modules.Remove(moduleImpl);
                Debug.Assert(b);
            }
        }
Example #11
0
        public static DbgEngineModule CreateModule <T>(DbgEngineImpl engine, DbgObjectFactory objectFactory, DbgAppDomain appDomain, DnModule dnModule, T data) where T : class
        {
            ulong  address     = dnModule.Address;
            uint   size        = dnModule.Size;
            var    imageLayout = CalculateImageLayout(dnModule);
            string name        = GetFilename(dnModule.Name);
            string filename    = dnModule.Name;
            bool   isDynamic   = dnModule.IsDynamic;
            bool   isInMemory  = dnModule.IsInMemory;
            bool   isOptimized = CalculateIsOptimized(dnModule);
            int    order       = dnModule.UniqueId;

            InitializeExeFields(dnModule, filename, imageLayout, out var isExe, out var timestamp, out var version);

            var reflectionAppDomain = ((DbgCorDebugInternalAppDomainImpl)appDomain.InternalAppDomain).ReflectionAppDomain;

            var         closedListenerCollection = new ClosedListenerCollection();
            var         modules            = dnModule.Assembly.Modules;
            bool        isManifestModule   = modules[0] == dnModule;
            var         getMetadata        = CreateGetMetadataDelegate(engine, objectFactory.Runtime, dnModule, closedListenerCollection, imageLayout);
            var         fullyQualifiedName = DmdModule.GetFullyQualifiedName(isInMemory, isDynamic, dnModule.Name);
            DmdAssembly reflectionAssembly;
            DmdModule   reflectionModule;

            if (isManifestModule)
            {
                var assemblyLocation = isInMemory || isDynamic ? string.Empty : dnModule.Name;
                reflectionAssembly = reflectionAppDomain.CreateAssembly(getMetadata, isInMemory, isDynamic, fullyQualifiedName, assemblyLocation);
                reflectionModule   = reflectionAssembly.ManifestModule;
            }
            else
            {
                var manifestModule = engine.TryGetModule(modules[0].CorModule);
                if (manifestModule == null)
                {
                    throw new InvalidOperationException();
                }
                reflectionAssembly = ((DbgCorDebugInternalModuleImpl)manifestModule.InternalModule).ReflectionModule.Assembly;
                reflectionModule   = reflectionAppDomain.CreateModule(reflectionAssembly, getMetadata, isInMemory, isDynamic, fullyQualifiedName);
            }

            var internalModule = new DbgCorDebugInternalModuleImpl(reflectionModule, closedListenerCollection);

            return(objectFactory.CreateModule(appDomain, internalModule, isExe, address, size, imageLayout, name, filename, isDynamic, isInMemory, isOptimized, order, timestamp, version, engine.GetMessageFlags(), data: data, onCreated: engineModule => internalModule.SetModule(engineModule.Module)));
        }
Example #12
0
        internal void GetFrameMethodInfo(out DmdModule module, out int methodMetadataToken, out IList <DmdType> genericTypeArguments, out IList <DmdType> genericMethodArguments)
        {
            engine.VerifyMonoDebugThread();
            methodMetadataToken = MonoFrame.Method.MetadataToken;
            module = Module.GetReflectionModule() ?? throw new InvalidOperationException();

            TypeMirror[] typeGenArgs;
            TypeMirror[] methGenArgs;
            if (MonoFrame.VirtualMachine.Version.AtLeast(2, 15))
            {
                typeGenArgs = MonoFrame.Method.DeclaringType.GetGenericArguments();
                methGenArgs = MonoFrame.Method.GetGenericArguments();
            }
            else
            {
                typeGenArgs = Array.Empty <TypeMirror>();
                methGenArgs = Array.Empty <TypeMirror>();
            }

            var reflectionAppDomain = module.AppDomain;

            genericTypeArguments   = Convert(reflectionAppDomain, typeGenArgs);
            genericMethodArguments = Convert(reflectionAppDomain, methGenArgs);
        }
Example #13
0
 DmdMarshalBlobReader(DmdModule module, DmdDataStream reader, IList <DmdType> genericTypeArguments)
 {
     this.module = module;
     this.reader = reader;
     this.genericTypeArguments = genericTypeArguments ?? Array.Empty <DmdType>();
 }
Example #14
0
        public static (DmdMethodSignature methodSignature, bool containedGenericParams) ReadMethodSignature(DmdModule module, DmdDataStream reader, IList <DmdType> genericTypeArguments, IList <DmdType> genericMethodArguments, bool isProperty, bool resolve)
        {
            try {
                using (var sigReader = new DmdSignatureReader(module, reader, genericTypeArguments, genericMethodArguments, resolve)) {
                    sigReader.ReadMethodSignature(out var flags, out var genericParameterCount, out var returnType, out var parameterTypes, out var varArgsParameterTypes);
                    if (((flags & DmdSignatureCallingConvention.Mask) == DmdSignatureCallingConvention.Property) == isProperty)
                    {
                        var methodSignature = new DmdMethodSignature(flags, genericParameterCount, returnType, parameterTypes, varArgsParameterTypes);
                        return(methodSignature, sigReader.containedGenericParams);
                    }
                }
            }
            catch (IOException) {
            }
            catch (OutOfMemoryException) {
            }
            var dummySig = new DmdMethodSignature(isProperty ? DmdSignatureCallingConvention.Property : DmdSignatureCallingConvention.Default, 0, module.AppDomain.System_Void, null, null);

            return(dummySig, false);
        }
Example #15
0
 public static DmdMarshalType Read(DmdModule module, DmdDataStream reader, IList <DmdType> genericTypeArguments)
 {
     using (var marshalReader = new DmdMarshalBlobReader(module, reader, genericTypeArguments))
         return(marshalReader.Read());
 }
 public static            DmdCustomAttributeData[] Read(DmdModule module, DmdDataStream signature, SecurityAction action, IList <DmdType> genericTypeArguments)
 {
     using (var reader = new DmdDeclSecurityReader(module, signature, genericTypeArguments))
         return(reader.Read(action));
 }
Example #17
0
 public DmdNullMetadataReader(DmdModule module) => globalType = new DmdNullGlobalType(module, null);
 public DmdCreatedGenericParameterType(DmdModule module, bool isGenericTypeParameter, int position, IList <DmdCustomModifier> customModifiers) : base(position, customModifiers)
 {
     this.module = module ?? throw new ArgumentNullException(nameof(module));
     this.isGenericTypeParameter = isGenericTypeParameter;
 }
 public static            DmdCustomAttributeData[] Read(DmdModule module, DmdDataStream signature, SecurityAction action) => Read(module, signature, action, null);
Example #20
0
 public static DmdType?Parse(DmdModule ownerModule, string typeFullName) =>
 Parse(ownerModule, typeFullName, null);
Example #21
0
 public static DmdType ParseThrow(DmdModule ownerModule, string typeFullName, IList <DmdType>?genericTypeArguments)
 {
     using (var parser = new DmdTypeNameParser(ownerModule, typeFullName, genericTypeArguments))
         return(parser.ParseCore());
 }
Example #22
0
 public TypeKey(DmdType type)
 {
     module        = type.Module;
     metadataToken = type.MetadataToken;
 }
Example #23
0
 public DmdNullGlobalType(DmdModule module, IList <DmdCustomModifier> customModifiers) : base(1, customModifiers) =>
Example #24
0
 DmdDeclSecurityReader(DmdModule module, DmdDataStream reader, IList <DmdType>?genericTypeArguments)
 {
     this.reader = reader;
     this.module = module;
     this.genericTypeArguments = genericTypeArguments ?? Array.Empty <DmdType>();
 }
Example #25
0
 public static (DmdType[] types, bool containedGenericParams) ReadMethodSpecSignature(DmdModule module, DmdDataStream reader, IList <DmdType> genericTypeArguments, IList <DmdType> genericMethodArguments, bool resolve)
 {
     try {
         using (var sigReader = new DmdSignatureReader(module, reader, genericTypeArguments, genericMethodArguments, resolve)) {
             var types = sigReader.ReadInstantiation();
             return(types, sigReader.containedGenericParams);
         }
     }
     catch (IOException) {
     }
     catch (OutOfMemoryException) {
     }
     return(Array.Empty <DmdType>(), false);
 }
Example #26
0
        DbgEngineModule CreateModuleCore <T>(T data) where T : class
        {
            DbgImageLayout imageLayout;

            string       filename             = monoModule.FullyQualifiedName;
            const string InMemoryModulePrefix = "data-";

            if (filename.StartsWith(InMemoryModulePrefix, StringComparison.Ordinal))
            {
                isDynamic     = false;
                isInMemory    = true;
                moduleAddress = 0;
                moduleSize    = 0;
                imageLayout   = DbgImageLayout.File;

                var  hexAddrString = filename.Substring(InMemoryModulePrefix.Length);
                bool b             = ulong.TryParse(hexAddrString, NumberStyles.HexNumber, null, out var inMemoryAddr);
                Debug.Assert(b);
                if (b)
                {
                    moduleAddress = inMemoryAddr;
                    b             = PortableExecutableHelper.TryGetSizeOfImage(engine.DbgRuntime.Process, moduleAddress, imageLayout == DbgImageLayout.File, out uint imageSize);
                    Debug.Assert(b);
                    if (b)
                    {
                        moduleSize = imageSize;
                    }
                }
            }
            else if (File.Exists(filename))
            {
                filename      = NormalizeFilename(filename);
                isDynamic     = false;
                isInMemory    = false;
                moduleAddress = 0;
                moduleSize    = 0;
                if (PortableExecutableHelper.TryGetModuleAddressAndSize(engine.DbgRuntime.Process, filename, out var imageAddr, out var imageSize))
                {
                    moduleAddress = imageAddr;
                    moduleSize    = imageSize;
                }
                imageLayout = moduleAddress == 0 ? DbgImageLayout.File : DbgImageLayout.Memory;
            }
            else
            {
                isDynamic     = true;
                isInMemory    = true;
                moduleAddress = 0;
                moduleSize    = 0;
                imageLayout   = DbgImageLayout.Unknown;
            }

            string name        = GetFilename(filename);
            bool?  isOptimized = CalculateIsOptimized();

            InitializeExeFields(filename, imageLayout, out var isExe, out var isDll, out var timestamp, out var version, out var assemblySimpleName);

            var reflectionAppDomain = ((DbgMonoDebugInternalAppDomainImpl)appDomain.InternalAppDomain).ReflectionAppDomain;

            var         closedListenerCollection = new ClosedListenerCollection();
            var         getMetadata        = CreateGetMetadataDelegate(closedListenerCollection, imageLayout);
            var         fullyQualifiedName = DmdModule.GetFullyQualifiedName(isInMemory, isDynamic, filename);
            DmdAssembly reflectionAssembly;
            DmdModule   reflectionModule;

            if (monoModule == monoModule.Assembly.ManifestModule)
            {
                var assemblyLocation = isInMemory || isDynamic ? string.Empty : filename;
                var asmOptions       = DmdCreateAssemblyOptions.None;
                if (isInMemory)
                {
                    asmOptions |= DmdCreateAssemblyOptions.InMemory;
                }
                if (isDynamic)
                {
                    asmOptions |= DmdCreateAssemblyOptions.Dynamic;
                }
                if (isExe)
                {
                    asmOptions |= DmdCreateAssemblyOptions.IsEXE;
                }
                else if (isDll)
                {
                    asmOptions |= DmdCreateAssemblyOptions.IsDLL;
                }
                var asmInfo = new DmdCreateAssemblyInfo(asmOptions, fullyQualifiedName, assemblyLocation, assemblySimpleName);
                reflectionAssembly = reflectionAppDomain.CreateAssembly(getMetadata, asmInfo);
                reflectionModule   = reflectionAssembly.ManifestModule;
            }
            else
            {
                var manifestModule = engine.TryGetModule(monoModule.Assembly.ManifestModule);
                if (manifestModule == null)
                {
                    throw new InvalidOperationException();
                }
                reflectionAssembly = ((DbgMonoDebugInternalModuleImpl)manifestModule.InternalModule).ReflectionModule.Assembly;
                reflectionModule   = reflectionAppDomain.CreateModule(reflectionAssembly, getMetadata, isInMemory, isDynamic, fullyQualifiedName);
            }

            var internalModule = new DbgMonoDebugInternalModuleImpl(reflectionModule, closedListenerCollection);

            return(objectFactory.CreateModule(appDomain, internalModule, isExe, moduleAddress, moduleSize, imageLayout, name, filename, isDynamic, isInMemory, isOptimized, moduleOrder, timestamp, version, engine.GetMessageFlags(), data: data, onCreated: engineModule => internalModule.SetModule(engineModule.Module)));
        }
 public abstract GetModuleReferencesResult GetModuleReferences(DbgRuntime runtime, DmdModule module, DmdType[] typeReferences);
Example #28
0
 public static (DmdType fieldType, DmdMethodSignature methodSignature, bool containedGenericParams) ReadMethodSignatureOrFieldType(DmdModule module, DmdDataStream reader, IList <DmdType> genericTypeArguments, IList <DmdType> genericMethodArguments, bool resolve)
 {
     try {
         using (var sigReader = new DmdSignatureReader(module, reader, genericTypeArguments, genericMethodArguments, resolve)) {
             var flags = (DmdSignatureCallingConvention)reader.ReadByte();
             if ((flags & DmdSignatureCallingConvention.Mask) == DmdSignatureCallingConvention.Field)
             {
                 var fieldType = sigReader.ReadType().type;
                 return(fieldType, null, sigReader.containedGenericParams);
             }
             else
             {
                 sigReader.ReadMethodSignature(flags, out var genericParameterCount, out var returnType, out var parameterTypes, out var varArgsParameterTypes);
                 if ((flags & DmdSignatureCallingConvention.Mask) != DmdSignatureCallingConvention.Property)
                 {
                     var methodSignature = new DmdMethodSignature(flags, genericParameterCount, returnType, parameterTypes, varArgsParameterTypes);
                     return(null, methodSignature, sigReader.containedGenericParams);
                 }
             }
         }
     }
     catch (IOException) {
     }
     catch (OutOfMemoryException) {
     }
     return(module.AppDomain.System_Void, null, false);
 }
Example #29
0
 public DbgCorDebugInternalModuleImpl(DmdModule reflectionModule, ClosedListenerCollection closedListenerCollection)
 {
     ReflectionModule = reflectionModule ?? throw new ArgumentNullException(nameof(reflectionModule));
     this.closedListenerCollection = closedListenerCollection ?? throw new ArgumentNullException(nameof(closedListenerCollection));
 }
Example #30
0
 public DmdTypeRef(DmdModule ownerModule, uint rid, IList <DmdCustomModifier> customModifiers) : base(customModifiers)
 {
     this.ownerModule = ownerModule ?? throw new ArgumentNullException(nameof(ownerModule));
     this.rid         = rid;
 }