Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a13d23

Browse files
committed
Add moke test for restricted callouts.
1 parent 07bb690 commit 6a13d23

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<CLRTestKind>BuildAndRun</CLRTestKind>
6+
<CLRTestPriority>0</CLRTestPriority>
7+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Compile Include="Program.cs" />
12+
<Compile Include="RuntimeImportAttribute.cs" />
13+
<Compile Include="RuntimeImports.cs" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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;
5+
using System.Runtime;
6+
using System.Runtime.InteropServices;
7+
using System.Runtime.CompilerServices;
8+
9+
static class Program
10+
{
11+
static readonly ConditionalWeakTable<object, object> s_weakTable = new();
12+
static readonly object s_inTableObject = new();
13+
static readonly object s_notInTableObject = new();
14+
15+
static volatile bool s_testPass;
16+
17+
static unsafe int Main()
18+
{
19+
s_weakTable.Add(s_inTableObject, new object());
20+
21+
Console.WriteLine("RhRegisterGcCallout");
22+
RuntimeImports.RhRegisterGcCallout(RuntimeImports.GcRestrictedCalloutKind.AfterMarkPhase,
23+
(IntPtr)(delegate* unmanaged<uint, void>)&GcCallback);
24+
25+
Console.WriteLine("GC.Collect");
26+
GC.Collect();
27+
28+
Console.WriteLine("Test passed: " + s_testPass);
29+
return s_testPass ? 100 : 1;
30+
}
31+
32+
[UnmanagedCallersOnly]
33+
static void GcCallback(uint uiCondemnedGeneration)
34+
{
35+
s_testPass = s_weakTable.TryGetValue(s_inTableObject, out object _) &&
36+
!s_weakTable.TryGetValue(s_notInTableObject, out object _);
37+
}
38+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
namespace System.Runtime
5+
{
6+
// Exposed in Internal.CompilerServices only
7+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)]
8+
public sealed class RuntimeImportAttribute : Attribute
9+
{
10+
public string DllName { get; }
11+
public string EntryPoint { get; }
12+
13+
public RuntimeImportAttribute(string entry)
14+
{
15+
EntryPoint = entry;
16+
}
17+
18+
public RuntimeImportAttribute(string dllName, string entry)
19+
{
20+
EntryPoint = entry;
21+
DllName = dllName;
22+
}
23+
}
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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;
5+
using System.Runtime;
6+
using System.Runtime.CompilerServices;
7+
8+
namespace System.Runtime
9+
{
10+
// Copied from src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs
11+
static class RuntimeImports
12+
{
13+
private const string RuntimeLibrary = "*";
14+
15+
internal enum GcRestrictedCalloutKind
16+
{
17+
StartCollection = 0, // Collection is about to begin
18+
EndCollection = 1, // Collection has completed
19+
AfterMarkPhase = 2, // All live objects are marked (not including ready for finalization objects),
20+
// no handles have been cleared
21+
}
22+
23+
[MethodImplAttribute(MethodImplOptions.InternalCall)]
24+
[RuntimeImport(RuntimeLibrary, "RhRegisterGcCallout")]
25+
internal static extern bool RhRegisterGcCallout(GcRestrictedCalloutKind eKind, IntPtr pCalloutMethod);
26+
27+
[MethodImplAttribute(MethodImplOptions.InternalCall)]
28+
[RuntimeImport(RuntimeLibrary, "RhUnregisterGcCallout")]
29+
internal static extern void RhUnregisterGcCallout(GcRestrictedCalloutKind eKind, IntPtr pCalloutMethod);
30+
}
31+
}

0 commit comments

Comments
 (0)