Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FrameworkStringResourceBlockingPolicy.cs « Compiler « src « ILCompiler.Compiler « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dd53aa02425ee4c00a22b32d5781fa35326222dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;

using Internal.TypeSystem;
using Internal.TypeSystem.Ecma;

namespace ILCompiler
{
    /// <summary>
    /// A resource blocking policy that blocks RESX resources in framework assemblies.
    /// This is useful for size-conscious scenarios where the conveniece of having
    /// proper exception messages in framework-throw exceptions is not important.
    /// </summary>
    public sealed class FrameworkStringResourceBlockingPolicy : ManifestResourceBlockingPolicy
    {
        public override bool IsManifestResourceBlocked(ModuleDesc module, string resourceName)
        {
            // The embedded RESX files all have names that end with .resources, so use that as the initial filter.
            if (!resourceName.EndsWith(".resources", StringComparison.OrdinalIgnoreCase))
                return false;

            // Assuming multimodule and non-ecma assemblies are unsupported
            EcmaModule ecmaModule = (EcmaModule)module;

            // If this is not a framework assembly, no resources are blocked
            if (!IsFrameworkAssembly(ecmaModule))
                return false;

            MetadataReader reader = ecmaModule.MetadataReader;
            
            // We have a resource in the framework assembly. Now check if this is a RESX
            foreach (ManifestResourceHandle resourceHandle in reader.ManifestResources)
            {
                ManifestResource resource = reader.GetManifestResource(resourceHandle);
                if (reader.StringComparer.Equals(resource.Name, resourceName) &&
                    resource.Implementation.IsNil)
                {
                    PEMemoryBlock resourceDirectory =
                        ecmaModule.PEReader.GetSectionData(ecmaModule.PEReader.PEHeaders.CorHeader.ResourcesDirectory.RelativeVirtualAddress);
                    BlobReader blob = resourceDirectory.GetReader((int)resource.Offset, resourceDirectory.Length - (int)resource.Offset);
                    int length = (int)blob.ReadUInt32();
                    if (length > 4)
                    {
                        // Check for magic bytes that correspond to RESX
                        if (blob.ReadUInt32() == 0xBEEFCACE)
                            return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Gets a value indicating whether '<paramref name="module"/>' is a framework assembly.
        /// </summary>
        private static bool IsFrameworkAssembly(EcmaModule module)
        {
            MetadataReader reader = module.MetadataReader;

            // We look for [assembly:AssemblyMetadata(".NETFrameworkAssembly", "")]

            foreach (CustomAttributeHandle attributeHandle in reader.GetAssemblyDefinition().GetCustomAttributes())
            {
                if (!reader.GetAttributeNamespaceAndName(attributeHandle, out StringHandle namespaceHandle, out StringHandle nameHandle))
                    continue;

                if (!reader.StringComparer.Equals(namespaceHandle, "System.Reflection") ||
                    !reader.StringComparer.Equals(nameHandle, "AssemblyMetadataAttribute"))
                    continue;

                var attributeTypeProvider = new CustomAttributeTypeProvider(module);
                CustomAttribute attribute = reader.GetCustomAttribute(attributeHandle);
                CustomAttributeValue<TypeDesc> decodedAttribute = attribute.DecodeValue(attributeTypeProvider);

                if (decodedAttribute.FixedArguments.Length != 2)
                    continue;

                if (decodedAttribute.FixedArguments[0].Value is string s && s == ".NETFrameworkAssembly")
                    return true;
            }

            return false;
        }
    }
}