Skip to content

Fix variable shadowing bug in DacFreeRegionEnumerator::AddServerRegions#125068

Merged
max-charlamb merged 2 commits intomainfrom
copilot/fix-variable-shadowing-bug
Mar 3, 2026
Merged

Fix variable shadowing bug in DacFreeRegionEnumerator::AddServerRegions#125068
max-charlamb merged 2 commits intomainfrom
copilot/fix-variable-shadowing-bug

Conversation

Copy link
Contributor

Copilot AI commented Mar 2, 2026

Description

Variable shadowing bug in DacFreeRegionEnumerator::AddServerRegions — the inner loop over count_free_region_kinds reused i as its loop variable, shadowing the outer heap index i. After the inner loop, the AddSegmentList calls for freeable_soh_segment and freeable_uoh_segment passed count_free_region_kinds instead of the heap index.

Before:

for (int i = 0; i < GCHeapCount(); i++)
{
    // ...
    for (int i = 0; i < count_free_region_kinds; i++)  // shadows outer i
        AddSegmentList(heap.free_regions[i].head_free_region, FreeRegionKind::FreeRegion, i);

    AddSegmentList(heap.freeable_soh_segment, FreeRegionKind::FreeSohSegment, i);  // i == count_free_region_kinds, not heap index
    AddSegmentList(heap.freeable_uoh_segment, FreeRegionKind::FreeUohSegment, i);
}

After:

for (int i = 0; i < GCHeapCount(); i++)
{
    // ...
    for (int kind = 0; kind < count_free_region_kinds; kind++)
        AddSegmentList(heap.free_regions[kind].head_free_region, FreeRegionKind::FreeRegion, i);

    AddSegmentList(heap.freeable_soh_segment, FreeRegionKind::FreeSohSegment, i);
    AddSegmentList(heap.freeable_uoh_segment, FreeRegionKind::FreeUohSegment, i);
}

Renamed the inner loop variable to kind. All three AddSegmentList calls now correctly pass the heap index i.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

The inner for loop reused variable name 'i', shadowing the outer heap
index loop variable. This caused AddSegmentList calls to receive
count_free_region_kinds instead of the heap index for freeable_soh/uoh
segments, and the region kind index instead of the heap index for free
regions.

Rename the inner loop variable to 'kind' and ensure all AddSegmentList
calls pass 'i' (the heap index) as the last argument.

Co-authored-by: max-charlamb <44248479+max-charlamb@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix variable shadowing bug in DacFreeRegionEnumerator Fix variable shadowing bug in DacFreeRegionEnumerator::AddServerRegions Mar 2, 2026
@max-charlamb max-charlamb requested a review from leculver March 2, 2026 19:17
@max-charlamb max-charlamb requested a review from noahfalk March 2, 2026 19:17
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag
See info in area-owners.md if you want to be subscribed.

@max-charlamb max-charlamb marked this pull request as ready for review March 2, 2026 20:00
Copilot AI review requested due to automatic review settings March 2, 2026 20:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a variable shadowing bug in DacFreeRegionEnumerator::AddServerRegions in the DAC (Data Access Component) layer used for GC heap inspection. The inner loop variable i shadowed the outer heap-index loop variable i, causing AddSegmentList calls for freeable_soh_segment and freeable_uoh_segment to pass count_free_region_kinds as the heap index instead of the actual heap index. This would corrupt the mem.Heap field in reported SOSMemoryRegion entries, misattributing SOH/UOH freeable segments to the wrong GC heap.

Changes:

  • Renamed the inner loop variable in AddServerRegions from i to kind, eliminating the shadowing and ensuring all three AddSegmentList calls correctly pass the outer heap index i.

@max-charlamb
Copy link
Member

@copilot, can you analyze the CI failures?

@rcj1
Copy link
Contributor

rcj1 commented Mar 3, 2026

@copilot, can you analyze the CI failures?

These seem completely unrelated to DAC things, perhaps copilot will just be able to fix them :)

Copy link
Contributor Author

Copilot AI commented Mar 3, 2026

@copilot, can you analyze the CI failures?

CI has 2 failed jobs out of 80 total (78 passed) on build 1316693. None are related to this PR's change (src/coreclr/debug/daccess/request_svr.cpp):

  1. Libraries Test Run checked coreclr linux x64 ReleaseSystem.Text.Json.Tests failed. Known flaky test tracked by System.Text.Json.Tests fails with GC hole assert #117186, OOM on System.Text.Json.Tests.Utf8JsonWriterTests.WriteRawLargeJsonToStreamWithoutFlushing #88272.

  2. android-x64 Release AllSubsets_CoreCLR_SmokeSystem.Net.Http.Functional.Tests and System.Net.WebSockets.Client.Tests failed. Known Android test issues tracked by [android][clr] No peer certificates when executing System.Net.Http.Functional.Tests on Android emulator #124526 (android HTTP cert failures) and System.Net.WebSockets.Client.Tests android fails #119520 / Android WebSocket failure #121518 (android WebSocket failures).

These failures are in completely unrelated areas (JSON serialization, HTTP networking, WebSockets) and have open "Known Build Error" tracking issues. The Build Analysis check is red because not all failures are automatically matched, but manual inspection confirms they're pre-existing. Safe to retry with /azp run runtime.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • helixr1107v0xdcypoyl9e7f.blob.core.windows.net
    • Triggering command: /usr/bin/pwsh pwsh .github/skills/ci-analysis/scripts/Get-CIStatus.ps1 -BuildId 1316693 -ShowLogs (dns block)
  • https://api.github.com/graphql
    • Triggering command: /usr/bin/gh gh pr view --json number,title,headRefName,url (http block)
    • Triggering command: /usr/bin/gh /usr/bin/gh pr checks 125068 --repo dotnet/runtime (http block)

If you need me to access, download, or install something from one of these locations, you can either:

@max-charlamb
Copy link
Member

/ba-g test failures unrelated to change

@max-charlamb max-charlamb merged commit a8a0309 into main Mar 3, 2026
112 of 116 checks passed
@max-charlamb max-charlamb deleted the copilot/fix-variable-shadowing-bug branch March 3, 2026 15:19
Copilot stopped work on behalf of rcj1 due to an error March 3, 2026 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants