| 
 | 1 | +// Licensed to the .NET Foundation under one or more agreements.  | 
 | 2 | +// The .NET Foundation licenses this file to you under the MIT license.  | 
 | 3 | + | 
 | 4 | +using System;  | 
 | 5 | +using System.Collections.Generic;  | 
 | 6 | +using System.Runtime.CompilerServices;  | 
 | 7 | +using System.Diagnostics.CodeAnalysis;  | 
 | 8 | +using System.Reflection.Metadata;  | 
 | 9 | +using System.Reflection.Metadata.Ecma335;  | 
 | 10 | +using System.Collections.Immutable;  | 
 | 11 | +using System.IO;  | 
 | 12 | +using System.Linq;  | 
 | 13 | +using System.Text;  | 
 | 14 | +using System.Reflection;  | 
 | 15 | +using System.Reflection.PortableExecutable;  | 
 | 16 | +using Microsoft.Build.Framework;  | 
 | 17 | +using Microsoft.Build.Utilities;  | 
 | 18 | + | 
 | 19 | +namespace MonoTargetsTasks  | 
 | 20 | +{  | 
 | 21 | +    public class MarshalingPInvokeScanner : Task  | 
 | 22 | +    {  | 
 | 23 | +        [Required]  | 
 | 24 | +        public string[] Assemblies { get; set; } = Array.Empty<string>();  | 
 | 25 | + | 
 | 26 | +        [Output]  | 
 | 27 | +        public string[]? IncompatibleAssemblies { get; private set; }  | 
 | 28 | + | 
 | 29 | +        public override bool Execute()  | 
 | 30 | +        {  | 
 | 31 | +            if (Assemblies is null || Assemblies!.Length == 0)  | 
 | 32 | +            {  | 
 | 33 | +                Log.LogError($"{nameof(MarshalingPInvokeScanner)}.{nameof(Assemblies)} cannot be empty");  | 
 | 34 | +                return false;  | 
 | 35 | +            }  | 
 | 36 | + | 
 | 37 | +            try  | 
 | 38 | +            {  | 
 | 39 | +                ExecuteInternal();  | 
 | 40 | +                return !Log.HasLoggedErrors;  | 
 | 41 | +            }  | 
 | 42 | +            catch (LogAsErrorException e)  | 
 | 43 | +            {  | 
 | 44 | +                Log.LogError(e.Message);  | 
 | 45 | +                return false;  | 
 | 46 | +            }  | 
 | 47 | +        }  | 
 | 48 | + | 
 | 49 | +        private void ExecuteInternal()  | 
 | 50 | +        {  | 
 | 51 | +            IncompatibleAssemblies = ScanAssemblies(Assemblies);  | 
 | 52 | +        }  | 
 | 53 | + | 
 | 54 | +        private string[] ScanAssemblies(string[] assemblies)  | 
 | 55 | +        {  | 
 | 56 | +            HashSet<string> incompatible = new HashSet<string>();  | 
 | 57 | +            MinimalMarshalingTypeCompatibilityProvider mmtcp = new(Log);  | 
 | 58 | +            foreach (string aname in assemblies)  | 
 | 59 | +            {  | 
 | 60 | +                if (IsAssemblyIncompatible(aname, mmtcp))  | 
 | 61 | +                    incompatible.Add(aname);  | 
 | 62 | +            }  | 
 | 63 | + | 
 | 64 | +            if (mmtcp.IsSecondPassNeeded)  | 
 | 65 | +            {  | 
 | 66 | +                foreach (string aname in assemblies)  | 
 | 67 | +                    ResolveInconclusiveTypes(incompatible, aname, mmtcp);  | 
 | 68 | +            }  | 
 | 69 | + | 
 | 70 | +            return incompatible.ToArray();  | 
 | 71 | +        }  | 
 | 72 | + | 
 | 73 | +        private static string GetMethodName(MetadataReader mr, MethodDefinition md) => mr.GetString(md.Name);  | 
 | 74 | + | 
 | 75 | +        private void ResolveInconclusiveTypes(HashSet<string> incompatible, string assyPath, MinimalMarshalingTypeCompatibilityProvider mmtcp)  | 
 | 76 | +        {  | 
 | 77 | +            string assyName = MetadataReader.GetAssemblyName(assyPath).Name!;  | 
 | 78 | +            HashSet<string> inconclusiveTypes = mmtcp.GetInconclusiveTypesForAssembly(assyName);  | 
 | 79 | +            if(inconclusiveTypes.Count == 0)  | 
 | 80 | +                return;  | 
 | 81 | + | 
 | 82 | +            using FileStream file = new FileStream(assyPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);  | 
 | 83 | +            using PEReader peReader = new PEReader(file);  | 
 | 84 | +            MetadataReader mdtReader = peReader.GetMetadataReader();  | 
 | 85 | + | 
 | 86 | +            SignatureDecoder<Compatibility, object> decoder = new(mmtcp, mdtReader, null!);  | 
 | 87 | + | 
 | 88 | +            foreach (TypeDefinitionHandle typeDefHandle in mdtReader.TypeDefinitions)  | 
 | 89 | +            {  | 
 | 90 | +                TypeDefinition typeDef = mdtReader.GetTypeDefinition(typeDefHandle);  | 
 | 91 | +                string fullTypeName = string.Join(":", mdtReader.GetString(typeDef.Namespace), mdtReader.GetString(typeDef.Name));  | 
 | 92 | + | 
 | 93 | +                // This is not perfect, but should work right for enums defined in other assemblies,  | 
 | 94 | +                // which is the only case where we use Compatibility.Inconclusive.  | 
 | 95 | +                if (inconclusiveTypes.Contains(fullTypeName) &&  | 
 | 96 | +                    mmtcp.GetTypeFromDefinition(mdtReader, typeDefHandle, 0) != Compatibility.Compatible)  | 
 | 97 | +                {  | 
 | 98 | +                    Log.LogMessage(MessageImportance.Low, string.Format("Type {0} is marshaled and requires marshal-ilgen.", fullTypeName));  | 
 | 99 | + | 
 | 100 | +                    incompatible.Add("(unknown assembly)");  | 
 | 101 | +                }  | 
 | 102 | +            }  | 
 | 103 | +        }  | 
 | 104 | + | 
 | 105 | +        private bool IsAssemblyIncompatible(string assyPath, MinimalMarshalingTypeCompatibilityProvider mmtcp)  | 
 | 106 | +        {  | 
 | 107 | +            using FileStream file = new FileStream(assyPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);  | 
 | 108 | +            using PEReader peReader = new PEReader(file);  | 
 | 109 | +            MetadataReader mdtReader = peReader.GetMetadataReader();  | 
 | 110 | + | 
 | 111 | +            foreach(CustomAttributeHandle attrHandle in mdtReader.CustomAttributes)  | 
 | 112 | +            {  | 
 | 113 | +                CustomAttribute attr = mdtReader.GetCustomAttribute(attrHandle);  | 
 | 114 | + | 
 | 115 | +                if(attr.Constructor.Kind == HandleKind.MethodDefinition)  | 
 | 116 | +                {  | 
 | 117 | +                    MethodDefinitionHandle mdh = (MethodDefinitionHandle)attr.Constructor;  | 
 | 118 | +                    MethodDefinition md = mdtReader.GetMethodDefinition(mdh);  | 
 | 119 | +                    TypeDefinitionHandle tdh = md.GetDeclaringType();  | 
 | 120 | +                    TypeDefinition td = mdtReader.GetTypeDefinition(tdh);  | 
 | 121 | + | 
 | 122 | +                    if(mdtReader.GetString(td.Namespace) == "System.Runtime.CompilerServices" &&  | 
 | 123 | +                        mdtReader.GetString(td.Name) == "DisableRuntimeMarshallingAttribute")  | 
 | 124 | +                        return false;  | 
 | 125 | +                }  | 
 | 126 | +            }  | 
 | 127 | + | 
 | 128 | +            foreach (TypeDefinitionHandle typeDefHandle in mdtReader.TypeDefinitions)  | 
 | 129 | +            {  | 
 | 130 | +                TypeDefinition typeDef = mdtReader.GetTypeDefinition(typeDefHandle);  | 
 | 131 | +                string ns = mdtReader.GetString(typeDef.Namespace);  | 
 | 132 | +                string name = mdtReader.GetString(typeDef.Name);  | 
 | 133 | + | 
 | 134 | +                foreach(MethodDefinitionHandle mthDefHandle in typeDef.GetMethods())  | 
 | 135 | +                {  | 
 | 136 | +                    MethodDefinition mthDef = mdtReader.GetMethodDefinition(mthDefHandle);  | 
 | 137 | +                    if(!mthDef.Attributes.HasFlag(MethodAttributes.PinvokeImpl))  | 
 | 138 | +                        continue;  | 
 | 139 | + | 
 | 140 | +                    BlobReader sgnBlobReader = mdtReader.GetBlobReader(mthDef.Signature);  | 
 | 141 | +                    SignatureDecoder<Compatibility, object> decoder = new(mmtcp, mdtReader, null!);  | 
 | 142 | + | 
 | 143 | +                    MethodSignature<Compatibility> sgn = decoder.DecodeMethodSignature(ref sgnBlobReader);  | 
 | 144 | +                    if(sgn.ReturnType == Compatibility.Incompatible || sgn.ParameterTypes.Any(p => p == Compatibility.Incompatible))  | 
 | 145 | +                    {  | 
 | 146 | +                        Log.LogMessage(MessageImportance.Low, string.Format("Assembly {0} requires marhsal-ilgen for method {1}.{2}:{3} (first pass).",  | 
 | 147 | +                            assyPath, ns, name, mdtReader.GetString(mthDef.Name)));  | 
 | 148 | + | 
 | 149 | +                        return true;  | 
 | 150 | +                    }  | 
 | 151 | +                }  | 
 | 152 | +            }  | 
 | 153 | + | 
 | 154 | +            return false;  | 
 | 155 | +        }  | 
 | 156 | +    }  | 
 | 157 | +}  | 
0 commit comments