-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add cDAC tests and implementation for GetGenerationTable and GetFinalizationFillPointers #124674
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Microsoft.Diagnostics.DataContractReader.Legacy | ||
|
|
||
| This project contains `SOSDacImpl`, which implements the `ISOSDacInterface*` and | ||
| `IXCLRDataProcess` COM-style APIs by delegating to the cDAC contract layer. | ||
|
|
||
| ## Implementing a new SOSDacImpl method | ||
|
|
||
| When a method currently delegates to `_legacyImpl` (returning `E_NOTIMPL` when null), | ||
| replace it with a cDAC implementation following this pattern: | ||
|
|
||
| ```csharp | ||
| int ISOSDacInterface8.ExampleMethod(uint* pResult) | ||
| { | ||
| // 1. Validate pointer arguments before the try block | ||
| if (pResult == null) | ||
| return HResults.E_INVALIDARG; | ||
|
|
||
| int hr = HResults.S_OK; | ||
| try | ||
| { | ||
| // 2. Get the relevant contract and call it | ||
| IGC gc = _target.Contracts.GC; | ||
| *pResult = gc.SomeMethod(); | ||
| } | ||
| catch (System.Exception ex) | ||
| { | ||
| hr = ex.HResult; | ||
| } | ||
|
|
||
| // 3. Cross-validate with legacy DAC in debug builds | ||
| #if DEBUG | ||
| if (_legacyImpl8 is not null) | ||
| { | ||
| uint resultLocal; | ||
| int hrLocal = _legacyImpl8.ExampleMethod(&resultLocal); | ||
| Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}"); | ||
| if (hr == HResults.S_OK) | ||
| { | ||
| Debug.Assert(*pResult == resultLocal); | ||
| } | ||
| } | ||
| #endif | ||
| return hr; | ||
| } | ||
| ``` | ||
|
|
||
| ### Key conventions | ||
|
|
||
| - **HResult returns**: Methods return `int` HResult codes, not exceptions. | ||
| Use `HResults.S_OK`, `HResults.S_FALSE`, `HResults.E_INVALIDARG`, etc. | ||
| - **Null pointer checks**: Validate output pointer arguments *before* the try block | ||
| and return `E_INVALIDARG`. This matches the native DAC behavior. | ||
| - **Exception handling**: Wrap all contract calls in try/catch. The catch converts | ||
| exceptions to HResult codes via `ex.HResult`. | ||
| - **Debug cross-validation**: In `#if DEBUG`, call the legacy implementation (if | ||
| available) and assert the results match. This catches discrepancies during testing. | ||
|
|
||
| ### Sized-buffer protocol | ||
|
|
||
| Several `ISOSDacInterface8` methods use a two-call pattern where the caller first | ||
| queries the needed buffer size, then calls again with a sufficiently large buffer: | ||
|
|
||
| ```csharp | ||
| int GetSomeTable(uint count, Data* buffer, uint* pNeeded) | ||
| ``` | ||
|
|
||
| The protocol is: | ||
| 1. Always set `*pNeeded` to the required count (if `pNeeded` is not null). | ||
| 2. If `count > 0 && buffer == null`: return `E_INVALIDARG`. | ||
| 3. If `count < needed`: return `S_FALSE` (buffer too small, but `*pNeeded` is set). | ||
| 4. If `count >= needed`: populate `buffer` and return `S_OK`. | ||
|
|
||
| This matches the native implementation in `src/coreclr/debug/daccess/request.cpp`. | ||
|
|
||
| ### Pointer conversions | ||
|
|
||
| - `TargetPointer` → `ClrDataAddress`: use `pointer.ToClrDataAddress(_target)`. | ||
| On 32-bit targets, this **sign-extends** the value (e.g., `0xAA000000` becomes | ||
| `0xFFFFFFFF_AA000000`). This matches native DAC behavior. | ||
| - `ClrDataAddress` → `TargetPointer`: use `address.ToTargetPointer(_target)`. | ||
|
|
||
| Both are defined in `ConversionExtensions.cs`. |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3984,17 +3984,189 @@ int ISOSDacInterface8.GetNumberGenerations(uint* pGenerations) | |||||||||||||
| return hr; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // WKS | ||||||||||||||
| int ISOSDacInterface8.GetGenerationTable(uint cGenerations, /*struct DacpGenerationData*/ void* pGenerationData, uint* pNeeded) | ||||||||||||||
| => _legacyImpl8 is not null ? _legacyImpl8.GetGenerationTable(cGenerations, pGenerationData, pNeeded) : HResults.E_NOTIMPL; | ||||||||||||||
| { | ||||||||||||||
| if (cGenerations > 0 && pGenerationData == null) | ||||||||||||||
| return HResults.E_INVALIDARG; | ||||||||||||||
|
|
||||||||||||||
| int hr = HResults.S_OK; | ||||||||||||||
| try | ||||||||||||||
| { | ||||||||||||||
| IGC gc = _target.Contracts.GC; | ||||||||||||||
| uint totalGenerationCount = _target.ReadGlobal<uint>(Constants.Globals.TotalGenerationCount); | ||||||||||||||
|
|
||||||||||||||
| if (pNeeded != null) | ||||||||||||||
| *pNeeded = totalGenerationCount; | ||||||||||||||
|
|
||||||||||||||
| if (cGenerations < totalGenerationCount) | ||||||||||||||
| { | ||||||||||||||
| hr = HResults.S_FALSE; | ||||||||||||||
| } | ||||||||||||||
| else | ||||||||||||||
| { | ||||||||||||||
| GCHeapData heapData = gc.GetHeapData(); | ||||||||||||||
| DacpGenerationData* genData = (DacpGenerationData*)pGenerationData; | ||||||||||||||
|
|
||||||||||||||
| for (int i = 0; i < (int)totalGenerationCount && i < heapData.GenerationTable.Count; i++) | ||||||||||||||
| { | ||||||||||||||
| GCGenerationData gen = heapData.GenerationTable[i]; | ||||||||||||||
| genData[i].start_segment = gen.StartSegment.ToClrDataAddress(_target); | ||||||||||||||
| genData[i].allocation_start = gen.AllocationStart.ToClrDataAddress(_target); | ||||||||||||||
| genData[i].allocContextPtr = gen.AllocationContextPointer.ToClrDataAddress(_target); | ||||||||||||||
| genData[i].allocContextLimit = gen.AllocationContextLimit.ToClrDataAddress(_target); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| catch (System.Exception ex) | ||||||||||||||
| { | ||||||||||||||
| hr = ex.HResult; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #if DEBUG | ||||||||||||||
| if (_legacyImpl8 is not null) | ||||||||||||||
| { | ||||||||||||||
| uint pNeededLocal; | ||||||||||||||
| int hrLocal = _legacyImpl8.GetGenerationTable(cGenerations, pGenerationData, &pNeededLocal); | ||||||||||||||
| Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}"); | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+4025
to
+4031
|
||||||||||||||
| #endif | ||||||||||||||
| return hr; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| int ISOSDacInterface8.GetFinalizationFillPointers(uint cFillPointers, ClrDataAddress* pFinalizationFillPointers, uint* pNeeded) | ||||||||||||||
| => _legacyImpl8 is not null ? _legacyImpl8.GetFinalizationFillPointers(cFillPointers, pFinalizationFillPointers, pNeeded) : HResults.E_NOTIMPL; | ||||||||||||||
| { | ||||||||||||||
| if (cFillPointers > 0 && pFinalizationFillPointers == null) | ||||||||||||||
| return HResults.E_INVALIDARG; | ||||||||||||||
|
|
||||||||||||||
| int hr = HResults.S_OK; | ||||||||||||||
| try | ||||||||||||||
| { | ||||||||||||||
| IGC gc = _target.Contracts.GC; | ||||||||||||||
| GCHeapData heapData = gc.GetHeapData(); | ||||||||||||||
| uint numFillPointers = (uint)heapData.FillPointers.Count; | ||||||||||||||
|
|
||||||||||||||
| if (pNeeded != null) | ||||||||||||||
| *pNeeded = numFillPointers; | ||||||||||||||
|
|
||||||||||||||
| if (cFillPointers < numFillPointers) | ||||||||||||||
| { | ||||||||||||||
| hr = HResults.S_FALSE; | ||||||||||||||
| } | ||||||||||||||
| else | ||||||||||||||
| { | ||||||||||||||
| for (int i = 0; i < (int)numFillPointers; i++) | ||||||||||||||
| { | ||||||||||||||
| pFinalizationFillPointers[i] = heapData.FillPointers[i].ToClrDataAddress(_target); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| catch (System.Exception ex) | ||||||||||||||
| { | ||||||||||||||
| hr = ex.HResult; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #if DEBUG | ||||||||||||||
| if (_legacyImpl8 is not null) | ||||||||||||||
| { | ||||||||||||||
| uint pNeededLocal; | ||||||||||||||
| int hrLocal = _legacyImpl8.GetFinalizationFillPointers(cFillPointers, pFinalizationFillPointers, &pNeededLocal); | ||||||||||||||
| Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}"); | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+4068
to
+4074
|
||||||||||||||
| #endif | ||||||||||||||
| return hr; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // SVR | ||||||||||||||
| int ISOSDacInterface8.GetGenerationTableSvr(ClrDataAddress heapAddr, uint cGenerations, /*struct DacpGenerationData*/ void* pGenerationData, uint* pNeeded) | ||||||||||||||
| => _legacyImpl8 is not null ? _legacyImpl8.GetGenerationTableSvr(heapAddr, cGenerations, pGenerationData, pNeeded) : HResults.E_NOTIMPL; | ||||||||||||||
| { | ||||||||||||||
| if (heapAddr == 0 || (cGenerations > 0 && pGenerationData == null)) | ||||||||||||||
| return HResults.E_INVALIDARG; | ||||||||||||||
|
|
||||||||||||||
| int hr = HResults.S_OK; | ||||||||||||||
| try | ||||||||||||||
| { | ||||||||||||||
| IGC gc = _target.Contracts.GC; | ||||||||||||||
| uint totalGenerationCount = _target.ReadGlobal<uint>(Constants.Globals.TotalGenerationCount); | ||||||||||||||
|
|
||||||||||||||
| if (pNeeded != null) | ||||||||||||||
| *pNeeded = totalGenerationCount; | ||||||||||||||
|
|
||||||||||||||
| if (cGenerations < totalGenerationCount) | ||||||||||||||
| { | ||||||||||||||
| hr = HResults.S_FALSE; | ||||||||||||||
| } | ||||||||||||||
| else | ||||||||||||||
| { | ||||||||||||||
| GCHeapData heapData = gc.GetHeapData(heapAddr.ToTargetPointer(_target)); | ||||||||||||||
| DacpGenerationData* genData = (DacpGenerationData*)pGenerationData; | ||||||||||||||
|
|
||||||||||||||
| for (int i = 0; i < (int)totalGenerationCount && i < heapData.GenerationTable.Count; i++) | ||||||||||||||
| { | ||||||||||||||
| GCGenerationData gen = heapData.GenerationTable[i]; | ||||||||||||||
| genData[i].start_segment = gen.StartSegment.ToClrDataAddress(_target); | ||||||||||||||
| genData[i].allocation_start = gen.AllocationStart.ToClrDataAddress(_target); | ||||||||||||||
| genData[i].allocContextPtr = gen.AllocationContextPointer.ToClrDataAddress(_target); | ||||||||||||||
| genData[i].allocContextLimit = gen.AllocationContextLimit.ToClrDataAddress(_target); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| catch (System.Exception ex) | ||||||||||||||
| { | ||||||||||||||
| hr = ex.HResult; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #if DEBUG | ||||||||||||||
| if (_legacyImpl8 is not null) | ||||||||||||||
| { | ||||||||||||||
| uint pNeededLocal; | ||||||||||||||
| int hrLocal = _legacyImpl8.GetGenerationTableSvr(heapAddr, cGenerations, pGenerationData, &pNeededLocal); | ||||||||||||||
| Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}"); | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+4117
to
+4123
|
||||||||||||||
| #endif | ||||||||||||||
| return hr; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| int ISOSDacInterface8.GetFinalizationFillPointersSvr(ClrDataAddress heapAddr, uint cFillPointers, ClrDataAddress* pFinalizationFillPointers, uint* pNeeded) | ||||||||||||||
| => _legacyImpl8 is not null ? _legacyImpl8.GetFinalizationFillPointersSvr(heapAddr, cFillPointers, pFinalizationFillPointers, pNeeded) : HResults.E_NOTIMPL; | ||||||||||||||
| { | ||||||||||||||
| if (heapAddr == 0 || (cFillPointers > 0 && pFinalizationFillPointers == null)) | ||||||||||||||
| return HResults.E_INVALIDARG; | ||||||||||||||
|
|
||||||||||||||
| int hr = HResults.S_OK; | ||||||||||||||
| try | ||||||||||||||
| { | ||||||||||||||
| IGC gc = _target.Contracts.GC; | ||||||||||||||
| GCHeapData heapData = gc.GetHeapData(heapAddr.ToTargetPointer(_target)); | ||||||||||||||
| uint numFillPointers = (uint)heapData.FillPointers.Count; | ||||||||||||||
|
|
||||||||||||||
| if (pNeeded != null) | ||||||||||||||
| *pNeeded = numFillPointers; | ||||||||||||||
|
|
||||||||||||||
| if (cFillPointers < numFillPointers) | ||||||||||||||
| { | ||||||||||||||
| hr = HResults.S_FALSE; | ||||||||||||||
| } | ||||||||||||||
| else | ||||||||||||||
| { | ||||||||||||||
| for (int i = 0; i < (int)numFillPointers; i++) | ||||||||||||||
| { | ||||||||||||||
| pFinalizationFillPointers[i] = heapData.FillPointers[i].ToClrDataAddress(_target); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| catch (System.Exception ex) | ||||||||||||||
| { | ||||||||||||||
| hr = ex.HResult; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #if DEBUG | ||||||||||||||
| if (_legacyImpl8 is not null) | ||||||||||||||
| { | ||||||||||||||
| uint pNeededLocal; | ||||||||||||||
| int hrLocal = _legacyImpl8.GetFinalizationFillPointersSvr(heapAddr, cFillPointers, pFinalizationFillPointers, &pNeededLocal); | ||||||||||||||
| Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}"); | ||||||||||||||
|
||||||||||||||
| Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}"); | |
| Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}"); | |
| if (pNeeded is not null) | |
| { | |
| Debug.Assert(*pNeeded == pNeededLocal); | |
| } |
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.
@stephentoub - Since earlier you mentioned context window sizes were a concern, any thoughts on using something like this as a mechanism for progressive discovery? I wanted to add more component-specific guidance (see READMEs elsewhere in this PR as an example) but I didn't want to bloat the context window for prompts working in unrelated parts of the code.