Skip to content

Truly independent web browser

LibGC: Discard freed heap blocks without a region query on Windows

Aliaksandr Kalenik410f54abe277
diff --git a/Libraries/LibGC/BlockAllocator.cpp b/Libraries/LibGC/BlockAllocator.cpp
index 31eac42b505..21f97e91df4 100644
--- a/Libraries/LibGC/BlockAllocator.cpp
+++ b/Libraries/LibGC/BlockAllocator.cpp
@@ -151,11 +151,15 @@ static HeapRegion& heap_region()
static void madvise_block_for_decommit(void* block)
{
#if defined(AK_OS_WINDOWS)
- DWORD ret = DiscardVirtualMemory(block, HeapBlock::BLOCK_SIZE);
- if (ret != ERROR_SUCCESS) {
- warnln("{}", Error::from_windows_error(ret));
+ // NB: DiscardVirtualMemory() validates its range with VirtualQuery(), which makes the kernel check every page in
+ // the run of identical pages that starts at the block. All chunks are committed back to back in one
+ // reservation, so that run is the whole committed heap. MEM_RESET drops the contents without that walk, and
+ // VirtualUnlock() on pages that are not locked takes them out of the working set right away.
+ if (!VirtualAlloc(block, HeapBlock::BLOCK_SIZE, MEM_RESET, PAGE_READWRITE)) {
+ warnln("{}", Error::from_windows_error());
VERIFY_NOT_REACHED();
}
+ (void)VirtualUnlock(block, HeapBlock::BLOCK_SIZE);
#elif defined(MADV_FREE_REUSE) && defined(MADV_FREE_REUSABLE)
// macOS uses the FREE_REUSABLE/FREE_REUSE paired protocol, which integrates
// with its RSS accounting properly.
diff --git a/Tests/LibGC/TestBlockAllocator.cpp b/Tests/LibGC/TestBlockAllocator.cpp
index 11b463e3bc4..15f598b5414 100644
--- a/Tests/LibGC/TestBlockAllocator.cpp
+++ b/Tests/LibGC/TestBlockAllocator.cpp
@@ -4,6 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Time.h>
+#include <LibCore/System.h>
#include <LibGC/BlockAllocator.h>
#include <LibGC/HeapBlock.h>
#include <LibGC/HeapRegion.h>
@@ -33,3 +35,36 @@ TEST_CASE(all_blocks_share_one_reserved_region)
first_allocator.deallocate_block(first_block, GC::DeferDecommit::No);
second_allocator.deallocate_block(second_block, GC::DeferDecommit::No);
}
+
+TEST_CASE(decommitted_block_can_be_reused)
+{
+ GC::BlockAllocator allocator;
+
+ auto* block = static_cast<u8*>(allocator.allocate_block("decommit"));
+ for (size_t i = 0; i < GC::HeapBlock::BLOCK_SIZE; ++i)
+ block[i] = 0xab;
+ auto block_count_before_free = allocator.block_count();
+
+ allocator.deallocate_block(block, GC::DeferDecommit::Yes);
+ GC::BlockAllocator::wake_decommit_worker_async();
+
+ // The decommit worker returns the block to the reusable pool once it has released the block's pages.
+ auto deadline = MonotonicTime::now() + AK::Duration::from_seconds(10);
+ while (allocator.block_count() == block_count_before_free && MonotonicTime::now() < deadline)
+ (void)Core::System::sleep_ms(10);
+ EXPECT_EQ(allocator.block_count(), block_count_before_free + 1);
+
+ auto* reused_block = static_cast<u8*>(allocator.allocate_block("decommit"));
+ EXPECT_EQ(reused_block, block);
+
+ for (size_t i = 0; i < GC::HeapBlock::BLOCK_SIZE; ++i)
+ reused_block[i] = static_cast<u8>(i);
+ bool contents_match = true;
+ for (size_t i = 0; i < GC::HeapBlock::BLOCK_SIZE; ++i) {
+ if (reused_block[i] != static_cast<u8>(i))
+ contents_match = false;
+ }
+ EXPECT(contents_match);
+
+ allocator.deallocate_block(reused_block, GC::DeferDecommit::No);
+}