Fix access mode of memory-mapped apphost (#49341)
authorMateo Torres-Ruiz <mateoatr@users.noreply.github.com>
Tue, 9 Mar 2021 14:39:27 +0000 (06:39 -0800)
committerGitHub <noreply@github.com>
Tue, 9 Mar 2021 14:39:27 +0000 (09:39 -0500)
* Use read access on apphost memory mapped file

* Add test

src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs
src/installer/tests/Microsoft.NET.HostModel.Tests/Microsoft.NET.HostModel.AppHost.Tests/AppHostUpdateTests.cs

index 6ff8ec4..8bc479d 100644 (file)
@@ -7,7 +7,6 @@ using System.IO;
 using System.IO.MemoryMappedFiles;
 using System.Runtime.InteropServices;
 using System.Text;
-using System.Threading;
 
 namespace Microsoft.NET.HostModel.AppHost
 {
@@ -91,7 +90,7 @@ namespace Microsoft.NET.HostModel.AppHost
                     try
                     {
                         // Open the source host file.
-                        memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostSourceFilePath, FileMode.Open, null, 0, MemoryMappedFileAccess.CopyOnWrite);
+                        memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostSourceFilePath, FileMode.Open, null, 0, MemoryMappedFileAccess.Read);
                         memoryMappedViewAccessor = memoryMappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.CopyOnWrite);
 
                         // Transform the host file in-memory.
index 992506c..5fa06a5 100644 (file)
@@ -201,6 +201,25 @@ namespace Microsoft.NET.HostModel.Tests
             int GetLastError() => Marshal.GetLastWin32Error();
         }
 
+        [Fact]
+        public void CanCreateAppHost()
+        {
+            using (TestDirectory testDirectory = TestDirectory.Create())
+            {
+                string sourceAppHostMock = PrepareAppHostMockFile(testDirectory);
+                File.SetAttributes(sourceAppHostMock, FileAttributes.ReadOnly);
+                string destinationFilePath = Path.Combine(testDirectory.Path, "DestinationAppHost.exe.mock");
+                string appBinaryFilePath = "Test/App/Binary/Path.dll";
+                HostWriter.CreateAppHost(
+                   sourceAppHostMock,
+                   destinationFilePath,
+                   appBinaryFilePath,
+                   windowsGraphicalUserInterface: false);
+
+                File.SetAttributes(sourceAppHostMock, FileAttributes.Normal);
+            }
+        }
+
         private string PrepareAppHostMockFile(TestDirectory testDirectory, Action<byte[]> customize = null)
         {
             // For now we're testing the AppHost on Windows PE files only.