ladybird/Userland/Libraries/LibJS/Runtime/FinalizationRegistry.cpp
Andreas Kling 3c74dc9f4d LibJS: Segregate GC-allocated objects by type
This patch adds two macros to declare per-type allocators:

- JS_DECLARE_ALLOCATOR(TypeName)
- JS_DEFINE_ALLOCATOR(TypeName)

When used, they add a type-specific CellAllocator that the Heap will
delegate allocation requests to.

The result of this is that GC objects of the same type always end up
within the same HeapBlock, drastically reducing the ability to perform
type confusion attacks.

It also improves HeapBlock utilization, since each block now has cells
sized exactly to the type used within that block. (Previously we only
had a handful of block sizes available, and most GC allocations ended
up with a large amount of slack in their tails.)

There is a small performance hit from this, but I'm sure we can make
up for it elsewhere.

Note that the old size-based allocators still exist, and we fall back
to them for any type that doesn't have its own CellAllocator.
2023-11-19 12:10:31 +01:00

104 lines
3.5 KiB
C++

/*
* Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/FinalizationRegistry.h>
namespace JS {
JS_DEFINE_ALLOCATOR(FinalizationRegistry);
FinalizationRegistry::FinalizationRegistry(Realm& realm, JobCallback cleanup_callback, Object& prototype)
: Object(ConstructWithPrototypeTag::Tag, prototype)
, WeakContainer(heap())
, m_realm(realm)
, m_cleanup_callback(move(cleanup_callback))
{
}
void FinalizationRegistry::add_finalization_record(Cell& target, Value held_value, Cell* unregister_token)
{
VERIFY(!held_value.is_empty());
m_records.append({ &target, held_value, unregister_token });
}
// Extracted from FinalizationRegistry.prototype.unregister ( unregisterToken )
bool FinalizationRegistry::remove_by_token(Cell& unregister_token)
{
// 4. Let removed be false.
auto removed = false;
// 5. For each Record { [[WeakRefTarget]], [[HeldValue]], [[UnregisterToken]] } cell of finalizationRegistry.[[Cells]], do
for (auto it = m_records.begin(); it != m_records.end(); ++it) {
// a. If cell.[[UnregisterToken]] is not empty and SameValue(cell.[[UnregisterToken]], unregisterToken) is true, then
if (it->unregister_token == &unregister_token) {
// i. Remove cell from finalizationRegistry.[[Cells]].
it.remove(m_records);
// ii. Set removed to true.
removed = true;
}
}
// 6. Return removed.
return removed;
}
void FinalizationRegistry::remove_dead_cells(Badge<Heap>)
{
auto any_cells_were_removed = false;
for (auto& record : m_records) {
if (!record.target || record.target->state() == Cell::State::Live)
continue;
record.target = nullptr;
any_cells_were_removed = true;
break;
}
if (any_cells_were_removed)
vm().host_enqueue_finalization_registry_cleanup_job(*this);
}
// 9.13 CleanupFinalizationRegistry ( finalizationRegistry ), https://tc39.es/ecma262/#sec-cleanup-finalization-registry
ThrowCompletionOr<void> FinalizationRegistry::cleanup(Optional<JobCallback> callback)
{
auto& vm = this->vm();
// 1. Assert: finalizationRegistry has [[Cells]] and [[CleanupCallback]] internal slots.
// Note: Ensured by type.
// 2. Let callback be finalizationRegistry.[[CleanupCallback]].
auto& cleanup_callback = callback.has_value() ? callback.value() : m_cleanup_callback;
// 3. While finalizationRegistry.[[Cells]] contains a Record cell such that cell.[[WeakRefTarget]] is empty, an implementation may perform the following steps:
for (auto it = m_records.begin(); it != m_records.end(); ++it) {
// a. Choose any such cell.
if (it->target != nullptr)
continue;
// b. Remove cell from finalizationRegistry.[[Cells]].
MarkedVector<Value> arguments(vm.heap());
arguments.append(it->held_value);
it.remove(m_records);
// c. Perform ? HostCallJobCallback(callback, undefined, « cell.[[HeldValue]] »).
TRY(vm.host_call_job_callback(cleanup_callback, js_undefined(), move(arguments)));
}
// 4. Return unused.
return {};
}
void FinalizationRegistry::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_realm);
for (auto& record : m_records) {
visitor.visit(record.held_value);
visitor.visit(record.unregister_token);
}
}
}