예제 #1
0
        private static MetadataManager PickMetadataManager(CompilerTypeSystemContext context, CompilationModuleGroup compilationModuleGroup, IEnumerable <ModuleDesc> inputModules, IEnumerable <ModuleDesc> inputMetadataOnlyAssemblies, string metadataFile, bool emitStackTraceMetadata, bool disableExceptionMessages, bool disableInvokeThunks)
        {
            if (metadataFile == null)
            {
                return(new EmptyMetadataManager(context));
            }
            else
            {
                // Set Policies according to passed arguments
                StackTraceEmissionPolicy stackTraceEmissionPolicy;
                if (emitStackTraceMetadata)
                {
                    stackTraceEmissionPolicy = new UtcStackTraceEmissionPolicy();
                }
                else
                {
                    stackTraceEmissionPolicy = new NoStackTraceEmissionPolicy();
                }

                ManifestResourceBlockingPolicy resourceBlockingPolicy;
                if (disableExceptionMessages)
                {
                    resourceBlockingPolicy = new FrameworkStringResourceBlockingPolicy();
                }
                else
                {
                    resourceBlockingPolicy = new NoManifestResourceBlockingPolicy();
                }

                return(new PrecomputedMetadataManager(compilationModuleGroup, context, FindMetadataDescribingModuleInInputSet(inputModules), inputModules, inputMetadataOnlyAssemblies, ReadBytesFromFile(metadataFile), stackTraceEmissionPolicy, resourceBlockingPolicy, disableInvokeThunks));
            }
        }
예제 #2
0
        private void ProcessAssembly(string inputFile, IRootingServiceProvider rootProvider)
        {
            EcmaModule assembly;

            try
            {
                assembly = (EcmaModule)_context.ResolveAssembly(new AssemblyName(inputFile), false);
            }
            catch (TypeSystemException.BadImageFormatException)
            {
                // Native files can sometimes end up in the input. It would be nice if they didn't, but
                // it's pretty safe to just ignore them.
                // See: https://github.com/dotnet/corert/issues/2785
                return;
            }

            if (FrameworkStringResourceBlockingPolicy.IsFrameworkAssembly(assembly))
            {
                return;
            }

            rootProvider.RootModuleMetadata(assembly, "Application assembly root");

            foreach (TypeDesc type in assembly.GetAllTypes())
            {
                RdXmlRootProvider.RootType(rootProvider, type, "Application assembly root");
            }
        }
        private void ProcessAssembly(string inputFile, IRootingServiceProvider rootProvider)
        {
            EcmaModule assembly;

            try
            {
                // We use GetModuleFromPath because it's more resilient to slightly wrong inputs
                // (e.g. name of the file not matching the assembly name)
                assembly = _context.GetModuleFromPath(inputFile);
            }
            catch (TypeSystemException.BadImageFormatException)
            {
                // Native files can sometimes end up in the input. It would be nice if they didn't, but
                // it's pretty safe to just ignore them.
                // See: https://github.com/dotnet/corert/issues/2785
                return;
            }

            if (FrameworkStringResourceBlockingPolicy.IsFrameworkAssembly(assembly))
            {
                return;
            }

            rootProvider.RootModuleMetadata(assembly, "Application assembly root");

            foreach (TypeDesc type in assembly.GetAllTypes())
            {
                RdXmlRootProvider.RootType(rootProvider, type, "Application assembly root");
            }
        }
예제 #4
0
        private void ProcessAssembly(string inputFile, IRootingServiceProvider rootProvider)
        {
            var assembly = (EcmaModule)_context.ResolveAssembly(new AssemblyName(inputFile), false);

            if (FrameworkStringResourceBlockingPolicy.IsFrameworkAssembly(assembly))
            {
                return;
            }

            rootProvider.RootModuleMetadata(assembly, "Application assembly root");

            foreach (TypeDesc type in assembly.GetAllTypes())
            {
                RdXmlRootProvider.RootType(rootProvider, type, "Application assembly root");
            }
        }
예제 #5
0
        private RemoveAction GetAction(MethodDesc method)
        {
            TypeDesc owningType = method.OwningType;

            if ((_removedFeature & RemovedFeature.Etw) != 0)
            {
                if (!method.Signature.IsStatic)
                {
                    if (IsEventSourceType(owningType))
                    {
                        if (method.IsConstructor)
                        {
                            return(RemoveAction.ConvertToStub);
                        }

                        if (method.Name == "IsEnabled" || method.IsFinalizer || method.Name == "Dispose")
                        {
                            return(RemoveAction.ConvertToStub);
                        }

                        return(RemoveAction.ConvertToThrow);
                    }
                    else if (IsEventSourceImplementation(owningType))
                    {
                        if (method.IsFinalizer)
                        {
                            return(RemoveAction.ConvertToStub);
                        }

                        if (method.IsConstructor)
                        {
                            return(RemoveAction.ConvertToStub);
                        }

                        if (method.HasCustomAttribute("System.Diagnostics.Tracing", "NonEventAttribute"))
                        {
                            return(RemoveAction.Nothing);
                        }

                        return(RemoveAction.ConvertToThrow);
                    }
                }
            }

            if ((_removedFeature & RemovedFeature.FrameworkResources) != 0)
            {
                if (method.Signature.IsStatic &&
                    owningType is Internal.TypeSystem.Ecma.EcmaType mdType &&
                    mdType.Name == "SR" && mdType.Namespace == "System" &&
                    FrameworkStringResourceBlockingPolicy.IsFrameworkAssembly(mdType.EcmaModule))
                {
                    if (method.Name == "UsingResourceKeys")
                    {
                        return(RemoveAction.ConvertToTrueStub);
                    }
                    else if (method.Name == "GetResourceString" && method.Signature.Length == 2)
                    {
                        return(RemoveAction.ConvertToGetResourceStringStub);
                    }

                    return(RemoveAction.Nothing);
                }
            }

            if ((_removedFeature & RemovedFeature.Globalization) != 0)
            {
                if (owningType is Internal.TypeSystem.Ecma.EcmaType mdType &&
                    mdType.Module == method.Context.SystemModule)
                {
                    if (method.Signature.IsStatic &&
                        mdType.Name == "GlobalizationMode" && mdType.Namespace == "System.Globalization" &&
                        method.Name == "get_Invariant")
                    {
                        return(RemoveAction.ConvertToTrueStub);
                    }

                    if (method.IsConstructor &&
                        method.Signature.Length == 3 &&
                        mdType.Name == "CalendarData" && mdType.Namespace == "System.Globalization")
                    {
                        return(RemoveAction.ConvertToThrow);
                    }

                    // We remove this one explicitly because it ends up calling EnumCalendarInfoExEx on Windows
                    // and brings in delegate reverse p/invoke support into the app.
                    if (method.Name == "GetCalendars" &&
                        method.Signature.Length == 3 &&
                        mdType.Name == "CalendarData" && mdType.Namespace == "System.Globalization")
                    {
                        return(RemoveAction.ConvertToThrow);
                    }

                    // We remove this one explicitly because it ends up calling EnumTimeFormatsEx on Windows
                    // and brings in delegate reverse p/invoke support into the app.
                    if ((method.Name == "GetTimeFormats" || method.Name == "GetShortTimeFormats") &&
                        method.Signature.Length == 0 &&
                        mdType.Name == "CultureData" && mdType.Namespace == "System.Globalization")
                    {
                        return(RemoveAction.ConvertToThrow);
                    }

                    if (method.Signature.Length == 1 &&
                        method.Name == "GetCalendarInstanceRare" &&
                        mdType.Name == "CultureInfo" && mdType.Namespace == "System.Globalization")
                    {
                        return(RemoveAction.ConvertToThrow);
                    }

                    // Make sure that there are no ICU dependencies left
                    if (method.IsPInvoke && method.GetPInvokeMethodMetadata().Module == "System.Globalization.Native")
                    {
                        return(RemoveAction.ConvertToThrow);
                    }
                }
            }

            if ((_removedFeature & RemovedFeature.Comparers) != 0)
            {
                if (owningType.GetTypeDefinition() is Internal.TypeSystem.Ecma.EcmaType mdType &&
                    mdType.Module == method.Context.SystemModule &&
                    method.Name == "Create" &&
                    mdType.Namespace == "System.Collections.Generic")
                {
                    if (mdType.Name == "EqualityComparer`1")
                    {
                        return(RemoveAction.ConvertToGetKnownObjectEqualityComparer);
                    }
                    else if (mdType.Name == "Comparer`1")
                    {
                        return(RemoveAction.ConvertToGetKnownObjectComparer);
                    }
                }
            }

            if ((_removedFeature & RemovedFeature.SerializationGuard) != 0)
            {
                if (method.Name == "ThrowIfDeserializationInProgress" &&
                    owningType is Internal.TypeSystem.Ecma.EcmaType mdType &&
                    mdType.Namespace == "System.Runtime.Serialization" &&
                    (mdType.Name == ((mdType.Module != method.Context.SystemModule) ? "SerializationGuard" : "SerializationInfo")))
                {
                    return(RemoveAction.ConvertToStub);
                }
            }

            return(RemoveAction.Nothing);
        }
