Repository navigation
Move debugger func-eval allocation and resolution into managed code #136975
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8ee2fd5
Move debugger func-eval allocation and resolution into managed code
jkoritzinsky d01db86
Refine managed func-eval dispatch and type decoding
jkoritzinsky 320e77d
Merge main into managed func-eval branch
jkoritzinsky 4e1dee5
Merge main into managed func-eval
jkoritzinsky cedaf65
Merge main into managed func-eval
jkoritzinsky 45d3c28
Address managed func-eval review feedback
jkoritzinsky 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
254 changes: 254 additions & 0 deletions
254
...System.Private.CoreLib/src/System/Diagnostics/Debugger.FunctionEvaluation.TypeDataWalk.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,254 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Buffers.Binary; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Reflection; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace System.Diagnostics; | ||
|
|
||
| public static partial class Debugger | ||
| { | ||
| internal sealed unsafe partial class FunctionEvaluation | ||
| { | ||
| // DebuggerIPCE_TypeArgData uses fixed-width, little-endian debugger cookies, not native pointers. | ||
| // corelib.h verifies this layout against the native record. | ||
| [ExtendedLayout(ExtendedLayoutKind.CStruct)] | ||
| internal struct NativeTypeArgument | ||
| { | ||
| internal NativeExpandedTypeData Data; | ||
| internal uint ChildCount; | ||
| } | ||
|
|
||
| [ExtendedLayout(ExtendedLayoutKind.CStruct)] | ||
| internal struct NativeExpandedTypeData | ||
| { | ||
| internal uint ElementType; | ||
| internal NativeTypeDataUnion TypeData; | ||
| } | ||
|
|
||
| [ExtendedLayout(ExtendedLayoutKind.CUnion)] | ||
| internal struct NativeTypeDataUnion | ||
| { | ||
| internal NativeClassTypeData ClassTypeData; | ||
| internal NativeUnaryTypeData UnaryTypeData; | ||
| internal NativeArrayTypeData ArrayTypeData; | ||
| internal NativeNaryTypeData NaryTypeData; | ||
| } | ||
|
|
||
| [ExtendedLayout(ExtendedLayoutKind.CStruct)] | ||
| internal struct NativeBasicTypeData | ||
| { | ||
| internal uint ElementType; | ||
| internal uint MetadataToken; | ||
| internal ulong Assembly; | ||
| internal ulong TypeHandle; | ||
| } | ||
|
|
||
| [ExtendedLayout(ExtendedLayoutKind.CStruct)] | ||
| internal struct NativeClassTypeData | ||
| { | ||
| internal uint MetadataToken; | ||
| internal ulong Assembly; | ||
| internal ulong TypeHandle; | ||
| } | ||
|
|
||
| [ExtendedLayout(ExtendedLayoutKind.CStruct)] | ||
| internal struct NativeUnaryTypeData | ||
| { | ||
| internal NativeBasicTypeData UnaryTypeArgument; | ||
| } | ||
|
|
||
| [ExtendedLayout(ExtendedLayoutKind.CStruct)] | ||
| internal struct NativeArrayTypeData | ||
| { | ||
| internal NativeBasicTypeData ArrayTypeArgument; | ||
| internal uint ArrayRank; | ||
| } | ||
|
|
||
| [ExtendedLayout(ExtendedLayoutKind.CStruct)] | ||
| internal struct NativeNaryTypeData | ||
| { | ||
| internal ulong TypeHandle; | ||
| } | ||
|
|
||
| [DebuggerHidden] | ||
| internal static RuntimeType[] ReadTypeArguments(ReadOnlySpan<byte> typeData, int count) | ||
| { | ||
| if (typeData.Length % sizeof(NativeTypeArgument) != 0) | ||
| { | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| TypeDataWalk walk = new(MemoryMarshal.Cast<byte, NativeTypeArgument>(typeData)); | ||
| RuntimeType[] types = walk.ReadTypeHandles(count); | ||
| if (!walk.IsFinished) | ||
| { | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| return types; | ||
| } | ||
|
|
||
| private static ReadOnlySpan<byte> GetTypeData(byte* data, uint nodeCount) => | ||
| new(data, checked((int)nodeCount * sizeof(NativeTypeArgument))); | ||
|
|
||
| private static RuntimeModule GetModule(nint nativeAssembly) | ||
| { | ||
| if (nativeAssembly == 0) | ||
| { | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| return (RuntimeModule)RuntimeAssembly.GetAssemblyFromHandle(nativeAssembly).ManifestModule; | ||
| } | ||
|
|
||
| [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", | ||
| Justification = "The debugger supplies tokens from the target's actual loaded metadata.")] | ||
| [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", | ||
| Justification = "The debugger supplies the target's actual runtime generic arguments.")] | ||
| private static RuntimeType ReadInstantiation( | ||
| RuntimeModule module, int token, RuntimeType[] arguments, bool allowUninstantiated = false) | ||
| { | ||
| RuntimeType type = (RuntimeType)module.ResolveType(token); | ||
| if (arguments.Length != 0 || (!allowUninstantiated && type.IsGenericTypeDefinition)) | ||
| { | ||
| type = (RuntimeType)type.MakeGenericType(arguments); | ||
| } | ||
|
|
||
| return type; | ||
| } | ||
|
|
||
| [StackTraceHidden] | ||
| private ref struct TypeDataWalk | ||
| { | ||
| private ReadOnlySpan<NativeTypeArgument> _remaining; | ||
|
|
||
| internal TypeDataWalk(ReadOnlySpan<NativeTypeArgument> data) | ||
| { | ||
| _remaining = data; | ||
| } | ||
|
|
||
| internal readonly bool IsFinished => _remaining.IsEmpty; | ||
|
|
||
| internal RuntimeType[] ReadTypeHandles(int count) | ||
| { | ||
| if ((uint)count > (uint)_remaining.Length) | ||
| { | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| RuntimeType[] types = count == 0 ? [] : new RuntimeType[count]; | ||
| for (int i = 0; i < types.Length; i++) | ||
| { | ||
| types[i] = ReadTypeHandle(); | ||
| } | ||
|
|
||
| return types; | ||
| } | ||
|
|
||
| private RuntimeType ReadTypeHandle() | ||
| { | ||
| if (_remaining.IsEmpty) | ||
| { | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| NativeTypeArgument node = _remaining[0]; | ||
| _remaining = _remaining[1..]; | ||
| if (!BitConverter.IsLittleEndian) | ||
| { | ||
| node.Data.ElementType = BinaryPrimitives.ReverseEndianness(node.Data.ElementType); | ||
| node.Data.TypeData.ClassTypeData.MetadataToken = | ||
| BinaryPrimitives.ReverseEndianness(node.Data.TypeData.ClassTypeData.MetadataToken); | ||
| node.Data.TypeData.ClassTypeData.Assembly = | ||
| BinaryPrimitives.ReverseEndianness(node.Data.TypeData.ClassTypeData.Assembly); | ||
| node.Data.TypeData.ArrayTypeData.ArrayRank = | ||
| BinaryPrimitives.ReverseEndianness(node.Data.TypeData.ArrayTypeData.ArrayRank); | ||
| node.ChildCount = BinaryPrimitives.ReverseEndianness(node.ChildCount); | ||
| } | ||
|
|
||
| uint elementValue = node.Data.ElementType; | ||
| if (elementValue > byte.MaxValue) | ||
| { | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| CorElementType elementType = (CorElementType)elementValue; | ||
| uint childCount = node.ChildCount; | ||
| if (childCount > (uint)_remaining.Length) | ||
| { | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| switch (elementType) | ||
| { | ||
| case CorElementType.ELEMENT_TYPE_ARRAY: | ||
| case CorElementType.ELEMENT_TYPE_SZARRAY: | ||
| case CorElementType.ELEMENT_TYPE_PTR: | ||
| case CorElementType.ELEMENT_TYPE_BYREF: | ||
| if (childCount != 1) | ||
| { | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| RuntimeType element = ReadTypeHandle(); | ||
| return elementType switch | ||
| { | ||
| CorElementType.ELEMENT_TYPE_ARRAY => (RuntimeType)element.MakeArrayType( | ||
| unchecked((int)node.Data.TypeData.ArrayTypeData.ArrayRank)), | ||
| CorElementType.ELEMENT_TYPE_SZARRAY => (RuntimeType)element.MakeArrayType(), | ||
| CorElementType.ELEMENT_TYPE_PTR => (RuntimeType)element.MakePointerType(), | ||
| _ => (RuntimeType)element.MakeByRefType(), | ||
| }; | ||
|
|
||
| case CorElementType.ELEMENT_TYPE_CLASS: | ||
| case CorElementType.ELEMENT_TYPE_VALUETYPE: | ||
| nint assembly = (nint)checked((nuint)node.Data.TypeData.ClassTypeData.Assembly); | ||
| RuntimeModule module = GetModule(assembly); | ||
| int token = unchecked((int)node.Data.TypeData.ClassTypeData.MetadataToken); | ||
| return ReadInstantiation(module, token, ReadTypeHandles((int)childCount)); | ||
|
|
||
| case CorElementType.ELEMENT_TYPE_FNPTR: | ||
| if (childCount == 0) | ||
| { | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| RuntimeType[] signature = ReadTypeHandles((int)childCount); | ||
| return signature[0].TypeHandle.MakeFunctionPointer(signature.AsSpan(1).ToArray(), isUnmanaged: false); | ||
|
|
||
| default: | ||
| if (childCount != 0) | ||
| { | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| return (RuntimeType)(elementType switch | ||
| { | ||
| CorElementType.ELEMENT_TYPE_VOID => typeof(void), | ||
| CorElementType.ELEMENT_TYPE_BOOLEAN => typeof(bool), | ||
| CorElementType.ELEMENT_TYPE_CHAR => typeof(char), | ||
| CorElementType.ELEMENT_TYPE_I1 => typeof(sbyte), | ||
| CorElementType.ELEMENT_TYPE_U1 => typeof(byte), | ||
| CorElementType.ELEMENT_TYPE_I2 => typeof(short), | ||
| CorElementType.ELEMENT_TYPE_U2 => typeof(ushort), | ||
| CorElementType.ELEMENT_TYPE_I4 => typeof(int), | ||
| CorElementType.ELEMENT_TYPE_U4 => typeof(uint), | ||
| CorElementType.ELEMENT_TYPE_I8 => typeof(long), | ||
| CorElementType.ELEMENT_TYPE_U8 => typeof(ulong), | ||
| CorElementType.ELEMENT_TYPE_R4 => typeof(float), | ||
| CorElementType.ELEMENT_TYPE_R8 => typeof(double), | ||
| CorElementType.ELEMENT_TYPE_STRING => typeof(string), | ||
| CorElementType.ELEMENT_TYPE_TYPEDBYREF => typeof(TypedReference), | ||
| CorElementType.ELEMENT_TYPE_I => typeof(nint), | ||
| CorElementType.ELEMENT_TYPE_U => typeof(nuint), | ||
| CorElementType.ELEMENT_TYPE_OBJECT => typeof(object), | ||
| _ => throw new ArgumentException(), | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.