Skip to content

ICorDebugProcess5::EnumerateHeapRegions returned a COR_SEGMENT that grow backwards under USE_REGIONS #56653

Closed

Description

@cshung

I attempted to enumerate the POH regions using the API, but the API gave me a COR_SEGMENT structure with start >= end, which does not make sense.

Activity

  1. added
    untriaged
    on Jul 31, 2021
  2. josalem commented on Aug 2, 2021

    @josalem
    Contributor
  3. davmason commented on Aug 2, 2021

    @davmason
    Contributor

    The heap data parsing happens in

    HRESULT DacDbiInterfaceImpl::GetHeapSegments(OUT DacDbiArrayList<COR_SEGMENT> *pSegments)
    {
    DD_ENTER_MAY_THROW;
    size_t heapCount = 0;
    HeapData *heaps = 0;
    #ifdef FEATURE_SVR_GC
    HRESULT hr = GCHeapUtilities::IsServerHeap() ? DacHeapWalker::InitHeapDataSvr(heaps, heapCount) : DacHeapWalker::InitHeapDataWks(heaps, heapCount);
    #else
    HRESULT hr = DacHeapWalker::InitHeapDataWks(heaps, heapCount);
    #endif
    NewArrayHolder<HeapData> _heapHolder = heaps;
    // Count the number of segments to know how much to allocate.
    int total = 0;
    for (size_t i = 0; i < heapCount; ++i)
    {
    // SegmentCount is +1 due to the ephemeral segment containing more than one
    // generation (Gen1 + Gen0, and sometimes part of Gen2).
    total += (int)heaps[i].SegmentCount + 1;
    // It's possible that part of Gen2 lives on the ephemeral segment. If so,
    // we need to add one more to the output.
    const size_t eph = heaps[i].EphemeralSegment;
    _ASSERTE(eph < heaps[i].SegmentCount);
    if (heaps[i].Segments[eph].Start != heaps[i].Gen1Start)
    total++;
    }
    pSegments->Alloc(total);
    // Now walk all segments and write them to the array.
    int curr = 0;
    for (size_t i = 0; i < heapCount; ++i)
    {
    // Generation 0 is not in the segment list.
    _ASSERTE(curr < total);
    {
    COR_SEGMENT &seg = (*pSegments)[curr++];
    seg.start = heaps[i].Gen0Start;
    seg.end = heaps[i].Gen0End;
    seg.type = CorDebug_Gen0;
    seg.heap = (ULONG)i;
    }
    for (size_t j = 0; j < heaps[i].SegmentCount; ++j)
    {
    if (heaps[i].Segments[j].Generation == 1)
    {
    // This is the ephemeral segment. We have already written Gen0,
    // now write Gen1.
    _ASSERTE(heaps[i].Segments[j].Start <= heaps[i].Gen1Start);
    _ASSERTE(heaps[i].Segments[j].End > heaps[i].Gen1Start);
    {
    _ASSERTE(curr < total);
    COR_SEGMENT &seg = (*pSegments)[curr++];
    seg.start = heaps[i].Gen1Start;
    seg.end = heaps[i].Gen0Start;
    seg.type = CorDebug_Gen1;
    seg.heap = (ULONG)i;
    }
    // It's possible for Gen2 to take up a portion of the ephemeral segment.
    // We test for that here.
    if (heaps[i].Segments[j].Start != heaps[i].Gen1Start)
    {
    _ASSERTE(curr < total);
    COR_SEGMENT &seg = (*pSegments)[curr++];
    seg.start = heaps[i].Segments[j].Start;
    seg.end = heaps[i].Gen1Start;
    seg.type = CorDebug_Gen2;
    seg.heap = (ULONG)i;
    }
    }
    else
    {
    // Otherwise, we have a gen2 or gen3 (LOH) segment
    _ASSERTE(curr < total);
    COR_SEGMENT &seg = (*pSegments)[curr++];
    seg.start = heaps[i].Segments[j].Start;
    seg.end = heaps[i].Segments[j].End;
    _ASSERTE(heaps[i].Segments[j].Generation <= CorDebug_POH);
    seg.type = (CorDebugGenerationTypes)heaps[i].Segments[j].Generation;
    seg.heap = (ULONG)i;
    }
    }
    }
    _ASSERTE(total == curr);
    return hr;
    }

    It looks like it gets the data from this function:

    HRESULT DacHeapWalker::InitHeapDataWks(HeapData *&pHeaps, size_t &pCount)
    {
    // Scrape basic heap details
    pCount = 1;
    pHeaps = new (nothrow) HeapData[1];
    if (pHeaps == NULL)
    return E_OUTOFMEMORY;
    dac_generation gen0 = *GenerationTableIndex(g_gcDacGlobals->generation_table, 0);
    dac_generation gen1 = *GenerationTableIndex(g_gcDacGlobals->generation_table, 1);
    dac_generation gen2 = *GenerationTableIndex(g_gcDacGlobals->generation_table, 2);
    dac_generation loh = *GenerationTableIndex(g_gcDacGlobals->generation_table, 3);
    dac_generation poh = *GenerationTableIndex(g_gcDacGlobals->generation_table, 4);
    pHeaps[0].YoungestGenPtr = (CORDB_ADDRESS)gen0.allocation_context.alloc_ptr;
    pHeaps[0].YoungestGenLimit = (CORDB_ADDRESS)gen0.allocation_context.alloc_limit;
    pHeaps[0].Gen0Start = (CORDB_ADDRESS)gen0.allocation_start;
    pHeaps[0].Gen0End = (CORDB_ADDRESS)*g_gcDacGlobals->alloc_allocated;
    pHeaps[0].Gen1Start = (CORDB_ADDRESS)gen1.allocation_start;
    // Segments
    int count = GetSegmentCount(loh.start_segment);
    count += GetSegmentCount(poh.start_segment);
    count += GetSegmentCount(gen2.start_segment);
    pHeaps[0].SegmentCount = count;
    pHeaps[0].Segments = new (nothrow) SegmentData[count];
    if (pHeaps[0].Segments == NULL)
    return E_OUTOFMEMORY;
    // Small object heap segments
    DPTR(dac_heap_segment) seg = gen2.start_segment;
    int i = 0;
    for (; seg && (i < count); ++i)
    {
    pHeaps[0].Segments[i].Start = (CORDB_ADDRESS)seg->mem;
    if (seg.GetAddr() == (TADDR)*g_gcDacGlobals->ephemeral_heap_segment)
    {
    pHeaps[0].Segments[i].End = (CORDB_ADDRESS)*g_gcDacGlobals->alloc_allocated;
    pHeaps[0].Segments[i].Generation = 1;
    pHeaps[0].EphemeralSegment = i;
    }
    else
    {
    pHeaps[0].Segments[i].End = (CORDB_ADDRESS)seg->allocated;
    pHeaps[0].Segments[i].Generation = 2;
    }
    seg = seg->next;
    }
    // Large object heap segments
    seg = loh.start_segment;
    for (; seg && (i < count); ++i)
    {
    pHeaps[0].Segments[i].Generation = 3;
    pHeaps[0].Segments[i].Start = (CORDB_ADDRESS)seg->mem;
    pHeaps[0].Segments[i].End = (CORDB_ADDRESS)seg->allocated;
    seg = seg->next;
    }
    // Pinned object heap segments
    seg = poh.start_segment;
    for (; seg && (i < count); ++i)
    {
    pHeaps[0].Segments[i].Generation = 4;
    pHeaps[0].Segments[i].Start = (CORDB_ADDRESS)seg->mem;
    pHeaps[0].Segments[i].End = (CORDB_ADDRESS)seg->allocated;
    seg = seg->next;
    }
    return S_OK;
    }

    Specifically the line dac_generation poh = *GenerationTableIndex(g_gcDacGlobals->generation_table, 4); Seems suspicious. Is this the same issue that we saw for the profiler, where g_gcDacGlobals is wrong for regions?

  4. removed
    untriaged
    on Aug 2, 2021
  5. ghost added
    in-pr
    on Aug 3, 2021
  6. added this to the 6.0.0 milestone on Aug 16, 2021
  7. ghost removed
    in-pr
    on Aug 17, 2021
  8. ghost locked as resolved and limited conversation to collaborators on Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions