PEReader doesn't work with "IsLoadedImage" and "PrefetchMetadata" options (#80106)
authorBadre BSAILA <54767641+pedrobsaila@users.noreply.github.com>
Fri, 13 Jan 2023 01:16:49 +0000 (02:16 +0100)
committerGitHub <noreply@github.com>
Fri, 13 Jan 2023 01:16:49 +0000 (17:16 -0800)
* PEReader doesn't work with "IsLoadedImage" and "PrefetchMetadata" options

* Apply suggestions from code review

Co-authored-by: MartyIX <203266+MartyIX@users.noreply.github.com>
Co-authored-by: Buyaa Namnan <buyankhishig.namnan@microsoft.com>
src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEReader.cs
src/libraries/System.Reflection.Metadata/tests/PortableExecutable/PEReaderTests.cs

index 674a050..d2e1083 100644 (file)
@@ -199,7 +199,7 @@ namespace System.Reflection.PortableExecutable
                     else
                     {
                         // The peImage is left null, but the lazyMetadataBlock is initialized up front.
-                        _lazyPEHeaders = new PEHeaders(peStream);
+                        _lazyPEHeaders = new PEHeaders(peStream, actualSize, IsLoadedImage);
                         _lazyMetadataBlock = StreamMemoryBlockProvider.ReadMemoryBlockNoLock(peStream, _lazyPEHeaders.MetadataStartOffset, _lazyPEHeaders.MetadataSize);
                     }
                     // We read all we need, the stream is going to be closed.
index a2e0764..f5358ca 100644 (file)
@@ -1,13 +1,14 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-using System;
 using System.Collections.Immutable;
 using System.IO;
+using System.Reflection.Internal;
 using System.Reflection.Metadata;
 using System.Reflection.Metadata.Ecma335;
 using System.Reflection.Metadata.Tests;
 using System.Runtime.CompilerServices;
+using Microsoft.Win32.SafeHandles;
 using Xunit;
 
 namespace System.Reflection.PortableExecutable.Tests
@@ -849,5 +850,25 @@ namespace System.Reflection.PortableExecutable.Tests
 
             Assert.Equal(@"Debug", reader.GetString(reader.GetAssemblyDefinition().Name));
         }
+
+        [Fact]
+        [PlatformSpecific(TestPlatforms.Windows)]  // Uses P/Invokes only suported on windows
+        public unsafe void InvokeCtorWithIsLoadedImageAndPrefetchMetadataOptions2()
+        {
+            using (var tempFile = new TempFile(Path.GetTempFileName()))
+            {
+                File.WriteAllBytes(tempFile.Path, Misc.Members);
+
+                using (SafeLibraryHandle libHandle = global::Interop.Kernel32.LoadLibraryExW(tempFile.Path, IntPtr.Zero, 0))
+                {
+                    byte* peImagePtr = (byte*)global::Interop.Kernel32.GetModuleHandle(Path.GetFileName(tempFile.Path));
+
+                    Assert.True(peImagePtr != null);
+
+                    var peReader = new PEReader(new ReadOnlyUnmanagedMemoryStream(peImagePtr, int.MaxValue), PEStreamOptions.IsLoadedImage | PEStreamOptions.PrefetchMetadata);
+                    peReader.Dispose();
+                }
+            }
+        }
     }
 }