Skip to content

[cdac] Add helper methods to Target for reading UTF-8/16 strings #106483

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 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions docs/design/datacontracts/RuntimeTypeSystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ partial interface IRuntimeTypeSystem : IContract
// generated by the runtime, or a MethodDesc that describes a method represented by the System.Reflection.Emit.DynamicMethod class
// Or something else similar.
// A no metadata method is also a StoredSigMethodDesc
public virtual bool IsNoMetadataMethod(MethodDescHandle methodDesc, out ReadOnlySpan<byte> methodName);
public virtual bool IsNoMetadataMethod(MethodDescHandle methodDesc, out string methodName);

// A StoredSigMethodDesc is a MethodDesc for which the signature isn't found in metadata.
public virtual bool IsStoredSigMethodDesc(MethodDescHandle methodDesc, out ReadOnlySpan<byte> signature);
Expand Down Expand Up @@ -708,9 +708,9 @@ And the various apis are implemented with the following algorithms
public uint GetMethodToken(MethodDescHandle methodDescHandle)
{
MethodDesc methodDesc = _methodDescs[methodDescHandle.Address];

TargetPointer methodDescChunk = // Using ChunkIndex from methodDesc, compute the wrapping MethodDescChunk

ushort Flags3AndTokenRemainder = // Read Flags3AndTokenRemainder field from MethodDesc contract using address methodDescHandle.Address

ushort FlagsAndTokenRange = // Read FlagsAndTokenRange field from MethodDescChunk contract using address methodDescChunk
Expand Down
23 changes: 1 addition & 22 deletions src/native/managed/cdacreader/src/Contracts/Loader_1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Microsoft.Diagnostics.DataContractReader.Contracts;

Expand Down Expand Up @@ -39,26 +37,7 @@ ModuleFlags ILoader.GetFlags(ModuleHandle handle)
string ILoader.GetPath(ModuleHandle handle)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);

// TODO: [cdac] Add/use APIs on Target for reading strings in target endianness
TargetPointer addr = module.Path;
while (true)
{
// Read characters until we find the null terminator
char nameChar = _target.Read<char>(addr);
if (nameChar == 0)
break;

addr += sizeof(char);
}

int length = (int)(addr.Value - module.Path.Value);
if (length == 0)
return string.Empty;

Span<byte> span = stackalloc byte[length];
_target.ReadBuffer(module.Path, span);
return new string(MemoryMarshal.Cast<byte, char>(span));
return _target.ReadUtf16String(module.Path);
}

