diff --git a/compiler-rt/lib/scudo/standalone/primary32.h b/compiler-rt/lib/scudo/standalone/primary32.h --- a/compiler-rt/lib/scudo/standalone/primary32.h +++ b/compiler-rt/lib/scudo/standalone/primary32.h @@ -708,8 +708,10 @@ if (AllocatedGroupSize == 0) continue; + // TransferBatches are pushed in fromt of BG.Batches. The first one may + // not have all caches used. const uptr NumBlocks = (BG.Batches.size() - 1) * BG.MaxCachedPerBatch + - BG.Batches.back()->getCount(); + BG.Batches.front()->getCount(); const uptr BytesInBG = NumBlocks * BlockSize; // Given the randomness property, we try to release the pages only if the // bytes used by free blocks exceed certain proportion of allocated diff --git a/compiler-rt/lib/scudo/standalone/primary64.h b/compiler-rt/lib/scudo/standalone/primary64.h --- a/compiler-rt/lib/scudo/standalone/primary64.h +++ b/compiler-rt/lib/scudo/standalone/primary64.h @@ -702,16 +702,22 @@ BG.PushedBlocks - BG.PushedBlocksAtLastCheckpoint; if (PushedBytesDelta * BlockSize < PageSize) continue; - const uptr BatchGroupEnd = - batchGroupBase(BG.GroupId, CompactPtrBase) + GroupSize; - const uptr AllocatedGroupSize = AllocatedUserEnd >= BatchGroupEnd - ? GroupSize - : AllocatedUserEnd - BatchGroupEnd; + // Group boundary is not necessary to have same alignment as Region. It + // may sit across Region boundary. + const uptr BatchGroupBeg = + Max(batchGroupBase(BG.GroupId, CompactPtrBase), Region->RegionBeg); + DCHECK_GE(AllocatedUserEnd, BatchGroupBeg); + const uptr AllocatedGroupSize = + AllocatedUserEnd >= BatchGroupBeg + GroupSize + ? GroupSize + : AllocatedUserEnd - BatchGroupBeg; if (AllocatedGroupSize == 0) continue; + // TransferBatches are pushed in fromt of BG.Batches. The first one may + // not have all caches used. const uptr NumBlocks = (BG.Batches.size() - 1) * BG.MaxCachedPerBatch + - BG.Batches.back()->getCount(); + BG.Batches.front()->getCount(); const uptr BytesInBG = NumBlocks * BlockSize; // Given the randomness property, we try to release the pages only if the // bytes used by free blocks exceed certain proportion of group size. Note