Skip to content
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
2 changes: 1 addition & 1 deletion src/cdac/runtime-source.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ed0fb46706a7c6b99f62a0825871fa00855077cf
1e8e623dc1563ce2779c1253ad986d7c2e448453
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ private sealed class X86TransitionBlock : TransitionBlock
// Argument registers, callee-save registers, return address
public override int SizeOfTransitionBlock => SizeOfArgumentRegisters + SizeOfCalleeSavedRegisters + PointerSize;
public override int OffsetOfArgumentRegisters => 0;
// CALLDESCR_FPARGREGS is not set for X86
// HAS_FLOAT_ARGUMENT_REGISTERS is not set for X86
public override int OffsetOfFloatArgumentRegisters => 0;
// offsetof(ArgumentRegisters.ECX)
public override int ThisOffset => X86Constants.OffsetOfEcx;
Expand Down Expand Up @@ -495,7 +495,7 @@ private sealed class X64WindowsTransitionBlock : X64TransitionBlock
// Callee-saved registers, return address
public override int SizeOfTransitionBlock => SizeOfCalleeSavedRegisters + PointerSize;
public override int OffsetOfArgumentRegisters => SizeOfTransitionBlock;
// CALLDESCR_FPARGREGS is not set for Amd64 on
// HAS_FLOAT_ARGUMENT_REGISTERS is not set for Amd64 on
public override int OffsetOfFloatArgumentRegisters => 0;
public override int EnregisteredParamTypeMaxSize => 8;
public override int EnregisteredReturnTypeIntegerMaxSize => 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ public interface IRuntimeTypeSystem : IContract
// A IL Stub method is also a StoredSigMethodDesc, and a NoMetadataMethod
bool IsILStub(MethodDescHandle methodDesc) => throw new NotImplementedException();

// Return true for runtime plumbing omitted from diagnostic stacks, code viewing, and stepping.
bool IsDiagnosticsHidden(MethodDescHandle methodDesc) => throw new NotImplementedException();

// Return true for an LCG method explicitly marked as diagnostics-hidden.
bool IsDiagnosticsHiddenLCGMethod(MethodDescHandle methodDesc) => throw new NotImplementedException();

