Skip to content

Truly independent web browser

LibGC: Let foreign allocators tag the blocks they allocate

Shannon Booth0eb931b72703
diff --git a/Libraries/LibGC/CAPI.cpp b/Libraries/LibGC/CAPI.cpp
index 4e1f43596b8..8de3c5e502a 100644
--- a/Libraries/LibGC/CAPI.cpp
+++ b/Libraries/LibGC/CAPI.cpp
@@ -66,8 +66,8 @@ static_assert(decode_primitive_storage_handle(GC_PRIMITIVE_STORAGE_NULL_HANDLE).
class CAPICellAllocator final : public CellAllocatorDescriptorBase {
public:
- CAPICellAllocator(CellTypeInfo const& type_info, StringView class_name)
- : CellAllocatorDescriptorBase(type_info, class_name)
+ CAPICellAllocator(CellTypeInfo const& type_info, StringView class_name, void const* foreign_type)
+ : CellAllocatorDescriptorBase(type_info, class_name, foreign_type)
{
}
};
@@ -127,6 +127,8 @@ struct CAPI {
.heap_allocated_bytes_since_last_gc_offset = offsetof(Heap, m_allocated_bytes_since_last_gc),
.heap_gc_bytes_threshold_offset = offsetof(Heap, m_gc_bytes_threshold),
.heap_total_allocated_bytes_offset = offsetof(Heap, m_total_allocated_bytes),
+ .block_size = HeapBlockBase::BLOCK_SIZE,
+ .block_foreign_type_offset = offsetof(HeapBlockBase, m_foreign_type),
.heap_region_offset_mask = HEAP_REGION_OFFSET_MASK,
.primitive_storage_cage_offset_mask = PrimitiveStorage::cage_offset_mask,
};
@@ -316,14 +318,14 @@ void gc_heap_did_free_external_memory(GCHeap* heap, size_t size)
as_heap(heap).did_free_external_memory(size);
}
-GCAllocator* gc_allocator_create(GCCellTypeInfo const* type_info, char const* name, size_t name_length)
+GCAllocator* gc_allocator_create(GCCellTypeInfo const* type_info, char const* name, size_t name_length, void const* foreign_type)
{
VERIFY(type_info->visit_edges);
VERIFY(type_info->cell_size >= HeapBlock::min_possible_cell_size);
VERIFY(type_info->cell_size <= HeapBlock::BLOCK_SIZE - sizeof(HeapBlock));
VERIFY(type_info->alignment <= max_cell_alignment);
VERIFY(type_info->cell_size % type_info->alignment == 0);
- auto* allocator = new CAPICellAllocator(*reinterpret_cast<CellTypeInfo const*>(type_info), StringView { name, name_length });
+ auto* allocator = new CAPICellAllocator(*reinterpret_cast<CellTypeInfo const*>(type_info), StringView { name, name_length }, foreign_type);
return reinterpret_cast<GCAllocator*>(allocator);
}
diff --git a/Libraries/LibGC/CAPI.h b/Libraries/LibGC/CAPI.h
index 59e9ca24fba..1908870c883 100644
--- a/Libraries/LibGC/CAPI.h
+++ b/Libraries/LibGC/CAPI.h
@@ -93,6 +93,10 @@ typedef struct GCLayout {
uint32_t heap_allocated_bytes_since_last_gc_offset;
uint32_t heap_gc_bytes_threshold_offset;
uint32_t heap_total_allocated_bytes_offset;
+ // A cell's block starts at the cell's address rounded down to a multiple of block_size, and holds the foreign
+ // type the cell's allocator was created with at block_foreign_type_offset.
+ uint32_t block_size;
+ uint32_t block_foreign_type_offset;
uint64_t heap_region_offset_mask;
uint64_t primitive_storage_cage_offset_mask;
} GCLayout;
@@ -129,7 +133,8 @@ GC_API void gc_heap_did_allocate_external_memory(GCHeap*, size_t);
GC_API void gc_heap_did_free_external_memory(GCHeap*, size_t);
// The type info and the name must outlive the allocator, and the allocator must outlive every heap it allocated from.
-GC_API GCAllocator* gc_allocator_create(GCCellTypeInfo const*, char const* name, size_t name_length);
+// The blocks of the allocator hold foreign_type, which LibGC never reads.
+GC_API GCAllocator* gc_allocator_create(GCCellTypeInfo const*, char const* name, size_t name_length, void const* foreign_type);
GC_API void gc_allocator_destroy(GCAllocator*);
// Returns uninitialized storage for one cell. *must_mark tells the caller what to store as the cell's mark: cells that
diff --git a/Libraries/LibGC/CellAllocator.h b/Libraries/LibGC/CellAllocator.h
index d873b736fb0..05195a7eafd 100644
--- a/Libraries/LibGC/CellAllocator.h
+++ b/Libraries/LibGC/CellAllocator.h
@@ -34,6 +34,7 @@ public:
Optional<StringView> class_name() const { return m_class_name; }
CellTypeInfo const& type_info() const { return m_type_info; }
size_t cell_size() const { return m_type_info.cell_size; }
+ void const* foreign_type() const { return m_foreign_type; }
CellAllocator& for_heap(Heap&);
@@ -46,15 +47,17 @@ public:
}
protected:
- CellAllocatorDescriptorBase(CellTypeInfo const& type_info, StringView class_name)
+ CellAllocatorDescriptorBase(CellTypeInfo const& type_info, StringView class_name, void const* foreign_type = nullptr)
: m_type_info(type_info)
, m_class_name(class_name)
+ , m_foreign_type(foreign_type)
{
}
private:
CellTypeInfo const& m_type_info;
Optional<StringView> m_class_name;
+ void const* m_foreign_type { nullptr };
Heap* m_last_heap { nullptr };
CellAllocator* m_last_allocator { nullptr };
@@ -73,6 +76,7 @@ public:
Optional<StringView> class_name() const { return m_descriptor.class_name(); }
size_t cell_size() const { return m_descriptor.cell_size(); }
CellTypeInfo const& type_info() const { return m_descriptor.type_info(); }
+ void const* foreign_type() const { return m_descriptor.foreign_type(); }
// Cells come from the free cells that the allocator took from one of its
// blocks (the local block) at once, so allocating is usually just popping
diff --git a/Libraries/LibGC/HeapBlock.cpp b/Libraries/LibGC/HeapBlock.cpp
index 1ac9c05670f..cf2e1494e81 100644
--- a/Libraries/LibGC/HeapBlock.cpp
+++ b/Libraries/LibGC/HeapBlock.cpp
@@ -28,7 +28,7 @@ NonnullOwnPtr<HeapBlock> HeapBlock::create(Heap& heap, CellAllocator& cell_alloc
}
HeapBlock::HeapBlock(Heap& heap, CellAllocator& cell_allocator)
- : HeapBlockBase(heap, cell_allocator.type_info())
+ : HeapBlockBase(heap, cell_allocator.type_info(), cell_allocator.foreign_type())
, m_cell_allocator(cell_allocator)
, m_cell_size(cell_allocator.cell_size())
, m_cell_count((BLOCK_SIZE - sizeof(HeapBlock)) / m_cell_size)
diff --git a/Libraries/LibGC/Internals.h b/Libraries/LibGC/Internals.h
index 8556821c35e..0e8118fa0c8 100644
--- a/Libraries/LibGC/Internals.h
+++ b/Libraries/LibGC/Internals.h
@@ -27,15 +27,23 @@ public:
Heap& heap() { return m_heap; }
CellTypeInfo const& type_info() const { return *m_type_info; }
+ // What the foreign implementation that created the block's allocator says the type of its cells is.
+ void const* foreign_type() const { return m_foreign_type; }
+
protected:
- HeapBlockBase(Heap& heap, CellTypeInfo const& type_info)
+ HeapBlockBase(Heap& heap, CellTypeInfo const& type_info, void const* foreign_type)
: m_heap(heap)
, m_type_info(&type_info)
+ , m_foreign_type(foreign_type)
{
}
Heap& m_heap;
CellTypeInfo const* m_type_info { nullptr };
+ void const* m_foreign_type { nullptr };
+
+private:
+ friend struct CAPI;
};
}
diff --git a/Libraries/LibJS/Rust/src/gc/capi.rs b/Libraries/LibJS/Rust/src/gc/capi.rs
index f7670ee9912..ed8c7191bd7 100644
--- a/Libraries/LibJS/Rust/src/gc/capi.rs
+++ b/Libraries/LibJS/Rust/src/gc/capi.rs
@@ -65,6 +65,8 @@ pub struct GCLayout {
pub heap_allocated_bytes_since_last_gc_offset: u32,
pub heap_gc_bytes_threshold_offset: u32,
pub heap_total_allocated_bytes_offset: u32,
+ pub block_size: u32,
+ pub block_foreign_type_offset: u32,
pub heap_region_offset_mask: u64,
pub primitive_storage_cage_offset_mask: u64,
}
@@ -119,6 +121,7 @@ unsafe extern "C" {
type_info: *const CellTypeInfo,
name: *const c_char,
name_length: usize,
+ foreign_type: *const c_void,
) -> *mut GCAllocator;
pub fn gc_allocator_destroy(allocator: *mut GCAllocator);
pub fn gc_heap_allocate_cell(heap: *mut GCHeap, allocator: *mut GCAllocator, must_mark: *mut bool) -> *mut c_void;
diff --git a/Libraries/LibJS/Rust/src/gc/heap.rs b/Libraries/LibJS/Rust/src/gc/heap.rs
index b7b834b996c..c5d00449b8b 100644
--- a/Libraries/LibJS/Rust/src/gc/heap.rs
+++ b/Libraries/LibJS/Rust/src/gc/heap.rs
@@ -478,14 +478,22 @@ impl Heap {
}
}
- /// Creates an allocator of cells with `type_info`.
+ /// Creates an allocator of cells of `class` with `type_info`, whose blocks hold the class (see `class_of`).
///
/// # Safety
///
- /// The type info and the name must outlive the allocator.
- unsafe fn create_allocator(&self, type_info: *const CellTypeInfo, name: &str) -> HeapAllocator {
- // SAFETY: The caller keeps the type info and the name alive for as long as the allocator.
- let raw = unsafe { capi::gc_allocator_create(type_info, name.as_ptr().cast(), name.len()) };
+ /// The type info must outlive the allocator.
+ unsafe fn create_allocator(&self, type_info: *const CellTypeInfo, class: &'static Class) -> HeapAllocator {
+ let name = class.name;
+ // SAFETY: The caller keeps the type info alive for as long as the allocator, and the class lives forever.
+ let raw = unsafe {
+ capi::gc_allocator_create(
+ type_info,
+ name.as_ptr().cast(),
+ name.len(),
+ core::ptr::from_ref(class).cast(),
+ )
+ };
let raw = NonNull::new(raw).expect("LibGC creates the allocator");
// SAFETY: The heap and the allocator are live.
let local_free_list = unsafe { capi::gc_heap_allocator_local_free_list(self.raw.as_ptr(), raw.as_ptr()) };
@@ -525,7 +533,7 @@ impl Heap {
.borrow_mut()
.entry(core::ptr::from_ref(class) as usize)
// SAFETY: The class and its name live as long as the process, so they outlive the allocator.
- .or_insert_with(|| unsafe { self.create_allocator(&raw const class.type_info, class.name) })
+ .or_insert_with(|| unsafe { self.create_allocator(&raw const class.type_info, class) })
}
/// The size classes of `class`, whose cells have `cell_sizes` bytes, in that order, which the first call for the
@@ -576,7 +584,7 @@ impl Heap {
class,
cell_size: type_info.cell_size,
// SAFETY: The heap destroys the allocator before the type info, and the class name is static.
- allocator: unsafe { self.create_allocator(type_info, class.name) },
+ allocator: unsafe { self.create_allocator(type_info, class) },
})
.collect();
SizeClassTable {
@@ -597,7 +605,7 @@ impl Heap {
class.name
);
// SAFETY: The class and its name are static, so they outlive the allocator.
- let allocator = unsafe { self.create_allocator(&raw const class.type_info, class.name) };
+ let allocator = unsafe { self.create_allocator(&raw const class.type_info, class) };
slot.set(Some(allocator));
allocator
}
diff --git a/Libraries/LibWasm/Engine/src/runtime/heap.rs b/Libraries/LibWasm/Engine/src/runtime/heap.rs
index d138d8a4e5b..7a0824b9f94 100644
--- a/Libraries/LibWasm/Engine/src/runtime/heap.rs
+++ b/Libraries/LibWasm/Engine/src/runtime/heap.rs
@@ -51,6 +51,8 @@ pub struct GcLayout {
pub heap_allocated_bytes_since_last_gc_offset: u32,
pub heap_gc_bytes_threshold_offset: u32,
pub heap_total_allocated_bytes_offset: u32,
+ pub block_size: u32,
+ pub block_foreign_type_offset: u32,
pub heap_region_offset_mask: u64,
pub primitive_storage_cage_offset_mask: u64,
}
@@ -71,8 +73,12 @@ pub struct PrimitiveStorageLayout {
pub struct GcInterface {
pub heap: *mut c_void,
pub gc_get_layout: unsafe extern "C" fn(layout: *mut GcLayout),
- pub gc_allocator_create:
- unsafe extern "C" fn(type_info: *const CellTypeInfo, name: *const c_char, name_length: usize) -> *mut c_void,
+ pub gc_allocator_create: unsafe extern "C" fn(
+ type_info: *const CellTypeInfo,
+ name: *const c_char,
+ name_length: usize,
+ foreign_type: *const c_void,
+ ) -> *mut c_void,
pub gc_heap_allocate_cell:
unsafe extern "C" fn(heap: *mut c_void, allocator: *mut c_void, must_mark: *mut bool) -> *mut c_void,
pub gc_heap_defer_gc: unsafe extern "C" fn(heap: *mut c_void),
@@ -270,7 +276,7 @@ impl Allocators {
fn new(interface: &GcInterface) -> Self {
let create = |cell_type: CellType| {
let (info, name) = type_info(cell_type);
- unsafe { (interface.gc_allocator_create)(info, name.as_ptr().cast(), name.len()) }
+ unsafe { (interface.gc_allocator_create)(info, name.as_ptr().cast(), name.len(), core::ptr::null()) }
};
Self {
objects: (0..OBJECT_DATA_SIZE_CLASSES.len())
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/mod.rs
index 8dee72a5301..c1e50bce5ab 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/mod.rs
@@ -66,7 +66,12 @@ pub(crate) const PAGE_SIZE: u64 = 65536;
unsafe extern "C" {
// The functions of Libraries/LibGC/CAPI.h that the engine uses, with the types of its GcInterface.
fn gc_get_layout(layout: *mut GcLayout);
- fn gc_allocator_create(type_info: *const CellTypeInfo, name: *const c_char, name_length: usize) -> *mut c_void;
+ fn gc_allocator_create(
+ type_info: *const CellTypeInfo,
+ name: *const c_char,
+ name_length: usize,
+ foreign_type: *const c_void,
+ ) -> *mut c_void;
fn gc_heap_allocate_cell(heap: *mut c_void, allocator: *mut c_void, must_mark: *mut bool) -> *mut c_void;
fn gc_heap_defer_gc(heap: *mut c_void);
fn gc_heap_undefer_gc(heap: *mut c_void);