Skip to content

Commit 968afdf

Browse files
committed
Merge in 'release/5.0' changes
2 parents 0260401 + 83b4457 commit 968afdf

5 files changed

Lines changed: 119 additions & 1 deletion

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<linker>
2+
<assembly fullname="System.IO.MemoryMappedFiles">
3+
<type fullname="Microsoft.Win32.SafeHandles.SafeMemoryMappedViewHandle">
4+
<!-- don't trim the default constructor, since it is necessary for p/invokes made by external code -->
5+
<method name=".ctor" />
6+
</type>
7+
</assembly>
8+
</linker>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Runtime.InteropServices;
5+
using Microsoft.Win32.SafeHandles;
6+
using Xunit;
7+
8+
namespace System.IO.MemoryMappedFiles.Tests
9+
{
10+
/// <summary>
11+
/// Tests for SafeMemoryMappedViewHandle
12+
/// </summary>
13+
public class SafeMemoryMappedViewHandleTests : MemoryMappedFilesTestBase
14+
{
15+
/// <summary>
16+
/// Tests that external code can use SafeMemoryMappedViewHandle as the result of a P/Invoke on Windows.
17+
/// </summary>
18+
[Fact]
19+
[PlatformSpecific(TestPlatforms.Windows)]
20+
public void SafeMemoryMappedViewHandle_CanUseInPInvoke_Windows()
21+
{
22+
const int BUF_SIZE = 256;
23+
24+
Interop.Kernel32.SECURITY_ATTRIBUTES secAttrs = default;
25+
using SafeMemoryMappedFileHandle fileHandle = Interop.Kernel32.CreateFileMapping(
26+
new IntPtr(-1),
27+
ref secAttrs,
28+
Interop.Kernel32.PageOptions.PAGE_EXECUTE_READWRITE,
29+
0,
30+
BUF_SIZE,
31+
CreateUniqueMapName());
32+
33+
using SafeMemoryMappedViewHandle handle = Interop.Kernel32.MapViewOfFile(
34+
fileHandle,
35+
Interop.Kernel32.FileMapOptions.FILE_MAP_READ,
36+
0,
37+
0,
38+
(UIntPtr)BUF_SIZE);
39+
40+
Assert.NotNull(handle);
41+
}
42+
43+
/// <summary>
44+
/// Tests that external code can use SafeMemoryMappedViewHandle as the result of a P/Invoke on Unix.
45+
/// </summary>
46+
[Fact]
47+
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)]
48+
public void SafeMemoryMappedViewHandle_CanUseInPInvoke_Unix()
49+
{
50+
const int MAP_PRIVATE = 0x02;
51+
const int MAP_ANONYMOUS = 0x10;
52+
53+
const int PROT_READ = 0x1;
54+
const int PROT_WRITE = 0x2;
55+
56+
// The handle returned may be invalid, but this is testing that the
57+
// SafeHandle object can successfully be created in a P/Invoke
58+
using SafeMemoryMappedViewHandle handle = mmap(
59+
IntPtr.Zero,
60+
1,
61+
PROT_READ | PROT_WRITE,
62+
MAP_PRIVATE | MAP_ANONYMOUS,
63+
-1,
64+
0);
65+
66+
Assert.NotNull(handle);
67+
}
68+
69+
[DllImport("libc")]
70+
private static unsafe extern SafeMemoryMappedViewHandle mmap(IntPtr addr, nint lengthint, int prot, int flags, int fd, nuint offset);
71+
}
72+
}

src/libraries/System.IO.MemoryMappedFiles/tests/System.IO.MemoryMappedFiles.Tests.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
<Compile Include="MemoryMappedFilesTestsBase.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
1818
<Compile Include="$(CommonTestPath)System\IO\TempFile.cs"
1919
Link="Common\System\IO\TempFile.cs" />
20+
<Compile Include="SafeMemoryMappedViewHandleTests.cs" />
2021
<Compile Include="XunitAssemblyAttributes.cs" />
22+
<Compile Include="$(CommonPath)Interop\Windows\Interop.BOOL.cs" Link="ProductionCode\Common\Interop\Windows\Interop.BOOL.cs" />
23+
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs" Link="ProductionCode\Common\Interop\Windows\Interop.Libraries.cs" />
24+
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.CreateFileMapping.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.CreateFileMapping.cs" />
25+
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.MapViewOfFile.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.MapViewOfFile.cs" />
26+
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.MemOptions.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.MemOptions.cs" />
27+
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.SECURITY_ATTRIBUTES.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.SECURITY_ATTRIBUTES.cs" />
2128
</ItemGroup>
22-
</Project>
29+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<linker>
2+
<assembly fullname="System.Net.Sockets">
3+
<type fullname="System.Net.Sockets.SafeSocketHandle">
4+
<!-- don't trim the default constructor, since it is necessary for p/invokes made by external code -->
5+
<method name=".ctor" />
6+
</type>
7+
</assembly>
8+
</linker>

src/libraries/System.Net.Sockets/tests/FunctionalTests/SafeHandleTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Runtime.InteropServices;
45
using Xunit;
56

67
namespace System.Net.Sockets.Tests
@@ -15,5 +16,27 @@ public static void SafeHandle_NotIsInvalid()
1516
Assert.False(s.SafeHandle.IsInvalid);
1617
}
1718
}
19+
20+
[Fact]
21+
[PlatformSpecific(TestPlatforms.Windows | TestPlatforms.AnyUnix)]
22+
public void SafeSocketHandle_CanUseInPInvoke()
23+
{
24+
const int AF_INET = 2;
25+
const int SOCK_STREAM = 1;
26+
27+
using SafeSocketHandle handle = Socket(AF_INET, SOCK_STREAM, 0);
28+
Assert.NotNull(handle);
29+
}
30+
31+
private static SafeSocketHandle Socket(int af, int type, int protocol) =>
32+
OperatingSystem.IsWindows() ?
33+
SocketWindows(af, type, protocol) :
34+
SocketUnix(af, type, protocol);
35+
36+
[DllImport("ws2_32.dll", EntryPoint = "socket")]
37+
private static extern SafeSocketHandle SocketWindows(int af, int type, int protocol);
38+
39+
[DllImport("libc", EntryPoint = "socket")]
40+
private static extern SafeSocketHandle SocketUnix(int af, int type, int protocol);
1841
}
1942
}

0 commit comments

Comments
 (0)