-
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 6 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
| 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 |
|---|---|---|
|
|
@@ -15,7 +15,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 +29,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 +49,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 | ||
|
|
@@ -138,6 +157,29 @@ private static bool TryReadContractDescriptor( | |
| return true; | ||
| } | ||
|
|
||
| public byte ReadUInt8(ulong address) | ||
elinor-fung marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (!TryReadUInt8(address, out byte value)) | ||
| throw new InvalidOperationException($"Failed to read uint8 at 0x{address:x8}."); | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| public bool TryReadUInt8(ulong address, out byte value) | ||
| => TryReadUInt8(address, _reader, out value); | ||
|
|
||
| private static bool TryReadUInt8(ulong address, Reader reader, out byte value) | ||
| { | ||
| value = 0; | ||
| fixed (byte* ptr = &value) | ||
| { | ||
| if (reader.ReadFromTarget(address, ptr, 1) < 0) | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public uint ReadUInt32(ulong address) | ||
| { | ||
| if (!TryReadUInt32(address, out uint value)) | ||
|
|
@@ -201,6 +243,55 @@ private static bool TryReadPointer(ulong address, Configuration config, Reader r | |
| return true; | ||
| } | ||
|
|
||
| public byte ReadGlobalUInt8(string name) | ||
| { | ||
| if (!TryReadGlobalUInt8(name, out byte value)) | ||
| throw new InvalidOperationException($"Failed to read global uint8 '{name}'."); | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| public bool TryReadGlobalUInt8(string name, out byte value) | ||
| { | ||
| value = 0; | ||
| if (!TryReadGlobalValue(name, out ulong globalValue, "uint8")) | ||
| return false; | ||
|
|
||
| value = (byte)globalValue; | ||
| 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 (!TryReadGlobalValue(name, out ulong globalValue, "pointer", "nint", "nuint")) | ||
| return false; | ||
|
|
||
| pointer = new TargetPointer(globalValue); | ||
| return true; | ||
| } | ||
|
|
||
| private bool TryReadGlobalValue(string name, out ulong value, params string[] expectedTypes) | ||
| { | ||
| value = 0; | ||
| if (!_globals.TryGetValue(name, out (ulong Value, string? Type) global)) | ||
| return false; | ||
|
|
||
| if (global.Type is not null && Array.IndexOf(expectedTypes, global.Type) == -1) | ||
| return false; | ||
|
|
||
| value = global.Value; | ||
| return true; | ||
| } | ||
|
|
||
| private sealed class Reader | ||
| { | ||
| private readonly delegate* unmanaged<ulong, byte*, uint, void*, int> _readFromTarget; | ||
|
|
||
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.