-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[cdac] Implement a JSON contract reader #100966
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 3 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
eddd004
WIP: compact descriptor format json parser
lambdageek 10c58f5
checkpoint: camelCase dict keys; compact fields
lambdageek 6e583da
more parsing
lambdageek a454c1e
Follow C# coding guidelines
lambdageek ce03508
additional comments
lambdageek 28d7afd
better doc comments
lambdageek 15b4ecb
suggestions from reviews; remove FieldDescriptor wrong conversion
lambdageek 22867c1
Make test project like the nativeoat+linker tests
lambdageek 28f5633
add tools.cdacreadertests subset; add to CLR_Tools_Tests test leg
lambdageek aa58b4b
one more code review chang
lambdageek 894c7ff
add tools.cdacreadertests for subset validation
lambdageek 6f65672
Apply suggestions from code review
lambdageek b0c34da
reorder cases
lambdageek 01e3fdd
no duplicate fields/sizes in types
lambdageek dcdc95a
Merge remote-tracking branch 'origin/main' into cdac-parse-json
lambdageek f0f8ef1
wip
lambdageek afdb85e
Make all cdacreader.csproj ProjectReferences use the same AdditionalP…
lambdageek 5d91235
Don't share the native compilation AdditionalProperties
lambdageek 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
318 changes: 318 additions & 0 deletions
318
src/native/managed/cdacreader/src/ContractDescriptorParser.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,318 @@ | ||
| // 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.Collections.Generic; | ||
| using System.Diagnostics.Contracts; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Microsoft.Diagnostics.DataContractReader; | ||
| public partial class ContractDescriptorParser | ||
| { | ||
| public const string TypeDescriptorSizeSigil = "!"; | ||
|
|
||
| /// <summary> | ||
| /// Parses the "compact" representation of a contract descriptor. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See data_descriptor.md for the format. | ||
| /// </remarks> | ||
| public static ContractDescriptor? ParseCompact(ReadOnlySpan<byte> json) | ||
| { | ||
| return JsonSerializer.Deserialize(json, ContractDescriptorContext.Default.ContractDescriptor); | ||
| } | ||
|
|
||
| [JsonSerializable(typeof(ContractDescriptor))] | ||
| [JsonSerializable(typeof(int))] | ||
| [JsonSerializable(typeof(string))] | ||
| [JsonSerializable(typeof(Dictionary<string, int>))] | ||
| [JsonSerializable(typeof(Dictionary<string, TypeDescriptor>))] | ||
| [JsonSerializable(typeof(Dictionary<string, FieldDescriptor>))] | ||
| [JsonSerializable(typeof(Dictionary<string, GlobalDescriptor>))] | ||
| [JsonSerializable(typeof(TypeDescriptor))] | ||
| [JsonSerializable(typeof(FieldDescriptor))] | ||
| [JsonSerializable(typeof(GlobalDescriptor))] | ||
| [JsonSourceGenerationOptions(AllowTrailingCommas = true, | ||
| DictionaryKeyPolicy = JsonKnownNamingPolicy.Unspecified, // contracts, types and globals are case sensitive | ||
| PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, | ||
| NumberHandling = JsonNumberHandling.AllowReadingFromString, | ||
| ReadCommentHandling = JsonCommentHandling.Skip)] | ||
| internal sealed partial class ContractDescriptorContext : JsonSerializerContext | ||
| { | ||
| } | ||
|
|
||
| public class ContractDescriptor | ||
| { | ||
| public int? Version { get; set; } | ||
| public string? Baseline { get; set; } | ||
| public Dictionary<string, int>? Contracts { get; set; } | ||
|
|
||
| public Dictionary<string, TypeDescriptor>? Types { get; set; } | ||
|
|
||
| public Dictionary<string, GlobalDescriptor>? Globals { get; set; } | ||
|
|
||
| [JsonExtensionData] | ||
| public Dictionary<string, object?>? Extras { get; set; } | ||
| } | ||
|
|
||
| [JsonConverter(typeof(TypeDescriptorConverter))] | ||
| public class TypeDescriptor | ||
| { | ||
| public uint? Size { get; set; } | ||
| public Dictionary<string, FieldDescriptor>? Fields { get; set; } | ||
| } | ||
|
|
||
| [JsonConverter(typeof(FieldDescriptorConverter))] | ||
| public class FieldDescriptor | ||
| { | ||
| public string? Type { get; set; } | ||
| public int Offset { get; set; } | ||
| } | ||
|
|
||
| [JsonConverter(typeof(GlobalDescriptorConverter))] | ||
| public class GlobalDescriptor | ||
| { | ||
| public string? Type { get; set; } | ||
| public ulong Value { get; set; } | ||
| public bool Indirect { get; set; } | ||
| } | ||
|
|
||
| internal sealed class TypeDescriptorConverter : JsonConverter<TypeDescriptor> | ||
| { | ||
| // Almost a normal dictionary converter except: | ||
| // 1. looks for a special key "!" to set the Size property | ||
| // 2. field names are property names, but treated case-sensitively | ||
| public override TypeDescriptor Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| if (reader.TokenType != JsonTokenType.StartObject) | ||
| throw new JsonException(); | ||
| uint? size = null; | ||
| Dictionary<string, FieldDescriptor>? fields = new(); | ||
| while (reader.Read()) | ||
| { | ||
| switch (reader.TokenType) | ||
| { | ||
| case JsonTokenType.EndObject: | ||
| return new TypeDescriptor { Size = size, Fields = fields }; | ||
| case JsonTokenType.PropertyName: | ||
| if (reader.GetString() == TypeDescriptorSizeSigil) | ||
| { | ||
| reader.Read(); | ||
| size = reader.GetUInt32(); | ||
| // FIXME: handle duplicates? | ||
lambdageek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| else | ||
| { | ||
| string? fieldName = reader.GetString(); | ||
| reader.Read(); | ||
lambdageek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var field = JsonSerializer.Deserialize(ref reader, ContractDescriptorContext.Default.FieldDescriptor); | ||
| // FIXME: duplicates? | ||
| if (fieldName != null && field != null) | ||
| fields.Add(fieldName, field); | ||
| else | ||
| throw new JsonException(); | ||
lambdageek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| break; | ||
| case JsonTokenType.Comment: | ||
| // unexpected - we specified to skip comments. but let's ignore anyway | ||
| break; | ||
| default: | ||
| throw new JsonException(); | ||
| } | ||
| } | ||
| throw new JsonException(); | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, TypeDescriptor value, JsonSerializerOptions options) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
|
|
||
| internal sealed class FieldDescriptorConverter : JsonConverter<FieldDescriptor> | ||
| { | ||
| // Compact Field descriptors are either one or two element arrays | ||
| // 1. [number] - no type, offset is given as the number | ||
lambdageek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // 2. [number, string] - has a type, offset is given as the number | ||
| public override FieldDescriptor Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| if (GetInt32FromToken(ref reader, out int offset)) | ||
| return new FieldDescriptor { Offset = offset }; | ||
| if (reader.TokenType != JsonTokenType.StartArray) | ||
| throw new JsonException(); | ||
| reader.Read(); | ||
| // two cases: | ||
| // [number] | ||
| // ^ we're here | ||
| // or | ||
| // [number, string] | ||
| // ^ we're here | ||
| if (!GetInt32FromToken(ref reader, out offset)) | ||
| throw new JsonException(); | ||
| reader.Read(); // end of array or string | ||
lambdageek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (reader.TokenType == JsonTokenType.EndArray) | ||
| return new FieldDescriptor { Offset = offset }; | ||
| if (reader.TokenType != JsonTokenType.String) | ||
| throw new JsonException(); | ||
| string? type = reader.GetString(); | ||
| reader.Read(); // end of array | ||
| if (reader.TokenType != JsonTokenType.EndArray) | ||
| throw new JsonException(); | ||
| return new FieldDescriptor { Type = type, Offset = offset }; | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, FieldDescriptor value, JsonSerializerOptions options) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
|
|
||
| internal sealed class GlobalDescriptorConverter : JsonConverter<GlobalDescriptor> | ||
| { | ||
| public override GlobalDescriptor Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| // four cases: | ||
| // 1. number - no type, direct value, given value | ||
| // 2. [number] - no type, indirect value, given aux data ptr | ||
| // 3. [number, string] - type, direct value, given value | ||
| // 4. [[number], string] - type, indirect value, given aux data ptr | ||
|
|
||
| // Case 1: number | ||
| if (GetUInt64FromToken(ref reader, out ulong valueCase1)) | ||
| return new GlobalDescriptor { Value = valueCase1 }; | ||
| if (reader.TokenType != JsonTokenType.StartArray) | ||
| throw new JsonException(); | ||
| reader.Read(); | ||
| // we're in case 2 or 3 or 4 | ||
| // case 2: [number] | ||
| // ^ we're here | ||
| // case 3: [number, string] | ||
| // ^ we're here | ||
| // case 4: [[number], string] | ||
| // ^ we're here | ||
| if (reader.TokenType == JsonTokenType.StartArray) | ||
lambdageek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| // case 4: [[number], string] | ||
| // ^ we're here | ||
| reader.Read(); // number | ||
| if (!GetUInt64FromToken(ref reader, out ulong value)) | ||
| throw new JsonException(); | ||
| reader.Read(); // end of inner array | ||
| if (reader.TokenType != JsonTokenType.EndArray) | ||
| throw new JsonException(); | ||
| reader.Read(); // string | ||
| if (reader.TokenType != JsonTokenType.String) | ||
| throw new JsonException(); | ||
| string? type = reader.GetString(); | ||
| reader.Read(); // end of outer array | ||
| if (reader.TokenType != JsonTokenType.EndArray) | ||
| throw new JsonException(); | ||
| return new GlobalDescriptor { Type = type, Value = value, Indirect = true }; | ||
| } | ||
| else | ||
| { | ||
| // case 2 or 3 | ||
| // case 2: [number] | ||
| // ^ we're here | ||
| // case 3: [number, string] | ||
| // ^ we're here | ||
| if (!GetUInt64FromToken(ref reader, out ulong valueCase2or3)) | ||
| throw new JsonException(); | ||
| reader.Read(); // end of array (case 2) or string (case 3) | ||
| if (reader.TokenType == JsonTokenType.EndArray) // it was case 2 | ||
| return new GlobalDescriptor { Value = valueCase2or3, Indirect = true }; | ||
| else if (reader.TokenType == JsonTokenType.String) // it was case 3 | ||
| { | ||
| string? type = reader.GetString(); | ||
| reader.Read(); // end of array for case 3 | ||
| if (reader.TokenType != JsonTokenType.EndArray) | ||
| throw new JsonException(); | ||
| return new GlobalDescriptor { Type = type, Value = valueCase2or3 }; | ||
| } | ||
| else | ||
| throw new JsonException(); | ||
lambdageek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, GlobalDescriptor value, JsonSerializerOptions options) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
|
|
||
| // Somewhat flexible parsing of numbers, allowing json number tokens or strings as decimal or hex, possibly negatated. | ||
| private static bool GetUInt64FromToken(ref Utf8JsonReader reader, out ulong value) | ||
lambdageek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (reader.TokenType == JsonTokenType.Number) | ||
| { | ||
| if (reader.TryGetUInt64(out value)) | ||
| return true; | ||
| else if (reader.TryGetInt64(out long signedValue)) | ||
lambdageek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| value = (ulong)signedValue; | ||
| return true; | ||
| } | ||
| } | ||
| if (reader.TokenType == JsonTokenType.String) | ||
| { | ||
| var s = reader.GetString(); | ||
| if (s == null) | ||
| { | ||
| value = 0u; | ||
| return false; | ||
| } | ||
| if (ulong.TryParse(s, out value)) | ||
| return true; | ||
| if (long.TryParse(s, out long signedValue)) | ||
| { | ||
| value = (ulong)signedValue; | ||
| return true; | ||
| } | ||
| if ((s.StartsWith("0x") || s.StartsWith("0X")) && | ||
lambdageek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ulong.TryParse(s.AsSpan(2), System.Globalization.NumberStyles.HexNumber, null, out value)) | ||
| return true; | ||
lambdageek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ((s.StartsWith("-0x") || s.StartsWith("-0X")) && | ||
| ulong.TryParse(s.AsSpan(3), System.Globalization.NumberStyles.HexNumber, null, out ulong negValue)) | ||
| { | ||
| value = ~negValue + 1; // twos complement | ||
lambdageek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return true; | ||
| } | ||
| } | ||
| value = 0; | ||
| return false; | ||
| } | ||
|
|
||
| // Somewhat flexible parsing of numbers, allowing json number tokens or strings as either decimal or hex, possibly negated | ||
| private static bool GetInt32FromToken(ref Utf8JsonReader reader, out int value) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.Number) | ||
| { | ||
| value = reader.GetInt32(); | ||
| return true; | ||
| } | ||
| if (reader.TokenType == JsonTokenType.String) | ||
| { | ||
| var s = reader.GetString(); | ||
| if (s == null) | ||
| { | ||
| value = 0; | ||
| return false; | ||
| } | ||
| if (int.TryParse(s, out value)) | ||
| return true; | ||
| if ((s.StartsWith("0x") || s.StartsWith("0X")) && | ||
| int.TryParse(s.AsSpan(2), System.Globalization.NumberStyles.HexNumber, null, out value)) | ||
lambdageek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return true; | ||
| if ((s.StartsWith("-0x") || s.StartsWith("-0X")) && | ||
| int.TryParse(s.AsSpan(3), System.Globalization.NumberStyles.HexNumber, null, out int negValue)) | ||
| { | ||
| value = -negValue; | ||
| return true; | ||
| } | ||
| } | ||
| value = 0; | ||
| return false; | ||
| } | ||
|
|
||
lambdageek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
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.