Skip to content

Truly independent web browser

LibGC: Keep both ends of the heap region uncommitted

Andreas Kling1cf2ef5a36d3
diff --git a/Libraries/LibGC/BlockAllocator.cpp b/Libraries/LibGC/BlockAllocator.cpp
index 21f97e91df4..a193c842934 100644
--- a/Libraries/LibGC/BlockAllocator.cpp
+++ b/Libraries/LibGC/BlockAllocator.cpp
@@ -38,8 +38,8 @@ GC_API FlatPtr js_heap_region_base = 0;
namespace GC {
-// Each BlockAllocator gets type-isolated 2 MiB chunks from one process-wide
-// reserved region and carves its 16 KiB HeapBlock slots out of those chunks.
+// Every BlockAllocator takes 2 MiB chunks from one process-wide reserved
+// region, and carves 16 KiB HeapBlock slots out of those chunks.
//
// Per-block madvise() is deferred to a single global background "decommit
// worker" so it never costs us GC pause time, and slots that are recycled
@@ -49,6 +49,8 @@ static constexpr size_t BLOCKS_PER_CHUNK = CHUNK_SIZE / HeapBlock::BLOCK_SIZE;
static_assert((HeapBlock::BLOCK_SIZE & (HeapBlock::BLOCK_SIZE - 1)) == 0);
static_assert(CHUNK_SIZE % HeapBlock::BLOCK_SIZE == 0);
static_assert(BLOCKS_PER_CHUNK == 128);
+static_assert(HEAP_REGION_LOW_GUARD_SIZE % CHUNK_SIZE == 0);
+static_assert(HEAP_REGION_HIGH_GUARD_SIZE % CHUNK_SIZE == 0);
class HeapRegion {
public:
@@ -108,7 +110,7 @@ public:
{
MutexLocker locker(m_mutex);
// NB: Running out of the region is left to allocate_chunk(), on the thread that needs another chunk.
- if (m_prepared_chunk || m_next_chunk_offset > size - CHUNK_SIZE)
+ if (m_prepared_chunk || m_next_chunk_offset > LAST_CHUNK_OFFSET)
return;
chunk = commit_next_chunk();
}
@@ -123,12 +125,23 @@ public:
FlatPtr start() const { return reinterpret_cast<FlatPtr>(m_base); }
FlatPtr end() const { return start() + size; }
+ FlatPtr committed_end()
+ {
+ MutexLocker locker(m_mutex);
+ return start() + m_next_chunk_offset;
+ }
+
private:
static void request_prepared_chunk();
+ // The guards at either end of the region are never committed.
+ static constexpr size_t FIRST_CHUNK_OFFSET = HEAP_REGION_LOW_GUARD_SIZE;
+ static constexpr size_t LAST_CHUNK_OFFSET = size - HEAP_REGION_HIGH_GUARD_SIZE - CHUNK_SIZE;
+
u8* commit_next_chunk()
{
- VERIFY(m_next_chunk_offset <= size - CHUNK_SIZE);
+ VERIFY(m_next_chunk_offset >= FIRST_CHUNK_OFFSET);
+ VERIFY(m_next_chunk_offset <= LAST_CHUNK_OFFSET);
auto* chunk = m_base + m_next_chunk_offset;
MUST(Core::System::commit_memory(chunk, CHUNK_SIZE, Core::System::MemoryTag::GarbageCollector));
m_next_chunk_offset += CHUNK_SIZE;
@@ -137,7 +150,7 @@ private:
u8* m_base { nullptr };
Mutex m_mutex;
- size_t m_next_chunk_offset { 0 };
+ size_t m_next_chunk_offset { FIRST_CHUNK_OFFSET };
// A committed chunk whose pages are faulted in already, which the next allocate_chunk() takes.
u8* m_prepared_chunk { nullptr };
};
@@ -391,6 +404,11 @@ FlatPtr BlockAllocator::heap_region_end()
return heap_region().end();
}
+FlatPtr BlockAllocator::heap_region_committed_end()
+{
+ return heap_region().committed_end();
+}
+
void* BlockAllocator::allocate_block([[maybe_unused]] char const* name)
{
void* block = nullptr;
diff --git a/Libraries/LibGC/BlockAllocator.h b/Libraries/LibGC/BlockAllocator.h
index f53b522ab19..13feceb8450 100644
--- a/Libraries/LibGC/BlockAllocator.h
+++ b/Libraries/LibGC/BlockAllocator.h
@@ -33,6 +33,8 @@ public:
static FlatPtr heap_region_start();
static FlatPtr heap_region_end();
+ // The end of the part of the region that chunks have been committed from so far. No block starts at or above it.
+ static FlatPtr heap_region_committed_end();
// Wake the global decommit worker so it processes any deferred madvise
// work that's piled up. Call this at the end of a GC sweep.
diff --git a/Libraries/LibGC/HeapRegion.h b/Libraries/LibGC/HeapRegion.h
index 8781eacd573..9370c154396 100644
--- a/Libraries/LibGC/HeapRegion.h
+++ b/Libraries/LibGC/HeapRegion.h
@@ -20,6 +20,16 @@ static constexpr size_t HEAP_REGION_SIZE = 32ull * GiB;
static_assert(is_power_of_two(HEAP_REGION_SIZE));
static constexpr size_t HEAP_REGION_OFFSET_MASK = HEAP_REGION_SIZE - 1;
+// The bottom of the region is reserved but never committed, so no cell has an offset below 4 GiB. A compressed cell
+// pointer below 512 Mi then never decodes to a cell. That keeps small integers from looking like one, and also the low
+// half of a pointer to memory less than 512 MiB into any mapping aligned to 4 GiB or more, such as this region itself.
+// Such low halves are all over the stack, so a smaller guard has the conservative scanner keep cells alive for them.
+static constexpr size_t HEAP_REGION_LOW_GUARD_SIZE = 4ull * GiB;
+
+// The top of the region is reserved but never committed either. Decoding any offset lands inside the region, and this
+// keeps a field access through the decoded address from reaching memory past the end of the reservation.
+static constexpr size_t HEAP_REGION_HIGH_GUARD_SIZE = 2ull * MiB;
+
}
extern "C" GC_API FlatPtr js_heap_region_base;
diff --git a/Tests/LibGC/TestBlockAllocator.cpp b/Tests/LibGC/TestBlockAllocator.cpp
index 15f598b5414..9a58186046f 100644
--- a/Tests/LibGC/TestBlockAllocator.cpp
+++ b/Tests/LibGC/TestBlockAllocator.cpp
@@ -5,6 +5,7 @@
*/
#include <AK/Time.h>
+#include <AK/Vector.h>
#include <LibCore/System.h>
#include <LibGC/BlockAllocator.h>
#include <LibGC/HeapBlock.h>
@@ -36,6 +37,32 @@ TEST_CASE(all_blocks_share_one_reserved_region)
second_allocator.deallocate_block(second_block, GC::DeferDecommit::No);
}
+TEST_CASE(blocks_stay_out_of_the_region_guards)
+{
+ GC::BlockAllocator allocator;
+ auto region_start = GC::BlockAllocator::heap_region_start();
+ auto region_end = GC::BlockAllocator::heap_region_end();
+
+ // Enough blocks to take several fresh chunks.
+ static constexpr size_t BLOCK_COUNT = 1024;
+ Vector<void*> blocks;
+ for (size_t i = 0; i < BLOCK_COUNT; ++i)
+ blocks.append(allocator.allocate_block("guards"));
+
+ auto committed_end = GC::BlockAllocator::heap_region_committed_end();
+ EXPECT(committed_end >= region_start + GC::HEAP_REGION_LOW_GUARD_SIZE + BLOCK_COUNT * GC::HeapBlock::BLOCK_SIZE);
+ EXPECT(committed_end <= region_end - GC::HEAP_REGION_HIGH_GUARD_SIZE);
+
+ for (auto* block : blocks) {
+ auto address = reinterpret_cast<FlatPtr>(block);
+ EXPECT(address >= region_start + GC::HEAP_REGION_LOW_GUARD_SIZE);
+ EXPECT(address + GC::HeapBlock::BLOCK_SIZE <= committed_end);
+ }
+
+ for (auto* block : blocks)
+ allocator.deallocate_block(block, GC::DeferDecommit::No);
+}
+
TEST_CASE(decommitted_block_can_be_reused)
{
GC::BlockAllocator allocator;