예제 #6
0
        protected override void GetMetadataDependenciesDueToReflectability(ref DependencyList dependencies, NodeFactory factory, TypeDesc type)
        {
            TypeMetadataNode.GetMetadataDependencies(ref dependencies, factory, type, "Reflectable type");

            // If we don't have precise field usage information, apply policy that all fields that
            // are eligible to have metadata get metadata.
            if (!_hasPreciseFieldUsageInformation)
            {
                TypeDesc typeDefinition = type.GetTypeDefinition();

                foreach (FieldDesc field in typeDefinition.GetFields())
                {
                    if ((GetMetadataCategory(field) & MetadataCategory.Description) != 0)
                    {
                        dependencies = dependencies ?? new DependencyList();
                        dependencies.Add(factory.FieldMetadata(field), "Field of a reflectable type");
                    }
                }
            }

            // If anonymous type heuristic is turned on and this is an anonymous type, make sure we have
            // method bodies for all properties. It's common to have anonymous types used with reflection
            // and it's hard to specify them in RD.XML.
            if ((_generationOptions & UsageBasedMetadataGenerationOptions.AnonymousTypeHeuristic) != 0)
            {
                if (type is MetadataType metadataType &&
                    metadataType.HasInstantiation &&
                    !metadataType.IsGenericDefinition &&
                    metadataType.HasCustomAttribute("System.Runtime.CompilerServices", "CompilerGeneratedAttribute") &&
                    metadataType.Name.Contains("AnonymousType"))
                {
                    foreach (MethodDesc method in type.GetMethods())
                    {
                        if (!method.Signature.IsStatic && method.IsSpecialName)
                        {
                            dependencies = dependencies ?? new DependencyList();
                            dependencies.Add(factory.CanonicalEntrypoint(method), "Anonymous type accessor");
                        }
                    }
                }
            }

            // If the option was specified to root types and methods in all user assemblies, do that.
            if ((_generationOptions & UsageBasedMetadataGenerationOptions.FullUserAssemblyRooting) != 0)
            {
                if (type is MetadataType metadataType &&
                    !_rootAllAssembliesExaminedModules.Contains(metadataType.Module))
                {
                    _rootAllAssembliesExaminedModules.Add(metadataType.Module);

                    if (metadataType.Module is Internal.TypeSystem.Ecma.EcmaModule ecmaModule &&
                        !FrameworkStringResourceBlockingPolicy.IsFrameworkAssembly(ecmaModule))
                    {
                        dependencies = dependencies ?? new DependencyList();
                        var rootProvider = new RootingServiceProvider(factory, dependencies.Add);
                        foreach (TypeDesc t in ecmaModule.GetAllTypes())
                        {
                            RootingHelpers.TryRootType(rootProvider, t, "RD.XML root");
                        }
                    }
                }
            }

            // If a type is marked [Serializable], make sure a couple things are also included.
            if (type.IsSerializable && !type.IsGenericDefinition)
            {
                foreach (MethodDesc method in type.GetAllMethods())
                {
                    MethodSignature signature = method.Signature;

                    if (method.IsConstructor &&
                        signature.Length == 2 &&
                        signature[0] == _serializationInfoType
                        /* && signature[1] is StreamingContext */)
                    {
                        dependencies = dependencies ?? new DependencyList();
                        dependencies.Add(factory.CanonicalEntrypoint(method), "Binary serialization");
                    }

                    // Methods with these attributes can be called during serialization
                    if (signature.Length == 1 && !signature.IsStatic && signature.ReturnType.IsVoid &&
                        (method.HasCustomAttribute("System.Runtime.Serialization", "OnSerializingAttribute") ||
                         method.HasCustomAttribute("System.Runtime.Serialization", "OnSerializedAttribute") ||
                         method.HasCustomAttribute("System.Runtime.Serialization", "OnDeserializingAttribute") ||
                         method.HasCustomAttribute("System.Runtime.Serialization", "OnDeserializedAttribute")))
                    {
                        dependencies = dependencies ?? new DependencyList();
                        dependencies.Add(factory.CanonicalEntrypoint(method), "Binary serialization");
                    }
                }
            }

            // Event sources need their special nested types
            if (type is MetadataType mdType && mdType.HasCustomAttribute("System.Diagnostics.Tracing", "EventSourceAttribute"))
            {
                AddEventSourceSpecialTypeDependencies(ref dependencies, factory, mdType.GetNestedType("Keywords"));
                AddEventSourceSpecialTypeDependencies(ref dependencies, factory, mdType.GetNestedType("Tasks"));
                AddEventSourceSpecialTypeDependencies(ref dependencies, factory, mdType.GetNestedType("Opcodes"));

                void AddEventSourceSpecialTypeDependencies(ref DependencyList dependencies, NodeFactory factory, MetadataType type)
                {
                    if (type != null)
                    {
                        const string reason = "Event source";
                        dependencies = dependencies ?? new DependencyList();
                        dependencies.Add(factory.TypeMetadata(type), reason);
                        foreach (FieldDesc field in type.GetFields())
                        {
                            if (field.IsLiteral)
                            {
                                dependencies.Add(factory.FieldMetadata(field), reason);
                            }
                        }
                    }
                }
            }
        }
