Skip to content

Commit

Permalink
P/Invoke function returning SafeSocketHandle throws MissingMethodExce…
Browse files Browse the repository at this point in the history
…ption

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 dotnet#45633
  • Loading branch information
eerhardt committed Dec 10, 2020
1 parent a7c3179 commit b38423b
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<linker>
<assembly fullname="System.IO.MemoryMappedFiles">
<type fullname="Microsoft.Win32.SafeHandles">
<!-- don't trim the default constructor, since it is necessary for p/invokes made by external code -->
<method name=".ctor" />
</type>
</assembly>
</linker>
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Tests for SafeMemoryMappedViewHandle
/// </summary>
public class SafeMemoryMappedViewHandleTests : MemoryMappedFilesTestBase
{
/// <summary>
/// Tests that external code can use SafeMemoryMappedViewHandle as the result of a P/Invoke on Windows.
/// </summary>
[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);
}

/// <summary>
/// Tests that external code can use SafeMemoryMappedViewHandle as the result of a P/Invoke on Unix.
/// </summary>
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix)]
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@
<Compile Include="MemoryMappedFilesTestsBase.Unix.cs" Condition="'$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true'" />
<Compile Include="MemoryMappedFilesTestsBase.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
<Compile Include="$(CommonTestPath)System\IO\TempFile.cs" Link="Common\System\IO\TempFile.cs" />
<Compile Include="SafeMemoryMappedViewHandleTests.cs" />
<Compile Include="XunitAssemblyAttributes.cs" />
<Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs" Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonTestPath)Tests\System\IO\StreamConformanceTests.cs" Link="Common\System\IO\StreamConformanceTests.cs" />
<Compile Include="$(CommonTestPath)System\IO\CallTrackingStream.cs" Link="Common\System\IO\CallTrackingStream.cs" />
<Compile Include="$(CommonTestPath)System\IO\ConnectedStreams.cs" Link="Common\System\IO\ConnectedStreams.cs" />
<Compile Include="$(CommonPath)System\Net\ArrayBuffer.cs" Link="ProductionCode\Common\System\Net\ArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs" Link="ProductionCode\Common\System\Net\StreamBuffer.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.BOOL.cs" Link="ProductionCode\Common\Interop\Windows\Interop.BOOL.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs" Link="ProductionCode\Common\Interop\Windows\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.CreateFileMapping.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.CreateFileMapping.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.MapViewOfFile.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.MapViewOfFile.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.MemOptions.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.MemOptions.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.SECURITY_ATTRIBUTES.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.SECURITY_ATTRIBUTES.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<linker>
<assembly fullname="System.Net.Sockets">
<type fullname="System.Net.Sockets.SafeSocketHandle">
<!-- don't trim the default constructor, since it is necessary for p/invokes made by external code -->
<method name=".ctor" />
</type>
</assembly>
</linker>
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
}

0 comments on commit b38423b

Please sign in to comment.