From 15a2b424bfd26cc5494711d5de5320d179a18183 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Thu, 10 Dec 2020 20:02:34 -0600 Subject: [PATCH 1/2] P/Invoke function returning SafeSocketHandle throws MissingMethodException (#45911) * P/Invoke function returning SafeSocketHandle throws MissingMethodException The ILLinker is trimming the private default constructors of SafeHandle classes if they are not used by the library. Adding an XML descriptor file to preserve the private constructors of these SafeHandles so they can be used in P/Invokes in external code. Fix #45633 Suppress P/Invoke test on Browser --- .../src/ILLink/ILLinkTrim_LibraryBuild.xml | 8 +++ .../tests/SafeMemoryMappedViewHandleTests.cs | 72 +++++++++++++++++++ .../System.IO.MemoryMappedFiles.Tests.csproj | 9 ++- .../src/ILLink/ILLinkTrim_LibraryBuild.xml | 8 +++ .../tests/FunctionalTests/SafeHandleTest.cs | 23 ++++++ 5 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 src/libraries/System.IO.MemoryMappedFiles/src/ILLink/ILLinkTrim_LibraryBuild.xml create mode 100644 src/libraries/System.IO.MemoryMappedFiles/tests/SafeMemoryMappedViewHandleTests.cs create mode 100644 src/libraries/System.Net.Sockets/src/ILLink/ILLinkTrim_LibraryBuild.xml diff --git a/src/libraries/System.IO.MemoryMappedFiles/src/ILLink/ILLinkTrim_LibraryBuild.xml b/src/libraries/System.IO.MemoryMappedFiles/src/ILLink/ILLinkTrim_LibraryBuild.xml new file mode 100644 index 0000000000000..6c7d766e65f5c --- /dev/null +++ b/src/libraries/System.IO.MemoryMappedFiles/src/ILLink/ILLinkTrim_LibraryBuild.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/src/libraries/System.IO.MemoryMappedFiles/tests/SafeMemoryMappedViewHandleTests.cs b/src/libraries/System.IO.MemoryMappedFiles/tests/SafeMemoryMappedViewHandleTests.cs new file mode 100644 index 0000000000000..3941ab7f8adac --- /dev/null +++ b/src/libraries/System.IO.MemoryMappedFiles/tests/SafeMemoryMappedViewHandleTests.cs @@ -0,0 +1,72 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; +using Xunit; + +namespace System.IO.MemoryMappedFiles.Tests +{ + /// + /// Tests for SafeMemoryMappedViewHandle + /// + public class SafeMemoryMappedViewHandleTests : MemoryMappedFilesTestBase + { + /// + /// Tests that external code can use SafeMemoryMappedViewHandle as the result of a P/Invoke on Windows. + /// + [Fact] + [PlatformSpecific(TestPlatforms.Windows)] + public void SafeMemoryMappedViewHandle_CanUseInPInvoke_Windows() + { + const int BUF_SIZE = 256; + + Interop.Kernel32.SECURITY_ATTRIBUTES secAttrs = default; + using SafeMemoryMappedFileHandle fileHandle = Interop.Kernel32.CreateFileMapping( + new IntPtr(-1), + ref secAttrs, + Interop.Kernel32.PageOptions.PAGE_EXECUTE_READWRITE, + 0, + BUF_SIZE, + CreateUniqueMapName()); + + using SafeMemoryMappedViewHandle handle = Interop.Kernel32.MapViewOfFile( + fileHandle, + Interop.Kernel32.FileMapOptions.FILE_MAP_READ, + 0, + 0, + (UIntPtr)BUF_SIZE); + + Assert.NotNull(handle); + } + + /// + /// Tests that external code can use SafeMemoryMappedViewHandle as the result of a P/Invoke on Unix. + /// + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)] + public void SafeMemoryMappedViewHandle_CanUseInPInvoke_Unix() + { + const int MAP_PRIVATE = 0x02; + const int MAP_ANONYMOUS = 0x10; + + const int PROT_READ = 0x1; + const int PROT_WRITE = 0x2; + + // The handle returned may be invalid, but this is testing that the + // SafeHandle object can successfully be created in a P/Invoke + using SafeMemoryMappedViewHandle handle = mmap( + IntPtr.Zero, + 1, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, + -1, + 0); + + Assert.NotNull(handle); + } + + [DllImport("libc")] + private static unsafe extern SafeMemoryMappedViewHandle mmap(IntPtr addr, nint lengthint, int prot, int flags, int fd, nuint offset); + } +} diff --git a/src/libraries/System.IO.MemoryMappedFiles/tests/System.IO.MemoryMappedFiles.Tests.csproj b/src/libraries/System.IO.MemoryMappedFiles/tests/System.IO.MemoryMappedFiles.Tests.csproj index f0c3ca4b20818..234773ebf7144 100644 --- a/src/libraries/System.IO.MemoryMappedFiles/tests/System.IO.MemoryMappedFiles.Tests.csproj +++ b/src/libraries/System.IO.MemoryMappedFiles/tests/System.IO.MemoryMappedFiles.Tests.csproj @@ -17,6 +17,13 @@ + + + + + + + - \ No newline at end of file + diff --git a/src/libraries/System.Net.Sockets/src/ILLink/ILLinkTrim_LibraryBuild.xml b/src/libraries/System.Net.Sockets/src/ILLink/ILLinkTrim_LibraryBuild.xml new file mode 100644 index 0000000000000..f135e9f7685c1 --- /dev/null +++ b/src/libraries/System.Net.Sockets/src/ILLink/ILLinkTrim_LibraryBuild.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SafeHandleTest.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SafeHandleTest.cs index c45786c30d7f5..36b594654f744 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SafeHandleTest.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SafeHandleTest.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Runtime.InteropServices; using Xunit; namespace System.Net.Sockets.Tests @@ -15,5 +16,27 @@ public static void SafeHandle_NotIsInvalid() Assert.False(s.SafeHandle.IsInvalid); } } + + [Fact] + [PlatformSpecific(TestPlatforms.Windows | TestPlatforms.AnyUnix)] + public void SafeSocketHandle_CanUseInPInvoke() + { + const int AF_INET = 2; + const int SOCK_STREAM = 1; + + using SafeSocketHandle handle = Socket(AF_INET, SOCK_STREAM, 0); + Assert.NotNull(handle); + } + + private static SafeSocketHandle Socket(int af, int type, int protocol) => + OperatingSystem.IsWindows() ? + SocketWindows(af, type, protocol) : + SocketUnix(af, type, protocol); + + [DllImport("ws2_32.dll", EntryPoint = "socket")] + private static extern SafeSocketHandle SocketWindows(int af, int type, int protocol); + + [DllImport("libc", EntryPoint = "socket")] + private static extern SafeSocketHandle SocketUnix(int af, int type, int protocol); } } From d15ad7ba9a133271289cb39ee638e499edd42708 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Fri, 11 Dec 2020 09:22:16 -0600 Subject: [PATCH 2/2] Move ILLinkTrim files to the .csproj folder, where the 5.0 build infrastructure expects them. --- .../src/{ILLink => }/ILLinkTrim_LibraryBuild.xml | 0 .../src/{ILLink => }/ILLinkTrim_LibraryBuild.xml | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/libraries/System.IO.MemoryMappedFiles/src/{ILLink => }/ILLinkTrim_LibraryBuild.xml (100%) rename src/libraries/System.Net.Sockets/src/{ILLink => }/ILLinkTrim_LibraryBuild.xml (100%) diff --git a/src/libraries/System.IO.MemoryMappedFiles/src/ILLink/ILLinkTrim_LibraryBuild.xml b/src/libraries/System.IO.MemoryMappedFiles/src/ILLinkTrim_LibraryBuild.xml similarity index 100% rename from src/libraries/System.IO.MemoryMappedFiles/src/ILLink/ILLinkTrim_LibraryBuild.xml rename to src/libraries/System.IO.MemoryMappedFiles/src/ILLinkTrim_LibraryBuild.xml diff --git a/src/libraries/System.Net.Sockets/src/ILLink/ILLinkTrim_LibraryBuild.xml b/src/libraries/System.Net.Sockets/src/ILLinkTrim_LibraryBuild.xml similarity index 100% rename from src/libraries/System.Net.Sockets/src/ILLink/ILLinkTrim_LibraryBuild.xml rename to src/libraries/System.Net.Sockets/src/ILLinkTrim_LibraryBuild.xml