예제 #7
0
        private RemoveAction GetAction(MethodDesc method)
        {
            TypeDesc owningType = method.OwningType;

            if ((_removedFeature & RemovedFeature.Etw) != 0)
            {
                if (!method.Signature.IsStatic)
                {
                    if (IsEventSourceType(owningType))
                    {
                        if (method.IsConstructor)
                        {
                            return(RemoveAction.ConvertToStub);
                        }

                        if (method.Name == "IsEnabled" || method.IsFinalizer || method.Name == "Dispose")
                        {
                            return(RemoveAction.ConvertToStub);
                        }

                        return(RemoveAction.ConvertToThrow);
                    }
                    else if (IsEventSourceImplementation(owningType))
                    {
                        if (method.IsFinalizer)
                        {
                            return(RemoveAction.ConvertToStub);
                        }

                        if (method.IsConstructor)
                        {
                            return(RemoveAction.ConvertToStub);
                        }

                        if (method.HasCustomAttribute("System.Diagnostics.Tracing", "NonEventAttribute"))
                        {
                            return(RemoveAction.Nothing);
                        }

                        return(RemoveAction.ConvertToThrow);
                    }
                }
            }

            if ((_removedFeature & RemovedFeature.FrameworkResources) != 0)
            {
                if (method.Signature.IsStatic &&
                    owningType is Internal.TypeSystem.Ecma.EcmaType mdType &&
                    mdType.Name == "SR" && mdType.Namespace == "System" &&
                    FrameworkStringResourceBlockingPolicy.IsFrameworkAssembly(mdType.EcmaModule))
                {
                    if (method.Name == "UsingResourceKeys")
                    {
                        return(RemoveAction.ConvertToTrueStub);
                    }
                    else if (method.Name == "GetResourceString" && method.Signature.Length == 2)
                    {
                        return(RemoveAction.ConvertToGetResourceStringStub);
                    }

                    return(RemoveAction.Nothing);
                }
            }

            if ((_removedFeature & RemovedFeature.Globalization) != 0)
            {
                if (owningType is Internal.TypeSystem.Ecma.EcmaType mdType &&
                    mdType.Module == method.Context.SystemModule)
                {
                    if (method.Signature.IsStatic &&
                        mdType.Name == "GlobalizationMode" && mdType.Namespace == "System.Globalization" &&
                        method.Name == "get_Invariant")
                    {
                        return(RemoveAction.ConvertToTrueStub);
                    }

                    if (method.IsConstructor &&
                        method.Signature.Length == 3 &&
                        mdType.Name == "CalendarData" && mdType.Namespace == "System.Globalization")
                    {
                        return(RemoveAction.ConvertToThrow);
                    }

                    if (method.Signature.Length == 1 &&
                        method.Name == "GetCalendarInstanceRare" &&
                        mdType.Name == "CultureInfo" && mdType.Namespace == "System.Globalization")
                    {
                        return(RemoveAction.ConvertToThrow);
                    }
                }
            }

            if ((_removedFeature & RemovedFeature.Comparers) != 0)
            {
                if (owningType.GetTypeDefinition() is Internal.TypeSystem.Ecma.EcmaType mdType &&
                    mdType.Module == method.Context.SystemModule &&
                    method.Name == "Create" &&
                    mdType.Namespace == "System.Collections.Generic")
                {
                    if (mdType.Name == "EqualityComparer`1")
                    {
                        return(RemoveAction.ConvertToGetKnownObjectEqualityComparer);
                    }
                    else if (mdType.Name == "Comparer`1")
                    {
                        return(RemoveAction.ConvertToGetKnownObjectComparer);
                    }
                }
            }

            if ((_removedFeature & RemovedFeature.CurlHandler) != 0)
            {
                if (owningType is Internal.TypeSystem.Ecma.EcmaType mdType &&
                    mdType.Module.Assembly.GetName().Name == "System.Net.Http" &&
                    mdType.Name == "CurlHandler" &&
                    mdType.Namespace == "System.Net.Http")
                {
                    return(RemoveAction.ConvertToThrow);
                }
            }

            return(RemoveAction.Nothing);
        }
