-
Notifications
You must be signed in to change notification settings - Fork 5.4k
[cDAC] Add debug type validation for global reads #126849
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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> | ||
| 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<T></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
|
||
| } | ||
|
|
||
| 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 |
|---|---|---|
|
|
@@ -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
|
||
| return true; | ||
| } | ||
|
|
||
|
|
@@ -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; | ||
| } | ||
|
|
||
|
|
@@ -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 _); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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. AlsoIsCompatiblePrimitiveType<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).