-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[cdac] Read/store globals from contract descriptor #101450
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 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fc3dcf2
Check cdac_reader_init result
elinor-fung fda47d3
Store globals from contract descriptor
elinor-fung 0700338
Read SOSBreakingChangeVersion from globals
elinor-fung a01b167
Read global pointer
elinor-fung 8527355
Add tests for reading global value
elinor-fung 7f77200
Add tests for reading indirect global value
elinor-fung c7bc9aa
Move calling init into CDAC::Create
elinor-fung 03ab101
Use generic with IBinaryInteger constraint
elinor-fung 7c773e9
Apply suggestions from code review
elinor-fung 125608f
Replace [Try]Read* with generic [Try]Read for numbers
elinor-fung c142884
Apply suggestions from code review
elinor-fung 28dc125
Add caller info to asserts in test
elinor-fung 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.Diagnostics.DataContractReader; | ||
|
|
||
| internal static class Constants | ||
| { | ||
| internal static class Globals | ||
| { | ||
| // See src/coreclr/debug/runtimeinfo/datadescriptor.h | ||
| internal const string SOSBreakingChangeVersion = nameof(SOSBreakingChangeVersion); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |
| using System; | ||
| using System.Buffers.Binary; | ||
| using System.Collections.Generic; | ||
| using System.Numerics; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace Microsoft.Diagnostics.DataContractReader; | ||
|
|
||
|
|
@@ -15,7 +17,7 @@ public struct TargetPointer | |
| public TargetPointer(ulong value) => Value = value; | ||
| } | ||
|
|
||
| internal sealed unsafe class Target | ||
| public sealed unsafe class Target | ||
| { | ||
| private const int StackAllocByteThreshold = 1024; | ||
|
|
||
|
|
@@ -29,7 +31,7 @@ private readonly struct Configuration | |
| private readonly Reader _reader; | ||
|
|
||
| private readonly IReadOnlyDictionary<string, int> _contracts = new Dictionary<string, int>(); | ||
| private readonly TargetPointer[] _pointerData = []; | ||
| private readonly IReadOnlyDictionary<string, (ulong Value, string? Type)> _globals = new Dictionary<string, (ulong, string?)>(); | ||
|
|
||
| public static bool TryCreate(ulong contractDescriptor, delegate* unmanaged<ulong, byte*, uint, void*, int> readFromTarget, void* readContext, out Target? target) | ||
| { | ||
|
|
@@ -49,11 +51,30 @@ private Target(Configuration config, ContractDescriptorParser.ContractDescriptor | |
| _config = config; | ||
| _reader = reader; | ||
|
|
||
| // TODO: [cdac] Read globals and types | ||
| // TODO: [cdac] Read types | ||
| // note: we will probably want to store the globals and types into a more usable form | ||
| _contracts = descriptor.Contracts ?? []; | ||
|
|
||
| _pointerData = pointerData; | ||
| // Read globals and map indirect values to pointer data | ||
| if (descriptor.Globals is not null) | ||
| { | ||
| Dictionary<string, (ulong Value, string? Type)> globals = []; | ||
| foreach ((string name, ContractDescriptorParser.GlobalDescriptor global) in descriptor.Globals) | ||
| { | ||
| ulong value = global.Value; | ||
| if (global.Indirect) | ||
| { | ||
| if (value >= (ulong)pointerData.Length) | ||
| throw new InvalidOperationException($"Invalid pointer data index {value}."); | ||
|
|
||
| value = pointerData[value].Value; | ||
| } | ||
|
|
||
| globals[name] = (value, global.Type); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just using the (optional) type string for now. I expect we'll do some mapping that isn't just string-based once we also read in the types. |
||
| } | ||
|
|
||
| _globals = globals; | ||
| } | ||
| } | ||
|
|
||
| // See docs/design/datacontracts/contract-descriptor.md | ||
|
|
@@ -81,7 +102,7 @@ private static bool TryReadContractDescriptor( | |
| return false; | ||
|
|
||
| // Flags - uint32_t | ||
| if (!TryReadUInt32(address, isLittleEndian, reader, out uint flags)) | ||
| if (!TryRead(address, isLittleEndian, reader, out uint flags)) | ||
| return false; | ||
|
|
||
| address += sizeof(uint); | ||
|
|
@@ -92,7 +113,7 @@ private static bool TryReadContractDescriptor( | |
| config = new Configuration { IsLittleEndian = isLittleEndian, PointerSize = pointerSize }; | ||
|
|
||
| // Descriptor size - uint32_t | ||
| if (!TryReadUInt32(address, config.IsLittleEndian, reader, out uint descriptorSize)) | ||
| if (!TryRead(address, config.IsLittleEndian, reader, out uint descriptorSize)) | ||
| return false; | ||
|
|
||
| address += sizeof(uint); | ||
|
|
@@ -104,7 +125,7 @@ private static bool TryReadContractDescriptor( | |
| address += (uint)pointerSize; | ||
|
|
||
| // Pointer data count - uint32_t | ||
| if (!TryReadUInt32(address, config.IsLittleEndian, reader, out uint pointerDataCount)) | ||
| if (!TryRead(address, config.IsLittleEndian, reader, out uint pointerDataCount)) | ||
| return false; | ||
|
|
||
| address += sizeof(uint); | ||
|
|
@@ -138,30 +159,33 @@ private static bool TryReadContractDescriptor( | |
| return true; | ||
| } | ||
|
|
||
| public uint ReadUInt32(ulong address) | ||
| public T Read<T>(ulong address, out T value) where T : unmanaged, IBinaryInteger<T>, IMinMaxValue<T> | ||
| { | ||
| if (!TryReadUInt32(address, out uint value)) | ||
| throw new InvalidOperationException($"Failed to read uint32 at 0x{address:x8}."); | ||
| if (!TryRead(address, out value)) | ||
| throw new InvalidOperationException($"Failed to read {typeof(T)} at 0x{address:x8}."); | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| public bool TryReadUInt32(ulong address, out uint value) | ||
| => TryReadUInt32(address, _config.IsLittleEndian, _reader, out value); | ||
| public bool TryRead<T>(ulong address, out T value) where T : unmanaged, IBinaryInteger<T>, IMinMaxValue<T> | ||
| => TryRead(address, _config.IsLittleEndian, _reader, out value); | ||
|
|
||
| private static bool TryReadUInt32(ulong address, bool isLittleEndian, Reader reader, out uint value) | ||
| private static bool TryRead<T>(ulong address, bool isLittleEndian, Reader reader, out T value) where T : unmanaged, IBinaryInteger<T>, IMinMaxValue<T> | ||
| { | ||
| value = 0; | ||
|
|
||
| Span<byte> buffer = stackalloc byte[sizeof(uint)]; | ||
| value = default; | ||
| Span<byte> buffer = stackalloc byte[sizeof(T)]; | ||
| if (reader.ReadFromTarget(address, buffer) < 0) | ||
| return false; | ||
|
|
||
| value = isLittleEndian | ||
| ? BinaryPrimitives.ReadUInt32LittleEndian(buffer) | ||
| : BinaryPrimitives.ReadUInt32BigEndian(buffer); | ||
| return isLittleEndian | ||
| ? T.TryReadLittleEndian(buffer, !IsSigned<T>(), out value) | ||
| : T.TryReadBigEndian(buffer, !IsSigned<T>(), out value); | ||
| } | ||
|
|
||
| return true; | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| private static bool IsSigned<T>() where T : struct, INumberBase<T>, IMinMaxValue<T> | ||
| { | ||
| return T.IsNegative(T.MinValue); | ||
| } | ||
|
|
||
| public TargetPointer ReadPointer(ulong address) | ||
|
|
@@ -183,21 +207,78 @@ private static bool TryReadPointer(ulong address, Configuration config, Reader r | |
| if (reader.ReadFromTarget(address, buffer) < 0) | ||
| return false; | ||
|
|
||
| if (config.PointerSize == sizeof(uint)) | ||
| if (config.PointerSize == sizeof(uint) | ||
| && TryRead(address, config.IsLittleEndian, reader, out uint value32)) | ||
| { | ||
| pointer = new TargetPointer( | ||
| config.IsLittleEndian | ||
| ? BinaryPrimitives.ReadUInt32LittleEndian(buffer) | ||
| : BinaryPrimitives.ReadUInt32BigEndian(buffer)); | ||
| pointer = new TargetPointer(value32); | ||
| return true; | ||
| } | ||
| else if (config.PointerSize == sizeof(ulong)) | ||
| else if (config.PointerSize == sizeof(ulong) | ||
| && TryRead(address, config.IsLittleEndian, reader, out ulong value64)) | ||
| { | ||
| pointer = new TargetPointer( | ||
| config.IsLittleEndian | ||
| ? BinaryPrimitives.ReadUInt64LittleEndian(buffer) | ||
| : BinaryPrimitives.ReadUInt64BigEndian(buffer)); | ||
| pointer = new TargetPointer(value64); | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public T ReadGlobal<T>(string name) where T : struct, INumber<T> | ||
| { | ||
| if (!TryReadGlobal(name, out T value)) | ||
| throw new InvalidOperationException($"Failed to read global {typeof(T)} '{name}'."); | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| public bool TryReadGlobal<T>(string name, out T value) where T : struct, INumber<T>, INumberBase<T> | ||
| { | ||
| value = default; | ||
| if (!_globals.TryGetValue(name, out (ulong Value, string? Type) global)) | ||
| return false; | ||
|
|
||
| if (global.Type is not null) | ||
elinor-fung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| string? expectedType = Type.GetTypeCode(typeof(T)) switch | ||
| { | ||
| TypeCode.SByte => "int8", | ||
| TypeCode.Byte => "uint8", | ||
| TypeCode.Int16 => "int16", | ||
| TypeCode.UInt16 => "uint16", | ||
| TypeCode.Int32 => "int32", | ||
| TypeCode.UInt32 => "uint32", | ||
| TypeCode.Int64 => "int64", | ||
| TypeCode.UInt64 => "uint64", | ||
| _ => null, | ||
| }; | ||
| if (expectedType is not null && global.Type != expectedType) | ||
elinor-fung marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| return false; | ||
| } | ||
lambdageek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| value = T.CreateChecked(global.Value); | ||
| return true; | ||
| } | ||
|
|
||
| public TargetPointer ReadGlobalPointer(string name) | ||
| { | ||
| if (!TryReadGlobalPointer(name, out TargetPointer pointer)) | ||
| throw new InvalidOperationException($"Failed to read global pointer '{name}'."); | ||
|
|
||
| return pointer; | ||
| } | ||
|
|
||
| public bool TryReadGlobalPointer(string name, out TargetPointer pointer) | ||
| { | ||
| pointer = TargetPointer.Null; | ||
| if (!_globals.TryGetValue(name, out (ulong Value, string? Type) global)) | ||
| return false; | ||
|
|
||
| if (global.Type is not null && Array.IndexOf(["pointer", "nint", "nuint"], global.Type) == -1) | ||
| return false; | ||
|
|
||
| pointer = new TargetPointer(global.Value); | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
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.