-
Notifications
You must be signed in to change notification settings - Fork 5.4k
[cDAC] Add continuation support to RuntimeTypeSystem #124918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
max-charlamb
merged 3 commits into
dotnet:main
from
max-charlamb:cdac-continuation-support
Mar 6, 2026
+410
−4
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
168 changes: 168 additions & 0 deletions
168
src/native/managed/cdac/tests/DumpTests/AsyncContinuationDumpTests.cs
This file contains hidden or 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,168 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Reflection.Metadata; | ||
| using System.Reflection.Metadata.Ecma335; | ||
| using Microsoft.Diagnostics.DataContractReader.Contracts; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Diagnostics.DataContractReader.DumpTests; | ||
|
|
||
| /// <summary> | ||
| /// Dump-based integration tests for async continuation support in the RuntimeTypeSystem contract. | ||
| /// Uses the AsyncContinuation debuggee dump, which runs an async2 method to trigger | ||
| /// continuation MethodTable creation. | ||
| /// </summary> | ||
| public class AsyncContinuationDumpTests : DumpTestBase | ||
| { | ||
| protected override string DebuggeeName => "AsyncContinuation"; | ||
| protected override string DumpType => "full"; | ||
|
|
||
| [ConditionalTheory] | ||
| [MemberData(nameof(TestConfigurations))] | ||
| [SkipOnVersion("net10.0", "Continuation support is not available in .NET 10")] | ||
| public void ContinuationMethodTable_IsNonNull(TestConfiguration config) | ||
| { | ||
| InitializeDumpTest(config); | ||
|
|
||
| TargetPointer continuationMTGlobal = Target.ReadGlobalPointer("ContinuationMethodTable"); | ||
| TargetPointer continuationMT = Target.ReadPointer(continuationMTGlobal); | ||
| Assert.NotEqual(TargetPointer.Null, continuationMT); | ||
| } | ||
|
|
||
| [ConditionalTheory] | ||
| [MemberData(nameof(TestConfigurations))] | ||
| [SkipOnVersion("net10.0", "Continuation support is not available in .NET 10")] | ||
| public void ContinuationBaseClass_IsNotContinuation(TestConfiguration config) | ||
| { | ||
| InitializeDumpTest(config); | ||
| IRuntimeTypeSystem rts = Target.Contracts.RuntimeTypeSystem; | ||
|
|
||
| // The ContinuationMethodTable global points to the Continuation base class itself. | ||
| // IsContinuation checks if a type's parent is the Continuation base class, | ||
| // so the base class itself is NOT considered a continuation (its parent is Object). | ||
| TargetPointer continuationMTGlobal = Target.ReadGlobalPointer("ContinuationMethodTable"); | ||
| TargetPointer continuationMT = Target.ReadPointer(continuationMTGlobal); | ||
| Assert.NotEqual(TargetPointer.Null, continuationMT); | ||
|
|
||
| TypeHandle handle = rts.GetTypeHandle(continuationMT); | ||
| Assert.False(rts.IsContinuation(handle)); | ||
| } | ||
|
|
||
| [ConditionalTheory] | ||
| [MemberData(nameof(TestConfigurations))] | ||
| [SkipOnVersion("net10.0", "Continuation support is not available in .NET 10")] | ||
| public void ObjectMethodTable_IsNotContinuation(TestConfiguration config) | ||
| { | ||
| InitializeDumpTest(config); | ||
| IRuntimeTypeSystem rts = Target.Contracts.RuntimeTypeSystem; | ||
|
|
||
| TargetPointer objectMTGlobal = Target.ReadGlobalPointer("ObjectMethodTable"); | ||
| TargetPointer objectMT = Target.ReadPointer(objectMTGlobal); | ||
| TypeHandle objectHandle = rts.GetTypeHandle(objectMT); | ||
| Assert.False(rts.IsContinuation(objectHandle)); | ||
| } | ||
|
|
||
| [ConditionalTheory] | ||
| [MemberData(nameof(TestConfigurations))] | ||
| [SkipOnVersion("net10.0", "Continuation support is not available in .NET 10")] | ||
| public void ThreadLocalContinuation_IsContinuation(TestConfiguration config) | ||
| { | ||
| InitializeDumpTest(config); | ||
| IRuntimeTypeSystem rts = Target.Contracts.RuntimeTypeSystem; | ||
| ILoader loader = Target.Contracts.Loader; | ||
| IThread threadContract = Target.Contracts.Thread; | ||
| IEcmaMetadata ecmaMetadata = Target.Contracts.EcmaMetadata; | ||
|
|
||
| // 1. Locate the AsyncDispatcherInfo type in System.Private.CoreLib. | ||
| TargetPointer systemAssembly = loader.GetSystemAssembly(); | ||
| ModuleHandle coreLibModule = loader.GetModuleHandleFromAssemblyPtr(systemAssembly); | ||
| TypeHandle asyncDispatcherInfoHandle = rts.GetTypeByNameAndModule( | ||
| "AsyncDispatcherInfo", | ||
| "System.Runtime.CompilerServices", | ||
| coreLibModule); | ||
| Assert.True(asyncDispatcherInfoHandle.Address != 0, | ||
| "Could not find AsyncDispatcherInfo type in CoreLib"); | ||
|
|
||
| // 2. Find the t_current field's offset within the non-GC thread statics block. | ||
| // Walk the FieldDescList to find the ThreadStatic field named "t_current". | ||
| System.Reflection.Metadata.MetadataReader? md = ecmaMetadata.GetMetadata(coreLibModule); | ||
| Assert.NotNull(md); | ||
|
|
||
| TargetPointer fieldDescList = rts.GetFieldDescList(asyncDispatcherInfoHandle); | ||
| ushort numStaticFields = rts.GetNumStaticFields(asyncDispatcherInfoHandle); | ||
| ushort numThreadStaticFields = rts.GetNumThreadStaticFields(asyncDispatcherInfoHandle); | ||
| ushort numInstanceFields = rts.GetNumInstanceFields(asyncDispatcherInfoHandle); | ||
|
|
||
| // FieldDescList has instance fields first, then static fields. | ||
| // Thread-static fields are among the static fields. | ||
| uint tCurrentOffset = 0; | ||
| bool foundField = false; | ||
| int totalFields = numInstanceFields + numStaticFields; | ||
| uint fieldDescSize = Target.GetTypeInfo(DataType.FieldDesc).Size!.Value; | ||
|
|
||
| for (int i = numInstanceFields; i < totalFields; i++) | ||
| { | ||
| TargetPointer fieldDesc = fieldDescList + (ulong)(i * (int)fieldDescSize); | ||
| if (!rts.IsFieldDescThreadStatic(fieldDesc)) | ||
| continue; | ||
|
|
||
| uint memberDef = rts.GetFieldDescMemberDef(fieldDesc); | ||
| var fieldDefHandle = (FieldDefinitionHandle)MetadataTokens.Handle((int)memberDef); | ||
| var fieldDef = md.GetFieldDefinition(fieldDefHandle); | ||
| string fieldName = md.GetString(fieldDef.Name); | ||
|
|
||
| if (fieldName == "t_current") | ||
| { | ||
| tCurrentOffset = rts.GetFieldDescOffset(fieldDesc, fieldDef); | ||
| foundField = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| Assert.True(foundField, $"Could not find t_current field. numStatic={numStaticFields} numThreadStatic={numThreadStaticFields} numInstance={numInstanceFields}"); | ||
|
|
||
| // 3. Walk all threads and read t_current at the discovered offset. | ||
| ThreadStoreData threadStore = threadContract.GetThreadStoreData(); | ||
| TargetPointer threadPtr = threadStore.FirstThread; | ||
|
|
||
| ulong continuationAddress = 0; | ||
| while (threadPtr != TargetPointer.Null) | ||
| { | ||
| ThreadData threadData = threadContract.GetThreadData(threadPtr); | ||
|
|
||
| TargetPointer nonGCBase = rts.GetNonGCThreadStaticsBasePointer( | ||
| asyncDispatcherInfoHandle, threadPtr); | ||
|
|
||
| if (nonGCBase != TargetPointer.Null) | ||
| { | ||
| TargetPointer tCurrent = Target.ReadPointer(nonGCBase + tCurrentOffset); | ||
|
|
||
| if (tCurrent != TargetPointer.Null) | ||
| { | ||
| // AsyncDispatcherInfo layout: | ||
| // offset 0: AsyncDispatcherInfo* Next | ||
| // offset PointerSize: Continuation? NextContinuation (object reference) | ||
| TargetPointer nextContinuation = Target.ReadPointer( | ||
| tCurrent.Value + (ulong)Target.PointerSize); | ||
|
|
||
| if (nextContinuation != TargetPointer.Null) | ||
| { | ||
| continuationAddress = nextContinuation.Value; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| threadPtr = threadData.NextThread; | ||
| } | ||
|
|
||
| Assert.NotEqual(0UL, continuationAddress); | ||
|
|
||
| // 4. Verify the object's MethodTable is a continuation subtype via the cDAC. | ||
| TargetPointer objMT = Target.Contracts.Object.GetMethodTableAddress( | ||
| new TargetPointer(continuationAddress)); | ||
| TypeHandle handle = rts.GetTypeHandle(objMT); | ||
| Assert.True(rts.IsContinuation(handle)); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/native/managed/cdac/tests/DumpTests/Debuggees/AsyncContinuation/AsyncContinuation.csproj
This file contains hidden or 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,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <!-- Enable Roslyn async2 codegen so the JIT creates continuation MethodTables --> | ||
| <Features>$(Features);runtime-async=on</Features> | ||
| <EnablePreviewFeatures>true</EnablePreviewFeatures> | ||
| <NoWarn>$(NoWarn);CA2007;CA2252</NoWarn> | ||
| <!-- Full dump needed so that module metadata is available for type lookup --> | ||
| <DumpTypes>Full</DumpTypes> | ||
| </PropertyGroup> | ||
| </Project> |
39 changes: 39 additions & 0 deletions
39
src/native/managed/cdac/tests/DumpTests/Debuggees/AsyncContinuation/Program.cs
This file contains hidden or 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,39 @@ | ||
| // 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.Threading.Tasks; | ||
|
|
||
| /// <summary> | ||
| /// Debuggee for cDAC dump test — exercises async continuations. | ||
| /// Uses the runtime-async=on compiler feature to generate async2 methods, | ||
| /// which cause the JIT/runtime to create continuation MethodTables | ||
| /// (setting g_pContinuationClassIfSubTypeCreated). | ||
| /// | ||
| /// FailFast is called from within the resumed inner async method | ||
| /// while DispatchContinuations is still on the call stack, ensuring | ||
| /// AsyncDispatcherInfo.t_current points to a live continuation chain. | ||
| /// </summary> | ||
| internal static class Program | ||
| { | ||
| internal static async Task<int> InnerAsync(int value) | ||
| { | ||
| await Task.Delay(1); | ||
|
|
||
| // Crash while still inside Resume — t_current is set on this thread | ||
| // and NextContinuation points to OuterAsync's continuation. | ||
| Environment.FailFast("cDAC dump test: AsyncContinuation debuggee intentional crash"); | ||
|
|
||
| return value + 1; | ||
| } | ||
|
|
||
| internal static async Task<int> OuterAsync(int value) | ||
| { | ||
| return await InnerAsync(value); | ||
| } | ||
|
|
||
| private static void Main() | ||
| { | ||
| OuterAsync(41).GetAwaiter().GetResult(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.