Skip to content

Truly independent web browser

LibJS: Call LibGC's heap functions through a reference to the heap

Andreas Kling7dda0d681522
diff --git a/Libraries/LibJS/Rust/src/gc/capi.rs b/Libraries/LibJS/Rust/src/gc/capi.rs
index beb38e39f30..536fceb188d 100644
--- a/Libraries/LibJS/Rust/src/gc/capi.rs
+++ b/Libraries/LibJS/Rust/src/gc/capi.rs
@@ -10,9 +10,12 @@ use core::ffi::{c_char, c_int, c_void};
use super::class::CellTypeInfo;
+/// LibGC's heap, which Rust only ever refers to. It has no size and is not Unpin, so a reference to it claims nothing
+/// about the memory LibGC changes behind it.
#[repr(C)]
pub struct GCHeap {
_private: [u8; 0],
+ _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}
#[repr(C)]
@@ -80,7 +83,7 @@ pub type GCCallback = unsafe extern "C" fn(context: *mut c_void);
pub type GCGatherRootsCallback = unsafe extern "C" fn(context: *mut c_void, root_visitor: *mut GCVisitor);
unsafe extern "C" {
- pub fn gc_get_layout(layout: *mut GCLayout);
+ pub safe fn gc_get_layout(layout: &mut GCLayout);
pub safe fn gc_heap_region_base() -> usize;
pub safe fn gc_primitive_storage_cage_base() -> usize;
@@ -90,19 +93,20 @@ unsafe extern "C" {
become_process_default: bool,
) -> *mut GCHeap;
pub fn gc_heap_destroy(heap: *mut GCHeap);
- pub fn gc_heap_collect_garbage(heap: *mut GCHeap, collection_type: i32, print_report: bool);
- pub fn gc_heap_is_collecting_everything(heap: *const GCHeap) -> bool;
- pub fn gc_heap_defer_gc(heap: *mut GCHeap);
- pub fn gc_heap_undefer_gc(heap: *mut GCHeap);
- pub fn gc_heap_should_collect_on_every_allocation(heap: *const GCHeap) -> bool;
- pub fn gc_heap_set_should_collect_on_every_allocation(heap: *mut GCHeap, should_collect: bool);
- pub fn gc_heap_set_incremental_sweep_enabled(heap: *mut GCHeap, enabled: bool);
+ // NB: The functions that take a reference to the heap have no other preconditions.
+ pub safe fn gc_heap_collect_garbage(heap: &GCHeap, collection_type: i32, print_report: bool);
+ pub safe fn gc_heap_is_collecting_everything(heap: &GCHeap) -> bool;
+ pub safe fn gc_heap_defer_gc(heap: &GCHeap);
+ pub safe fn gc_heap_undefer_gc(heap: &GCHeap);
+ pub safe fn gc_heap_should_collect_on_every_allocation(heap: &GCHeap) -> bool;
+ pub safe fn gc_heap_set_should_collect_on_every_allocation(heap: &GCHeap, should_collect: bool);
+ pub safe fn gc_heap_set_incremental_sweep_enabled(heap: &GCHeap, enabled: bool);
pub fn gc_heap_uproot_cell(heap: *mut GCHeap, cell: *mut c_void);
- pub fn gc_heap_stack_bounds(heap: *const GCHeap, base: *mut usize, top: *mut usize);
+ pub safe fn gc_heap_stack_bounds(heap: &GCHeap, base: &mut usize, top: &mut usize);
pub fn gc_heap_enqueue_post_gc_task(heap: *mut GCHeap, callback: GCCallback, context: *mut c_void);
pub fn gc_heap_register_sweep_callback(heap: *mut GCHeap, callback: GCCallback, context: *mut c_void);
- pub fn gc_heap_did_allocate_external_memory(heap: *mut GCHeap, size: usize);
- pub fn gc_heap_did_free_external_memory(heap: *mut GCHeap, size: usize);
+ pub safe fn gc_heap_did_allocate_external_memory(heap: &GCHeap, size: usize);
+ pub safe fn gc_heap_did_free_external_memory(heap: &GCHeap, size: usize);
pub fn gc_allocator_create(
type_info: *const CellTypeInfo,
@@ -125,7 +129,7 @@ unsafe extern "C" {
pub fn gc_root_cell(root: *const GCRoot) -> *mut c_void;
pub fn gc_heap_create_weak_impl(heap: *mut GCHeap, cell: *mut c_void) -> *mut GCWeakImpl;
- pub fn gc_weak_impl_null() -> *mut GCWeakImpl;
+ pub safe fn gc_weak_impl_null() -> *mut GCWeakImpl;
pub fn gc_weak_impl_ref(weak_impl: *mut GCWeakImpl);
pub fn gc_weak_impl_unref(weak_impl: *mut GCWeakImpl);
diff --git a/Libraries/LibJS/Rust/src/gc/heap.rs b/Libraries/LibJS/Rust/src/gc/heap.rs
index 00be6f299fe..3a22a005514 100644
--- a/Libraries/LibJS/Rust/src/gc/heap.rs
+++ b/Libraries/LibJS/Rust/src/gc/heap.rs
@@ -179,6 +179,12 @@ impl Heap {
self.raw.as_ptr()
}
+ /// LibGC's heap, for the functions of its C API that only need it to be live.
+ pub fn handle(&self) -> &GCHeap {
+ // SAFETY: This owns the heap, which stays live for as long as this does.
+ unsafe { self.raw.as_ref() }
+ }
+
/// Moves `cell` into the heap. Nothing can collect garbage between taking the storage and writing the cell, so
/// the collector never sees a partially written cell.
pub fn allocate<T: GcCell>(&self, cell: T) -> Gc<T> {
@@ -464,8 +470,7 @@ impl Heap {
}
pub fn collect_garbage(&self) {
- // SAFETY: The heap is live.
- unsafe { capi::gc_heap_collect_garbage(self.raw.as_ptr(), capi::GC_COLLECTION_TYPE_COLLECT_GARBAGE, false) };
+ capi::gc_heap_collect_garbage(self.handle(), capi::GC_COLLECTION_TYPE_COLLECT_GARBAGE, false);
}
/// Calls `callback` with `context` during every collection, after marking and finalization and before dead cells
@@ -491,13 +496,11 @@ impl Heap {
/// Counts memory a cell allocated outside the heap towards the next collection, which this may start.
pub fn did_allocate_external_memory(&self, size: usize) {
- // SAFETY: The heap is live.
- unsafe { capi::gc_heap_did_allocate_external_memory(self.raw.as_ptr(), size) };
+ capi::gc_heap_did_allocate_external_memory(self.handle(), size);
}
pub fn did_free_external_memory(&self, size: usize) {
- // SAFETY: The heap is live.
- unsafe { capi::gc_heap_did_free_external_memory(self.raw.as_ptr(), size) };
+ capi::gc_heap_did_free_external_memory(self.handle(), size);
}
pub fn account_external_memory_change(&self, old_size: usize, new_size: usize) {
@@ -509,13 +512,11 @@ impl Heap {
}
pub fn should_collect_on_every_allocation(&self) -> bool {
- // SAFETY: The heap is live.
- unsafe { capi::gc_heap_should_collect_on_every_allocation(self.raw.as_ptr()) }
+ capi::gc_heap_should_collect_on_every_allocation(self.handle())
}
pub fn set_should_collect_on_every_allocation(&self, should_collect: bool) {
- // SAFETY: The heap is live.
- unsafe { capi::gc_heap_set_should_collect_on_every_allocation(self.raw.as_ptr(), should_collect) };
+ capi::gc_heap_set_should_collect_on_every_allocation(self.handle(), should_collect);
}
/// Heap::uproot_cell(): has the next collection leave `cell` out of its roots, so that copies of it on the stack or
@@ -529,8 +530,7 @@ impl Heap {
/// The lowest and highest addresses of the stack of the thread that created the heap.
pub fn stack_bounds(&self) -> (usize, usize) {
let (mut base, mut top) = (0, 0);
- // SAFETY: The heap is live.
- unsafe { capi::gc_heap_stack_bounds(self.raw.as_ptr(), &raw mut base, &raw mut top) };
+ capi::gc_heap_stack_bounds(self.handle(), &mut base, &mut top);
(base, top)
}
}
@@ -566,8 +566,7 @@ pub fn cell_is_dead<T>(cell: Gc<T>) -> bool {
/// Checks the layouts this crate mirrors against the LibGC it is linked with, and returns LibGC's layout.
fn check_layout() -> GCLayout {
let mut layout = GCLayout::default();
- // SAFETY: LibGC fills in the whole struct.
- unsafe { capi::gc_get_layout(&raw mut layout) };
+ capi::gc_get_layout(&mut layout);
let expect = |what: &str, actual: u64, expected: u64| {
assert!(
actual == expected,
diff --git a/Libraries/LibJS/Rust/src/gc/weak.rs b/Libraries/LibJS/Rust/src/gc/weak.rs
index 2393c4f73bf..e3425660a62 100644
--- a/Libraries/LibJS/Rust/src/gc/weak.rs
+++ b/Libraries/LibJS/Rust/src/gc/weak.rs
@@ -33,8 +33,8 @@ impl<T> GcWeak<T> {
}
pub fn null() -> Self {
- // SAFETY: Returns the shared null impl with a reference this value owns.
- let weak_impl = unsafe { capi::gc_weak_impl_null() };
+ // NB: Returns the shared null impl with a reference this value owns.
+ let weak_impl = capi::gc_weak_impl_null();
Self {
weak_impl: NonNull::new(weak_impl).expect("LibGC has a null weak impl"),
_cell: PhantomData,
diff --git a/Libraries/LibJS/Rust/src/jit/mod.rs b/Libraries/LibJS/Rust/src/jit/mod.rs
index 277592d5a3d..2cbea16c5a9 100644
--- a/Libraries/LibJS/Rust/src/jit/mod.rs
+++ b/Libraries/LibJS/Rust/src/jit/mod.rs
@@ -460,16 +460,15 @@ pub(crate) struct DeferGc<'vm>(&'vm Vm);
impl<'vm> DeferGc<'vm> {
pub(crate) fn new(vm: &'vm Vm) -> Self {
- // SAFETY: The VM's heap is live.
- unsafe { capi::gc_heap_defer_gc(vm.heap().raw()) };
+ capi::gc_heap_defer_gc(vm.heap().handle());
Self(vm)
}
}
impl Drop for DeferGc<'_> {
fn drop(&mut self) {
- // SAFETY: As above; this undoes the deferral of new().
- unsafe { capi::gc_heap_undefer_gc(self.0.heap().raw()) };
+ // NB: This undoes the deferral of new().
+ capi::gc_heap_undefer_gc(self.0.heap().handle());
}
}