-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[cDAC] Implement sign extension for ClrDataAddresses #116222
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
7 commits
Select commit
Hold shift + click to select a range
72d04e4
add ClrDataAddress
31797f3
add comment
max-charlamb 8c8d930
add custom marshaller
340f08c
rename field in ClrDataAddress
63cd9b2
rename extensions
28b3b16
convert in arguments to ClrDataAddress
70d609c
remove reference to dacimpl.h and unused method
max-charlamb 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
36 changes: 36 additions & 0 deletions
36
src/native/managed/cdac/mscordaccore_universal/Legacy/ClrDataAddress.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,36 @@ | ||
| // 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.Runtime.InteropServices.Marshalling; | ||
|
|
||
| namespace Microsoft.Diagnostics.DataContractReader.Legacy; | ||
|
|
||
| /// <summary> | ||
| /// Represents the native CLRDATA_ADDRESS 64-bit type which uses sign extending | ||
| /// when converting from 32-bit values to 64-bit values. | ||
| /// For more information see TO_CDADDR in dacimpl.h | ||
| /// </summary> | ||
| [NativeMarshalling(typeof(ClrDataAddressMarshaller))] | ||
| internal struct ClrDataAddress : IEquatable<ClrDataAddress> | ||
jkotas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| public ulong Value; | ||
|
|
||
| public ClrDataAddress(ulong value) => Value = value; | ||
|
|
||
| public static implicit operator ulong(ClrDataAddress a) => a.Value; | ||
| public static implicit operator ClrDataAddress(ulong v) => new ClrDataAddress(v); | ||
|
|
||
| public override bool Equals(object? obj) => obj is ClrDataAddress address && Equals(address); | ||
| public readonly bool Equals(ClrDataAddress other) => Value == other.Value; | ||
| public override readonly int GetHashCode() => Value.GetHashCode(); | ||
|
|
||
| public override readonly string ToString() => $"0x{Value:x}"; | ||
| } | ||
|
|
||
| [CustomMarshaller(typeof(ClrDataAddress), MarshalMode.Default, typeof(ClrDataAddressMarshaller))] | ||
| internal static class ClrDataAddressMarshaller | ||
| { | ||
| public static ClrDataAddress ConvertToManaged(ulong address) => new ClrDataAddress(address); | ||
| public static ulong ConvertToUnmanaged(ClrDataAddress address) => address.Value; | ||
| } | ||
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
64 changes: 64 additions & 0 deletions
64
src/native/managed/cdac/mscordaccore_universal/Legacy/ConversionExtensions.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,64 @@ | ||
| // 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.Legacy; | ||
|
|
||
| internal static class ConversionExtensions | ||
| { | ||
| /// <summary> | ||
| /// Converts a TargetPointer to a ClrDataAddress using sign extension if required. | ||
| /// </summary> | ||
| public static ClrDataAddress ToClrDataAddress(this TargetPointer address, Target target) | ||
| { | ||
| if (target.PointerSize == sizeof(ulong)) | ||
| { | ||
| return address.Value; | ||
| } | ||
| else | ||
| { | ||
| return (ulong)(int)address.Value; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Converts a ClrDataAddress to a TargetPointer, ensuring the address is within the valid range for the target platform. | ||
| /// </summary> | ||
| public static TargetPointer ToTargetPointer(this ClrDataAddress address, Target target) | ||
| { | ||
| if (target.PointerSize == sizeof(ulong)) | ||
| { | ||
| return new TargetPointer(address); | ||
| } | ||
| else | ||
| { | ||
| long signedAddr = (long)address.Value; | ||
| if (signedAddr > int.MaxValue || signedAddr < int.MinValue) | ||
| { | ||
| throw new ArgumentException(nameof(address), "ClrDataAddress out of range for the target platform."); | ||
| } | ||
| return new TargetPointer((ulong)address); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Converts a ClrDataAddress to a TargetCodePointer, ensuring the address is within the valid range for the target platform. | ||
| /// </summary> | ||
| public static TargetCodePointer ToTargetCodePointer(this ClrDataAddress address, Target target) | ||
|
||
| { | ||
| if (target.PointerSize == sizeof(ulong)) | ||
| { | ||
| return new TargetCodePointer(address); | ||
| } | ||
| else | ||
| { | ||
| long signedAddr = (long)address.Value; | ||
| if (signedAddr > int.MaxValue || signedAddr < int.MinValue) | ||
| { | ||
| throw new ArgumentException(nameof(address), "ClrDataAddress out of range for the target platform."); | ||
| } | ||
| return new TargetCodePointer((ulong)address); | ||
| } | ||
| } | ||
| } | ||
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.
Nit: We hope dacimpl.h is going to disappear eventually, so this comment is not durable