Skip to content

Commit

Permalink
Fix a bug in finaliser handling (#1908)
Browse files Browse the repository at this point in the history
Previously, the finaliser bitmap was being checked against the slot
bitmap, and if the slot had not been freed, the loop continued. This
resulted in an infinite loop for a small chunk with allocations that
were both still in use and had finalisers. This change ANDs the
finaliser bitmap with the slot bitmap before running finalisers,
fixing this problem.
  • Loading branch information
sylvanc authored May 14, 2017
1 parent 494a67f commit 2e6974a
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/libponyrt/mem/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ static void final_small(chunk_t* chunk, uint32_t mark)
uint64_t bit = 0;

// if there's a finaliser to run for a used slot
while((finalisers != 0) && (0 != (bit = __pony_ctzl(finalisers)))) {
while((finalisers != 0) && (0 != (bit = __pony_ctzl(finalisers))))
{
p = chunk->m + (bit << HEAP_MINBITS);

// run finaliser
Expand All @@ -102,24 +103,19 @@ static void final_small_freed(chunk_t* chunk)
// run any finalisers that need to be run for any newly freed slots
void* p = NULL;

uint32_t finalisers = chunk->finalisers;
uint32_t finalisers = chunk->finalisers & chunk->slots;
chunk->finalisers = chunk->finalisers & ~chunk->slots;
uint64_t bit = 0;

// if there's a finaliser to run for a used slot
while((finalisers != 0) && (0 != (bit = __pony_ctzl(finalisers)))) {
// nothing to do if the slot isn't empty
if((chunk->slots & (1 << bit)) == 0)
continue;

while((finalisers != 0) && (0 != (bit = __pony_ctzl(finalisers))))
{
p = chunk->m + (bit << HEAP_MINBITS);

// run finaliser
pony_assert((*(pony_type_t**)p)->final != NULL);
(*(pony_type_t**)p)->final(p);

// clear finaliser in chunk
chunk->finalisers &= ~(1 << bit);

// clear bit just found in our local finaliser map
finalisers &= (finalisers - 1);
}
Expand Down

0 comments on commit 2e6974a

Please sign in to comment.