dotnet / dotnet/runtime

[cDAC] ClrDataFrame.GetContext should use contextFlags to compute required context size

Open
#125,791 2 comments 0 reactions 0 assignees View on GitHub
area-Diagnostics-cdac
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

## Summary

`ClrDataFrame.GetContext` in the managed cDAC always returns the full platform context regardless of the `contextFlags` parameter. The native DAC implementation (`stack.cpp`) uses `ContextSizeForFlags` to compute the required context size based on the requested flags, and only copies/requires that subset.

## Current Behavior

The cDAC implementation ignores `contextFlags` and always:
- Reports `contextSize` as the full platform context length
- Requires `contextBufSize >= full context length` (returns `E_INVALIDARG` otherwise)
- Copies the entire context into the output buffer

## Expected Behavior

Should match the native DAC:
1. Use `contextFlags` to compute the required size via a `ContextSizeForFlags` equivalent
2. Report the computed size via `contextSize`
3. Only require `contextBufSize >= computed size`
4. Copy only the relevant context fields

## Affected Code

- **cDAC**: `src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Legacy/ClrDataFrame.cs` lines 38-92
- **Native reference**: `src/coreclr/debug/daccess/stack.cpp` `ClrDataFrame::GetContext`

The TODO is at line 50:
```csharp
// TODO: Use contextFlags to compute the required size via ContextSizeForFlags
// (see native ClrDataFrame::GetContext in stack.cpp). Currently we always
// return the full platform context regardless of the requested flags.
```

## Impact

Currently callers that pass a smaller buffer sized for a subset of context flags will get `E_INVALIDARG` even though the native DAC would succeed. This doesn't affect SOS's `!clrstack` (which always requests the full context), but could affect other diagnostic tools.

/cc @max-charlamb

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.