diff --git a/src/libraries/System.IO.MemoryMappedFiles/src/ILLinkTrim_LibraryBuild.xml b/src/libraries/System.IO.MemoryMappedFiles/src/ILLinkTrim_LibraryBuild.xml new file mode 100644 index 0000000000000..6c7d766e65f5c --- /dev/null +++ b/src/libraries/System.IO.MemoryMappedFiles/src/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/ILLinkTrim_LibraryBuild.xml b/src/libraries/System.Net.Sockets/src/ILLinkTrim_LibraryBuild.xml new file mode 100644 index 0000000000000..f135e9f7685c1 --- /dev/null +++ b/src/libraries/System.Net.Sockets/src/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); } }