Skip to content

Truly independent web browser

7.6 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/JsonObject.h>
#include <LibGC/Cell.h>
#include <LibGC/CellAllocator.h>
#include <LibGC/DeferGC.h>
#include <LibGC/Heap.h>
#include <LibGC/HeapBlock.h>
#include <LibGC/Root.h>
#include <LibGC/Testing.h>
#include <LibTest/TestCase.h>

namespace {

bool s_cell_that_allocates_when_destroyed_was_destroyed = false;
bool s_reachable_cell_was_destroyed = false;

class CellOfAnotherType final : public GC::Cell {
    GC_CELL(CellOfAnotherType, GC::Cell);
    GC_DECLARE_ALLOCATOR(CellOfAnotherType);

private:
    CellOfAnotherType() = default;

    [[maybe_unused]] FlatPtr m_room_for_a_freelist_entry[2] {};
};

GC_DEFINE_ALLOCATOR(CellOfAnotherType);

CellOfAnotherType* s_cell_of_another_type_allocated_by_a_destructor = nullptr;

class SweptCell final : public GC::Cell {
    GC_CELL(SweptCell, GC::Cell);
    GC_DECLARE_ALLOCATOR(SweptCell);

public:
    enum class Role : u8 {
        AllocatesWhenDestroyed,
        AllocatesCellOfAnotherTypeWhenDestroyed,
        Reachable,
        AllocatedByADestructor,
    };

    virtual ~SweptCell() override
    {
        switch (m_role) {
        case Role::AllocatesWhenDestroyed:
            s_cell_that_allocates_when_destroyed_was_destroyed = true;
            (void)heap().allocate<SweptCell>(Role::AllocatedByADestructor);
            break;
        case Role::AllocatesCellOfAnotherTypeWhenDestroyed:
            s_cell_of_another_type_allocated_by_a_destructor = heap().allocate<CellOfAnotherType>().ptr();
            break;
        case Role::Reachable:
            s_reachable_cell_was_destroyed = true;
            break;
        case Role::AllocatedByADestructor:
            break;
        }
    }

private:
    explicit SweptCell(Role role)
        : m_role(role)
    {
    }

    Role m_role;
    [[maybe_unused]] FlatPtr m_room_for_a_freelist_entry[2] {};
};

GC_DEFINE_ALLOCATOR(SweptCell);

// NB: Both cells come from the same block of a fresh heap, the unreachable one first, so the sweep of that block
//     destroys it before it reaches the reachable one.
NEVER_INLINE GC::Root<SweptCell> allocate_unreachable_then_reachable_cell(GC::Heap& heap)
{
    (void)heap.allocate<SweptCell>(SweptCell::Role::AllocatesWhenDestroyed);
    return GC::make_root(heap.allocate<SweptCell>(SweptCell::Role::Reachable));
}

NEVER_INLINE GC::Root<CellOfAnotherType> allocate_reachable_cell_of_another_type_then_unreachable_cell(GC::Heap& heap)
{
    auto reachable_cell = GC::make_root(heap.allocate<CellOfAnotherType>());
    (void)heap.allocate<SweptCell>(SweptCell::Role::AllocatesCellOfAnotherTypeWhenDestroyed);
    return reachable_cell;
}

}

TEST_CASE(allocating_from_a_destructor_during_a_sweep_does_not_free_reachable_cells_of_the_block)
{
    GC::Heap heap([](auto&) { }, GC::Heap::BecomeThreadDefault::No);

    auto reachable_cell = allocate_unreachable_then_reachable_cell(heap);
    GC::scrub_stack_for_testing();

    // This collection marks the reachable cell and leaves the destruction of the other one to the incremental sweep.
    heap.collect_garbage();
    EXPECT(heap.is_incremental_sweep_active());
    EXPECT(!s_cell_that_allocates_when_destroyed_was_destroyed);

    // Finishing that sweep destroys the unreachable cell, and its allocation starts another collection.
    heap.set_should_collect_on_every_allocation(true);
    heap.collect_garbage();
    heap.set_should_collect_on_every_allocation(false);

    EXPECT(s_cell_that_allocates_when_destroyed_was_destroyed);
    EXPECT(!s_reachable_cell_was_destroyed);
}

