Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Numerics;

namespace Microsoft.Diagnostics.DataContractReader;

/// <summary>
/// Debug-only helpers that validate cDAC type annotations match the C# read type.
/// In release builds, all methods are completely elided by the <see cref="ConditionalAttribute"/>.
/// </summary>
Comment on lines +9 to +12
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The XML doc claims all methods are “completely elided” in release builds via ConditionalAttribute, but the methods still exist in the assembly and only call sites are conditionally omitted. Also IsCompatiblePrimitiveType<T> is not conditional, so it is never “elided” at all. Please reword the documentation to reflect the actual behavior (or adjust the implementation to match the docs).

Copilot uses AI. Check for mistakes.
public static class DataDescriptorTypeValidation
{
/// <summary>
/// Assert that a declared field type name is compatible with the C# primitive integer type <typeparamref name="T"/>.
/// </summary>
[Conditional("DEBUG")]
public static void AssertPrimitiveType<T>(string? typeName, string context)
where T : unmanaged, IBinaryInteger<T>, IMinMaxValue<T>
{
Debug.Assert(
typeName is null or "" || IsCompatiblePrimitiveType<T>(typeName),
$"Type mismatch reading {context}: declared as '{typeName}', reading as {typeof(T).Name}");
}

/// <summary>
/// Assert that a declared field type name is "pointer" (or absent).
/// </summary>
[Conditional("DEBUG")]
public static void AssertPointerType(string? typeName, string context)
{
Debug.Assert(
typeName is null or "" or "pointer",
$"Type mismatch reading {context}: declared as '{typeName}', expected pointer");
}

/// <summary>
/// Assert that a global's declared type is compatible with <c>ReadGlobal&lt;T&gt;</c>.
/// Pointer-like types (nuint, nint, pointer) must use <c>ReadGlobalPointer</c> instead.
/// String-typed globals are allowed since they may carry dual numeric/string values.
/// </summary>
[Conditional("DEBUG")]
public static void AssertGlobalType<T>(string? typeName, string globalName)
where T : struct, INumber<T>
{
if (typeName is null or "" or "string")
return;

bool compatible = typeName switch
{
"uint8" => typeof(T) == typeof(byte),
"int8" => typeof(T) == typeof(sbyte),
"uint16" => typeof(T) == typeof(ushort),
"int16" => typeof(T) == typeof(short),
"uint32" => typeof(T) == typeof(uint),
"int32" => typeof(T) == typeof(int),
"uint64" => typeof(T) == typeof(ulong),
"int64" => typeof(T) == typeof(long),
"bool" => typeof(T) == typeof(byte),
_ => false,
};

Debug.Assert(compatible,
$"Type mismatch reading global '{globalName}': declared as '{typeName}', reading as {typeof(T).Name}. " +
$"Pointer-like globals (pointer, nuint, nint) should be read via ReadGlobalPointer.");
}

/// <summary>
/// Assert that a global's declared type is compatible with <c>ReadGlobalPointer</c>.
/// Accepts pointer, nuint, nint, or absent type information.
/// </summary>
[Conditional("DEBUG")]
public static void AssertGlobalPointerType(string? typeName, string globalName)
{
Debug.Assert(
typeName is null or "" or "pointer" or "nuint" or "nint" or "string",
$"Type mismatch reading global '{globalName}' as pointer: declared as '{typeName}', expected pointer/nuint/nint");
Comment on lines +69 to +78
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AssertGlobalPointerType’s XML doc says it accepts only pointer/nuint/nint (or absent type info), but the implementation also allows "string". Either update the documentation to include string (and why it’s allowed) or tighten the assertion to match the doc.

Copilot uses AI. Check for mistakes.
}

public static bool IsCompatiblePrimitiveType<T>(string typeName)
where T : unmanaged, IBinaryInteger<T>, IMinMaxValue<T>
{
return typeName switch
{
"uint8" => typeof(T) == typeof(byte),
"int8" => typeof(T) == typeof(sbyte),
"uint16" => typeof(T) == typeof(ushort),
"int16" => typeof(T) == typeof(short),
"uint32" => typeof(T) == typeof(uint),
"int32" => typeof(T) == typeof(int),
"uint64" => typeof(T) == typeof(ulong),
"int64" => typeof(T) == typeof(long),
"bool" => typeof(T) == typeof(byte),
_ => false,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public static T ReadField<T>(this Target target, ulong address, Target.TypeInfo
where T : unmanaged, IBinaryInteger<T>, IMinMaxValue<T>
{
Target.FieldInfo field = typeInfo.Fields[fieldName];
AssertPrimitiveType<T>(field, fieldName);

DataDescriptorTypeValidation.AssertPrimitiveType<T>(field.TypeName, $"field '{fieldName}'");
return target.Read<T>(address + (ulong)field.Offset);
}

Expand All @@ -36,8 +35,7 @@ public static T ReadFieldOrDefault<T>(this Target target, ulong address, Target.
if (!typeInfo.Fields.TryGetValue(fieldName, out Target.FieldInfo field))
return defaultValue;

AssertPrimitiveType<T>(field, fieldName);

DataDescriptorTypeValidation.AssertPrimitiveType<T>(field.TypeName, $"field '{fieldName}'");
return target.Read<T>(address + (ulong)field.Offset);
}

Expand All @@ -47,8 +45,7 @@ public static T ReadFieldOrDefault<T>(this Target target, ulong address, Target.
public static TargetPointer ReadPointerField(this Target target, ulong address, Target.TypeInfo typeInfo, string fieldName)
{
Target.FieldInfo field = typeInfo.Fields[fieldName];
AssertPointerType(field, fieldName);

DataDescriptorTypeValidation.AssertPointerType(field.TypeName, $"field '{fieldName}'");
return target.ReadPointer(address + (ulong)field.Offset);
}

Expand All @@ -61,8 +58,7 @@ public static TargetPointer ReadPointerFieldOrNull(this Target target, ulong add
if (!typeInfo.Fields.TryGetValue(fieldName, out Target.FieldInfo field))
return TargetPointer.Null;

AssertPointerType(field, fieldName);

DataDescriptorTypeValidation.AssertPointerType(field.TypeName, $"field '{fieldName}'");
return target.ReadPointer(address + (ulong)field.Offset);
}

Expand All @@ -75,7 +71,6 @@ public static TargetNUInt ReadNUIntField(this Target target, ulong address, Targ
Debug.Assert(
field.TypeName is null or "" or "nuint",
$"Type mismatch reading field '{fieldName}': declared as '{field.TypeName}', expected nuint");

return target.ReadNUInt(address + (ulong)field.Offset);
}

Expand All @@ -88,13 +83,11 @@ public static TargetCodePointer ReadCodePointerField(this Target target, ulong a
Debug.Assert(
field.TypeName is null or "" or "CodePointer",
$"Type mismatch reading field '{fieldName}': declared as '{field.TypeName}', expected CodePointer");

return target.ReadCodePointer(address + (ulong)field.Offset);
}

/// <summary>
/// Read a field that contains an inline Data struct type, with type validation.
/// Returns the data object created by <see cref="Target.IDataCache.GetOrAdd{T}"/>.
/// </summary>
public static T ReadDataField<T>(this Target target, ulong address, Target.TypeInfo typeInfo, string fieldName)
where T : IData<T>
Expand All @@ -103,25 +96,21 @@ public static T ReadDataField<T>(this Target target, ulong address, Target.TypeI
Debug.Assert(
field.TypeName is null or "" || field.TypeName == typeof(T).Name,
$"Type mismatch reading field '{fieldName}': declared as '{field.TypeName}', reading as {typeof(T).Name}");

return target.ProcessedData.GetOrAdd<T>(address + (ulong)field.Offset);
}

/// <summary>
/// Read a field that contains a pointer to a Data struct type, with type validation.
/// Reads the pointer, then creates the data object via <see cref="Target.IDataCache.GetOrAdd{T}"/>.
/// Returns null if the pointer is null.
/// </summary>
public static T? ReadDataFieldPointer<T>(this Target target, ulong address, Target.TypeInfo typeInfo, string fieldName)
where T : IData<T>
{
Target.FieldInfo field = typeInfo.Fields[fieldName];
AssertPointerType(field, fieldName);

DataDescriptorTypeValidation.AssertPointerType(field.TypeName, $"field '{fieldName}'");
TargetPointer pointer = target.ReadPointer(address + (ulong)field.Offset);
if (pointer == TargetPointer.Null)
return default;

return target.ProcessedData.GetOrAdd<T>(pointer);
}

Expand All @@ -135,47 +124,10 @@ public static T ReadDataField<T>(this Target target, ulong address, Target.TypeI
if (!typeInfo.Fields.TryGetValue(fieldName, out Target.FieldInfo field))
return default;

AssertPointerType(field, fieldName);

DataDescriptorTypeValidation.AssertPointerType(field.TypeName, $"field '{fieldName}'");
TargetPointer pointer = target.ReadPointer(address + (ulong)field.Offset);
if (pointer == TargetPointer.Null)
return default;

return target.ProcessedData.GetOrAdd<T>(pointer);
}

[Conditional("DEBUG")]
private static void AssertPrimitiveType<T>(Target.FieldInfo field, string fieldName)
where T : unmanaged, IBinaryInteger<T>, IMinMaxValue<T>
{
Debug.Assert(
field.TypeName is null or "" || IsCompatiblePrimitiveType<T>(field.TypeName),
$"Type mismatch reading field '{fieldName}': declared as '{field.TypeName}', reading as {typeof(T).Name}");
}

[Conditional("DEBUG")]
private static void AssertPointerType(Target.FieldInfo field, string fieldName)
{
Debug.Assert(
field.TypeName is null or "" or "pointer",
$"Type mismatch reading field '{fieldName}': declared as '{field.TypeName}', expected pointer");
}

private static bool IsCompatiblePrimitiveType<T>(string typeName)
where T : unmanaged, IBinaryInteger<T>, IMinMaxValue<T>
{
return typeName switch
{
"uint8" => typeof(T) == typeof(byte),
"int8" => typeof(T) == typeof(sbyte),
"uint16" => typeof(T) == typeof(ushort),
"int16" => typeof(T) == typeof(short),
"uint32" => typeof(T) == typeof(uint),
"int32" => typeof(T) == typeof(int),
"uint64" => typeof(T) == typeof(ulong),
"int64" => typeof(T) == typeof(long),
"bool" => typeof(T) == typeof(byte),
_ => false,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -706,15 +706,13 @@ public override bool TryReadGlobal<T>(string name, [NotNullWhen(true)] out T? va

public bool TryReadGlobal<T>(string name, [NotNullWhen(true)] out T? value, out string? type) where T : struct, INumber<T>
{
value = null;
type = null;
if (!_globals.TryGetValue(name, out GlobalValue global) || global.NumericValue is null)
if (!TryReadGlobalRaw(name, out ulong? rawValue, out type))
{
// Not found or does not contain a numeric value
value = null;
return false;
}
type = global.Type;
value = T.CreateChecked(global.NumericValue.Value);
DataDescriptorTypeValidation.AssertGlobalType<T>(type, name);
value = T.CreateChecked(rawValue.Value);
Comment on lines +714 to +715
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AssertGlobalType<T> will assert for globals declared as nuint/nint when callers currently use ReadGlobal<ulong>/ReadGlobal<long>. For example, CoreCLR’s datadescriptor declares CCWThisMask as T_NUINT, but BuiltInCOM_1 reads it via ReadGlobal<ulong>, which will now trip this debug assert. Either update affected call sites to use ReadGlobalPointer (and use .Value as needed) or relax the validation to treat nuint/nint as compatible with corresponding integral reads.

Copilot uses AI. Check for mistakes.
return true;
}

Expand All @@ -735,10 +733,11 @@ public override bool TryReadGlobalPointer(string name, [NotNullWhen(true)] out T
public bool TryReadGlobalPointer(string name, [NotNullWhen(true)] out TargetPointer? value, out string? type)
{
value = null;
if (!TryReadGlobal(name, out ulong? innerValue, out type))
if (!TryReadGlobalRaw(name, out ulong? rawValue, out type))
return false;

value = new TargetPointer(innerValue.Value);
DataDescriptorTypeValidation.AssertGlobalPointerType(type, name);
value = new TargetPointer(rawValue.Value);
return true;
}

Expand All @@ -753,6 +752,18 @@ public TargetPointer ReadGlobalPointer(string name, out string? type)
return value.Value;
}

private bool TryReadGlobalRaw(string name, [NotNullWhen(true)] out ulong? value, out string? type)
{
value = null;
type = null;
if (!_globals.TryGetValue(name, out GlobalValue global) || global.NumericValue is null)
return false;

type = global.Type;
value = global.NumericValue;
return true;
}

public override string ReadGlobalString(string name)
=> ReadStringGlobal(name, out _);

Expand Down
Loading