// Return true if a MethodDesc represents an IL stub with a special MethodDesc context arg
bool HasMDContextArg(MethodDescHandle methodDesc) => throw new NotImplementedException();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ IReadOnlyList<ExternalMemoryHandleRootData> IExternalMemoryHandles.GetRoots(bool
throw new InvalidOperationException("ExternalMemoryHandle list is cyclic.");

Data.ExternalMemoryHandle handle = _target.ProcessedData.GetOrAdd<Data.ExternalMemoryHandle>(current);
ITypeHandle typeHandle = _rts.GetTypeHandle(handle.MethodTable);
ITypeHandle typeHandle = _rts.GetTypeHandle(handle.TypeHandle);
CorElementType elementType = _rts.GetSignatureCorElementType(typeHandle);

if (_rts.IsValueType(typeHandle))
if (elementType == CorElementType.Byref)
{
AddValueTypeRoots(roots, typeHandle, handle.Memory, resolveInteriorPointers);
AddInteriorRoot(roots, handle.Memory, resolveInteriorPointers);
}
else if (handle.GCFlags != 0)
else if (_rts.IsValueType(typeHandle))
{
AddInteriorRoot(roots, handle.Memory, resolveInteriorPointers);
AddValueTypeRoots(roots, typeHandle, handle.Memory, resolveInteriorPointers);
}
else
else if (elementType is not (CorElementType.Ptr or CorElementType.FnPtr))
{
roots.Add(new ExternalMemoryHandleRootData { Address = handle.Memory });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ internal enum DynamicMethodDescExtendedFlags : uint
{
IsLCGMethod = 0x00004000,
IsILStub = 0x00008000,
IsDiagnosticsHidden = 0x00020000,
ILStubTypeMask = 0x000007FF,
}

Expand Down Expand Up @@ -1873,6 +1874,38 @@ public bool IsILStub(MethodDescHandle methodDescHandle)
return AsDynamicMethodDesc(methodDesc).IsILStub;
}

public bool IsDiagnosticsHidden(MethodDescHandle methodDescHandle)
{
MethodDesc methodDesc = _methodDescs[methodDescHandle.Address];
if (methodDesc.Classification == MethodClassification.Dynamic)
{
DynamicMethodDesc dynamicMethod = AsDynamicMethodDesc(methodDesc);
if (dynamicMethod.IsILStub ||
dynamicMethod.ExtendedFlags.HasFlag(DynamicMethodDescExtendedFlags.IsDiagnosticsHidden))
{
return true;
}
}

AsyncMethodFlags flags = GetAsyncMethodFlags(methodDescHandle);
return IsWrapperStub(methodDesc) ||
(flags.HasFlag(AsyncMethodFlags.Thunk) &&
(flags.HasFlag(AsyncMethodFlags.ReturnDroppingThunk) || !flags.HasFlag(AsyncMethodFlags.IsAsyncVariant)));
}

public bool IsDiagnosticsHiddenLCGMethod(MethodDescHandle methodDescHandle)
{
MethodDesc methodDesc = _methodDescs[methodDescHandle.Address];
if (methodDesc.Classification != MethodClassification.Dynamic)
{
return false;
}

DynamicMethodDesc dynamicMethod = AsDynamicMethodDesc(methodDesc);
return dynamicMethod.IsDynamicMethod &&
dynamicMethod.ExtendedFlags.HasFlag(DynamicMethodDescExtendedFlags.IsDiagnosticsHidden);
}

public bool HasMDContextArg(MethodDescHandle methodDescHandle)
{
MethodDesc methodDesc = _methodDescs[methodDescHandle.Address];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace Microsoft.Diagnostics.DataContractReader.Data;
internal sealed partial class ExternalMemoryHandle : IData<ExternalMemoryHandle>
{
[Field] public partial TargetPointer Next { get; }
[Field] public partial TargetPointer MethodTable { get; }
[Field] public partial TargetPointer TypeHandle { get; }
[Field] public partial TargetPointer Memory { get; }
[Field] public partial uint GCFlags { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2253,7 +2253,7 @@ private void InitFrameData(IStackDataFrameHandle handle, FrameType ft, nint pFra

data.v.mapping = (int)CorDebugMappingResult.MAPPING_NO_INFO;
data.v.fVarArgs = (byte)(rts.IsVarArg(md) ? 1 : 0);
data.v.fNoMetadata = (byte)((rts.IsNoMetadataMethod(md, out _) || rts.IsILStub(md) || IsDiagnosticsHidden(rts.GetAsyncMethodFlags(md)) || rts.IsWrapperStub(md)) ? 1 : 0);
data.v.fNoMetadata = (byte)((rts.IsNoMetadataMethod(md, out _) || rts.IsDiagnosticsHidden(md)) ? 1 : 0);
GenericContextLoc genericContextLoc = rts.GetGenericContextLoc(md);
if (genericContextLoc != GenericContextLoc.None)
{
Expand Down Expand Up @@ -2326,10 +2326,6 @@ private TargetPointer GetAmbientSP(IStackDataFrameHandle handle, CodeBlockHandle
return gcInfo.GetAmbientSP(gcInfoHandle, nativeOffset, stackWalk.GetContextFramePointer(handle), stackWalk.GetStackPointer(handle));
}

private static bool IsDiagnosticsHidden(AsyncMethodFlags flags)
=> flags.HasFlag(AsyncMethodFlags.Thunk)
&& (flags.HasFlag(AsyncMethodFlags.ReturnDroppingThunk) || !flags.HasFlag(AsyncMethodFlags.IsAsyncVariant));

private static bool HasClassOrMethodInstantiation(IRuntimeTypeSystem rts, MethodDescHandle md)
{
try
Expand Down Expand Up @@ -2666,7 +2662,11 @@ public int IsDiagnosticsHiddenOrLCGMethod(ulong vmMethodDesc, int* pRetVal)
{
Contracts.IRuntimeTypeSystem rts = _target.Contracts.RuntimeTypeSystem;
Contracts.MethodDescHandle md = rts.GetMethodDescHandle(new TargetPointer(vmMethodDesc));
if (rts.IsILStub(md) || IsDiagnosticsHidden(rts.GetAsyncMethodFlags(md)) || rts.IsWrapperStub(md))
if (rts.IsDiagnosticsHiddenLCGMethod(md))
{
*pRetVal = (int)DynamicMethodType.kDiagnosticHiddenLCGMethod;
}
else if (rts.IsDiagnosticsHidden(md))
{
*pRetVal = (int)DynamicMethodType.kDiagnosticHidden;
}
Expand Down Expand Up @@ -5354,45 +5354,34 @@ public int WalkRefs(nuint handle, uint count, [In, MarshalUsing(CountElementName
#if DEBUG
if (_legacy is not null && walk.LegacyHandle != 0 && count > 0)
{
// Parity check covers the handle prefix only.
// External roots can be duplicated and span different batch boundaries.
// Compare complete root sets before the stack-reference portion of each walk.
DacGcReference[] legacyRefs = new DacGcReference[(int)count];
uint legacyFetched = 0;
int hrLocal = _legacy.WalkRefs(walk.LegacyHandle, count, legacyRefs, &legacyFetched);
// The number of reported stack refs is not guaranteed to match between cDAC and legacy DAC.
// If this is the case, the cDAC may report S_FALSE while the legacy DAC reports S_OK, or vice versa.
// Allow divergent success codes, but still validate the rest of the results.
Debug.ValidateHResult(hr, hrLocal, HResultValidationMode.AllowDivergentSuccess);
uint cdacHandlePrefix = CountHandlePrefix(refs, i);
uint legacyHandlePrefix = CountHandlePrefix(legacyRefs, legacyFetched);
Debug.Assert(
cdacHandlePrefix == legacyHandlePrefix,
$"cDAC handle-prefix count {cdacHandlePrefix}, legacy {legacyHandlePrefix}");

uint compare = Math.Min(cdacHandlePrefix, legacyHandlePrefix);
for (uint j = 0; j < compare; j++)
{
Debug.Assert(refs[j].dwType == legacyRefs[j].dwType,
$"refs[{j}].dwType cDAC={refs[j].dwType:X}, legacy={legacyRefs[j].dwType:X}");
Debug.Assert(refs[j].vmDomain == legacyRefs[j].vmDomain,
$"refs[{j}].vmDomain cDAC=0x{refs[j].vmDomain:X}, legacy=0x{legacyRefs[j].vmDomain:X}");
Debug.Assert(refs[j].objHnd == legacyRefs[j].objHnd,
$"refs[{j}].objHnd cDAC=0x{refs[j].objHnd:X}, legacy=0x{legacyRefs[j].objHnd:X}");
Debug.Assert(refs[j].i64ExtraData == legacyRefs[j].i64ExtraData,
$"refs[{j}].i64ExtraData cDAC=0x{refs[j].i64ExtraData:X}, legacy=0x{legacyRefs[j].i64ExtraData:X}");
RefWalkHandlePrefixValidator validator = walk.HandlePrefixValidator;
validator.AddBatch(refs.AsSpan(0, (int)i), legacy: false, endOfWalk: hr == HResults.S_FALSE);
validator.AddBatch(legacyRefs.AsSpan(0, (int)legacyFetched), legacy: true, endOfWalk: hrLocal == HResults.S_FALSE);
while (validator.NeedsLegacyBatch && hrLocal >= 0)
{
hrLocal = _legacy.WalkRefs(walk.LegacyHandle, count, legacyRefs, &legacyFetched);
Debug.ValidateHResult(hr, hrLocal, HResultValidationMode.AllowDivergentSuccess);
validator.AddBatch(legacyRefs.AsSpan(0, (int)legacyFetched), legacy: true,
endOfWalk: hrLocal == HResults.S_FALSE);
Debug.Assert(hrLocal != HResults.S_OK || legacyFetched > 0,
"Legacy handle-prefix walk did not make progress.");
if (legacyFetched == 0)
break;
}
}

static uint CountHandlePrefix(DacGcReference[] buffer, uint length)
{
for (uint j = 0; j < length; j++)
if (validator.TryCompare(out bool matches))
{
CorGCReferenceType dwType = buffer[j].dwType;
if (dwType == CorGCReferenceType.CorReferenceStack)
{
return j;
}
Debug.Assert(matches, "cDAC and legacy handle-prefix root sets differ.");
}
return length;
}
#endif

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Collections.Generic;
using Microsoft.Diagnostics.DataContractReader.Contracts;

Expand All @@ -20,6 +21,9 @@ internal sealed class RefWalk : IEnum<DacGcReference>

public IEnumerator<DacGcReference> Enumerator { get; }
public nuint LegacyHandle { get; set; } = 0;
#if DEBUG
internal RefWalkHandlePrefixValidator HandlePrefixValidator { get; } = new();
#endif

public RefWalk(Target target, bool walkStacks, CorGCReferenceType handleWalkMask)
{
Expand Down Expand Up @@ -194,3 +198,49 @@ private IEnumerable<DacGcReference> WalkStacks()
}
}
}

internal sealed class RefWalkHandlePrefixValidator
{
private readonly HashSet<(CorGCReferenceType Type, ulong Domain, ulong Object, ulong ExtraData)> _cdac = [];
private readonly HashSet<(CorGCReferenceType Type, ulong Domain, ulong Object, ulong ExtraData)> _legacy = [];
private bool _cdacComplete;
private bool _legacyComplete;
private bool _validated;

internal bool NeedsLegacyBatch => _cdacComplete && !_legacyComplete;

internal void AddBatch(ReadOnlySpan<DacGcReference> references, bool legacy, bool endOfWalk)
{
ref bool complete = ref (legacy ? ref _legacyComplete : ref _cdacComplete);
if (complete)
return;

HashSet<(CorGCReferenceType Type, ulong Domain, ulong Object, ulong ExtraData)> roots =
legacy ? _legacy : _cdac;
foreach (DacGcReference reference in references)
{
if (reference.dwType == CorGCReferenceType.CorReferenceStack)
{
complete = true;
break;
}

roots.Add((reference.dwType, reference.vmDomain, reference.objHnd, reference.i64ExtraData));
}

complete |= endOfWalk;
}

internal bool TryCompare(out bool matches)
{
matches = false;
if (_validated || !_cdacComplete || !_legacyComplete)
return false;

_validated = true;
matches = _cdac.SetEquals(_legacy);
_cdac.Clear();
_legacy.Clear();
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ public enum DynamicMethodType
kNone = 0,
kDiagnosticHidden = 1,
kLCGMethod = 2,
kDiagnosticHiddenLCGMethod = 3,
}

public enum CorDebugThreadState
Expand Down
Loading