-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Add cDAC ObjectiveCMarshal contract and IsTrackedReferenceWithFinalizer API #125895
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
Draft
Copilot
wants to merge
3
commits into
main
Choose a base branch
from
copilot/add-cdac-apis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Contract ObjectiveCMarshal | ||
|
|
||
| This contract is for getting information related to Objective-C interop marshaling. | ||
|
|
||
| ## APIs of contract | ||
|
|
||
| ``` csharp | ||
| // Get the tagged memory for an Objective-C tracked reference object. | ||
| // Returns false if the object does not have tagged memory. | ||
| // On success, size is set to the size of the tagged memory in bytes; otherwise size is set to default. | ||
| bool TryGetTaggedMemory(TargetPointer address, out TargetNUInt size, out TargetPointer taggedMemory); | ||
| ``` | ||
|
|
||
| ## Version 1 | ||
|
|
||
| Data descriptors used: | ||
| | Data Descriptor Name | Field | Meaning | | ||
| | --- | --- | --- | | ||
| | `InteropSyncBlockInfo` | `TaggedMemory` | Pointer to the tagged memory for the object (if it exists) | | ||
| | `Object` | `m_pMethTab` | Method table for the object | | ||
| | `ObjectHeader` | `SyncBlockValue` | Sync block value for the object header | | ||
|
|
||
| Contracts used: | ||
| | Contract Name | | ||
| | --- | | ||
| | `SyncBlock` | | ||
|
|
||
| Globals used: | ||
| | Global Name | Type | Purpose | | ||
| | --- | --- | --- | | ||
| | `SyncBlockIsHashOrSyncBlockIndex` | uint32 | Bitmask: set if sync block value is a hash code or sync block index | | ||
| | `SyncBlockIsHashCode` | uint32 | Bitmask: set if sync block value is a hash code | | ||
| | `SyncBlockIndexMask` | uint32 | Mask to extract the sync block index from the sync block value | | ||
|
|
||
| ``` csharp | ||
| bool TryGetTaggedMemory(TargetPointer address, out TargetNUInt size, out TargetPointer taggedMemory) | ||
| { | ||
| size = default; | ||
| taggedMemory = TargetPointer.Null; | ||
|
|
||
| ulong objectHeaderSize = /* ObjectHeader size */; | ||
| uint syncBlockValue = target.Read<uint>(address - objectHeaderSize + /* ObjectHeader::SyncBlockValue offset */); | ||
|
|
||
| // Check if the sync block value represents a sync block index | ||
| if ((syncBlockValue & (SyncBlockIsHashCode | SyncBlockIsHashOrSyncBlockIndex)) | ||
| != SyncBlockIsHashOrSyncBlockIndex) | ||
| return false; | ||
|
|
||
| uint index = syncBlockValue & SyncBlockIndexMask; | ||
| TargetPointer syncBlockPtr = target.Contracts.SyncBlock.GetSyncBlock(index); | ||
|
|
||
| TargetPointer interopInfoPtr = target.ReadPointer(syncBlockPtr + /* SyncBlock::InteropInfo offset */); | ||
| if (interopInfoPtr == TargetPointer.Null) | ||
| return false; | ||
|
|
||
| taggedMemory = target.ReadPointer(interopInfoPtr + /* InteropSyncBlockInfo::TaggedMemory offset */); | ||
| if (taggedMemory != TargetPointer.Null) | ||
| size = new TargetNUInt(2 * target.PointerSize); | ||
| return taggedMemory != TargetPointer.Null; | ||
| } | ||
| ``` | ||
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
21 changes: 21 additions & 0 deletions
21
...dac/Microsoft.Diagnostics.DataContractReader.Abstractions/Contracts/IObjectiveCMarshal.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,21 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.Diagnostics.DataContractReader.Contracts; | ||
|
|
||
| public interface IObjectiveCMarshal : IContract | ||
| { | ||
| static string IContract.Name { get; } = nameof(ObjectiveCMarshal); | ||
|
|
||
| // Get the tagged memory for an Objective-C tracked reference object. | ||
| // Returns false if the object does not have tagged memory. | ||
| // On success, size is set to the size of the tagged memory in bytes; otherwise size is set to default. | ||
| bool TryGetTaggedMemory(TargetPointer address, out TargetNUInt size, out TargetPointer taggedMemory) => throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public readonly struct ObjectiveCMarshal : IObjectiveCMarshal | ||
| { | ||
| // Everything throws NotImplementedException | ||
| } |
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
19 changes: 19 additions & 0 deletions
19
.../Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/ObjectiveCMarshalFactory.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,19 @@ | ||
| // 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.Contracts; | ||
|
|
||
| public sealed class ObjectiveCMarshalFactory : IContractFactory<IObjectiveCMarshal> | ||
| { | ||
| IObjectiveCMarshal IContractFactory<IObjectiveCMarshal>.CreateContract(Target target, int version) | ||
| { | ||
| uint syncBlockIsHashOrSyncBlockIndex = target.ReadGlobal<uint>(Constants.Globals.SyncBlockIsHashOrSyncBlockIndex); | ||
| uint syncBlockIsHashCode = target.ReadGlobal<uint>(Constants.Globals.SyncBlockIsHashCode); | ||
| uint syncBlockIndexMask = target.ReadGlobal<uint>(Constants.Globals.SyncBlockIndexMask); | ||
| return version switch | ||
| { | ||
| 1 => new ObjectiveCMarshal_1(target, syncBlockIsHashOrSyncBlockIndex, syncBlockIsHashCode, syncBlockIndexMask), | ||
| _ => default(ObjectiveCMarshal), | ||
| }; | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
.../cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/ObjectiveCMarshal_1.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,45 @@ | ||
| // 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.Contracts; | ||
|
|
||
| internal readonly struct ObjectiveCMarshal_1 : IObjectiveCMarshal | ||
| { | ||
| private readonly Target _target; | ||
| private readonly uint _syncBlockIsHashOrSyncBlockIndex; | ||
| private readonly uint _syncBlockIsHashCode; | ||
| private readonly uint _syncBlockIndexMask; | ||
|
|
||
| internal ObjectiveCMarshal_1(Target target, | ||
| uint syncBlockIsHashOrSyncBlockIndex, uint syncBlockIsHashCode, uint syncBlockIndexMask) | ||
| { | ||
| _target = target; | ||
| _syncBlockIsHashOrSyncBlockIndex = syncBlockIsHashOrSyncBlockIndex; | ||
| _syncBlockIsHashCode = syncBlockIsHashCode; | ||
| _syncBlockIndexMask = syncBlockIndexMask; | ||
| } | ||
|
|
||
| public bool TryGetTaggedMemory(TargetPointer address, out TargetNUInt size, out TargetPointer taggedMemory) | ||
| { | ||
| size = default; | ||
| taggedMemory = TargetPointer.Null; | ||
|
|
||
| ulong objectHeaderSize = _target.GetTypeInfo(DataType.ObjectHeader).Size!.Value; | ||
| Data.ObjectHeader header = _target.ProcessedData.GetOrAdd<Data.ObjectHeader>(address - objectHeaderSize); | ||
| uint syncBlockValue = header.SyncBlockValue; | ||
|
|
||
| // Check if the sync block value represents a sync block index | ||
| if ((syncBlockValue & (_syncBlockIsHashCode | _syncBlockIsHashOrSyncBlockIndex)) | ||
| != _syncBlockIsHashOrSyncBlockIndex) | ||
| return false; | ||
|
|
||
| uint index = syncBlockValue & _syncBlockIndexMask; | ||
| TargetPointer syncBlock = _target.Contracts.SyncBlock.GetSyncBlock(index); | ||
| Data.SyncBlock sb = _target.ProcessedData.GetOrAdd<Data.SyncBlock>(syncBlock); | ||
|
|
||
| taggedMemory = sb.InteropInfo?.TaggedMemory ?? TargetPointer.Null; | ||
| if (taggedMemory != TargetPointer.Null) | ||
| size = new TargetNUInt(2 * (ulong)_target.PointerSize); | ||
| return taggedMemory != TargetPointer.Null; | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ internal enum WFLAGS_HIGH : uint | |
| Collectible = 0x00200000, // GC depends on this bit. | ||
|
|
||
| ContainsGCPointers = 0x01000000, | ||
| IsTrackedReferenceWithFinalizer = 0x04000000, | ||
|
Member
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. We need to add this to the RuntimeTypeSystem.md markdown file. |
||
| HasComponentSize = 0x80000000, // This is set if lower 16 bits is used for the component size, | ||
| // otherwise the lower bits are used for WFLAGS_LOW | ||
| } | ||
|
|
@@ -99,6 +100,7 @@ private bool TestFlagWithMask(WFLAGS2_ENUM mask, WFLAGS2_ENUM flag) | |
| public bool HasInstantiation => !TestFlagWithMask(WFLAGS_LOW.GenericsMask, WFLAGS_LOW.GenericsMask_NonGeneric); | ||
| public bool ContainsGCPointers => GetFlag(WFLAGS_HIGH.ContainsGCPointers) != 0; | ||
| public bool IsCollectible => GetFlag(WFLAGS_HIGH.Collectible) != 0; | ||
| public bool IsTrackedReferenceWithFinalizer => GetFlag(WFLAGS_HIGH.IsTrackedReferenceWithFinalizer) != 0; | ||
| public bool IsDynamicStatics => GetFlag(WFLAGS2_ENUM.DynamicStatics) != 0; | ||
| public bool IsGenericTypeDefinition => TestFlagWithMask(WFLAGS_LOW.GenericsMask, WFLAGS_LOW.GenericsMask_TypicalInstantiation); | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
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.
I'd suggest factor out a little helper here that maps object address -> SyncBlock address and stick it on the object contract. The same algorithm shows up in getting BuiltInCom data and hashcode so far. As we keep going I expect you'll see at least two more usages of it for ENC added fields and monitor locks.