GeezerVison is showing github.com — Fix Arena::merge returning already-allocated memory from an empty des… · meta/folly@7737b3a
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: folly/memory/Arena-inl.h +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,8 +71,14 @@ void Arena<Alloc>::merge(Arena<Alloc>&& other) { |
71 | 71 | FOLLY_SAFE_CHECK( |
72 | 72 | blockGoodAllocSize() == other.blockGoodAllocSize(), |
73 | 73 | "cannot merge arenas of different minBlockSize"); |
| 74 | + const bool wasEmpty = blocks_.empty(); |
74 | 75 | blocks_.splice_after(blocks_.before_begin(), other.blocks_); |
75 | 76 | other.blocks_.clear(); |
| 77 | + if (wasEmpty) { |
| 78 | + // currentBlock_ is still before_begin(); leaving it there would let |
| 79 | + // canReuseExistingBlock() bump-allocate into a merged block. |
| 80 | + currentBlock_ = blocks_.last(); |
| 81 | + } |
76 | 82 | largeBlocks_.splice_after(largeBlocks_.before_begin(), other.largeBlocks_); |
77 | 83 | other.largeBlocks_.clear(); |
78 | 84 | other.ptr_ = other.end_ = nullptr; |
|
Collapse file
Copy file name to clipboardExpand all lines: folly/memory/test/ArenaTest.cpp +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -374,6 +374,21 @@ TEST(Arena, Merge) { |
374 | 374 | EXPECT_EQ(0, arena2.bytesUsed()); |
375 | 375 | } |
376 | 376 |
|
| 377 | +TEST(Arena, MergeIntoUntouchedArena) { |
| 378 | + SysArena src; |
| 379 | + auto* live = static_cast<char*>(alloc(src, 1)); |
| 380 | + *live = 'x'; |
| 381 | + |
| 382 | + // dst has never allocated, so its currentBlock_ is still before_begin(). |
| 383 | + SysArena dst; |
| 384 | + dst.merge(std::move(src)); |
| 385 | + |
| 386 | + // The merged block holds live memory; it must not be handed out again, and |
| 387 | + // the allocation below (which fills with 0xff) must not land on top of it. |
| 388 | + EXPECT_NE(live, alloc(dst, 1)); |
| 389 | + EXPECT_EQ('x', *live); |
| 390 | +} |
| 391 | + |
377 | 392 | int main(int argc, char* argv[]) { |
378 | 393 | testing::InitGoogleTest(&argc, argv); |
379 | 394 | folly::gflags::ParseCommandLineFlags(&argc, &argv, true); |
|