예제 #8
0
        private RemoveAction GetAction(MethodDesc method)
        {
            TypeDesc owningType = method.OwningType;

            if ((_removedFeature & RemovedFeature.Etw) != 0)
            {
                if (!method.Signature.IsStatic)
                {
                    if (IsEventSourceType(owningType))
                    {
                        if (method.IsConstructor)
                        {
                            return(RemoveAction.ConvertToStub);
                        }

                        if (method.Name == "IsEnabled" || method.IsFinalizer || method.Name == "Dispose")
                        {
                            return(RemoveAction.ConvertToStub);
                        }

                        return(RemoveAction.ConvertToThrow);
                    }
                    else if (IsEventSourceImplementation(owningType))
                    {
                        if (method.IsFinalizer)
                        {
                            return(RemoveAction.ConvertToStub);
                        }

                        if (method.IsConstructor)
                        {
                            return(RemoveAction.ConvertToStub);
                        }

                        if (method.HasCustomAttribute("System.Diagnostics.Tracing", "NonEventAttribute"))
                        {
                            return(RemoveAction.Nothing);
                        }

                        return(RemoveAction.ConvertToThrow);
                    }
                }
            }

            if ((_removedFeature & RemovedFeature.FrameworkResources) != 0)
            {
                if (method.Signature.IsStatic &&
                    owningType is Internal.TypeSystem.Ecma.EcmaType mdType &&
                    mdType.Name == "SR" && mdType.Namespace == "System" &&
                    FrameworkStringResourceBlockingPolicy.IsFrameworkAssembly(mdType.EcmaModule))
                {
                    if (method.Name == "UsingResourceKeys")
                    {
                        return(RemoveAction.ConvertToTrueStub);
                    }
                    else if (method.Name == "GetResourceString" && method.Signature.Length == 2)
                    {
                        return(RemoveAction.ConvertToGetResourceStringStub);
                    }

                    return(RemoveAction.Nothing);
                }
            }

            return(RemoveAction.Nothing);
        }