From d1fabed337cc400528dc7c04d491d9e6b84aabaa Mon Sep 17 00:00:00 2001 From: Jan Dupej Date: Thu, 27 Jul 2023 11:50:38 +0200 Subject: [PATCH 1/2] Fix second pass of marshalling scanner to also tolerate metadata-free PEs. --- .../MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs b/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs index 850d3c787b50d1..f8ed38efebaa27 100644 --- a/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs +++ b/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs @@ -81,6 +81,9 @@ private void ResolveInconclusiveTypes(HashSet incompatible, string assyP using FileStream file = new FileStream(assyPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); using PEReader peReader = new PEReader(file); + if (!peReader.HasMetadata) + return; // Just return. There are no metadata in the DLL to help us with remaining types. + MetadataReader mdtReader = peReader.GetMetadataReader(); SignatureDecoder decoder = new(mmtcp, mdtReader, null!); @@ -107,9 +110,8 @@ private bool IsAssemblyIncompatible(string assyPath, MinimalMarshalingTypeCompat using FileStream file = new FileStream(assyPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); using PEReader peReader = new PEReader(file); if (!peReader.HasMetadata) - { - return false; - } + return false; // No types in this DLL means no incompatible marshaling constructs. + MetadataReader mdtReader = peReader.GetMetadataReader(); foreach(CustomAttributeHandle attrHandle in mdtReader.CustomAttributes) From 638f91adfdd0c0d4ec92b4a0968ea1d7f20ea361 Mon Sep 17 00:00:00 2001 From: Jan Dupej Date: Thu, 27 Jul 2023 14:31:40 +0200 Subject: [PATCH 2/2] Assembly name is now checked only when metadata are present. --- .../MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs b/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs index f8ed38efebaa27..5f7ec7fd590e3f 100644 --- a/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs +++ b/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs @@ -74,17 +74,16 @@ private string[] ScanAssemblies(string[] assemblies) private void ResolveInconclusiveTypes(HashSet incompatible, string assyPath, MinimalMarshalingTypeCompatibilityProvider mmtcp) { - string assyName = MetadataReader.GetAssemblyName(assyPath).Name!; - HashSet inconclusiveTypes = mmtcp.GetInconclusiveTypesForAssembly(assyName); - if(inconclusiveTypes.Count == 0) - return; - using FileStream file = new FileStream(assyPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); using PEReader peReader = new PEReader(file); if (!peReader.HasMetadata) return; // Just return. There are no metadata in the DLL to help us with remaining types. MetadataReader mdtReader = peReader.GetMetadataReader(); + string assyName = mdtReader.GetString(mdtReader.GetAssemblyDefinition().Name); + HashSet inconclusiveTypes = mmtcp.GetInconclusiveTypesForAssembly(assyName); + if(inconclusiveTypes.Count == 0) + return; SignatureDecoder decoder = new(mmtcp, mdtReader, null!);