forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add moke test for restricted callouts.
- Loading branch information
1 parent
07bb690
commit 6a13d23
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/tests/nativeaot/SmokeTests/GcRestrictedCallouts/GcRestrictedCallouts.csproj
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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<CLRTestKind>BuildAndRun</CLRTestKind> | ||
<CLRTestPriority>0</CLRTestPriority> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="RuntimeImportAttribute.cs" /> | ||
<Compile Include="RuntimeImports.cs" /> | ||
</ItemGroup> | ||
|
||
</Project> |
38 changes: 38 additions & 0 deletions
38
src/tests/nativeaot/SmokeTests/GcRestrictedCallouts/Program.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,38 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.CompilerServices; | ||
|
||
static class Program | ||
{ | ||
static readonly ConditionalWeakTable<object, object> s_weakTable = new(); | ||
static readonly object s_inTableObject = new(); | ||
static readonly object s_notInTableObject = new(); | ||
|
||
static volatile bool s_testPass; | ||
|
||
static unsafe int Main() | ||
{ | ||
s_weakTable.Add(s_inTableObject, new object()); | ||
|
||
Console.WriteLine("RhRegisterGcCallout"); | ||
RuntimeImports.RhRegisterGcCallout(RuntimeImports.GcRestrictedCalloutKind.AfterMarkPhase, | ||
(IntPtr)(delegate* unmanaged<uint, void>)&GcCallback); | ||
|
||
Console.WriteLine("GC.Collect"); | ||
GC.Collect(); | ||
|
||
Console.WriteLine("Test passed: " + s_testPass); | ||
return s_testPass ? 100 : 1; | ||
} | ||
|
||
[UnmanagedCallersOnly] | ||
static void GcCallback(uint uiCondemnedGeneration) | ||
{ | ||
s_testPass = s_weakTable.TryGetValue(s_inTableObject, out object _) && | ||
!s_weakTable.TryGetValue(s_notInTableObject, out object _); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/tests/nativeaot/SmokeTests/GcRestrictedCallouts/RuntimeImportAttribute.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,24 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Runtime | ||
{ | ||
// Exposed in Internal.CompilerServices only | ||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)] | ||
public sealed class RuntimeImportAttribute : Attribute | ||
{ | ||
public string DllName { get; } | ||
public string EntryPoint { get; } | ||
|
||
public RuntimeImportAttribute(string entry) | ||
{ | ||
EntryPoint = entry; | ||
} | ||
|
||
public RuntimeImportAttribute(string dllName, string entry) | ||
{ | ||
EntryPoint = entry; | ||
DllName = dllName; | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/tests/nativeaot/SmokeTests/GcRestrictedCallouts/RuntimeImports.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,31 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Runtime | ||
{ | ||
// Copied from src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs | ||
static class RuntimeImports | ||
{ | ||
private const string RuntimeLibrary = "*"; | ||
|
||
internal enum GcRestrictedCalloutKind | ||
{ | ||
StartCollection = 0, // Collection is about to begin | ||
EndCollection = 1, // Collection has completed | ||
AfterMarkPhase = 2, // All live objects are marked (not including ready for finalization objects), | ||
// no handles have been cleared | ||
} | ||
|
||
[MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
[RuntimeImport(RuntimeLibrary, "RhRegisterGcCallout")] | ||
internal static extern bool RhRegisterGcCallout(GcRestrictedCalloutKind eKind, IntPtr pCalloutMethod); | ||
|
||
[MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
[RuntimeImport(RuntimeLibrary, "RhUnregisterGcCallout")] | ||
internal static extern void RhUnregisterGcCallout(GcRestrictedCalloutKind eKind, IntPtr pCalloutMethod); | ||
} | ||
} |