TargetPointer ILoader.GetLoaderAllocator(ModuleHandle handle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Reflection.Metadata.Ecma335;

namespace Microsoft.Diagnostics.DataContractReader.Contracts;

Expand Down Expand Up @@ -148,11 +147,11 @@ static IContract IContract.Create(Target target, int version)
// An array method is also a StoredSigMethodDesc
public virtual bool IsArrayMethod(MethodDescHandle methodDesc, out ArrayFunctionType functionType) => throw new NotImplementedException();

// Return true if a MethodDesc represents a method without metadata method, either an IL Stub dynamically
// Return true if a MethodDesc represents a method without metadata, either an IL Stub dynamically
// generated by the runtime, or a MethodDesc that describes a method represented by the System.Reflection.Emit.DynamicMethod class
// Or something else similar.
// A no metadata method is also a StoredSigMethodDesc
public virtual bool IsNoMetadataMethod(MethodDescHandle methodDesc, out ReadOnlySpan<byte> methodName) => throw new NotImplementedException();
public virtual bool IsNoMetadataMethod(MethodDescHandle methodDesc, out string methodName) => throw new NotImplementedException();
// A StoredSigMethodDesc is a MethodDesc for which the signature isn't found in metadata.
public virtual bool IsStoredSigMethodDesc(MethodDescHandle methodDesc, out ReadOnlySpan<byte> signature) => throw new NotImplementedException();

Expand Down
36 changes: 8 additions & 28 deletions src/native/managed/cdacreader/src/Contracts/RuntimeTypeSystem_1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection.Metadata.Ecma335;
using Microsoft.Diagnostics.DataContractReader.Data;
using Microsoft.Diagnostics.DataContractReader.Contracts.RuntimeTypeSystem_1_NS;
using System.Diagnostics;
using System.Text;
using System.Reflection;
using Microsoft.Diagnostics.DataContractReader.Data;

namespace Microsoft.Diagnostics.DataContractReader.Contracts;

Expand Down Expand Up @@ -195,34 +193,16 @@ private class DynamicMethodDesc : IData<DynamicMethodDesc>
private DynamicMethodDesc(Target target, TargetPointer methodDescPointer)
{
_address = methodDescPointer;
List<byte> nameBytes = new();
_desc = target.ProcessedData.GetOrAdd<Data.DynamicMethodDesc>(methodDescPointer);

if (_desc.MethodName != TargetPointer.Null)
{
TargetPointer currentNameAddress = _desc.MethodName;
do
{
byte nameByte = target.Read<byte>(currentNameAddress);

if (nameByte == 0)
break;

nameBytes.Add(nameByte);
currentNameAddress++;
} while (true);

MethodName = nameBytes.ToArray();
}
else
{
MethodName = System.Array.Empty<byte>();
}
MethodName = _desc.MethodName != TargetPointer.Null
? target.ReadUtf8String(_desc.MethodName)
: string.Empty;

_storedSigDesc = target.ProcessedData.GetOrAdd<Data.StoredSigMethodDesc>(methodDescPointer);
}

public byte[] MethodName { get; }
public string MethodName { get; }
public DynamicMethodDescExtendedFlags ExtendedFlags => (DynamicMethodDescExtendedFlags)_storedSigDesc.ExtendedFlags;

public bool IsDynamicMethod => ExtendedFlags.HasFlag(DynamicMethodDescExtendedFlags.IsLCGMethod);
Expand Down Expand Up @@ -712,13 +692,13 @@ public bool IsArrayMethod(MethodDescHandle methodDescHandle, out ArrayFunctionTy
return true;
}

public bool IsNoMetadataMethod(MethodDescHandle methodDescHandle, out ReadOnlySpan<byte> methodName)
public bool IsNoMetadataMethod(MethodDescHandle methodDescHandle, out string methodName)
{
MethodDesc methodDesc = _methodDescs[methodDescHandle.Address];

if (methodDesc.Classification != MethodClassification.Dynamic)
{
methodName = default;
methodName = string.Empty;
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/native/managed/cdacreader/src/Legacy/TypeNameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public static void AppendMethodImpl(Target target, StringBuilder stringBuilder,
{
IRuntimeTypeSystem runtimeTypeSystem = target.Contracts.RuntimeTypeSystem;
ILoader loader = target.Contracts.Loader;
ReadOnlySpan<byte> methodNameSpan;
string methodName;
TypeHandle th = default;
Contracts.ModuleHandle module = default;

bool isNoMetadataMethod = runtimeTypeSystem.IsNoMetadataMethod(method, out methodNameSpan);
bool isNoMetadataMethod = runtimeTypeSystem.IsNoMetadataMethod(method, out methodName);
if (isNoMetadataMethod)
{
if (runtimeTypeSystem.IsDynamicMethod(method))
Expand All @@ -91,7 +91,7 @@ public static void AppendMethodImpl(Target target, StringBuilder stringBuilder,

if (isNoMetadataMethod)
{
stringBuilder.Append(Encoding.UTF8.GetString(methodNameSpan));
stringBuilder.Append(methodName);
}
else if (runtimeTypeSystem.IsArrayMethod(method, out ArrayFunctionType functionType))
{
Expand Down
71 changes: 70 additions & 1 deletion src/native/managed/cdacreader/src/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Diagnostics.DataContractReader.Data;

namespace Microsoft.Diagnostics.DataContractReader;
Expand Down Expand Up @@ -265,6 +265,12 @@ private static DataType GetDataType(string type)

public int PointerSize => _config.PointerSize;

/// <summary>
/// Read a value from the target in target endianness
/// </summary>
/// <typeparam name="T">Type of value to read</typeparam>
/// <param name="address">Address to start reading from</param>
/// <returns>Value read from the target</returns>
public T Read<T>(ulong address) where T : unmanaged, IBinaryInteger<T>, IMinMaxValue<T>
{
if (!TryRead(address, _config.IsLittleEndian, _reader, out T value))
Expand Down Expand Up @@ -319,6 +325,11 @@ private static bool IsSigned<T>() where T : struct, INumberBase<T>, IMinMaxValue
return T.IsNegative(T.MinValue);
}

/// <summary>
/// Read a pointer from the target in target endianness
/// </summary>
/// <param name="address">Address to start reading from</param>
/// <returns>Pointer read from the target</returns>}
public TargetPointer ReadPointer(ulong address)
{
if (!TryReadPointer(address, _config, _reader, out TargetPointer pointer))
Expand Down Expand Up @@ -352,6 +363,64 @@ public void ReadPointers(ulong address, Span<TargetPointer> buffer)
}
}

/// <summary>
/// Read a null-terminated UTF-8 string from the target
/// </summary>
/// <param name="address">Address to start reading from</param>
/// <returns>String read from the target</returns>}
public string ReadUtf8String(ulong address)
{
// Read characters until we find the null terminator
ulong end = address;
while (Read<byte>(end) != 0)
{
end += sizeof(byte);
}

int length = (int)(end - address);
if (length == 0)
return string.Empty;

Span<byte> span = length <= StackAllocByteThreshold
? stackalloc byte[length]
: new byte[length];
ReadBuffer(address, span);
return Encoding.UTF8.GetString(span);
}

/// <summary>
/// Read a null-terminated UTF-16 string from the target in target endianness
/// </summary>
/// <param name="address">Address to start reading from</param>
/// <returns>String read from the target</returns>}
public string ReadUtf16String(ulong address)
{
// Read characters until we find the null terminator
ulong end = address;
while (Read<char>(end) != 0)
{
end += sizeof(char);
}

int length = (int)(end - address);
if (length == 0)
return string.Empty;

Span<byte> span = length <= StackAllocByteThreshold
? stackalloc byte[length]
: new byte[length];
ReadBuffer(address, span);
string result = _config.IsLittleEndian
? Encoding.Unicode.GetString(span)
: Encoding.BigEndianUnicode.GetString(span);
return result;
}

/// <summary>
/// Read a native unsigned integer from the target in target endianness
/// </summary>
/// <param name="address">Address to start reading from</param>
/// <returns>Value read from the target</returns>
public TargetNUInt ReadNUInt(ulong address)
{
if (!TryReadNUInt(address, _config, _reader, out ulong value))
Expand Down
Loading