
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.
75 lines
2.5 KiB
C++
75 lines
2.5 KiB
C++
/*
|
|
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Badge.h>
|
|
#include <AK/OwnPtr.h>
|
|
#include <AK/StringView.h>
|
|
#include <AK/Weakable.h>
|
|
#include <LibJS/Bytecode/Builtins.h>
|
|
#include <LibJS/Heap/Cell.h>
|
|
#include <LibJS/Heap/CellAllocator.h>
|
|
#include <LibJS/Runtime/Intrinsics.h>
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
namespace JS {
|
|
|
|
// 9.3 Realms, https://tc39.es/ecma262/#realm-record
|
|
class Realm final
|
|
: public Cell
|
|
, public Weakable<Realm> {
|
|
JS_CELL(Realm, Cell);
|
|
JS_DECLARE_ALLOCATOR(Realm);
|
|
|
|
public:
|
|
struct HostDefined {
|
|
virtual ~HostDefined() = default;
|
|
|
|
virtual void visit_edges(Cell::Visitor&) { }
|
|
};
|
|
|
|
static ThrowCompletionOr<NonnullGCPtr<Realm>> create(VM&);
|
|
static ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> initialize_host_defined_realm(VM&, Function<Object*(Realm&)> create_global_object, Function<Object*(Realm&)> create_global_this_value);
|
|
|
|
void set_global_object(Object* global_object, Object* this_value);
|
|
|
|
[[nodiscard]] Object& global_object() const { return *m_global_object; }
|
|
[[nodiscard]] GlobalEnvironment& global_environment() const { return *m_global_environment; }
|
|
|
|
[[nodiscard]] Intrinsics const& intrinsics() const { return *m_intrinsics; }
|
|
[[nodiscard]] Intrinsics& intrinsics() { return *m_intrinsics; }
|
|
void set_intrinsics(Badge<Intrinsics>, Intrinsics& intrinsics)
|
|
{
|
|
VERIFY(!m_intrinsics);
|
|
m_intrinsics = &intrinsics;
|
|
}
|
|
|
|
HostDefined* host_defined() { return m_host_defined; }
|
|
void set_host_defined(OwnPtr<HostDefined> host_defined) { m_host_defined = move(host_defined); }
|
|
|
|
void define_builtin(Bytecode::Builtin builtin, Value value)
|
|
{
|
|
m_builtins[to_underlying(builtin)] = value;
|
|
}
|
|
|
|
static FlatPtr global_environment_offset() { return OFFSET_OF(Realm, m_global_environment); }
|
|
static FlatPtr builtins_offset() { return OFFSET_OF(Realm, m_builtins); }
|
|
|
|
private:
|
|
Realm() = default;
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
GCPtr<Intrinsics> m_intrinsics; // [[Intrinsics]]
|
|
GCPtr<Object> m_global_object; // [[GlobalObject]]
|
|
GCPtr<GlobalEnvironment> m_global_environment; // [[GlobalEnv]]
|
|
OwnPtr<HostDefined> m_host_defined; // [[HostDefined]]
|
|
AK::Array<Value, to_underlying(Bytecode::Builtin::__Count)> m_builtins;
|
|
};
|
|
|
|
}
|