From 0fdb7f54af1def2120f9297a4388cdd024a3ccf8 Mon Sep 17 00:00:00 2001 From: Jan Dupej Date: Tue, 25 Jul 2023 12:47:59 +0200 Subject: [PATCH 1/3] Acquire MetadataReader under try. --- .../MarshalingPInvokeScanner.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs b/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs index de5e6e3762fdf7..ddbbd1e4d3b2d5 100644 --- a/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs +++ b/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs @@ -106,7 +106,17 @@ private bool IsAssemblyIncompatible(string assyPath, MinimalMarshalingTypeCompat { using FileStream file = new FileStream(assyPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); using PEReader peReader = new PEReader(file); - MetadataReader mdtReader = peReader.GetMetadataReader(); + MetadataReader mdtReader; + + try + { + mdtReader = peReader.GetMetadataReader(); + } + catch(InvalidOperationException) + { + // If the DLL does not have metadata, the previous call will fail with InvalidOperationException. + return false; + } foreach(CustomAttributeHandle attrHandle in mdtReader.CustomAttributes) { From 7a6ffc810d29058ba2bb411b9465b9b82dcbced3 Mon Sep 17 00:00:00 2001 From: Jan Dupej Date: Tue, 25 Jul 2023 16:30:21 +0200 Subject: [PATCH 2/3] Logging failed metadata access. --- .../MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs b/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs index ddbbd1e4d3b2d5..d3c99abaf36cb4 100644 --- a/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs +++ b/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs @@ -112,9 +112,10 @@ private bool IsAssemblyIncompatible(string assyPath, MinimalMarshalingTypeCompat { mdtReader = peReader.GetMetadataReader(); } - catch(InvalidOperationException) + catch(InvalidOperationException ex) { // If the DLL does not have metadata, the previous call will fail with InvalidOperationException. + Log.LogMessage(MessageImportance.Low, string.Format("Cannot read metadata in file {0}: {1}.", assyPath, ex.Message)); return false; } From b44ead0a2b916cc89a2ecada89af8ddba07a5f28 Mon Sep 17 00:00:00 2001 From: Jan Dupej <109523496+jandupej@users.noreply.github.com> Date: Wed, 26 Jul 2023 10:59:46 +0200 Subject: [PATCH 3/3] Update src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs Co-authored-by: Theodore Tsirpanis --- .../MarshalingPInvokeScanner.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs b/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs index d3c99abaf36cb4..850d3c787b50d1 100644 --- a/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs +++ b/src/tasks/MonoTargetsTasks/MarshalingPInvokeScanner/MarshalingPInvokeScanner.cs @@ -106,18 +106,11 @@ private bool IsAssemblyIncompatible(string assyPath, MinimalMarshalingTypeCompat { using FileStream file = new FileStream(assyPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); using PEReader peReader = new PEReader(file); - MetadataReader mdtReader; - - try + if (!peReader.HasMetadata) { - mdtReader = peReader.GetMetadataReader(); - } - catch(InvalidOperationException ex) - { - // If the DLL does not have metadata, the previous call will fail with InvalidOperationException. - Log.LogMessage(MessageImportance.Low, string.Format("Cannot read metadata in file {0}: {1}.", assyPath, ex.Message)); return false; } + MetadataReader mdtReader = peReader.GetMetadataReader(); foreach(CustomAttributeHandle attrHandle in mdtReader.CustomAttributes) {