Fix finalizer issue with regions (#54550)
authorPeter Sollich <petersol@microsoft.com>
Wed, 23 Jun 2021 10:09:38 +0000 (12:09 +0200)
committerGitHub <noreply@github.com>
Wed, 23 Jun 2021 10:09:38 +0000 (12:09 +0200)
commitea3f4034314481acde910dc571da5006a571dbfa
tree38d511800d15253c8a9bde43c0a8ae314cd964c1
parent97de5c5aff67892ae66fe65d1da971affe59bd76
Fix finalizer issue with regions (#54550)

This fixes an issue in Server GC where an item in the finalizer queue became stale due to not being relocated.

The problem was that a finalizable object was allocated on one heap, but registered in the finalizer queue of another heap (this is possible due to heap balancing). In CFinalize::UpdatePromotedGenerations, we ask for the generation of an object, and move the object to the correct section of the finalizer queue. In the error case, we obtained the wrong result for the generation of the object because it lived on another heap, and that heap hadn't set the final generation for the region containing the object yet. So we ended up moving the finalizer entry to the section corresponding to gen 2, and missed a relocation of the object occurring in a gen 1 collection afterwards.

Fix: It seems best to make sure an object is always registered for finalization on the heap it's allocated from, so the fix simply fetches the heap from the alloc context after the allocation in the case of SOH, or determines it by calling gc_heap::heap_of in the case of LOH and POH. In the case of SOH, I added an assert to ensure that the heap obtained agrees with the result of calling gc_heap::heap_of.

I also added some dprintf calls to the finalizer logic to aid in future investigations.
src/coreclr/gc/gc.cpp