ladybird/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.h
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

145 lines
5.3 KiB
C++

/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedFlyString.h>
#include <AK/HashMap.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Environment.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
class DeclarativeEnvironment : public Environment {
JS_ENVIRONMENT(DeclarativeEnvironment, Environment);
JS_DECLARE_ALLOCATOR(DeclarativeEnvironment);
struct Binding {
static FlatPtr value_offset() { return OFFSET_OF(Binding, value); }
static FlatPtr initialized_offset() { return OFFSET_OF(Binding, initialized); }
DeprecatedFlyString name;
Value value;
bool strict { false };
bool mutable_ { false };
bool can_be_deleted { false };
bool initialized { false };
};
public:
static DeclarativeEnvironment* create_for_per_iteration_bindings(Badge<ForStatement>, DeclarativeEnvironment& other, size_t bindings_size);
virtual ~DeclarativeEnvironment() override = default;
virtual ThrowCompletionOr<bool> has_binding(DeprecatedFlyString const& name, Optional<size_t>* = nullptr) const override final;
virtual ThrowCompletionOr<void> create_mutable_binding(VM&, DeprecatedFlyString const& name, bool can_be_deleted) override final;
virtual ThrowCompletionOr<void> create_immutable_binding(VM&, DeprecatedFlyString const& name, bool strict) override final;
virtual ThrowCompletionOr<void> initialize_binding(VM&, DeprecatedFlyString const& name, Value, InitializeBindingHint) override final;
virtual ThrowCompletionOr<void> set_mutable_binding(VM&, DeprecatedFlyString const& name, Value, bool strict) override final;
virtual ThrowCompletionOr<Value> get_binding_value(VM&, DeprecatedFlyString const& name, bool strict) override;
virtual ThrowCompletionOr<bool> delete_binding(VM&, DeprecatedFlyString const& name) override;
void initialize_or_set_mutable_binding(Badge<ScopeNode>, VM&, DeprecatedFlyString const& name, Value value);
ThrowCompletionOr<void> initialize_or_set_mutable_binding(VM&, DeprecatedFlyString const& name, Value value);
// This is not a method defined in the spec! Do not use this in any LibJS (or other spec related) code.
[[nodiscard]] Vector<DeprecatedFlyString> bindings() const
{
Vector<DeprecatedFlyString> names;
names.ensure_capacity(m_bindings.size());
for (auto const& binding : m_bindings)
names.unchecked_append(binding.name);
return names;
}
ThrowCompletionOr<void> set_mutable_binding_direct(VM&, size_t index, Value, bool strict);
ThrowCompletionOr<Value> get_binding_value_direct(VM&, size_t index, bool strict);
void shrink_to_fit();
void ensure_capacity(size_t needed_capacity)
{
m_bindings.ensure_capacity(needed_capacity);
}
[[nodiscard]] u64 environment_serial_number() const { return m_environment_serial_number; }
static FlatPtr bindings_offset() { return OFFSET_OF(DeclarativeEnvironment, m_bindings); }
static FlatPtr environment_serial_number_offset() { return OFFSET_OF(DeclarativeEnvironment, m_environment_serial_number); }
private:
ThrowCompletionOr<Value> get_binding_value_direct(VM&, Binding&, bool strict);
ThrowCompletionOr<void> set_mutable_binding_direct(VM&, Binding&, Value, bool strict);
friend Completion dispose_resources(VM&, GCPtr<DeclarativeEnvironment>, Completion);
Vector<DisposableResource> const& disposable_resource_stack() const { return m_disposable_resource_stack; }
protected:
DeclarativeEnvironment();
explicit DeclarativeEnvironment(Environment* parent_environment);
DeclarativeEnvironment(Environment* parent_environment, ReadonlySpan<Binding> bindings);
virtual void visit_edges(Visitor&) override;
class BindingAndIndex {
public:
Binding& binding()
{
if (m_referenced_binding)
return *m_referenced_binding;
return m_temporary_binding;
}
BindingAndIndex(Binding* binding, Optional<size_t> index)
: m_referenced_binding(binding)
, m_index(move(index))
{
}
explicit BindingAndIndex(Binding temporary_binding)
: m_temporary_binding(move(temporary_binding))
{
}
Optional<size_t> const& index() const { return m_index; }
private:
Binding* m_referenced_binding { nullptr };
Binding m_temporary_binding {};
Optional<size_t> m_index;
};
friend class ModuleEnvironment;
virtual Optional<BindingAndIndex> find_binding_and_index(DeprecatedFlyString const& name) const
{
auto it = m_bindings.find_if([&](auto const& binding) {
return binding.name == name;
});
if (it == m_bindings.end())
return {};
return BindingAndIndex { const_cast<Binding*>(&(*it)), it.index() };
}
private:
virtual bool is_declarative_environment() const override { return true; }
Vector<Binding> m_bindings;
Vector<DisposableResource> m_disposable_resource_stack;
u64 m_environment_serial_number { 0 };
};
template<>
inline bool Environment::fast_is<DeclarativeEnvironment>() const { return is_declarative_environment(); }
}