TEST_CASE(ending_a_deferral_from_a_destructor_during_a_sweep_does_not_start_a_collection)
{
    GC::Heap heap([](auto&) { }, GC::Heap::BecomeThreadDefault::No);
    heap.enable_stats();
    s_cell_that_allocates_when_destroyed_was_destroyed = false;
    s_reachable_cell_was_destroyed = false;

    auto reachable_cell = allocate_unreachable_then_reachable_cell(heap);
    GC::scrub_stack_for_testing();

    // This collection marks the reachable cell and leaves the destruction of the other one to the incremental sweep.
    heap.collect_garbage();
    EXPECT(heap.is_incremental_sweep_active());
    EXPECT(!s_cell_that_allocates_when_destroyed_was_destroyed);

    // A collection requested under a deferral runs once the deferral ends, and finishes that sweep first. That destroys
    // the unreachable cell, and its allocation ends the deferral that the construction of the new cell starts.
    {
        GC::DeferGC defer_gc { heap };
        heap.collect_garbage();
    }

    EXPECT(s_cell_that_allocates_when_destroyed_was_destroyed);
    EXPECT(!s_reachable_cell_was_destroyed);

    AK::JsonObject stats;
    heap.add_stats(stats);
    EXPECT_EQ(stats.get_u64("gc.collections"sv), 2u);
}

TEST_CASE(free_cells_that_a_destructor_takes_while_a_collection_finishes_the_sweep_go_back_to_their_block)
{
    GC::Heap heap([](auto&) { }, GC::Heap::BecomeThreadDefault::No);

    auto reachable_cell = allocate_reachable_cell_of_another_type_then_unreachable_cell(heap);
    GC::scrub_stack_for_testing();

    // This collection leaves the destruction of the unreachable cell to the incremental sweep.
    heap.collect_garbage();
    EXPECT(heap.is_incremental_sweep_active());

    // The next collection finishes that sweep. The destructor takes the free cells of the block that holds the
    // reachable cell, and the cell it allocates there is unreachable.
    heap.collect_garbage();
    auto* cell_allocated_by_the_destructor = s_cell_of_another_type_allocated_by_a_destructor;
    EXPECT(cell_allocated_by_the_destructor);
    auto& block = *GC::HeapBlock::from_cell(cell_allocated_by_the_destructor);
    EXPECT_EQ(&block, GC::HeapBlock::from_cell(reachable_cell.ptr()));
    EXPECT(block.is_pending_sweep());

    // Sweeping the block, as the sweep timer would, frees that cell. Once no allocator holds free cells of the block,
    // the next cell comes from the free list of the block.
    heap.sweep_block(block);
    auto next_cell = GC::make_root(heap.allocate<CellOfAnotherType>());
    EXPECT_EQ(next_cell.ptr(), cell_allocated_by_the_destructor);
}

TEST_CASE(cells_allocated_into_blocks_pending_sweep_do_not_count_as_live)
{
    GC::Heap heap([](auto&) { }, GC::Heap::BecomeThreadDefault::No);
    heap.enable_stats();

    auto reachable_cell = GC::make_root(heap.allocate<CellOfAnotherType>());
    auto& block = *GC::HeapBlock::from_cell(reachable_cell.ptr());
    GC::scrub_stack_for_testing();

    heap.collect_garbage();
    EXPECT(heap.is_incremental_sweep_active());

    // Under a deferral the allocator takes cells from the block without sweeping it, and they get marked to survive.
    {
        GC::DeferGC defer_gc { heap };
        for (size_t i = 0; i < 16; ++i) {
            auto cell = heap.allocate<CellOfAnotherType>();
            EXPECT_EQ(GC::HeapBlock::from_cell(cell.ptr()), &block);
            EXPECT(block.is_pending_sweep());
        }
    }

    // The next collection finishes that sweep, which records what the collection before it found alive.
    heap.collect_garbage();
    AK::JsonObject stats;
    heap.add_stats(stats);
    EXPECT_EQ(stats.get_u64("gc.live_cell_bytes"sv), block.cell_size());
}

TEST_CASE(collect_everything_leaves_no_live_cell_bytes)
{
    GC::Heap heap([](auto&) { }, GC::Heap::BecomeThreadDefault::No);
    heap.enable_stats();

    {
        auto reachable_cell = GC::make_root(heap.allocate<CellOfAnotherType>());
        GC::scrub_stack_for_testing();
        heap.collect_garbage();
    }

    heap.collect_garbage(GC::Heap::CollectionType::CollectEverything);
    AK::JsonObject stats;
    heap.add_stats(stats);
    EXPECT_EQ(stats.get_u64("gc.live_cell_bytes"sv), 0u);
}