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

53 lines
1.6 KiB
C++

/*
* Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/NonnullRefPtr.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibJS/Heap/Handle.h>
#include <LibJS/ParserError.h>
#include <LibJS/Runtime/Realm.h>
namespace JS {
// 16.1.4 Script Records, https://tc39.es/ecma262/#sec-script-records
class Script final : public Cell {
JS_CELL(Script, Cell);
JS_DECLARE_ALLOCATOR(Script);
public:
struct HostDefined {
virtual ~HostDefined() = default;
virtual void visit_host_defined_self(Cell::Visitor&) = 0;
};
virtual ~Script() override;
static Result<NonnullGCPtr<Script>, Vector<ParserError>> parse(StringView source_text, Realm&, StringView filename = {}, HostDefined* = nullptr, size_t line_number_offset = 1);
Realm& realm() { return *m_realm; }
Program const& parse_node() const { return *m_parse_node; }
Vector<ModuleWithSpecifier> const& loaded_modules() const { return m_loaded_modules; }
HostDefined* host_defined() const { return m_host_defined; }
StringView filename() const { return m_filename; }
private:
Script(Realm&, StringView filename, NonnullRefPtr<Program>, HostDefined* = nullptr);
virtual void visit_edges(Cell::Visitor&) override;
GCPtr<Realm> m_realm; // [[Realm]]
NonnullRefPtr<Program> m_parse_node; // [[ECMAScriptCode]]
Vector<ModuleWithSpecifier> m_loaded_modules; // [[LoadedModules]]
// Needed for potential lookups of modules.
DeprecatedString m_filename;
HostDefined* m_host_defined { nullptr }; // [[HostDefined]]
};
}