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

59 lines
3 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/Environment.h>
namespace JS {
class GlobalEnvironment final : public Environment {
JS_ENVIRONMENT(GlobalEnvironment, Environment);
JS_DECLARE_ALLOCATOR(GlobalEnvironment);
public:
virtual bool has_this_binding() const final { return true; }
virtual ThrowCompletionOr<Value> get_this_binding(VM&) const final;
virtual ThrowCompletionOr<bool> has_binding(DeprecatedFlyString const& name, Optional<size_t>* = nullptr) const override;
virtual ThrowCompletionOr<void> create_mutable_binding(VM&, DeprecatedFlyString const& name, bool can_be_deleted) override;
virtual ThrowCompletionOr<void> create_immutable_binding(VM&, DeprecatedFlyString const& name, bool strict) override;
virtual ThrowCompletionOr<void> initialize_binding(VM&, DeprecatedFlyString const& name, Value, Environment::InitializeBindingHint) override;
virtual ThrowCompletionOr<void> set_mutable_binding(VM&, DeprecatedFlyString const& name, Value, bool strict) override;
virtual ThrowCompletionOr<Value> get_binding_value(VM&, DeprecatedFlyString const& name, bool strict) override;
virtual ThrowCompletionOr<bool> delete_binding(VM&, DeprecatedFlyString const& name) override;
ObjectEnvironment& object_record() { return *m_object_record; }
Object& global_this_value() { return *m_global_this_value; }
DeclarativeEnvironment& declarative_record() { return *m_declarative_record; }
bool has_var_declaration(DeprecatedFlyString const& name) const;
bool has_lexical_declaration(DeprecatedFlyString const& name) const;
ThrowCompletionOr<bool> has_restricted_global_property(DeprecatedFlyString const& name) const;
ThrowCompletionOr<bool> can_declare_global_var(DeprecatedFlyString const& name) const;
ThrowCompletionOr<bool> can_declare_global_function(DeprecatedFlyString const& name) const;
ThrowCompletionOr<void> create_global_var_binding(DeprecatedFlyString const& name, bool can_be_deleted);
ThrowCompletionOr<void> create_global_function_binding(DeprecatedFlyString const& name, Value, bool can_be_deleted);
static FlatPtr object_record_offset() { return OFFSET_OF(GlobalEnvironment, m_object_record); }
static FlatPtr declarative_record_offset() { return OFFSET_OF(GlobalEnvironment, m_declarative_record); }
private:
GlobalEnvironment(Object&, Object& this_value);
virtual bool is_global_environment() const override { return true; }
virtual void visit_edges(Visitor&) override;
GCPtr<ObjectEnvironment> m_object_record; // [[ObjectRecord]]
GCPtr<Object> m_global_this_value; // [[GlobalThisValue]]
GCPtr<DeclarativeEnvironment> m_declarative_record; // [[DeclarativeRecord]]
Vector<DeprecatedFlyString> m_var_names; // [[VarNames]]
};
template<>
inline bool Environment::fast_is<GlobalEnvironment>() const { return is_global_environment(); }
}