-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release/5.0] P/Invoke function returning SafeSocketHandle throws Mis…
…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
Showing
5 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/libraries/System.IO.MemoryMappedFiles/src/ILLinkTrim_LibraryBuild.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
72 changes: 72 additions & 0 deletions
72
src/libraries/System.IO.MemoryMappedFiles/tests/SafeMemoryMappedViewHandleTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/libraries/System.Net.Sockets/src/ILLinkTrim_LibraryBuild.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters