Skip to content

Commit

Permalink
[release/5.0] P/Invoke function returning SafeSocketHandle throws Mis…
Browse files Browse the repository at this point in the history
…singMethodException (#45911) (#45942)

* P/Invoke function returning SafeSocketHandle throws MissingMethodException (#45911)

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

cherry-pick of af1b1ad

* Move ILLinkTrim files to the .csproj folder, where the 5.0 build infrastructure expects them.
  • Loading branch information
eerhardt authored Dec 11, 2020
1 parent 7728efe commit 83b4457
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
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.SafeMemoryMappedViewHandle">
<!-- 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 & ~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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
<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)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>
</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 83b4457

Please sign in to comment.