Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

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

/// </summary>
[NativeMarshalling(typeof(ClrDataAddressMarshaller))]
internal struct ClrDataAddress : IEquatable<ClrDataAddress>
{
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ int IXCLRDataModule.StartEnumExtents(ulong* handle)
{
if (contract.TryGetLoadedImageContents(moduleHandle, out TargetPointer baseAddress, out uint size, out _))
{
_extents[0].baseAddress = baseAddress;
_extents[0].baseAddress = baseAddress.ToClrDataAddress(_target);
_extents[0].length = size;
_extents[0].type = 0x0; // CLRDATA_MODULE_PE_FILE
}
Expand Down Expand Up @@ -349,7 +349,7 @@ private int DacPrivateRequestGetModuleData(uint inBufferSize, byte* inBuffer, ui

bool isReflectionEmit = (contract.GetFlags(moduleHandle) & ModuleFlags.ReflectionEmit) != 0;

getModuleData->PEAssembly = _address;
getModuleData->PEAssembly = _address.ToClrDataAddress(_target);
getModuleData->IsDynamic = isReflectionEmit ? 1u : 0u;

if (peAssembly != TargetPointer.Null)
Expand All @@ -365,7 +365,7 @@ private int DacPrivateRequestGetModuleData(uint inBufferSize, byte* inBuffer, ui
}

contract.TryGetLoadedImageContents(moduleHandle, out TargetPointer baseAddress, out uint size, out uint flags);
getModuleData->LoadedPEAddress = baseAddress;
getModuleData->LoadedPEAddress = baseAddress.ToClrDataAddress(_target);
getModuleData->LoadedPESize = size;

// Can not get the assembly layout for a dynamic module
Expand All @@ -377,7 +377,7 @@ private int DacPrivateRequestGetModuleData(uint inBufferSize, byte* inBuffer, ui

if (contract.TryGetSymbolStream(moduleHandle, out TargetPointer symbolBuffer, out uint symbolBufferSize))
{
getModuleData->InMemoryPdbAddress = symbolBuffer;
getModuleData->InMemoryPdbAddress = symbolBuffer.ToClrDataAddress(_target);
getModuleData->InMemoryPdbSize = symbolBufferSize;
}

Expand Down
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks unused

{
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);
}
}
}
Loading
Loading